aboutsummaryrefslogtreecommitdiffstats
path: root/test/float.c
blob: 9940ff474da4998dee1af136fe638e3d1f77875d (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * The Rin Library – library "conformance" tests
 *
 * Copyright (C) 2015 Gediminas Jakutis
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; version 2.1
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "test.h"
#include "float_test_private.h"

struct test_results float_test(void)
{
	struct test_results ret;

	ret = rin_signbitf_test();
	ret = rin_signbitd_test();
	ret = rin_float_to_hexstring_test();
	ret = rin_double_to_hexstring_test();
	ret = rin_compare_float_test();
	ret = rin_compare_double_test();

	return ret;
}

static struct test_results rin_signbitf_test(void)
{
	struct test_results ret;
	size_t i;
	static const float in[] = { 1.0f, -1.0f, 987656.4321f, -987656.4321f };
	static const uint32_t expected[] = { 0, 0x80000000u, 0, 0x80000000u };

	for (i = 0; i < 4; ++i) {
		ret = ok(rin_signbitf(in[i]) != expected[i],
				"%s: expected: %"PRIu32", got: %"PRIu32" (%1.8e)",
				__func__,
				expected[i],
				rin_signbitf(in[i]),
				in[i]);
	}

	return ret;
}

static struct test_results rin_signbitd_test(void)
{
	struct test_results ret;
	size_t i;
	static const double in[] = { 1.0, -1.0, 987656.4321, -987656.4321 };
	static const uint64_t expected[] = { 0, 0x8000000000000000ull, 0, 0x8000000000000000ull };

	for (i = 0; i < 4; ++i) {
		ret = ok(rin_signbitd(in[i]) != expected[i],
				"%s: expected: %"PRIu64", got: %"PRIu64" (%1.16e)",
				__func__,
				expected[i],
				rin_signbitf(in[i]),
				in[i]);
	}

	return ret;
}

static struct test_results rin_float_to_hexstring_test(void)
{
	struct test_results ret;
	size_t i;
	static const float in[] = { 1.0f, -1.0f, 98765.4321f, -98765.4321f };
	static const char *expected_default[] = { "0x3f800000", "0xbf800000", "0x47c0e6b7", "0xc7c0e6b7" };
	static const char *expected_noprefix[] = { "3f800000", "bf800000", "47c0e6b7", "c7c0e6b7" };
	char buffer[16] = { '\0' };

	for (i = 0; i < 4; ++i) {
		ret = ok(strcmp(rin_float_to_hexstring(in[i], buffer, RIN_HEXSTRING_DEFAULT), expected_default[i]),
				"%s: expected: %s, got: %s (%1.8e)",
				__func__,
				expected_default[i],
				rin_float_to_hexstring(in[i], buffer, RIN_HEXSTRING_DEFAULT),
				in[i]);

		ret = ok(strcmp(rin_float_to_hexstring(in[i], buffer, RIN_HEXSTRING_NOPREFIX), expected_noprefix[i]),
				"%s: expected: %s, got: %s (%1.8e)",
				__func__,
				expected_noprefix[i],
				rin_float_to_hexstring(in[i], buffer, RIN_HEXSTRING_NOPREFIX),
				in[i]);
	}

	return ret;
}


static struct test_results rin_double_to_hexstring_test(void)
{
	struct test_results ret;
	size_t i;
	static const double in[] = { 1.0, -1.0, 98765.4321, -98765.4321 };
	static const char *expected_default[] = { "0x3ff0000000000000", "0xbff0000000000000", "0x40f81cd6e9e1b08a", "0xc0f81cd6e9e1b08a" };
	static const char *expected_noprefix[] = { "3ff0000000000000", "bff0000000000000", "40f81cd6e9e1b08a", "c0f81cd6e9e1b08a" };
	char buffer[32] = { '\0' };

	for (i = 0; i < 4; ++i) {
		ret = ok(strcmp(rin_double_to_hexstring(in[i], buffer, RIN_HEXSTRING_DEFAULT), expected_default[i]),
				"%s: expected: %s, got: %s (%1.16e)",
				__func__,
				expected_default[i],
				rin_double_to_hexstring(in[i], buffer, RIN_HEXSTRING_DEFAULT),
				in[i]);

		ret = ok(strcmp(rin_double_to_hexstring(in[i], buffer, RIN_HEXSTRING_NOPREFIX), expected_noprefix[i]),
				"%s: expected: %s, got: %s (%1.16e)",
				__func__,
				expected_noprefix[i],
				rin_double_to_hexstring(in[i], buffer, RIN_HEXSTRING_NOPREFIX),
				in[i]);
	}

	return ret;
}

static struct test_results rin_compare_float_test(void)
{
	struct test_results ret;
	size_t i;
	static const float a[] = {  0.0f, 1.0f, -1.0f, 1.0f,       -1.0f, 1.0f,      98765.4321f, 98765.4321f,  98765.4321f };
	static const float b[] = { -0.0f, 1.0f,  1.0f, 1.0000001f, -9.8f, 1.000001f, 98765.436f,  98777.7777f, -98765.4321f };
	static const unsigned int expected[] = { 0, 0, 1, 0, 1, 1, 0, 1, 1};

	for (i = 0; i < 4; ++i) {
		ret = ok(rin_compare_float(a[i], b[i], 2) != expected[i],
				"%s: expected: %s, got: %s (%1.8e, %1.8e)",
				__func__,
				expected[i] ? "non-equal" : "equal",
				rin_compare_float(a[i], b[i], 2) ? "non-equal" : "equal",
				a[i], b[i]);
	}

	return ret;
}


static struct test_results rin_compare_double_test(void)
{
	struct test_results ret;
	size_t i;
	static const double a[] = {  0.0, 1.0, -1.0, 1.0,                -1.0f, 1.0,      98765.4321,         98765.4321,  98765.4321 };
	static const double b[] = { -0.0, 1.0,  1.0, 1.0000000000000003, -9.8f, 1.000001, 98765.43210000002,  98777.7777, -98765.4321 };
	static const unsigned int expected[] = { 0, 0, 1, 0, 1, 1, 0, 1, 1};

	for (i = 0; i < 4; ++i) {
		ret = ok(rin_compare_double(a[i], b[i], 2) != expected[i],
				"%s: expected: %s, got: %s (%1.16e, %1.16e)",
				__func__,
				expected[i] ? "non-equal" : "equal",
				rin_compare_double(a[i], b[i], 2) ? "non-equal" : "equal",
				a[i], b[i]);
	}

	return ret;
}