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