Skip to content

Commit 77e767a

Browse files
committed
Simple definite maps test
1 parent d1a2416 commit 77e767a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/cbor_internal.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <assert.h>
44
#include <string.h>
55
#include <math.h>
6+
#include <ldap.h>
67

78
// TODO asserts
89
// TODO check mallocs
@@ -110,19 +111,22 @@ void _cbor_builder_append(cbor_item_t * item, struct _cbor_decoder_context * ctx
110111
if (cbor_map_is_definite(ctx->stack->top->item)) {
111112
assert(ctx->stack->top->subitems > 0);
112113
if (ctx->stack->top->subitems % 2) {
113-
/* Even record, this is a key */
114-
cbor_map_add(ctx->stack->top->item, (struct cbor_pair){ .key = item, .value = NULL });
115-
} else {
116114
// TODO this is fugly
117115
cbor_map_handle(ctx->stack->top->item)[cbor_map_size(ctx->stack->top->item) - 1].value = item;
116+
} else {
117+
/* Even record, this is a key */
118+
cbor_map_add(ctx->stack->top->item, (struct cbor_pair) {.key = item, .value = NULL});
118119
}
119120
ctx->stack->top->subitems--;
120121
if (ctx->stack->top->subitems == 0) {
121122
cbor_item_t *item = ctx->stack->top->item;
122123
_cbor_stack_pop(ctx->stack);
123124
_cbor_builder_append(item, ctx);
124125
}
126+
} else {
127+
// TODO uh oh
125128
}
129+
break;
126130
}
127131
case CBOR_TYPE_TAG:
128132
{

test/type_5_test.c

+21
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,30 @@ static void test_empty_map(void **state)
2525
}
2626

2727

28+
unsigned char simple_map[] = { 0xA2, 0x01, 0x02, 0x03, 0x04 }; /* {1: 2, 3: 4} */
29+
static void test_simple_map(void **state)
30+
{
31+
map = cbor_load(simple_map, 5, CBOR_FLAGS_NONE, &res);
32+
assert_non_null(map);
33+
assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
34+
assert_true(cbor_isa_map(map));
35+
assert_true(cbor_map_size(map) == 2);
36+
assert_true(res.read == 5);
37+
struct cbor_pair * handle = cbor_map_handle(map);
38+
assert_uint8(handle[0].key, 1);
39+
assert_uint8(handle[0].value, 2);
40+
assert_uint8(handle[1].key, 3);
41+
assert_uint8(handle[1].value, 4);
42+
cbor_decref(&map);
43+
assert_null(map);
44+
}
45+
46+
47+
2848
int main(void) {
2949
const UnitTest tests[] = {
3050
unit_test(test_empty_map),
51+
unit_test(test_simple_map)
3152
};
3253
return run_tests(tests);
3354
}

0 commit comments

Comments
 (0)