Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
external-tools
libcbor
Commits
8c0ebb19
Unverified
Commit
8c0ebb19
authored
1 year ago
by
Pavel Kalvoda
Browse files
Options
Download
Email Patches
Plain Diff
Barebones cbor_describe test
parent
0beff256
master
codepoint-fixes
dependabot/pip/doc/source/certifi-2024.7.4
dependabot/pip/doc/source/idna-3.7
dependabot/pip/doc/source/jinja2-3.1.4
dependabot/pip/doc/source/requests-2.32.0
dependabot/pip/doc/source/tornado-6.4.1
dependabot/pip/doc/source/urllib3-2.2.2
dependabot/pip/doc/source/zipp-3.19.1
describetest
describetest2
emaste/master
fix-build
testtweaks
v0.11.0
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/cbor.c
+7
-3
src/cbor.c
test/pretty_printer_test.c
+48
-15
test/pretty_printer_test.c
with
55 additions
and
18 deletions
+55
-18
src/cbor.c
View file @
8c0ebb19
...
...
@@ -301,17 +301,21 @@ static int _pow(int b, int ex) {
return
res
;
}
static
void
_cbor_type_marquee
(
FILE
*
out
,
char
*
label
,
int
indent
)
{
fprintf
(
out
,
"%*.*s[%s] "
,
indent
,
indent
,
" "
,
label
);
}
static
void
_cbor_nested_describe
(
cbor_item_t
*
item
,
FILE
*
out
,
int
indent
)
{
setlocale
(
LC_ALL
,
""
);
switch
(
cbor_typeof
(
item
))
{
case
CBOR_TYPE_UINT
:
{
fprintf
(
out
,
"
%*s[
CBOR_TYPE_UINT
]
"
,
indent
,
" "
);
_cbor_type_marquee
(
out
,
"CBOR_TYPE_UINT"
,
indent
);
fprintf
(
out
,
"Width: %dB, "
,
_pow
(
2
,
cbor_int_get_width
(
item
)));
fprintf
(
out
,
"Value: %"
PRIu64
"
\n
"
,
cbor_get_int
(
item
));
break
;
}
case
CBOR_TYPE_NEGINT
:
{
fprintf
(
out
,
"
%*s[
CBOR_TYPE_NEGINT
]
"
,
indent
,
" "
);
_cbor_type_marquee
(
out
,
"CBOR_TYPE_NEGINT"
,
indent
);
fprintf
(
out
,
"Width: %dB, "
,
_pow
(
2
,
cbor_int_get_width
(
item
)));
fprintf
(
out
,
"Value: -%"
PRIu64
" - 1
\n
"
,
cbor_get_int
(
item
));
break
;
...
...
@@ -351,7 +355,7 @@ static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
break
;
}
case
CBOR_TYPE_ARRAY
:
{
fprintf
(
out
,
"
%*s[
CBOR_TYPE_ARRAY
]
"
,
indent
,
" "
);
_cbor_type_marquee
(
out
,
"CBOR_TYPE_ARRAY"
,
indent
);
if
(
cbor_array_is_definite
(
item
))
{
fprintf
(
out
,
"Definite, size: %zu
\n
"
,
cbor_array_size
(
item
));
}
else
{
...
...
This diff is collapsed.
Click to expand it.
test/pretty_printer_test.c
View file @
8c0ebb19
...
...
@@ -6,33 +6,66 @@
*/
#include <stdio.h>
#include <string.h>
#include "assertions.h"
#include "cbor.h"
unsigned
char
data
[]
=
{
0x8B
,
0x01
,
0x20
,
0x5F
,
0x41
,
0x01
,
0x41
,
0x02
,
0xFF
,
0x7F
,
0x61
,
0x61
,
0x61
,
0x62
,
0xFF
,
0x9F
,
0xFF
,
0xA1
,
0x61
,
0x61
,
0x61
,
0x62
,
0xC0
,
0xBF
,
0xFF
,
0xFB
,
0x40
,
0x09
,
0x1E
,
0xB8
,
0x51
,
0xEB
,
0x85
,
0x1F
,
0xF6
,
0xF7
,
0xF5
};
static
void
test_pretty_printer
(
void
**
_CBOR_UNUSED
(
_state
))
{
void
assert_describe_result
(
cbor_item_t
*
item
,
char
*
result
[],
size_t
lines
)
{
#if CBOR_PRETTY_PRINTER
FILE
*
outfile
=
tmpfile
();
struct
cbor_load_result
res
;
cbor_item_t
*
item
=
cbor_load
(
data
,
37
,
&
res
);
cbor_describe
(
item
,
outfile
);
cbor_describe
(
item
,
stdout
);
rewind
(
outfile
);
for
(
size_t
i
=
0
;
i
<
lines
;
i
++
)
{
// Expected line + linebreak + null character
char
*
buffer
=
malloc
(
strlen
(
result
[
i
])
+
2
);
char
*
result_with_newline
=
malloc
(
strlen
(
result
[
i
])
+
2
);
assert_non_null
(
buffer
);
assert_non_null
(
result_with_newline
);
sprintf
(
result_with_newline
,
"%s
\n
"
,
result
[
i
]);
fgets
(
buffer
,
strlen
(
result
[
i
])
+
2
,
outfile
);
fprintf
(
stdout
,
"%s"
,
buffer
);
assert_int_equal
(
strlen
(
buffer
),
strlen
(
result_with_newline
));
assert_string_equal
(
buffer
,
result_with_newline
);
free
(
buffer
);
}
fgetc
(
outfile
);
assert_true
(
feof
(
outfile
));
fclose
(
outfile
);
#endif
}
static
void
test_uint
(
void
**
_CBOR_UNUSED
(
_state
))
{
cbor_item_t
*
item
=
cbor_build_uint8
(
42
);
char
*
expected
[]
=
{
"[CBOR_TYPE_UINT] Width: 1B, Value: 42"
};
assert_describe_result
(
item
,
expected
,
1
);
cbor_decref
(
&
item
);
}
item
=
cbor_new_ctrl
();
cbor_set_ctrl
(
item
,
1
);
cbor_describe
(
item
,
outfile
);
static
void
test_negint
(
void
**
_CBOR_UNUSED
(
_state
))
{
cbor_item_t
*
item
=
cbor_build_negint16
(
40
);
char
*
expected
[]
=
{
"[CBOR_TYPE_NEGINT] Width: 2B, Value: -40 - 1"
};
assert_describe_result
(
item
,
expected
,
1
);
cbor_decref
(
&
item
);
}
fclose
(
outfile
);
#endif
static
void
test_definite_array
(
void
**
_CBOR_UNUSED
(
_state
))
{
cbor_item_t
*
item
=
cbor_new_definite_array
(
2
);
assert_true
(
cbor_array_push
(
item
,
cbor_move
(
cbor_build_uint8
(
1
))));
assert_true
(
cbor_array_push
(
item
,
cbor_move
(
cbor_build_uint8
(
2
))));
char
*
expected
[]
=
{
"[CBOR_TYPE_ARRAY] Definite, size: 2"
,
" [CBOR_TYPE_UINT] Width: 1B, Value: 1"
,
" [CBOR_TYPE_UINT] Width: 1B, Value: 2"
,
};
assert_describe_result
(
item
,
expected
,
3
);
cbor_decref
(
&
item
);
}
int
main
(
void
)
{
const
struct
CMUnitTest
tests
[]
=
{
cmocka_unit_test
(
test_pretty_printer
)};
const
struct
CMUnitTest
tests
[]
=
{
cmocka_unit_test
(
test_uint
),
cmocka_unit_test
(
test_negint
),
cmocka_unit_test
(
test_definite_array
)};
return
cmocka_run_group_tests
(
tests
,
NULL
,
NULL
);
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help