xref: /freebsd/contrib/libcbor/test/array_encoders_test.c (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
1*5d3e7166SEd Maste /*
2*5d3e7166SEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3*5d3e7166SEd Maste  *
4*5d3e7166SEd Maste  * libcbor is free software; you can redistribute it and/or modify
5*5d3e7166SEd Maste  * it under the terms of the MIT license. See LICENSE for details.
6*5d3e7166SEd Maste  */
7*5d3e7166SEd Maste 
8*5d3e7166SEd Maste #include "assertions.h"
9*5d3e7166SEd Maste #include "cbor.h"
10*5d3e7166SEd Maste 
11*5d3e7166SEd Maste unsigned char buffer[512];
12*5d3e7166SEd Maste 
test_embedded_array_start(void ** _CBOR_UNUSED (_state))13*5d3e7166SEd Maste static void test_embedded_array_start(void **_CBOR_UNUSED(_state)) {
14*5d3e7166SEd Maste   assert_size_equal(1, cbor_encode_array_start(1, buffer, 512));
15*5d3e7166SEd Maste   assert_memory_equal(buffer, ((unsigned char[]){0x81}), 1);
16*5d3e7166SEd Maste }
17*5d3e7166SEd Maste 
test_array_start(void ** _CBOR_UNUSED (_state))18*5d3e7166SEd Maste static void test_array_start(void **_CBOR_UNUSED(_state)) {
19*5d3e7166SEd Maste   assert_size_equal(5, cbor_encode_array_start(1000000, buffer, 512));
20*5d3e7166SEd Maste   assert_memory_equal(buffer, ((unsigned char[]){0x9A, 0x00, 0x0F, 0x42, 0x40}),
21*5d3e7166SEd Maste                       5);
22*5d3e7166SEd Maste }
23*5d3e7166SEd Maste 
test_indef_array_start(void ** _CBOR_UNUSED (_state))24*5d3e7166SEd Maste static void test_indef_array_start(void **_CBOR_UNUSED(_state)) {
25*5d3e7166SEd Maste   assert_size_equal(1, cbor_encode_indef_array_start(buffer, 512));
26*5d3e7166SEd Maste   assert_size_equal(0, cbor_encode_indef_array_start(buffer, 0));
27*5d3e7166SEd Maste   assert_memory_equal(buffer, ((unsigned char[]){0x9F}), 1);
28*5d3e7166SEd Maste }
29*5d3e7166SEd Maste 
test_indef_array_encoding(void ** _CBOR_UNUSED (_state))30*5d3e7166SEd Maste static void test_indef_array_encoding(void **_CBOR_UNUSED(_state)) {
31*5d3e7166SEd Maste   cbor_item_t *array = cbor_new_indefinite_array();
32*5d3e7166SEd Maste   cbor_item_t *one = cbor_build_uint8(1);
33*5d3e7166SEd Maste   cbor_item_t *two = cbor_build_uint8(2);
34*5d3e7166SEd Maste   assert_true(cbor_array_push(array, one));
35*5d3e7166SEd Maste   assert_true(cbor_array_push(array, two));
36*5d3e7166SEd Maste 
37*5d3e7166SEd Maste   assert_size_equal(4, cbor_serialize_array(array, buffer, 512));
38*5d3e7166SEd Maste   assert_memory_equal(buffer, ((unsigned char[]){0x9F, 0x01, 0x02, 0xFF}), 4);
39*5d3e7166SEd Maste 
40*5d3e7166SEd Maste   cbor_decref(&array);
41*5d3e7166SEd Maste   cbor_decref(&one);
42*5d3e7166SEd Maste   cbor_decref(&two);
43*5d3e7166SEd Maste }
44*5d3e7166SEd Maste 
main(void)45*5d3e7166SEd Maste int main(void) {
46*5d3e7166SEd Maste   const struct CMUnitTest tests[] = {
47*5d3e7166SEd Maste       cmocka_unit_test(test_embedded_array_start),
48*5d3e7166SEd Maste       cmocka_unit_test(test_array_start),
49*5d3e7166SEd Maste       cmocka_unit_test(test_indef_array_start),
50*5d3e7166SEd Maste       cmocka_unit_test(test_indef_array_encoding)};
51*5d3e7166SEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
52*5d3e7166SEd Maste }
53