xref: /freebsd/contrib/libcbor/test/float_ctrl_test.c (revision abd872540f24cfc7dbd1ea29b6918c7082a22108)
15d3e7166SEd Maste /*
25d3e7166SEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
35d3e7166SEd Maste  *
45d3e7166SEd Maste  * libcbor is free software; you can redistribute it and/or modify
55d3e7166SEd Maste  * it under the terms of the MIT license. See LICENSE for details.
65d3e7166SEd Maste  */
75d3e7166SEd Maste 
85d3e7166SEd Maste #include <math.h>
95d3e7166SEd Maste #include <setjmp.h>
105d3e7166SEd Maste #include <stdarg.h>
115d3e7166SEd Maste #include <stddef.h>
125d3e7166SEd Maste #include <stdint.h>
135d3e7166SEd Maste #include <tgmath.h>
145d3e7166SEd Maste 
155d3e7166SEd Maste #include <cmocka.h>
165d3e7166SEd Maste 
175d3e7166SEd Maste #include "cbor.h"
185d3e7166SEd Maste #include "test_allocator.h"
195d3e7166SEd Maste 
205d3e7166SEd Maste cbor_item_t *float_ctrl;
215d3e7166SEd Maste struct cbor_load_result res;
225d3e7166SEd Maste 
235d3e7166SEd Maste static const float eps = 0.00001f;
245d3e7166SEd Maste 
255d3e7166SEd Maste unsigned char float2_data[] = {0xF9, 0x7B, 0xFF};
265d3e7166SEd Maste 
275d3e7166SEd Maste static void test_float2(void **_CBOR_UNUSED(_state)) {
285d3e7166SEd Maste   float_ctrl = cbor_load(float2_data, 3, &res);
295d3e7166SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
305d3e7166SEd Maste   assert_true(cbor_is_float(float_ctrl));
315d3e7166SEd Maste   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_16);
325d3e7166SEd Maste   assert_true(cbor_float_get_float2(float_ctrl) == 65504.0F);
33*abd87254SEd Maste   assert_float_equal(cbor_float_get_float(float_ctrl), 65504.0F, eps);
345d3e7166SEd Maste   cbor_decref(&float_ctrl);
355d3e7166SEd Maste   assert_null(float_ctrl);
365d3e7166SEd Maste }
375d3e7166SEd Maste 
385d3e7166SEd Maste unsigned char float4_data[] = {0xFA, 0x47, 0xC3, 0x50, 0x00};
395d3e7166SEd Maste 
405d3e7166SEd Maste static void test_float4(void **_CBOR_UNUSED(_state)) {
415d3e7166SEd Maste   float_ctrl = cbor_load(float4_data, 5, &res);
425d3e7166SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
435d3e7166SEd Maste   assert_true(cbor_is_float(float_ctrl));
445d3e7166SEd Maste   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_32);
455d3e7166SEd Maste   assert_true(cbor_float_get_float4(float_ctrl) == 100000.0F);
46*abd87254SEd Maste   assert_float_equal(cbor_float_get_float(float_ctrl), 100000.0F, eps);
475d3e7166SEd Maste   cbor_decref(&float_ctrl);
485d3e7166SEd Maste   assert_null(float_ctrl);
495d3e7166SEd Maste }
505d3e7166SEd Maste 
515d3e7166SEd Maste unsigned char float8_data[] = {0xFB, 0x7E, 0x37, 0xE4, 0x3C,
525d3e7166SEd Maste                                0x88, 0x00, 0x75, 0x9C};
535d3e7166SEd Maste 
545d3e7166SEd Maste static void test_float8(void **_CBOR_UNUSED(_state)) {
555d3e7166SEd Maste   float_ctrl = cbor_load(float8_data, 9, &res);
565d3e7166SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
575d3e7166SEd Maste   assert_true(cbor_is_float(float_ctrl));
585d3e7166SEd Maste   assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_64);
595d3e7166SEd Maste   // XXX: the cast prevents promotion to 80-bit floats on 32-bit x86
605d3e7166SEd Maste   assert_true(cbor_float_get_float8(float_ctrl) == (double)1.0e+300);
61*abd87254SEd Maste   // Not using `assert_double_equal` since CI has an old version of cmocka
62*abd87254SEd Maste   assert_true(fabs(cbor_float_get_float(float_ctrl) - (double)1.0e+300) < eps);
635d3e7166SEd Maste   cbor_decref(&float_ctrl);
645d3e7166SEd Maste   assert_null(float_ctrl);
655d3e7166SEd Maste }
665d3e7166SEd Maste 
675d3e7166SEd Maste unsigned char null_data[] = {0xF6};
685d3e7166SEd Maste 
695d3e7166SEd Maste static void test_null(void **_CBOR_UNUSED(_state)) {
705d3e7166SEd Maste   float_ctrl = cbor_load(null_data, 1, &res);
715d3e7166SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
725d3e7166SEd Maste   assert_true(cbor_is_null(float_ctrl));
735d3e7166SEd Maste   cbor_decref(&float_ctrl);
745d3e7166SEd Maste   assert_null(float_ctrl);
755d3e7166SEd Maste }
765d3e7166SEd Maste 
775d3e7166SEd Maste unsigned char undef_data[] = {0xF7};
785d3e7166SEd Maste 
795d3e7166SEd Maste static void test_undef(void **_CBOR_UNUSED(_state)) {
805d3e7166SEd Maste   float_ctrl = cbor_load(undef_data, 1, &res);
815d3e7166SEd Maste   assert_true(cbor_isa_float_ctrl(float_ctrl));
825d3e7166SEd Maste   assert_true(cbor_is_undef(float_ctrl));
835d3e7166SEd Maste   cbor_decref(&float_ctrl);
845d3e7166SEd Maste   assert_null(float_ctrl);
855d3e7166SEd Maste }
865d3e7166SEd Maste 
875d3e7166SEd Maste unsigned char bool_data[] = {0xF4, 0xF5};
885d3e7166SEd Maste 
895d3e7166SEd Maste static void test_bool(void **_CBOR_UNUSED(_state)) {
905d3e7166SEd Maste   _CBOR_TEST_DISABLE_ASSERT({
915d3e7166SEd Maste     float_ctrl = cbor_load(bool_data, 1, &res);
925d3e7166SEd Maste     assert_true(cbor_isa_float_ctrl(float_ctrl));
935d3e7166SEd Maste     assert_true(cbor_is_bool(float_ctrl));
945d3e7166SEd Maste     assert_false(cbor_get_bool(float_ctrl));
955d3e7166SEd Maste     cbor_set_bool(float_ctrl, true);
965d3e7166SEd Maste     assert_true(cbor_get_bool(float_ctrl));
975d3e7166SEd Maste     assert_true(isnan(cbor_float_get_float(float_ctrl)));
985d3e7166SEd Maste     cbor_decref(&float_ctrl);
995d3e7166SEd Maste     assert_null(float_ctrl);
1005d3e7166SEd Maste 
1015d3e7166SEd Maste     float_ctrl = cbor_load(bool_data + 1, 1, &res);
1025d3e7166SEd Maste     assert_true(cbor_isa_float_ctrl(float_ctrl));
1035d3e7166SEd Maste     assert_true(cbor_is_bool(float_ctrl));
1045d3e7166SEd Maste     assert_true(cbor_get_bool(float_ctrl));
1055d3e7166SEd Maste     cbor_set_bool(float_ctrl, false);
1065d3e7166SEd Maste     assert_false(cbor_get_bool(float_ctrl));
1075d3e7166SEd Maste     assert_true(isnan(cbor_float_get_float(float_ctrl)));
1085d3e7166SEd Maste     cbor_decref(&float_ctrl);
1095d3e7166SEd Maste     assert_null(float_ctrl);
1105d3e7166SEd Maste   });
1115d3e7166SEd Maste }
1125d3e7166SEd Maste 
1135d3e7166SEd Maste static void test_float_ctrl_creation(void **_CBOR_UNUSED(_state)) {
1145d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_ctrl()); });
1155d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_float2()); });
1165d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_float4()); });
1175d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_float8()); });
1185d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_null()); });
1195d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_undef()); });
1205d3e7166SEd Maste 
1215d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_bool(false)); });
1225d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_float2(3.14)); });
1235d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_float4(3.14)); });
1245d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_float8(3.14)); });
1255d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_ctrl(0xAF)); });
1265d3e7166SEd Maste }
1275d3e7166SEd Maste 
1285d3e7166SEd Maste int main(void) {
1295d3e7166SEd Maste   const struct CMUnitTest tests[] = {
1305d3e7166SEd Maste       cmocka_unit_test(test_float2),
1315d3e7166SEd Maste       cmocka_unit_test(test_float4),
1325d3e7166SEd Maste       cmocka_unit_test(test_float8),
1335d3e7166SEd Maste       cmocka_unit_test(test_null),
1345d3e7166SEd Maste       cmocka_unit_test(test_undef),
1355d3e7166SEd Maste       cmocka_unit_test(test_bool),
1365d3e7166SEd Maste       cmocka_unit_test(test_float_ctrl_creation),
1375d3e7166SEd Maste   };
1385d3e7166SEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
1395d3e7166SEd Maste }
140