xref: /freebsd/contrib/libcbor/test/negint_encoders_test.c (revision 02e9120893770924227138ba49df1edb3896112a)
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_negint8(void **_CBOR_UNUSED(_state)) {
14   assert_size_equal(1, cbor_encode_negint8(14, buffer, 512));
15   assert_memory_equal(buffer, (unsigned char[]){0x2E}, 1);
16 }
17 
18 static void test_negint8(void **_CBOR_UNUSED(_state)) {
19   assert_size_equal(0, cbor_encode_negint8(180, buffer, 1));
20   assert_size_equal(2, cbor_encode_negint8(255, buffer, 512));
21   assert_memory_equal(buffer, ((unsigned char[]){0x38, 0xFF}), 2);
22 }
23 
24 static void test_negint16(void **_CBOR_UNUSED(_state)) {
25   assert_size_equal(0, cbor_encode_negint16(1000, buffer, 2));
26   assert_size_equal(3, cbor_encode_negint16(1000, buffer, 512));
27   assert_memory_equal(buffer, ((unsigned char[]){0x39, 0x03, 0xE8}), 3);
28 }
29 
30 static void test_negint32(void **_CBOR_UNUSED(_state)) {
31   assert_size_equal(0, cbor_encode_negint32(1000000, buffer, 4));
32   assert_size_equal(5, cbor_encode_negint32(1000000, buffer, 512));
33   assert_memory_equal(buffer, ((unsigned char[]){0x3A, 0x00, 0x0F, 0x42, 0x40}),
34                       5);
35 }
36 
37 static void test_negint64(void **_CBOR_UNUSED(_state)) {
38   assert_size_equal(0,
39                     cbor_encode_negint64(18446744073709551615ULL, buffer, 8));
40   assert_size_equal(9,
41                     cbor_encode_negint64(18446744073709551615ULL, buffer, 512));
42   assert_memory_equal(
43       buffer,
44       ((unsigned char[]){0x3B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
45       9);
46 }
47 
48 static void test_unspecified(void **_CBOR_UNUSED(_state)) {
49   assert_size_equal(9,
50                     cbor_encode_negint(18446744073709551615ULL, buffer, 512));
51   assert_memory_equal(
52       buffer,
53       ((unsigned char[]){0x3B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
54       9);
55   assert_size_equal(5, cbor_encode_negint(1000000, buffer, 512));
56   assert_memory_equal(buffer, ((unsigned char[]){0x3A, 0x00, 0x0F, 0x42, 0x40}),
57                       5);
58   assert_size_equal(3, cbor_encode_negint(1000, buffer, 512));
59   assert_memory_equal(buffer, ((unsigned char[]){0x39, 0x03, 0xE8}), 3);
60   assert_size_equal(2, cbor_encode_negint(255, buffer, 512));
61   assert_memory_equal(buffer, ((unsigned char[]){0x38, 0xFF}), 2);
62 }
63 
64 int main(void) {
65   const struct CMUnitTest tests[] = {cmocka_unit_test(test_embedded_negint8),
66                                      cmocka_unit_test(test_negint8),
67                                      cmocka_unit_test(test_negint16),
68                                      cmocka_unit_test(test_negint32),
69                                      cmocka_unit_test(test_negint64),
70                                      cmocka_unit_test(test_unspecified)};
71   return cmocka_run_group_tests(tests, NULL, NULL);
72 }
73