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
|
//
// Copyright 2015-2016 Ettus Research LLC
//
// SPDX-License-Identifier: GPL-3.0
//
#include <uhd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define UHD_TEST_EXECUTE_OR_GOTO(label, ...) \
if(__VA_ARGS__){ \
fprintf(stderr, "Error occurred at %s:%d\n", __FILE__, (__LINE__-1)); \
return_code = EXIT_FAILURE; \
goto label; \
}
#define UHD_TEST_CHECK_CLOSE(lhs, rhs) (fabs(lhs-rhs) < 0.001)
#define BUFFER_SIZE 1024
static UHD_INLINE int test_range_values(
const uhd_range_t *range,
double start_input, double stop_input, double step_input
){
if(!UHD_TEST_CHECK_CLOSE(range->start, start_input)){
fprintf(stderr, "%s:%d: Starts did not match: %f vs. %f\n",
__FILE__, __LINE__,
range->start, start_input);
return 1;
}
if(!UHD_TEST_CHECK_CLOSE(range->stop, stop_input)){
fprintf(stderr, "%s:%d: Stops did not match: %f vs. %f\n",
__FILE__, __LINE__,
range->stop, stop_input);
return 1;
}
if(!UHD_TEST_CHECK_CLOSE(range->step, step_input)){
fprintf(stderr, "%s:%d: Steps did not match: %f vs. %f\n",
__FILE__, __LINE__,
range->step, step_input);
return 1;
}
return 0;
}
static UHD_INLINE int test_meta_range_values(
uhd_meta_range_handle meta_range,
double start_input, double stop_input, double step_input,
double start_test, double stop_test, double step_test
){
// Add range
uhd_range_t range;
range.start = start_input;
range.stop = stop_input;
range.step = step_input;
if(uhd_meta_range_push_back(meta_range, &range)){
fprintf(stderr, "%s:%d: Failed to push back range.\n",
__FILE__, __LINE__);
return 1;
}
// Test bounds
uhd_meta_range_start(meta_range, &range.start);
if(!UHD_TEST_CHECK_CLOSE(range.start, start_test)){
fprintf(stderr, "%s:%d: Starts did not match: %f vs. %f\n",
__FILE__, __LINE__,
range.start, start_test);
return 1;
}
uhd_meta_range_stop(meta_range, &range.stop);
if(!UHD_TEST_CHECK_CLOSE(range.stop, stop_test)){
fprintf(stderr, "%s:%d: Stops did not match: %f vs. %f\n",
__FILE__, __LINE__,
range.stop, stop_test);
return 1;
}
uhd_meta_range_step(meta_range, &range.step);
if(!UHD_TEST_CHECK_CLOSE(range.step, step_test)){
fprintf(stderr, "%s:%d: Steps did not match: %f vs. %f\n",
__FILE__, __LINE__,
range.step, step_test);
return 1;
}
return 0;
}
static UHD_INLINE int test_meta_range_clip(
uhd_meta_range_handle meta_range,
double clip_value, double test_value,
bool clip_step
){
double clip_result;
uhd_meta_range_clip(meta_range, clip_value, clip_step, &clip_result);
if(!UHD_TEST_CHECK_CLOSE(test_value, clip_result)){
fprintf(stderr, "%s:%d: Values did not match: %f vs. %f\n",
__FILE__, __LINE__,
test_value, clip_result);
return 1;
}
return 0;
}
int main(){
// Variables
int return_code;
uhd_range_t range;
uhd_meta_range_handle meta_range1, meta_range2;
char str_buffer[BUFFER_SIZE];
size_t size;
return_code = EXIT_SUCCESS;
// Create meta range 1
UHD_TEST_EXECUTE_OR_GOTO(end_of_test,
uhd_meta_range_make(&meta_range1)
)
// Test bounds
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_meta_range_values(meta_range1, -1.0, +1.0, 0.1,
-1.0, +1.0, 0.1)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_meta_range_values(meta_range1, 40.0, 60.0, 1.0,
-1.0, 60.0, 0.1)
)
uhd_meta_range_at(meta_range1, 0, &range);
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_range_values(&range, -1.0, +1.0, 0.1)
)
// Check meta range size
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
uhd_meta_range_size(meta_range1, &size)
)
if(size != 2){
fprintf(stderr, "%s:%d: Invalid size: %lu vs. 2",
__FILE__, __LINE__,
(unsigned long)size);
goto free_meta_range1;
}
// Test clipping (with steps)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_meta_range_clip(meta_range1, -30.0, -1.0, false)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_meta_range_clip(meta_range1, 70.0, 60.0, false)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_meta_range_clip(meta_range1, 20.0, 1.0, false)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_meta_range_clip(meta_range1, 50.0, 50.0, false)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_meta_range_clip(meta_range1, 50.9, 50.9, false)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
test_meta_range_clip(meta_range1, 50.9, 51.0, true)
)
// Create meta range 2
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range1,
uhd_meta_range_make(&meta_range2)
)
range.step = 0.0;
range.start = range.stop = 1.;
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2,
uhd_meta_range_push_back(meta_range2, &range)
)
range.start = range.stop = 2.;
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2,
uhd_meta_range_push_back(meta_range2, &range)
)
range.start = range.stop = 3.;
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2,
uhd_meta_range_push_back(meta_range2, &range)
)
// Test clipping (without steps)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2,
test_meta_range_clip(meta_range2, 2., 2., true)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2,
test_meta_range_clip(meta_range2, 0., 1., true)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2,
test_meta_range_clip(meta_range2, 1.2, 1., true)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2,
test_meta_range_clip(meta_range2, 3.1, 3., true)
)
UHD_TEST_EXECUTE_OR_GOTO(free_meta_range2,
test_meta_range_clip(meta_range2, 4., 3., true)
)
free_meta_range2:
if(return_code){
uhd_meta_range_last_error(meta_range2, str_buffer, BUFFER_SIZE);
fprintf(stderr, "meta_range2 error: %s\n", str_buffer);
}
uhd_meta_range_free(&meta_range1);
free_meta_range1:
if(return_code){
uhd_meta_range_last_error(meta_range1, str_buffer, BUFFER_SIZE);
fprintf(stderr, "meta_range1 error: %s\n", str_buffer);
}
uhd_meta_range_free(&meta_range1);
end_of_test:
if(!return_code){
printf("\nNo errors detected.\n");
}
return return_code;
}
|