xref: /src/contrib/libcbor/test/float_ctrl_test.c (revision abd872540f24cfc7dbd1ea29b6918c7082a22108)
1058aa793SEd Maste /*
2058aa793SEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3058aa793SEd Maste  *
4058aa793SEd Maste  * libcbor is free software; you can redistribute it and/or modify
5058aa793SEd Maste  * it under the terms of the MIT license. See LICENSE for details.
6058aa793SEd Maste  */
7058aa793SEd Maste 
8058aa793SEd Maste #include <math.h>
9058aa793SEd Maste #include <setjmp.h>
10058aa793SEd Maste #include <stdarg.h>
11058aa793SEd Maste #include <stddef.h>
12058aa793SEd Maste #include <stdint.h>
13058aa793SEd Maste #include <tgmath.h>
14058aa793SEd Maste 
15058aa793SEd Maste #include <cmocka.h>
16058aa793SEd Maste 
17058aa793SEd Maste #include "cbor.h"
18058aa793SEd Maste #include "test_allocator.h"
19058aa793SEd Maste 
20058aa793SEd Maste cbor_item_t *float_ctrl;
21058aa793SEd Maste struct cbor_load_result res;
22058aa793SEd Maste 
23058aa793SEd Maste static const float eps = 0.00001f;
24058aa793SEd Maste 
25058aa793SEd Maste unsigned char float2_data[] = {0xF9, 0x7B, 0xFF};
26058aa793SEd Maste 
test_float2(void ** _CBOR_UNUSED (_state))27058aa793SEd Maste static void test_float2(void **_CBOR_UNUSED(_state)) {
28058aa793SEd Maste   float_ctrl = cbor_load(float2_data, 3, &res);
29058aa793SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
30058aa793SEd Maste   assert_true(cbor_is_float(float_ctrl));
31058aa793SEd Maste   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_16);
32058aa793SEd Maste   assert_true(cbor_float_get_float2(float_ctrl) == 65504.0F);
335f50d6b3SEd Maste   assert_float_equal(cbor_float_get_float(float_ctrl), 65504.0F, eps);
34058aa793SEd Maste   cbor_decref(&float_ctrl);
35058aa793SEd Maste   assert_null(float_ctrl);
36058aa793SEd Maste }
37058aa793SEd Maste 
38058aa793SEd Maste unsigned char float4_data[] = {0xFA, 0x47, 0xC3, 0x50, 0x00};
39058aa793SEd Maste 
test_float4(void ** _CBOR_UNUSED (_state))40058aa793SEd Maste static void test_float4(void **_CBOR_UNUSED(_state)) {
41058aa793SEd Maste   float_ctrl = cbor_load(float4_data, 5, &res);
42058aa793SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
43058aa793SEd Maste   assert_true(cbor_is_float(float_ctrl));
44058aa793SEd Maste   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_32);
45058aa793SEd Maste   assert_true(cbor_float_get_float4(float_ctrl) == 100000.0F);
465f50d6b3SEd Maste   assert_float_equal(cbor_float_get_float(float_ctrl), 100000.0F, eps);
47058aa793SEd Maste   cbor_decref(&float_ctrl);
48058aa793SEd Maste   assert_null(float_ctrl);
49058aa793SEd Maste }
50058aa793SEd Maste 
51058aa793SEd Maste unsigned char float8_data[] = {0xFB, 0x7E, 0x37, 0xE4, 0x3C,
52058aa793SEd Maste                                0x88, 0x00, 0x75, 0x9C};
53058aa793SEd Maste 
test_float8(void ** _CBOR_UNUSED (_state))54058aa793SEd Maste static void test_float8(void **_CBOR_UNUSED(_state)) {
55058aa793SEd Maste   float_ctrl = cbor_load(float8_data, 9, &res);
56058aa793SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
57058aa793SEd Maste   assert_true(cbor_is_float(float_ctrl));
58058aa793SEd Maste   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_64);
59058aa793SEd Maste   // XXX: the cast prevents promotion to 80-bit floats on 32-bit x86
60058aa793SEd Maste   assert_true(cbor_float_get_float8(float_ctrl) == (double)1.0e+300);
615f50d6b3SEd Maste   // Not using `assert_double_equal` since CI has an old version of cmocka
625f50d6b3SEd Maste   assert_true(fabs(cbor_float_get_float(float_ctrl) - (double)1.0e+300) < eps);
63058aa793SEd Maste   cbor_decref(&float_ctrl);
64058aa793SEd Maste   assert_null(float_ctrl);
65058aa793SEd Maste }
66058aa793SEd Maste 
67058aa793SEd Maste unsigned char null_data[] = {0xF6};
68058aa793SEd Maste 
test_null(void ** _CBOR_UNUSED (_state))69058aa793SEd Maste static void test_null(void **_CBOR_UNUSED(_state)) {
70058aa793SEd Maste   float_ctrl = cbor_load(null_data, 1, &res);
71058aa793SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
72058aa793SEd Maste   assert_true(cbor_is_null(float_ctrl));
73058aa793SEd Maste   cbor_decref(&float_ctrl);
74058aa793SEd Maste   assert_null(float_ctrl);
75058aa793SEd Maste }
76058aa793SEd Maste 
77058aa793SEd Maste unsigned char undef_data[] = {0xF7};
78058aa793SEd Maste 
test_undef(void ** _CBOR_UNUSED (_state))79058aa793SEd Maste static void test_undef(void **_CBOR_UNUSED(_state)) {
80058aa793SEd Maste   float_ctrl = cbor_load(undef_data, 1, &res);
81058aa793SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
82058aa793SEd Maste   assert_true(cbor_is_undef(float_ctrl));
83058aa793SEd Maste   cbor_decref(&float_ctrl);
84058aa793SEd Maste   assert_null(float_ctrl);
85058aa793SEd Maste }
86058aa793SEd Maste 
87058aa793SEd Maste unsigned char bool_data[] = {0xF4, 0xF5};
88058aa793SEd Maste 
test_bool(void ** _CBOR_UNUSED (_state))89058aa793SEd Maste static void test_bool(void **_CBOR_UNUSED(_state)) {
90058aa793SEd Maste   _CBOR_TEST_DISABLE_ASSERT({
91058aa793SEd Maste     float_ctrl = cbor_load(bool_data, 1, &res);
92058aa793SEd Maste     assert_true(cbor_isa_float_ctrl(float_ctrl));
93058aa793SEd Maste     assert_true(cbor_is_bool(float_ctrl));
94058aa793SEd Maste     assert_false(cbor_get_bool(float_ctrl));
95058aa793SEd Maste     cbor_set_bool(float_ctrl, true);
96058aa793SEd Maste     assert_true(cbor_get_bool(float_ctrl));
97058aa793SEd Maste     assert_true(isnan(cbor_float_get_float(float_ctrl)));
98058aa793SEd Maste     cbor_decref(&float_ctrl);
99058aa793SEd Maste     assert_null(float_ctrl);
100058aa793SEd Maste 
101058aa793SEd Maste     float_ctrl = cbor_load(bool_data + 1, 1, &res);
102058aa793SEd Maste     assert_true(cbor_isa_float_ctrl(float_ctrl));
103058aa793SEd Maste     assert_true(cbor_is_bool(float_ctrl));
104058aa793SEd Maste     assert_true(cbor_get_bool(float_ctrl));
105058aa793SEd Maste     cbor_set_bool(float_ctrl, false);
106058aa793SEd Maste     assert_false(cbor_get_bool(float_ctrl));
107058aa793SEd Maste     assert_true(isnan(cbor_float_get_float(float_ctrl)));
108058aa793SEd Maste     cbor_decref(&float_ctrl);
109058aa793SEd Maste     assert_null(float_ctrl);
110058aa793SEd Maste   });
111058aa793SEd Maste }
112058aa793SEd Maste 
test_float_ctrl_creation(void ** _CBOR_UNUSED (_state))113058aa793SEd Maste static void test_float_ctrl_creation(void **_CBOR_UNUSED(_state)) {
114058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_ctrl()); });
115058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_float2()); });
116058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_float4()); });
117058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_float8()); });
118058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_null()); });
119058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_undef()); });
120058aa793SEd Maste 
121058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_bool(false)); });
122058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_float2(3.14)); });
123058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_float4(3.14)); });
124058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_float8(3.14)); });
125058aa793SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_ctrl(0xAF)); });
126058aa793SEd Maste }
127058aa793SEd Maste 
main(void)128058aa793SEd Maste int main(void) {
129058aa793SEd Maste   const struct CMUnitTest tests[] = {
130058aa793SEd Maste       cmocka_unit_test(test_float2),
131058aa793SEd Maste       cmocka_unit_test(test_float4),
132058aa793SEd Maste       cmocka_unit_test(test_float8),
133058aa793SEd Maste       cmocka_unit_test(test_null),
134058aa793SEd Maste       cmocka_unit_test(test_undef),
135058aa793SEd Maste       cmocka_unit_test(test_bool),
136058aa793SEd Maste       cmocka_unit_test(test_float_ctrl_creation),
137058aa793SEd Maste   };
138058aa793SEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
139058aa793SEd Maste }
140