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_true(fabs(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_true(fabs(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 cbor_decref(&float_ctrl); 62 assert_null(float_ctrl); 63 } 64 65 unsigned char null_data[] = {0xF6}; 66 67 static void test_null(void **_CBOR_UNUSED(_state)) { 68 float_ctrl = cbor_load(null_data, 1, &res); 69 assert_true(cbor_isa_float_ctrl(float_ctrl)); 70 assert_true(cbor_is_null(float_ctrl)); 71 cbor_decref(&float_ctrl); 72 assert_null(float_ctrl); 73 } 74 75 unsigned char undef_data[] = {0xF7}; 76 77 static void test_undef(void **_CBOR_UNUSED(_state)) { 78 float_ctrl = cbor_load(undef_data, 1, &res); 79 assert_true(cbor_isa_float_ctrl(float_ctrl)); 80 assert_true(cbor_is_undef(float_ctrl)); 81 cbor_decref(&float_ctrl); 82 assert_null(float_ctrl); 83 } 84 85 unsigned char bool_data[] = {0xF4, 0xF5}; 86 87 static void test_bool(void **_CBOR_UNUSED(_state)) { 88 _CBOR_TEST_DISABLE_ASSERT({ 89 float_ctrl = cbor_load(bool_data, 1, &res); 90 assert_true(cbor_isa_float_ctrl(float_ctrl)); 91 assert_true(cbor_is_bool(float_ctrl)); 92 assert_false(cbor_get_bool(float_ctrl)); 93 cbor_set_bool(float_ctrl, true); 94 assert_true(cbor_get_bool(float_ctrl)); 95 assert_true(isnan(cbor_float_get_float(float_ctrl))); 96 cbor_decref(&float_ctrl); 97 assert_null(float_ctrl); 98 99 float_ctrl = cbor_load(bool_data + 1, 1, &res); 100 assert_true(cbor_isa_float_ctrl(float_ctrl)); 101 assert_true(cbor_is_bool(float_ctrl)); 102 assert_true(cbor_get_bool(float_ctrl)); 103 cbor_set_bool(float_ctrl, false); 104 assert_false(cbor_get_bool(float_ctrl)); 105 assert_true(isnan(cbor_float_get_float(float_ctrl))); 106 cbor_decref(&float_ctrl); 107 assert_null(float_ctrl); 108 }); 109 } 110 111 static void test_float_ctrl_creation(void **_CBOR_UNUSED(_state)) { 112 WITH_FAILING_MALLOC({ assert_null(cbor_new_ctrl()); }); 113 WITH_FAILING_MALLOC({ assert_null(cbor_new_float2()); }); 114 WITH_FAILING_MALLOC({ assert_null(cbor_new_float4()); }); 115 WITH_FAILING_MALLOC({ assert_null(cbor_new_float8()); }); 116 WITH_FAILING_MALLOC({ assert_null(cbor_new_null()); }); 117 WITH_FAILING_MALLOC({ assert_null(cbor_new_undef()); }); 118 119 WITH_FAILING_MALLOC({ assert_null(cbor_build_bool(false)); }); 120 WITH_FAILING_MALLOC({ assert_null(cbor_build_float2(3.14)); }); 121 WITH_FAILING_MALLOC({ assert_null(cbor_build_float4(3.14)); }); 122 WITH_FAILING_MALLOC({ assert_null(cbor_build_float8(3.14)); }); 123 WITH_FAILING_MALLOC({ assert_null(cbor_build_ctrl(0xAF)); }); 124 } 125 126 int main(void) { 127 const struct CMUnitTest tests[] = { 128 cmocka_unit_test(test_float2), 129 cmocka_unit_test(test_float4), 130 cmocka_unit_test(test_float8), 131 cmocka_unit_test(test_null), 132 cmocka_unit_test(test_undef), 133 cmocka_unit_test(test_bool), 134 cmocka_unit_test(test_float_ctrl_creation), 135 }; 136 return cmocka_run_group_tests(tests, NULL, NULL); 137 } 138