xref: /freebsd/contrib/libcbor/test/map_encoders_test.c (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include "assertions.h"
9 #include "cbor.h"
10 
11 unsigned char buffer[512];
12 
13 static void test_embedded_map_start(void **_CBOR_UNUSED(_state)) {
14   assert_size_equal(1, cbor_encode_map_start(1, buffer, 512));
15   assert_memory_equal(buffer, ((unsigned char[]){0xA1}), 1);
16 }
17 
18 static void test_map_start(void **_CBOR_UNUSED(_state)) {
19   assert_size_equal(5, cbor_encode_map_start(1000000, buffer, 512));
20   assert_memory_equal(buffer, ((unsigned char[]){0xBA, 0x00, 0x0F, 0x42, 0x40}),
21                       5);
22 }
23 
24 static void test_indef_map_start(void **_CBOR_UNUSED(_state)) {
25   assert_size_equal(1, cbor_encode_indef_map_start(buffer, 512));
26   assert_size_equal(0, cbor_encode_indef_map_start(buffer, 0));
27   assert_memory_equal(buffer, ((unsigned char[]){0xBF}), 1);
28 }
29 
30 int main(void) {
31   const struct CMUnitTest tests[] = {cmocka_unit_test(test_embedded_map_start),
32                                      cmocka_unit_test(test_map_start),
33                                      cmocka_unit_test(test_indef_map_start)};
34   return cmocka_run_group_tests(tests, NULL, NULL);
35 }
36