File: type_7_test.c

package info (click to toggle)
libcbor 0.5.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 1,176 kB
  • sloc: ansic: 7,131; makefile: 154; sh: 71; python: 54; ruby: 17; cpp: 6
file content (117 lines) | stat: -rw-r--r-- 3,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2014-2017 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include "cbor.h"
#include "assertions.h"
#include <inttypes.h>
#include <tgmath.h>

cbor_item_t *float_ctrl;
struct cbor_load_result res;

static const float eps = 0.00001f;

unsigned char float2_data[] = {0xF9, 0x7B, 0xFF};

static void test_float2(void **state)
{
	float_ctrl = cbor_load(float2_data, 3, &res);
	assert_true(cbor_isa_float_ctrl(float_ctrl));
	assert_true(cbor_is_float(float_ctrl));
	assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_16);
	assert_true(cbor_float_get_float2(float_ctrl) == 65504.0F);
	assert_true(fabs(cbor_float_get_float(float_ctrl) - 65504.0F) < eps);
	cbor_decref(&float_ctrl);
	assert_null(float_ctrl);
}

unsigned char float4_data[] = {0xFA, 0x47, 0xC3, 0x50, 0x00};

static void test_float4(void **state)
{
	float_ctrl = cbor_load(float4_data, 5, &res);
	assert_true(cbor_isa_float_ctrl(float_ctrl));
	assert_true(cbor_is_float(float_ctrl));
	assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_32);
	assert_true(cbor_float_get_float4(float_ctrl) == 100000.0F);
	assert_true(fabs(cbor_float_get_float(float_ctrl) - 100000.0F) < eps);
	cbor_decref(&float_ctrl);
	assert_null(float_ctrl);
}

unsigned char float8_data[] = {0xFB, 0x7E, 0x37, 0xE4, 0x3C, 0x88, 0x00, 0x75, 0x9C};

static void test_float8(void **state)
{
	float_ctrl = cbor_load(float8_data, 9, &res);
	assert_true(cbor_isa_float_ctrl(float_ctrl));
	assert_true(cbor_is_float(float_ctrl));
	assert_true(cbor_float_get_width(float_ctrl) == CBOR_FLOAT_64);
	// XXX: the cast prevents promotion to 80-bit floats on 32-bit x86
	assert_true(cbor_float_get_float8(float_ctrl) == (double) 1.0e+300);
	cbor_decref(&float_ctrl);
	assert_null(float_ctrl);
}

unsigned char null_data[] = {0xF6};

static void test_null(void **state)
{
	float_ctrl = cbor_load(null_data, 1, &res);
	assert_true(cbor_isa_float_ctrl(float_ctrl));
	assert_true(cbor_is_null(float_ctrl));
	cbor_decref(&float_ctrl);
	assert_null(float_ctrl);
}

unsigned char undef_data[] = {0xF7};

static void test_undef(void **state)
{
	float_ctrl = cbor_load(undef_data, 1, &res);
	assert_true(cbor_isa_float_ctrl(float_ctrl));
	assert_true(cbor_is_undef(float_ctrl));
	cbor_decref(&float_ctrl);
	assert_null(float_ctrl);
}

unsigned char bool_data[] = {0xF4, 0xF5};

static void test_bool(void **state)
{
	float_ctrl = cbor_load(bool_data, 1, &res);
	assert_true(cbor_isa_float_ctrl(float_ctrl));
	assert_true(cbor_is_bool(float_ctrl));
	assert_false(cbor_ctrl_is_bool(float_ctrl));
	cbor_decref(&float_ctrl);
	assert_null(float_ctrl);

	float_ctrl = cbor_load(bool_data + 1, 1, &res);
	assert_true(cbor_isa_float_ctrl(float_ctrl));
	assert_true(cbor_is_bool(float_ctrl));
	assert_true(cbor_ctrl_is_bool(float_ctrl));
	cbor_decref(&float_ctrl);
	assert_null(float_ctrl);
}

int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_float2),
		cmocka_unit_test(test_float4),
		cmocka_unit_test(test_float8),
		cmocka_unit_test(test_null),
		cmocka_unit_test(test_undef),
		cmocka_unit_test(test_bool)
	};
	return cmocka_run_group_tests(tests, NULL, NULL);
}