]>
Commit | Line | Data |
---|---|---|
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 | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <check.h> | |
22 | #include <locale.h> | |
23 | #include <libsigrok/libsigrok.h> | |
24 | #include "lib.h" | |
25 | ||
26 | #if 0 | |
27 | static void test_vsnprintf(const char *expected, char *format, ...) | |
28 | { | |
29 | va_list args; | |
30 | char *s; | |
31 | int len; | |
32 | ||
33 | len = 16; | |
34 | s = g_malloc0(len + 1); | |
35 | ||
36 | va_start(args, format); | |
37 | len = vsnprintf(s, len, format, args); | |
38 | va_end(args); | |
39 | ||
40 | fail_unless(s != NULL, | |
41 | "Invalid result for '%s': len = %i.", expected, len); | |
42 | fail_unless(!strcmp(s, expected), | |
43 | "Invalid result for '%s': %s.", expected, s); | |
44 | g_free(s); | |
45 | } | |
46 | #endif | |
47 | ||
48 | static void test_sr_vsnprintf_ascii(const char *expected, char *format, ...) | |
49 | { | |
50 | va_list args; | |
51 | char *s; | |
52 | int len; | |
53 | ||
54 | len = 16; | |
55 | s = g_malloc0(len + 1); | |
56 | ||
57 | va_start(args, format); | |
58 | len = sr_vsnprintf_ascii(s, len, format, args); | |
59 | va_end(args); | |
60 | ||
61 | fail_unless(s != NULL, | |
62 | "Invalid result for '%s': len = %i.", expected, len); | |
63 | fail_unless(!strcmp(s, expected), | |
64 | "Invalid result for '%s': %s.", expected, s); | |
65 | g_free(s); | |
66 | } | |
67 | ||
68 | static void test_sr_vsprintf_ascii(const char *expected, char *format, ...) | |
69 | { | |
70 | va_list args, args_copy; | |
71 | char *s; | |
72 | int len; | |
73 | ||
74 | /* Get length of buffer required. */ | |
75 | va_start(args, format); | |
76 | va_copy(args_copy, args); | |
77 | len = sr_vsnprintf_ascii(NULL, 0, format, args); | |
78 | va_end(args); | |
79 | ||
80 | /* Allocate buffer and write out command. */ | |
81 | s = g_malloc0(len + 1); | |
82 | len = sr_vsprintf_ascii(s, format, args_copy); | |
83 | va_end(args_copy); | |
84 | ||
85 | fail_unless(s != NULL, | |
86 | "Invalid result for '%s': len = %i.", expected, len); | |
87 | fail_unless(!strcmp(s, expected), | |
88 | "Invalid result for '%s': %s.", expected, s); | |
89 | g_free(s); | |
90 | } | |
91 | ||
92 | static void test_samplerate(uint64_t samplerate, const char *expected) | |
93 | { | |
94 | char *s; | |
95 | ||
96 | s = sr_samplerate_string(samplerate); | |
97 | fail_unless(s != NULL); | |
98 | fail_unless(!strcmp(s, expected), | |
99 | "Invalid result for '%s': %s.", expected, s); | |
100 | g_free(s); | |
101 | } | |
102 | ||
103 | static void test_period(uint64_t v_p, uint64_t v_q, const char *expected) | |
104 | { | |
105 | char *s; | |
106 | ||
107 | s = sr_period_string(v_p, v_q); | |
108 | fail_unless(s != NULL); | |
109 | fail_unless(!strcmp(s, expected), | |
110 | "Invalid result for '%s': %s.", expected, s); | |
111 | g_free(s); | |
112 | } | |
113 | ||
114 | static void test_rational(const char *input, struct sr_rational expected) | |
115 | { | |
116 | int ret; | |
117 | struct sr_rational rational; | |
118 | ||
119 | ret = sr_parse_rational(input, &rational); | |
120 | fail_unless(ret == SR_OK); | |
121 | fail_unless((expected.p == rational.p) && (expected.q == rational.q), | |
122 | "Invalid result for '%s': %ld/%ld'.", | |
123 | input, rational.p, rational.q); | |
124 | } | |
125 | ||
126 | static void test_voltage(uint64_t v_p, uint64_t v_q, const char *expected) | |
127 | { | |
128 | char *s; | |
129 | ||
130 | s = sr_voltage_string(v_p, v_q); | |
131 | fail_unless(s != NULL); | |
132 | fail_unless(!strcmp(s, expected), | |
133 | "Invalid result for '%s': %s.", expected, s); | |
134 | g_free(s); | |
135 | } | |
136 | ||
137 | START_TEST(test_locale) | |
138 | { | |
139 | char *old_locale, *saved_locale; | |
140 | ||
141 | /* Get the the current locale. */ | |
142 | old_locale = setlocale(LC_NUMERIC, NULL); | |
143 | fprintf(stderr, "Old locale = %s\n", old_locale); | |
144 | /* Copy the name so it won’t be clobbered by setlocale. */ | |
145 | saved_locale = g_strdup(old_locale); | |
146 | ck_assert_msg(saved_locale != NULL); | |
147 | ||
148 | #ifdef _WIN32 | |
149 | /* | |
150 | * See: https://msdn.microsoft.com/en-us/library/cc233982.aspx | |
151 | * Doesn't work! Locale is not set! | |
152 | */ | |
153 | setlocale(LC_NUMERIC, "de-DE"); | |
154 | #else | |
155 | /* | |
156 | * For all *nix and OSX systems, change the locale for all threads to | |
157 | * one that is known for not working correctly with printf(), e.g. | |
158 | * "de_DE.UTF-8". | |
159 | * | |
160 | * Find all your available system locales with "locale -a". | |
161 | */ | |
162 | setlocale(LC_NUMERIC, "de_DE.UTF-8"); | |
163 | #endif | |
164 | fprintf(stderr, "New locale = %s\n", setlocale(LC_NUMERIC, NULL)); | |
165 | ||
166 | test_sr_vsnprintf_ascii("0.1", "%.1f", (double)0.1); | |
167 | test_sr_vsnprintf_ascii("0.12", "%.2f", (double)0.12); | |
168 | test_sr_vsnprintf_ascii("0.123", "%.3f", (double)0.123); | |
169 | test_sr_vsnprintf_ascii("0.1234", "%.4f", (double)0.1234); | |
170 | test_sr_vsnprintf_ascii("0.12345", "%.5f", (double)0.12345); | |
171 | test_sr_vsnprintf_ascii("0.123456", "%.6f", (double)0.123456); | |
172 | ||
173 | test_sr_vsprintf_ascii("0.1", "%.1f", (double)0.1); | |
174 | test_sr_vsprintf_ascii("0.12", "%.2f", (double)0.12); | |
175 | test_sr_vsprintf_ascii("0.123", "%.3f", (double)0.123); | |
176 | test_sr_vsprintf_ascii("0.1234", "%.4f", (double)0.1234); | |
177 | test_sr_vsprintf_ascii("0.12345", "%.5f", (double)0.12345); | |
178 | test_sr_vsprintf_ascii("0.123456", "%.6f", (double)0.123456); | |
179 | ||
180 | #if 0 | |
181 | /* | |
182 | * These tests can be used to tell on which platforms the printf() | |
183 | * functions are locale-dependent (i.e. these tests will fail). | |
184 | */ | |
185 | test_vsnprintf("0.1", "%.1f", (double)0.1); | |
186 | test_vsnprintf("0.12", "%.2f", (double)0.12); | |
187 | test_vsnprintf("0.123", "%.3f", (double)0.123); | |
188 | test_vsnprintf("0.1234", "%.4f", (double)0.1234); | |
189 | test_vsnprintf("0.12345", "%.5f", (double)0.12345); | |
190 | test_vsnprintf("0.123456", "%.6f", (double)0.123456); | |
191 | #endif | |
192 | ||
193 | /* Restore the original locale. */ | |
194 | setlocale(LC_NUMERIC, saved_locale); | |
195 | g_free(saved_locale); | |
196 | } | |
197 | END_TEST | |
198 | ||
199 | /* | |
200 | * Check various inputs for sr_samplerate_string(): | |
201 | * | |
202 | * - One, two, or three digit results (e.g. 5/55/555 MHz). | |
203 | * - Results which contain commas (e.g. 1.234 / 12.34 / 123.4 kHz). | |
204 | * - Results with zeroes right after the comma (e.g. 1.034 Hz). | |
205 | * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=73 | |
206 | * - Results with zeroes in the middle (e.g. 1.204 kHz). | |
207 | * - All of the above, but using SR_MHZ() and friends. | |
208 | * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=72 | |
209 | * | |
210 | * All of the above tests are done for the Hz/kHz/MHz/GHz ranges. | |
211 | */ | |
212 | ||
213 | START_TEST(test_hz) | |
214 | { | |
215 | test_samplerate(0, "0 Hz"); | |
216 | test_samplerate(1, "1 Hz"); | |
217 | test_samplerate(23, "23 Hz"); | |
218 | test_samplerate(644, "644 Hz"); | |
219 | test_samplerate(604, "604 Hz"); | |
220 | test_samplerate(550, "550 Hz"); | |
221 | ||
222 | /* Again, but now using SR_HZ(). */ | |
223 | test_samplerate(SR_HZ(0), "0 Hz"); | |
224 | test_samplerate(SR_HZ(1), "1 Hz"); | |
225 | test_samplerate(SR_HZ(23), "23 Hz"); | |
226 | test_samplerate(SR_HZ(644), "644 Hz"); | |
227 | test_samplerate(SR_HZ(604), "604 Hz"); | |
228 | test_samplerate(SR_HZ(550), "550 Hz"); | |
229 | } | |
230 | END_TEST | |
231 | ||
232 | START_TEST(test_khz) | |
233 | { | |
234 | test_samplerate(1000, "1 kHz"); | |
235 | test_samplerate(99000, "99 kHz"); | |
236 | test_samplerate(225000, "225 kHz"); | |
237 | test_samplerate(1234, "1.234 kHz"); | |
238 | test_samplerate(12345, "12.345 kHz"); | |
239 | test_samplerate(123456, "123.456 kHz"); | |
240 | test_samplerate(1034, "1.034 kHz"); | |
241 | test_samplerate(1004, "1.004 kHz"); | |
242 | test_samplerate(1230, "1.23 kHz"); | |
243 | ||
244 | /* Again, but now using SR_KHZ(). */ | |
245 | test_samplerate(SR_KHZ(1), "1 kHz"); | |
246 | test_samplerate(SR_KHZ(99), "99 kHz"); | |
247 | test_samplerate(SR_KHZ(225), "225 kHz"); | |
248 | test_samplerate(SR_KHZ(1.234), "1.234 kHz"); | |
249 | test_samplerate(SR_KHZ(12.345), "12.345 kHz"); | |
250 | test_samplerate(SR_KHZ(123.456), "123.456 kHz"); | |
251 | test_samplerate(SR_KHZ(1.204), "1.204 kHz"); | |
252 | test_samplerate(SR_KHZ(1.034), "1.034 kHz"); | |
253 | test_samplerate(SR_KHZ(1.004), "1.004 kHz"); | |
254 | test_samplerate(SR_KHZ(1.230), "1.23 kHz"); | |
255 | } | |
256 | END_TEST | |
257 | ||
258 | START_TEST(test_mhz) | |
259 | { | |
260 | test_samplerate(1000000, "1 MHz"); | |
261 | test_samplerate(28000000, "28 MHz"); | |
262 | test_samplerate(775000000, "775 MHz"); | |
263 | test_samplerate(1234567, "1.234567 MHz"); | |
264 | test_samplerate(12345678, "12.345678 MHz"); | |
265 | test_samplerate(123456789, "123.456789 MHz"); | |
266 | test_samplerate(1230007, "1.230007 MHz"); | |
267 | test_samplerate(1034567, "1.034567 MHz"); | |
268 | test_samplerate(1000007, "1.000007 MHz"); | |
269 | test_samplerate(1234000, "1.234 MHz"); | |
270 | ||
271 | /* Again, but now using SR_MHZ(). */ | |
272 | test_samplerate(SR_MHZ(1), "1 MHz"); | |
273 | test_samplerate(SR_MHZ(28), "28 MHz"); | |
274 | test_samplerate(SR_MHZ(775), "775 MHz"); | |
275 | test_samplerate(SR_MHZ(1.234567), "1.234567 MHz"); | |
276 | test_samplerate(SR_MHZ(12.345678), "12.345678 MHz"); | |
277 | test_samplerate(SR_MHZ(123.456789), "123.456789 MHz"); | |
278 | test_samplerate(SR_MHZ(1.230007), "1.230007 MHz"); | |
279 | test_samplerate(SR_MHZ(1.034567), "1.034567 MHz"); | |
280 | test_samplerate(SR_MHZ(1.000007), "1.000007 MHz"); | |
281 | test_samplerate(SR_MHZ(1.234000), "1.234 MHz"); | |
282 | } | |
283 | END_TEST | |
284 | ||
285 | START_TEST(test_ghz) | |
286 | { | |
287 | test_samplerate(UINT64_C(1000000000), "1 GHz"); | |
288 | test_samplerate(UINT64_C(5000000000), "5 GHz"); | |
289 | test_samplerate(UINT64_C(72000000000), "72 GHz"); | |
290 | test_samplerate(UINT64_C(388000000000), "388 GHz"); | |
291 | test_samplerate(UINT64_C(4417594444), "4.417594444 GHz"); | |
292 | test_samplerate(UINT64_C(44175944444), "44.175944444 GHz"); | |
293 | test_samplerate(UINT64_C(441759444441), "441.759444441 GHz"); | |
294 | test_samplerate(UINT64_C(441759000001), "441.759000001 GHz"); | |
295 | test_samplerate(UINT64_C(441050000000), "441.05 GHz"); | |
296 | test_samplerate(UINT64_C(441000000005), "441.000000005 GHz"); | |
297 | test_samplerate(UINT64_C(441500000000), "441.5 GHz"); | |
298 | ||
299 | /* Again, but now using SR_GHZ(). */ | |
300 | test_samplerate(SR_GHZ(1), "1 GHz"); | |
301 | test_samplerate(SR_GHZ(5), "5 GHz"); | |
302 | test_samplerate(SR_GHZ(72), "72 GHz"); | |
303 | test_samplerate(SR_GHZ(388), "388 GHz"); | |
304 | test_samplerate(SR_GHZ(4.417594444), "4.417594444 GHz"); | |
305 | test_samplerate(SR_GHZ(44.175944444), "44.175944444 GHz"); | |
306 | test_samplerate(SR_GHZ(441.759444441), "441.759444441 GHz"); | |
307 | test_samplerate(SR_GHZ(441.759000001), "441.759000001 GHz"); | |
308 | test_samplerate(SR_GHZ(441.050000000), "441.05 GHz"); | |
309 | test_samplerate(SR_GHZ(441.000000005), "441.000000005 GHz"); | |
310 | test_samplerate(SR_GHZ(441.500000000), "441.5 GHz"); | |
311 | ||
312 | /* Now check the biggest-possible samplerate (2^64 Hz). */ | |
313 | // test_samplerate(UINT64_C(18446744073709551615), "18446744073.709551615 GHz"); | |
314 | // test_samplerate(SR_GHZ(UINT64_C(18446744073)), "18446744073 GHz"); | |
315 | } | |
316 | END_TEST | |
317 | ||
318 | START_TEST(test_hz_period) | |
319 | { | |
320 | test_period(1, 1, "1 s"); | |
321 | test_period(1, 5, "200 ms"); | |
322 | test_period(1, 72, "13.889 ms"); | |
323 | test_period(1, 388, "2.577 ms"); | |
324 | test_period(10, 1000, "10 ms"); | |
325 | ||
326 | /* Again, but now using SR_HZ(). */ | |
327 | test_period(1, SR_HZ(1), "1 s"); | |
328 | test_period(1, SR_HZ(5), "200 ms"); | |
329 | test_period(1, SR_HZ(72), "13.889 ms"); | |
330 | test_period(1, SR_HZ(388), "2.577 ms"); | |
331 | test_period(10, SR_HZ(100), "100 ms"); | |
332 | } | |
333 | END_TEST | |
334 | ||
335 | START_TEST(test_ghz_period) | |
336 | { | |
337 | test_period(1, UINT64_C(1000000000), "1 ns"); | |
338 | test_period(1, UINT64_C(5000000000), "200 ps"); | |
339 | test_period(1, UINT64_C(72000000000), "13.889 ps"); | |
340 | test_period(1, UINT64_C(388000000000), "2.577 ps"); | |
341 | test_period(10, UINT64_C(1000000000000), "10 ps"); | |
342 | test_period(200, UINT64_C(1000000000000), "200 ps"); | |
343 | ||
344 | /* Again, but now using SR_GHZ(). */ | |
345 | test_period(1, SR_GHZ(1), "1 ns"); | |
346 | test_period(1, SR_GHZ(5), "200 ps"); | |
347 | test_period(1, SR_GHZ(72), "13.889 ps"); | |
348 | test_period(1, SR_GHZ(388), "2.577 ps"); | |
349 | test_period(10, SR_GHZ(1), "10 ns"); | |
350 | test_period(200, SR_GHZ(1000), "200 ps"); | |
351 | } | |
352 | END_TEST | |
353 | ||
354 | START_TEST(test_volt) | |
355 | { | |
356 | test_voltage(34, 1, "34 V"); | |
357 | test_voltage(34, 2, "17 V"); | |
358 | test_voltage(1, 1, "1 V"); | |
359 | test_voltage(1, 5, "0.2 V"); | |
360 | test_voltage(200, 1000, "200 mV"); | |
361 | test_voltage(1, 72, "0.0138889 V"); | |
362 | test_voltage(1, 388, "0.00257732 V"); | |
363 | test_voltage(10, 1000, "10 mV"); | |
364 | } | |
365 | END_TEST | |
366 | ||
367 | START_TEST(test_integral) | |
368 | { | |
369 | test_rational("1", (struct sr_rational){1, 1}); | |
370 | test_rational("2", (struct sr_rational){2, 1}); | |
371 | test_rational("10", (struct sr_rational){10, 1}); | |
372 | test_rational("-255", (struct sr_rational){-255, 1}); | |
373 | } | |
374 | END_TEST | |
375 | ||
376 | START_TEST(test_fractional) | |
377 | { | |
378 | test_rational("0.1", (struct sr_rational){1, 10}); | |
379 | test_rational("1.0", (struct sr_rational){10, 10}); | |
380 | test_rational("1.2", (struct sr_rational){12, 10}); | |
381 | test_rational("12.34", (struct sr_rational){1234, 100}); | |
382 | test_rational("-12.34", (struct sr_rational){-1234, 100}); | |
383 | test_rational("10.00", (struct sr_rational){1000, 100}); | |
384 | test_rational(".1", (struct sr_rational){1, 10}); | |
385 | test_rational("+0.1", (struct sr_rational){1, 10}); | |
386 | test_rational("+.1", (struct sr_rational){1, 10}); | |
387 | test_rational("-0.1", (struct sr_rational){-1, 10}); | |
388 | test_rational("-.1", (struct sr_rational){-1, 10}); | |
389 | } | |
390 | END_TEST | |
391 | ||
392 | START_TEST(test_exponent) | |
393 | { | |
394 | test_rational("1e0", (struct sr_rational){1, 1}); | |
395 | test_rational("1E0", (struct sr_rational){1, 1}); | |
396 | test_rational("1E1", (struct sr_rational){10, 1}); | |
397 | test_rational("1e-1", (struct sr_rational){1, 10}); | |
398 | test_rational("-1.234e-0", (struct sr_rational){-1234, 1000}); | |
399 | test_rational("-1.234e3", (struct sr_rational){-1234, 1}); | |
400 | test_rational("-1.234e-3", (struct sr_rational){-1234, 1000000}); | |
401 | test_rational("0.001e3", (struct sr_rational){1, 1}); | |
402 | test_rational("0.001e0", (struct sr_rational){1, 1000}); | |
403 | test_rational("0.001e-3", (struct sr_rational){1, 1000000}); | |
404 | test_rational("43.737E-3", (struct sr_rational){43737, 1000000}); | |
405 | test_rational("-0.1e-2", (struct sr_rational){-1, 1000}); | |
406 | test_rational("-.1e-2", (struct sr_rational){-1, 1000}); | |
407 | test_rational("-.0e-2", (struct sr_rational){0, 1000}); | |
408 | test_rational("+.0e-2", (struct sr_rational){0, 1000}); | |
409 | } | |
410 | END_TEST | |
411 | ||
412 | Suite *suite_strutil(void) | |
413 | { | |
414 | Suite *s; | |
415 | TCase *tc; | |
416 | ||
417 | s = suite_create("strutil"); | |
418 | ||
419 | tc = tcase_create("sr_samplerate_string"); | |
420 | tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown); | |
421 | tcase_add_test(tc, test_locale); | |
422 | tcase_add_test(tc, test_hz); | |
423 | tcase_add_test(tc, test_khz); | |
424 | tcase_add_test(tc, test_mhz); | |
425 | tcase_add_test(tc, test_ghz); | |
426 | tcase_add_test(tc, test_hz_period); | |
427 | tcase_add_test(tc, test_ghz_period); | |
428 | tcase_add_test(tc, test_volt); | |
429 | tcase_add_test(tc, test_integral); | |
430 | tcase_add_test(tc, test_fractional); | |
431 | tcase_add_test(tc, test_exponent); | |
432 | suite_add_tcase(s, tc); | |
433 | ||
434 | return s; | |
435 | } |