]>
Commit | Line | Data |
---|---|---|
79bb0e97 UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
79bb0e97 UH |
18 | */ |
19 | ||
6ec6c43b | 20 | #include <config.h> |
79bb0e97 | 21 | #include <check.h> |
4960aeb0 | 22 | #include <libsigrok/libsigrok.h> |
17794067 | 23 | #include "lib.h" |
79bb0e97 | 24 | |
79bb0e97 UH |
25 | static void test_samplerate(uint64_t samplerate, const char *expected) |
26 | { | |
27 | char *s; | |
28 | ||
29 | s = sr_samplerate_string(samplerate); | |
30 | fail_unless(s != NULL); | |
31 | fail_unless(!strcmp(s, expected), | |
32 | "Invalid result for '%s': %s.", expected, s); | |
33 | g_free(s); | |
34 | } | |
35 | ||
6984cfb2 | 36 | static void test_period(uint64_t v_p, uint64_t v_q, const char *expected) |
5223412e SB |
37 | { |
38 | char *s; | |
39 | ||
6984cfb2 | 40 | s = sr_period_string(v_p, v_q); |
5223412e SB |
41 | fail_unless(s != NULL); |
42 | fail_unless(!strcmp(s, expected), | |
43 | "Invalid result for '%s': %s.", expected, s); | |
44 | g_free(s); | |
45 | } | |
46 | ||
5ec172d7 SB |
47 | static void test_rational(const char *input, struct sr_rational expected) |
48 | { | |
49 | int ret; | |
50 | struct sr_rational rational; | |
51 | ||
52 | ret = sr_parse_rational(input, &rational); | |
53 | fail_unless(ret == SR_OK); | |
54 | fail_unless((expected.p == rational.p) && (expected.q == rational.q), | |
55 | "Invalid result for '%s': %ld/%ld'.", | |
56 | input, rational.p, rational.q); | |
57 | } | |
58 | ||
c911599d UH |
59 | static void test_voltage(uint64_t v_p, uint64_t v_q, const char *expected) |
60 | { | |
61 | char *s; | |
62 | ||
63 | s = sr_voltage_string(v_p, v_q); | |
64 | fail_unless(s != NULL); | |
65 | fail_unless(!strcmp(s, expected), | |
66 | "Invalid result for '%s': %s.", expected, s); | |
67 | g_free(s); | |
68 | } | |
69 | ||
79bb0e97 UH |
70 | /* |
71 | * Check various inputs for sr_samplerate_string(): | |
72 | * | |
73 | * - One, two, or three digit results (e.g. 5/55/555 MHz). | |
74 | * - Results which contain commas (e.g. 1.234 / 12.34 / 123.4 kHz). | |
75 | * - Results with zeroes right after the comma (e.g. 1.034 Hz). | |
76 | * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=73 | |
79bb0e97 UH |
77 | * - Results with zeroes in the middle (e.g. 1.204 kHz). |
78 | * - All of the above, but using SR_MHZ() and friends. | |
79 | * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=72 | |
80 | * | |
81 | * All of the above tests are done for the Hz/kHz/MHz/GHz ranges. | |
82 | */ | |
83 | ||
84 | START_TEST(test_hz) | |
85 | { | |
86 | test_samplerate(0, "0 Hz"); | |
87 | test_samplerate(1, "1 Hz"); | |
88 | test_samplerate(23, "23 Hz"); | |
89 | test_samplerate(644, "644 Hz"); | |
90 | test_samplerate(604, "604 Hz"); | |
91 | test_samplerate(550, "550 Hz"); | |
92 | ||
93 | /* Again, but now using SR_HZ(). */ | |
94 | test_samplerate(SR_HZ(0), "0 Hz"); | |
95 | test_samplerate(SR_HZ(1), "1 Hz"); | |
96 | test_samplerate(SR_HZ(23), "23 Hz"); | |
97 | test_samplerate(SR_HZ(644), "644 Hz"); | |
98 | test_samplerate(SR_HZ(604), "604 Hz"); | |
99 | test_samplerate(SR_HZ(550), "550 Hz"); | |
100 | } | |
101 | END_TEST | |
102 | ||
103 | START_TEST(test_khz) | |
104 | { | |
105 | test_samplerate(1000, "1 kHz"); | |
106 | test_samplerate(99000, "99 kHz"); | |
107 | test_samplerate(225000, "225 kHz"); | |
108 | test_samplerate(1234, "1.234 kHz"); | |
109 | test_samplerate(12345, "12.345 kHz"); | |
110 | test_samplerate(123456, "123.456 kHz"); | |
111 | test_samplerate(1034, "1.034 kHz"); | |
112 | test_samplerate(1004, "1.004 kHz"); | |
ba253f2b | 113 | test_samplerate(1230, "1.23 kHz"); |
79bb0e97 UH |
114 | |
115 | /* Again, but now using SR_KHZ(). */ | |
116 | test_samplerate(SR_KHZ(1), "1 kHz"); | |
117 | test_samplerate(SR_KHZ(99), "99 kHz"); | |
118 | test_samplerate(SR_KHZ(225), "225 kHz"); | |
119 | test_samplerate(SR_KHZ(1.234), "1.234 kHz"); | |
120 | test_samplerate(SR_KHZ(12.345), "12.345 kHz"); | |
121 | test_samplerate(SR_KHZ(123.456), "123.456 kHz"); | |
122 | test_samplerate(SR_KHZ(1.204), "1.204 kHz"); | |
123 | test_samplerate(SR_KHZ(1.034), "1.034 kHz"); | |
124 | test_samplerate(SR_KHZ(1.004), "1.004 kHz"); | |
ba253f2b | 125 | test_samplerate(SR_KHZ(1.230), "1.23 kHz"); |
79bb0e97 UH |
126 | } |
127 | END_TEST | |
128 | ||
129 | START_TEST(test_mhz) | |
130 | { | |
131 | test_samplerate(1000000, "1 MHz"); | |
132 | test_samplerate(28000000, "28 MHz"); | |
133 | test_samplerate(775000000, "775 MHz"); | |
134 | test_samplerate(1234567, "1.234567 MHz"); | |
135 | test_samplerate(12345678, "12.345678 MHz"); | |
136 | test_samplerate(123456789, "123.456789 MHz"); | |
137 | test_samplerate(1230007, "1.230007 MHz"); | |
138 | test_samplerate(1034567, "1.034567 MHz"); | |
139 | test_samplerate(1000007, "1.000007 MHz"); | |
ba253f2b | 140 | test_samplerate(1234000, "1.234 MHz"); |
79bb0e97 UH |
141 | |
142 | /* Again, but now using SR_MHZ(). */ | |
143 | test_samplerate(SR_MHZ(1), "1 MHz"); | |
144 | test_samplerate(SR_MHZ(28), "28 MHz"); | |
145 | test_samplerate(SR_MHZ(775), "775 MHz"); | |
146 | test_samplerate(SR_MHZ(1.234567), "1.234567 MHz"); | |
147 | test_samplerate(SR_MHZ(12.345678), "12.345678 MHz"); | |
148 | test_samplerate(SR_MHZ(123.456789), "123.456789 MHz"); | |
149 | test_samplerate(SR_MHZ(1.230007), "1.230007 MHz"); | |
150 | test_samplerate(SR_MHZ(1.034567), "1.034567 MHz"); | |
151 | test_samplerate(SR_MHZ(1.000007), "1.000007 MHz"); | |
ba253f2b | 152 | test_samplerate(SR_MHZ(1.234000), "1.234 MHz"); |
79bb0e97 UH |
153 | } |
154 | END_TEST | |
155 | ||
156 | START_TEST(test_ghz) | |
157 | { | |
158 | /* Note: Numbers > 2^32 need a ULL suffix. */ | |
159 | ||
160 | test_samplerate(1000000000, "1 GHz"); | |
161 | test_samplerate(5000000000ULL, "5 GHz"); | |
162 | test_samplerate(72000000000ULL, "72 GHz"); | |
163 | test_samplerate(388000000000ULL, "388 GHz"); | |
164 | test_samplerate(4417594444ULL, "4.417594444 GHz"); | |
165 | test_samplerate(44175944444ULL, "44.175944444 GHz"); | |
166 | test_samplerate(441759444441ULL, "441.759444441 GHz"); | |
167 | test_samplerate(441759000001ULL, "441.759000001 GHz"); | |
168 | test_samplerate(441050000000ULL, "441.05 GHz"); | |
169 | test_samplerate(441000000005ULL, "441.000000005 GHz"); | |
ba253f2b | 170 | test_samplerate(441500000000ULL, "441.5 GHz"); |
79bb0e97 UH |
171 | |
172 | /* Again, but now using SR_GHZ(). */ | |
173 | test_samplerate(SR_GHZ(1), "1 GHz"); | |
174 | test_samplerate(SR_GHZ(5), "5 GHz"); | |
175 | test_samplerate(SR_GHZ(72), "72 GHz"); | |
176 | test_samplerate(SR_GHZ(388), "388 GHz"); | |
177 | test_samplerate(SR_GHZ(4.417594444), "4.417594444 GHz"); | |
178 | test_samplerate(SR_GHZ(44.175944444), "44.175944444 GHz"); | |
179 | test_samplerate(SR_GHZ(441.759444441), "441.759444441 GHz"); | |
180 | test_samplerate(SR_GHZ(441.759000001), "441.759000001 GHz"); | |
181 | test_samplerate(SR_GHZ(441.050000000), "441.05 GHz"); | |
182 | test_samplerate(SR_GHZ(441.000000005), "441.000000005 GHz"); | |
ba253f2b | 183 | test_samplerate(SR_GHZ(441.500000000), "441.5 GHz"); |
79bb0e97 UH |
184 | |
185 | /* Now check the biggest-possible samplerate (2^64 Hz). */ | |
059f3632 UH |
186 | // test_samplerate(18446744073709551615ULL, "18446744073.709551615 GHz"); |
187 | // test_samplerate(SR_GHZ(18446744073ULL), "18446744073 GHz"); | |
79bb0e97 UH |
188 | } |
189 | END_TEST | |
190 | ||
5223412e SB |
191 | START_TEST(test_hz_period) |
192 | { | |
6984cfb2 SA |
193 | test_period(1, 1, "1 s"); |
194 | test_period(1, 5, "200 ms"); | |
195 | test_period(1, 72, "13.889 ms"); | |
196 | test_period(1, 388, "2.577 ms"); | |
197 | test_period(10, 1000, "10 ms"); | |
5223412e SB |
198 | |
199 | /* Again, but now using SR_HZ(). */ | |
6984cfb2 SA |
200 | test_period(1, SR_HZ(1), "1 s"); |
201 | test_period(1, SR_HZ(5), "200 ms"); | |
202 | test_period(1, SR_HZ(72), "13.889 ms"); | |
203 | test_period(1, SR_HZ(388), "2.577 ms"); | |
204 | test_period(10, SR_HZ(100), "100 ms"); | |
5223412e SB |
205 | } |
206 | END_TEST | |
207 | ||
208 | START_TEST(test_ghz_period) | |
209 | { | |
210 | /* Note: Numbers > 2^32 need a ULL suffix. */ | |
6984cfb2 SA |
211 | test_period(1, 1000000000, "1 ns"); |
212 | test_period(1, 5000000000ULL, "200 ps"); | |
213 | test_period(1, 72000000000ULL, "13.889 ps"); | |
214 | test_period(1, 388000000000ULL, "2.577 ps"); | |
215 | test_period(10, 1000000000000, "10 ps"); | |
216 | test_period(200, 1000000000000ULL, "200 ps"); | |
5223412e SB |
217 | |
218 | /* Again, but now using SR_GHZ(). */ | |
6984cfb2 SA |
219 | test_period(1, SR_GHZ(1), "1 ns"); |
220 | test_period(1, SR_GHZ(5), "200 ps"); | |
221 | test_period(1, SR_GHZ(72), "13.889 ps"); | |
222 | test_period(1, SR_GHZ(388), "2.577 ps"); | |
223 | test_period(10, SR_GHZ(1), "10 ns"); | |
224 | test_period(200, SR_GHZ(1000), "200 ps"); | |
5223412e SB |
225 | } |
226 | END_TEST | |
227 | ||
c911599d UH |
228 | START_TEST(test_volt) |
229 | { | |
b5df922e UH |
230 | test_voltage(34, 1, "34 V"); |
231 | test_voltage(34, 2, "17 V"); | |
232 | test_voltage(1, 1, "1 V"); | |
233 | test_voltage(1, 5, "0.2 V"); | |
234 | test_voltage(200, 1000, "200 mV"); | |
235 | test_voltage(1, 72, "0.0138889 V"); | |
236 | test_voltage(1, 388, "0.00257732 V"); | |
237 | test_voltage(10, 1000, "10 mV"); | |
c911599d UH |
238 | } |
239 | END_TEST | |
240 | ||
5ec172d7 SB |
241 | START_TEST(test_integral) |
242 | { | |
243 | test_rational("1", (struct sr_rational){1, 1}); | |
244 | test_rational("2", (struct sr_rational){2, 1}); | |
245 | test_rational("10", (struct sr_rational){10, 1}); | |
246 | test_rational("-255", (struct sr_rational){-255, 1}); | |
247 | } | |
248 | END_TEST | |
249 | ||
250 | START_TEST(test_fractional) | |
251 | { | |
252 | test_rational("0.1", (struct sr_rational){1, 10}); | |
253 | test_rational("1.0", (struct sr_rational){10, 10}); | |
254 | test_rational("1.2", (struct sr_rational){12, 10}); | |
255 | test_rational("12.34", (struct sr_rational){1234, 100}); | |
256 | test_rational("-12.34", (struct sr_rational){-1234, 100}); | |
257 | test_rational("10.00", (struct sr_rational){1000, 100}); | |
258 | } | |
259 | END_TEST | |
260 | ||
261 | START_TEST(test_exponent) | |
262 | { | |
263 | test_rational("1e0", (struct sr_rational){1, 1}); | |
264 | test_rational("1E0", (struct sr_rational){1, 1}); | |
265 | test_rational("1E1", (struct sr_rational){10, 1}); | |
266 | test_rational("1e-1", (struct sr_rational){1, 10}); | |
267 | test_rational("-1.234e-0", (struct sr_rational){-1234, 1000}); | |
268 | test_rational("-1.234e3", (struct sr_rational){-1234, 1}); | |
269 | test_rational("-1.234e-3", (struct sr_rational){-1234, 1000000}); | |
270 | test_rational("0.001e3", (struct sr_rational){1, 1}); | |
271 | test_rational("0.001e0", (struct sr_rational){1, 1000}); | |
272 | test_rational("0.001e-3", (struct sr_rational){1, 1000000}); | |
273 | } | |
274 | END_TEST | |
275 | ||
79bb0e97 UH |
276 | Suite *suite_strutil(void) |
277 | { | |
278 | Suite *s; | |
279 | TCase *tc; | |
280 | ||
281 | s = suite_create("strutil"); | |
282 | ||
283 | tc = tcase_create("sr_samplerate_string"); | |
98de0c78 | 284 | tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown); |
79bb0e97 UH |
285 | tcase_add_test(tc, test_hz); |
286 | tcase_add_test(tc, test_khz); | |
287 | tcase_add_test(tc, test_mhz); | |
288 | tcase_add_test(tc, test_ghz); | |
5223412e SB |
289 | tcase_add_test(tc, test_hz_period); |
290 | tcase_add_test(tc, test_ghz_period); | |
c911599d | 291 | tcase_add_test(tc, test_volt); |
5ec172d7 SB |
292 | tcase_add_test(tc, test_integral); |
293 | tcase_add_test(tc, test_fractional); | |
294 | tcase_add_test(tc, test_exponent); | |
79bb0e97 UH |
295 | suite_add_tcase(s, tc); |
296 | ||
297 | return s; | |
298 | } |