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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
//
// Copyright 2014 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include <uhd/utils/math.hpp>
#include <boost/test/unit_test.hpp>
using namespace uhd::math::fp_compare;
BOOST_AUTO_TEST_CASE(fp_compare_delta_constructors) {
// Test default constructor
fp_compare_delta<float> alpha = fp_compare_delta<float>(7457392.0);
fp_compare_delta<float> beta = fp_compare_delta<float>(alpha._value);
BOOST_CHECK_EQUAL(alpha._value, beta._value);
BOOST_CHECK_EQUAL(alpha._delta, beta._delta);
// Test constructor with specified delta
fp_compare_delta<float> foxtrot = fp_compare_delta<float>(alpha._value,
uhd::math::SINGLE_PRECISION_DELTA);
fp_compare_delta<float> gamma = fp_compare_delta<float>(alpha._value,
2 * uhd::math::SINGLE_PRECISION_DELTA);
BOOST_CHECK_EQUAL(alpha._delta, foxtrot._delta);
BOOST_CHECK(not (alpha._delta == gamma._delta));
// Test copy-constructor
fp_compare_delta<float> charlie = fp_compare_delta<float>(alpha);
BOOST_CHECK_EQUAL(alpha._value, charlie._value);
BOOST_CHECK_EQUAL(alpha._delta, charlie._delta);
// Test assignment operator
fp_compare_delta<float> delta = beta;
BOOST_CHECK_EQUAL(alpha._value, delta._value);
BOOST_CHECK_EQUAL(alpha._delta, delta._delta);
}
BOOST_AUTO_TEST_CASE(double_compare_constructors) {
// Test default constructor
fp_compare_delta<double> alpha = fp_compare_delta<double>(45739210286.0101);
fp_compare_delta<double> beta = fp_compare_delta<double>(alpha._value);
BOOST_CHECK_EQUAL(alpha._value, beta._value);
BOOST_CHECK_EQUAL(alpha._delta, beta._delta);
// Test constructor with specified delta
fp_compare_delta<double> foxtrot = fp_compare_delta<double>(alpha._value,
uhd::math::DOUBLE_PRECISION_DELTA);
fp_compare_delta<double> gamma = fp_compare_delta<double>(alpha._value, 2.0e-6);
BOOST_CHECK_EQUAL(alpha._delta, foxtrot._delta);
BOOST_CHECK(not (alpha._delta == gamma._delta));
// Test copy-constructor
fp_compare_delta<double> charlie = fp_compare_delta<double>(alpha);
BOOST_CHECK_EQUAL(alpha._value, charlie._value);
BOOST_CHECK_EQUAL(alpha._delta, charlie._delta);
// Test assignment operator
fp_compare_delta<double> delta = beta;
BOOST_CHECK_EQUAL(alpha._value, delta._value);
BOOST_CHECK_EQUAL(alpha._delta, delta._delta);
}
BOOST_AUTO_TEST_CASE(float_equality_operators) {
// Test basic equality operator
fp_compare_delta<float> alpha = fp_compare_delta<float>(1.0);
fp_compare_delta<float> beta = fp_compare_delta<float>(alpha._value);
BOOST_CHECK(alpha == beta);
BOOST_CHECK(alpha == float(alpha._value));
// Test equality edge case at difference = delta
fp_compare_delta<float> charlie = fp_compare_delta<float>(alpha._value
+ uhd::math::SINGLE_PRECISION_DELTA);
BOOST_CHECK(not (alpha == charlie));
BOOST_CHECK(not (alpha == float(alpha._value + uhd::math::SINGLE_PRECISION_DELTA)));
}
BOOST_AUTO_TEST_CASE(double_equality_operators) {
// Test basic equality operator
fp_compare_delta<double> alpha = fp_compare_delta<double>(1.0);
fp_compare_delta<double> beta = fp_compare_delta<double>(alpha._value);
BOOST_CHECK(alpha == beta);
BOOST_CHECK(alpha == double(beta._value));
// Test equality edge case at delta = delta
fp_compare_delta<double> charlie = fp_compare_delta<double>(alpha._value
+ uhd::math::DOUBLE_PRECISION_DELTA);
BOOST_CHECK(not (alpha == charlie));
BOOST_CHECK(not (alpha == double(alpha._value + uhd::math::DOUBLE_PRECISION_DELTA)));
}
BOOST_AUTO_TEST_CASE(float_inequality_operators) {
// Test inequality operator, which is based on equality operator
fp_compare_delta<float> alpha = fp_compare_delta<float>(127.0f);
fp_compare_delta<float> beta = fp_compare_delta<float>(alpha._value + 1.19e-3f);
BOOST_CHECK(alpha != beta);
BOOST_CHECK(alpha != float(alpha._value + 1.19e-3));
}
BOOST_AUTO_TEST_CASE(double_inequality_operators) {
// Test inequality operator, which is based on equality operator
fp_compare_delta<double> alpha = fp_compare_delta<double>(1.0);
fp_compare_delta<double> beta = fp_compare_delta<double>(alpha._value + 1.19e-5);
BOOST_CHECK(alpha != beta);
BOOST_CHECK(alpha != double(alpha._value + 1.19e-5));
}
BOOST_AUTO_TEST_CASE(float_lessthan_operators) {
// Test less-than operator
fp_compare_delta<float> alpha = fp_compare_delta<float>(274192.7f);
fp_compare_delta<float> beta = fp_compare_delta<float>(alpha._value - 0.2f);
BOOST_CHECK(beta < alpha);
BOOST_CHECK(float(alpha._value - 0.2) < alpha);
// Confirm false less-than case
fp_compare_delta<float> charlie = fp_compare_delta<float>(alpha._value - 1.2f);
BOOST_CHECK(not (alpha < charlie));
BOOST_CHECK(not (alpha < float(alpha._value - 1.2f)));
}
BOOST_AUTO_TEST_CASE(double_lessthan_operators) {
// Test less-than operator
fp_compare_delta<double> alpha = fp_compare_delta<double>(274192856.762312);
fp_compare_delta<double> beta = fp_compare_delta<double>(alpha._value - 0.0002);
BOOST_CHECK(beta < alpha);
BOOST_CHECK(double(alpha._value - 0.0002) < alpha);
// Confirm false less-than case
fp_compare_delta<double> charlie = fp_compare_delta<double>(alpha._value - 1.0012);
BOOST_CHECK(not (alpha < charlie));
BOOST_CHECK(not (alpha < double(alpha._value - 1.0012)));
}
BOOST_AUTO_TEST_CASE(float_lessthanequals_operators) {
// Test that <= correctly reports for equal values
fp_compare_delta<float> alpha = fp_compare_delta<float>(827.3f);
fp_compare_delta<float> beta = fp_compare_delta<float>(alpha._value);
BOOST_CHECK(alpha <= beta);
BOOST_CHECK(alpha <= float(alpha._value));
// Test that <= correctly reports for less-than values
fp_compare_delta<float> charlie = fp_compare_delta<float>(alpha._value - 1.2f);
BOOST_CHECK(charlie <= alpha);
BOOST_CHECK(float(alpha._value - 1.2) <= alpha);
}
BOOST_AUTO_TEST_CASE(double_lessthanequals_operators) {
// Test that <= correctly reports for equal values
fp_compare_delta<double> alpha = fp_compare_delta<double>(837652123.383764);
fp_compare_delta<double> beta = fp_compare_delta<double>(alpha._value);
BOOST_CHECK(alpha <= beta);
BOOST_CHECK(alpha <= double(alpha._value));
// Test that <= correctly reports for less-than values
fp_compare_delta<double> charlie = fp_compare_delta<double>(alpha._value - 0.0012);
BOOST_CHECK(charlie <= alpha);
BOOST_CHECK(double(alpha._value - 0.0012) <= alpha);
}
BOOST_AUTO_TEST_CASE(float_greaterthan_operators) {
// Test basic greater-than functionality
fp_compare_delta<float> alpha = fp_compare_delta<float>(98325.4f);
fp_compare_delta<float> beta = fp_compare_delta<float>(alpha._value + 0.15f);
BOOST_CHECK(beta > alpha);
BOOST_CHECK(float(alpha._value + 0.15) > alpha);
// Test false greater-than case
fp_compare_delta<float> charlie = fp_compare_delta<float>(alpha._value + 1.2f);
BOOST_CHECK(not (alpha > charlie));
BOOST_CHECK(not (alpha > float(alpha._value + 1.2)));
}
BOOST_AUTO_TEST_CASE(double_greaterthan_operators) {
// Test basic greater-than functionality
fp_compare_delta<double> alpha = fp_compare_delta<double>(643907213.428475);
fp_compare_delta<double> beta = fp_compare_delta<double>(alpha._value + 0.0002);
BOOST_CHECK(beta > alpha);
BOOST_CHECK(double(alpha._value + 0.0002) > alpha);
// Test false greater-than case
fp_compare_delta<double> charlie = fp_compare_delta<double>(alpha._value + 0.0012);
BOOST_CHECK(not (alpha > charlie));
BOOST_CHECK(not (alpha > double(alpha._value + 0.0012)));
}
BOOST_AUTO_TEST_CASE(float_greaterthanequals_operators) {
// Test that >= correctly reports for equal values
fp_compare_delta<float> alpha = fp_compare_delta<float>(7834.89f);
fp_compare_delta<float> beta = fp_compare_delta<float>(alpha._value);
BOOST_CHECK(alpha >= beta);
BOOST_CHECK(alpha >= float(alpha._value));
// Test that >= correctly reports for greater-than values
fp_compare_delta<float> charlie = fp_compare_delta<float>(alpha._value + 4.8f);
BOOST_CHECK(charlie >= alpha);
BOOST_CHECK(float(alpha._value + 4.8) >= alpha);
}
BOOST_AUTO_TEST_CASE(double_greaterthanequals_operators) {
// Test that >= correctly reports for equal values
fp_compare_delta<double> alpha = fp_compare_delta<double>(737623834.89843);
fp_compare_delta<double> beta = fp_compare_delta<double>(alpha._value);
BOOST_CHECK(alpha >= beta);
BOOST_CHECK(alpha >= double(alpha._value));
// Test that >= correctly reports for greater-than values
fp_compare_delta<double> charlie = fp_compare_delta<double>(alpha._value + 3.0008);
BOOST_CHECK(charlie >= alpha);
BOOST_CHECK(double(alpha._value + 3.0008) >= alpha);
}
BOOST_AUTO_TEST_CASE(fp_compare_large_delta) {
BOOST_CHECK(fp_compare_delta<double>(61440000.047870710492, 0.1) == 61440000.000000000000);
BOOST_CHECK(fp_compare_delta<double>(61440000.047870710492, 0.1) <= 61440000.000000000000);
BOOST_CHECK(fp_compare_delta<double>(61440000.047870710492, 0.1) >= 61440000.000000000000);
BOOST_CHECK(fp_compare_delta<double>(1.0, 10.0) == 2.0);
}
BOOST_AUTO_TEST_CASE(frequency_compare_function) {
BOOST_CHECK(uhd::math::frequencies_are_equal(6817333232.0, 6817333232.0));
BOOST_CHECK(!uhd::math::frequencies_are_equal(6817333233.0, 6817333232.0));
BOOST_CHECK(uhd::math::frequencies_are_equal(6817333232.1, 6817333232.1));
BOOST_CHECK(!uhd::math::frequencies_are_equal(6817333232.5, 6817333232.6));
BOOST_CHECK(uhd::math::frequencies_are_equal(16.8173332321e9, 16.8173332321e9));
BOOST_CHECK(!uhd::math::frequencies_are_equal(16.8173332322e9, 16.8173332321e9));
BOOST_CHECK(!uhd::math::frequencies_are_equal(5.0, 4.0));
BOOST_CHECK(uhd::math::frequencies_are_equal(48750000.0, 48749999.9946));
}
|