xref: /freebsd/contrib/libcbor/test/float_ctrl_test.c (revision abd872540f24cfc7dbd1ea29b6918c7082a22108)
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 <math.h>
9 #include <setjmp.h>
10 #include <stdarg.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <tgmath.h>
14 
15 #include <cmocka.h>
16 
17 #include "cbor.h"
18 #include "test_allocator.h"
19 
20 cbor_item_t *float_ctrl;
21 struct cbor_load_result res;
22 
23 static const float eps = 0.00001f;
24 
25 unsigned char float2_data[] = {0xF9, 0x7B, 0xFF};
26 
27 static void test_float2(void **_CBOR_UNUSED(_state)) {
28   float_ctrl = cbor_load(float2_data, 3, &res);
29   assert_true(cbor_isa_float_ctrl(float_ctrl));
30   assert_true(cbor_is_float(float_ctrl));
31   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_16);
32   assert_true(cbor_float_get_float2(float_ctrl) == 65504.0F);
33   assert_float_equal(cbor_float_get_float(float_ctrl), 65504.0F, eps);
34   cbor_decref(&float_ctrl);
35   assert_null(float_ctrl);
36 }
37 
38 unsigned char float4_data[] = {0xFA, 0x47, 0xC3, 0x50, 0x00};
39 
40 static void test_float4(void **_CBOR_UNUSED(_state)) {
41   float_ctrl = cbor_load(float4_data, 5, &res);
42   assert_true(cbor_isa_float_ctrl(float_ctrl));
43   assert_true(cbor_is_float(float_ctrl));
44   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_32);
45   assert_true(cbor_float_get_float4(float_ctrl) == 100000.0F);
46   assert_float_equal(cbor_float_get_float(float_ctrl), 100000.0F, eps);
47   cbor_decref(&float_ctrl);
48   assert_null(float_ctrl);
49 }
50 
51 unsigned char float8_data[] = {0xFB, 0x7E, 0x37, 0xE4, 0x3C,
52                                0x88, 0x00, 0x75, 0x9C};
53 
54 static void test_float8(void **_CBOR_UNUSED(_state)) {
55   float_ctrl = cbor_load(float8_data, 9, &res);
56   assert_true(cbor_isa_float_ctrl(float_ctrl));
57   assert_true(cbor_is_float(float_ctrl));
58   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_64);
59   // XXX: the cast prevents promotion to 80-bit floats on 32-bit x86
60   assert_true(cbor_float_get_float8(float_ctrl) == (double)1.0e+300);
61   // Not using `assert_double_equal` since CI has an old version of cmocka
62   assert_true(fabs(cbor_float_get_float(float_ctrl) - (double)1.0e+300) < eps);
63   cbor_decref(&float_ctrl);
64   assert_null(float_ctrl);
65 }
66 
67 unsigned char null_data[] = {0xF6};
68 
69 static void test_null(void **_CBOR_UNUSED(_state)) {
70   float_ctrl = cbor_load(null_data, 1, &res);
71   assert_true(cbor_isa_float_ctrl(float_ctrl));
72   assert_true(cbor_is_null(float_ctrl));
73   cbor_decref(&float_ctrl);
74   assert_null(float_ctrl);
75 }
76 
77 unsigned char undef_data[] = {0xF7};
78 
79 static void test_undef(void **_CBOR_UNUSED(_state)) {
80   float_ctrl = cbor_load(undef_data, 1, &res);
81   assert_true(cbor_isa_float_ctrl(float_ctrl));
82   assert_true(cbor_is_undef(float_ctrl));
83   cbor_decref(&float_ctrl);
84   assert_null(float_ctrl);
85 }
86 
87 unsigned char bool_data[] = {0xF4, 0xF5};
88 
89 static void test_bool(void **_CBOR_UNUSED(_state)) {
90   _CBOR_TEST_DISABLE_ASSERT({
91     float_ctrl = cbor_load(bool_data, 1, &res);
92     assert_true(cbor_isa_float_ctrl(float_ctrl));
93     assert_true(cbor_is_bool(float_ctrl));
94     assert_false(cbor_get_bool(float_ctrl));
95     cbor_set_bool(float_ctrl, true);
96     assert_true(cbor_get_bool(float_ctrl));
97     assert_true(isnan(cbor_float_get_float(float_ctrl)));
98     cbor_decref(&float_ctrl);
99     assert_null(float_ctrl);
100 
101     float_ctrl = cbor_load(bool_data + 1, 1, &res);
102     assert_true(cbor_isa_float_ctrl(float_ctrl));
103     assert_true(cbor_is_bool(float_ctrl));
104     assert_true(cbor_get_bool(float_ctrl));
105     cbor_set_bool(float_ctrl, false);
106     assert_false(cbor_get_bool(float_ctrl));
107     assert_true(isnan(cbor_float_get_float(float_ctrl)));
108     cbor_decref(&float_ctrl);
109     assert_null(float_ctrl);
110   });
111 }
112 
113 static void test_float_ctrl_creation(void **_CBOR_UNUSED(_state)) {
114   WITH_FAILING_MALLOC({ assert_null(cbor_new_ctrl()); });
115   WITH_FAILING_MALLOC({ assert_null(cbor_new_float2()); });
116   WITH_FAILING_MALLOC({ assert_null(cbor_new_float4()); });
117   WITH_FAILING_MALLOC({ assert_null(cbor_new_float8()); });
118   WITH_FAILING_MALLOC({ assert_null(cbor_new_null()); });
119   WITH_FAILING_MALLOC({ assert_null(cbor_new_undef()); });
120 
121   WITH_FAILING_MALLOC({ assert_null(cbor_build_bool(false)); });
122   WITH_FAILING_MALLOC({ assert_null(cbor_build_float2(3.14)); });
123   WITH_FAILING_MALLOC({ assert_null(cbor_build_float4(3.14)); });
124   WITH_FAILING_MALLOC({ assert_null(cbor_build_float8(3.14)); });
125   WITH_FAILING_MALLOC({ assert_null(cbor_build_ctrl(0xAF)); });
126 }
127 
128 int main(void) {
129   const struct CMUnitTest tests[] = {
130       cmocka_unit_test(test_float2),
131       cmocka_unit_test(test_float4),
132       cmocka_unit_test(test_float8),
133       cmocka_unit_test(test_null),
134       cmocka_unit_test(test_undef),
135       cmocka_unit_test(test_bool),
136       cmocka_unit_test(test_float_ctrl_creation),
137   };
138   return cmocka_run_group_tests(tests, NULL, NULL);
139 }
140