xref: /freebsd/contrib/libcbor/test/assertions.c (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
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 "stream_expectations.h"
10 
11 void assert_uint8(cbor_item_t* item, uint8_t num) {
12   assert_true(cbor_isa_uint(item));
13   assert_true(cbor_int_get_width(item) == CBOR_INT_8);
14   assert_true(cbor_get_uint8(item) == num);
15 }
16 
17 void assert_uint16(cbor_item_t* item, uint16_t num) {
18   assert_true(cbor_isa_uint(item));
19   assert_true(cbor_int_get_width(item) == CBOR_INT_16);
20   assert_true(cbor_get_uint16(item) == num);
21 }
22 
23 void assert_uint32(cbor_item_t* item, uint32_t num) {
24   assert_true(cbor_isa_uint(item));
25   assert_true(cbor_int_get_width(item) == CBOR_INT_32);
26   assert_true(cbor_get_uint32(item) == num);
27 }
28 
29 void assert_uint64(cbor_item_t* item, uint64_t num) {
30   assert_true(cbor_isa_uint(item));
31   assert_true(cbor_int_get_width(item) == CBOR_INT_64);
32   assert_true(cbor_get_uint64(item) == num);
33 }
34 
35 void assert_decoder_result(size_t expected_bytes_read,
36                            enum cbor_decoder_status expected_status,
37                            struct cbor_decoder_result actual_result) {
38   assert_true(actual_result.read == expected_bytes_read);
39   assert_true(actual_result.status == expected_status);
40   assert_true(actual_result.required == 0);
41 }
42 
43 void assert_decoder_result_nedata(size_t expected_bytes_required,
44                                   struct cbor_decoder_result actual_result) {
45   assert_true(actual_result.read == 0);
46   assert_true(actual_result.status == CBOR_DECODER_NEDATA);
47   assert_true(actual_result.required == expected_bytes_required);
48 }
49 
50 void assert_minimum_input_size(size_t expected, cbor_data data) {
51   for (size_t available = 1; available < expected; available++) {
52     assert_decoder_result_nedata(expected, decode(data, 1));
53   }
54 }
55 
56 void _assert_size_equal(size_t actual, size_t expected, const char* src_file,
57                         int src_line) {
58   if (actual == expected) return;
59   // Not using `fail_msg` since it mishandles variadic macro arguments, which
60   // causes compiler warnings/
61   // TODO file bug
62   printf("(%s:%d) assert_size_equal: Expected %zu to equal %zu\n", src_file,
63          src_line, actual, expected);
64   fail();
65 }
66