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