Path: blob/main/contrib/libcbor/test/array_encoders_test.c
39507 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#include "assertions.h"8#include "cbor.h"910unsigned char buffer[512];1112static void test_embedded_array_start(void **_CBOR_UNUSED(_state)) {13assert_size_equal(1, cbor_encode_array_start(1, buffer, 512));14assert_memory_equal(buffer, ((unsigned char[]){0x81}), 1);15}1617static void test_array_start(void **_CBOR_UNUSED(_state)) {18assert_size_equal(5, cbor_encode_array_start(1000000, buffer, 512));19assert_memory_equal(buffer, ((unsigned char[]){0x9A, 0x00, 0x0F, 0x42, 0x40}),205);21}2223static void test_indef_array_start(void **_CBOR_UNUSED(_state)) {24assert_size_equal(1, cbor_encode_indef_array_start(buffer, 512));25assert_size_equal(0, cbor_encode_indef_array_start(buffer, 0));26assert_memory_equal(buffer, ((unsigned char[]){0x9F}), 1);27}2829static void test_indef_array_encoding(void **_CBOR_UNUSED(_state)) {30cbor_item_t *array = cbor_new_indefinite_array();31cbor_item_t *one = cbor_build_uint8(1);32cbor_item_t *two = cbor_build_uint8(2);33assert_true(cbor_array_push(array, one));34assert_true(cbor_array_push(array, two));3536assert_size_equal(4, cbor_serialize_array(array, buffer, 512));37assert_memory_equal(buffer, ((unsigned char[]){0x9F, 0x01, 0x02, 0xFF}), 4);3839cbor_decref(&array);40cbor_decref(&one);41cbor_decref(&two);42}4344int main(void) {45const struct CMUnitTest tests[] = {46cmocka_unit_test(test_embedded_array_start),47cmocka_unit_test(test_array_start),48cmocka_unit_test(test_indef_array_start),49cmocka_unit_test(test_indef_array_encoding)};50return cmocka_run_group_tests(tests, NULL, NULL);51}525354