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