]> sigrok.org Git - libsigrok.git/blame - tests/strutil.c
rigol-ds: free memory that was allocated by SCPI get routines
[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
081aaebf
GS
128static void test_rational_fail(const char *input)
129{
130 int ret;
131 struct sr_rational rational;
132
133 ret = sr_parse_rational(input, &rational);
134 fail_unless(ret != SR_OK, "Unexpected success for '%s'.", input);
135}
136
c911599d
UH
137static void test_voltage(uint64_t v_p, uint64_t v_q, const char *expected)
138{
139 char *s;
140
141 s = sr_voltage_string(v_p, v_q);
142 fail_unless(s != NULL);
143 fail_unless(!strcmp(s, expected),
144 "Invalid result for '%s': %s.", expected, s);
145 g_free(s);
146}
147
16e88c6b
FS
148START_TEST(test_locale)
149{
150 char *old_locale, *saved_locale;
151
152 /* Get the the current locale. */
153 old_locale = setlocale(LC_NUMERIC, NULL);
154 fprintf(stderr, "Old locale = %s\n", old_locale);
155 /* Copy the name so it won’t be clobbered by setlocale. */
156 saved_locale = g_strdup(old_locale);
505a55f7 157 ck_assert(saved_locale != NULL);
16e88c6b
FS
158
159#ifdef _WIN32
160 /*
161 * See: https://msdn.microsoft.com/en-us/library/cc233982.aspx
162 * Doesn't work! Locale is not set!
163 */
164 setlocale(LC_NUMERIC, "de-DE");
165#else
166 /*
167 * For all *nix and OSX systems, change the locale for all threads to
168 * one that is known for not working correctly with printf(), e.g.
169 * "de_DE.UTF-8".
170 *
171 * Find all your available system locales with "locale -a".
172 */
173 setlocale(LC_NUMERIC, "de_DE.UTF-8");
174#endif
175 fprintf(stderr, "New locale = %s\n", setlocale(LC_NUMERIC, NULL));
176
177 test_sr_vsnprintf_ascii("0.1", "%.1f", (double)0.1);
178 test_sr_vsnprintf_ascii("0.12", "%.2f", (double)0.12);
179 test_sr_vsnprintf_ascii("0.123", "%.3f", (double)0.123);
180 test_sr_vsnprintf_ascii("0.1234", "%.4f", (double)0.1234);
181 test_sr_vsnprintf_ascii("0.12345", "%.5f", (double)0.12345);
182 test_sr_vsnprintf_ascii("0.123456", "%.6f", (double)0.123456);
183
7ca40d2d
FS
184 test_sr_vsprintf_ascii("0.1", "%.1f", (double)0.1);
185 test_sr_vsprintf_ascii("0.12", "%.2f", (double)0.12);
186 test_sr_vsprintf_ascii("0.123", "%.3f", (double)0.123);
187 test_sr_vsprintf_ascii("0.1234", "%.4f", (double)0.1234);
188 test_sr_vsprintf_ascii("0.12345", "%.5f", (double)0.12345);
189 test_sr_vsprintf_ascii("0.123456", "%.6f", (double)0.123456);
190
16e88c6b
FS
191#if 0
192 /*
193 * These tests can be used to tell on which platforms the printf()
194 * functions are locale-dependent (i.e. these tests will fail).
195 */
196 test_vsnprintf("0.1", "%.1f", (double)0.1);
197 test_vsnprintf("0.12", "%.2f", (double)0.12);
198 test_vsnprintf("0.123", "%.3f", (double)0.123);
199 test_vsnprintf("0.1234", "%.4f", (double)0.1234);
200 test_vsnprintf("0.12345", "%.5f", (double)0.12345);
201 test_vsnprintf("0.123456", "%.6f", (double)0.123456);
202#endif
203
204 /* Restore the original locale. */
205 setlocale(LC_NUMERIC, saved_locale);
206 g_free(saved_locale);
207}
208END_TEST
209
79bb0e97
UH
210/*
211 * Check various inputs for sr_samplerate_string():
212 *
213 * - One, two, or three digit results (e.g. 5/55/555 MHz).
214 * - Results which contain commas (e.g. 1.234 / 12.34 / 123.4 kHz).
215 * - Results with zeroes right after the comma (e.g. 1.034 Hz).
216 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=73
79bb0e97
UH
217 * - Results with zeroes in the middle (e.g. 1.204 kHz).
218 * - All of the above, but using SR_MHZ() and friends.
219 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=72
220 *
221 * All of the above tests are done for the Hz/kHz/MHz/GHz ranges.
222 */
223
224START_TEST(test_hz)
225{
226 test_samplerate(0, "0 Hz");
227 test_samplerate(1, "1 Hz");
228 test_samplerate(23, "23 Hz");
229 test_samplerate(644, "644 Hz");
230 test_samplerate(604, "604 Hz");
231 test_samplerate(550, "550 Hz");
232
233 /* Again, but now using SR_HZ(). */
234 test_samplerate(SR_HZ(0), "0 Hz");
235 test_samplerate(SR_HZ(1), "1 Hz");
236 test_samplerate(SR_HZ(23), "23 Hz");
237 test_samplerate(SR_HZ(644), "644 Hz");
238 test_samplerate(SR_HZ(604), "604 Hz");
239 test_samplerate(SR_HZ(550), "550 Hz");
240}
241END_TEST
242
243START_TEST(test_khz)
244{
245 test_samplerate(1000, "1 kHz");
246 test_samplerate(99000, "99 kHz");
247 test_samplerate(225000, "225 kHz");
248 test_samplerate(1234, "1.234 kHz");
249 test_samplerate(12345, "12.345 kHz");
250 test_samplerate(123456, "123.456 kHz");
251 test_samplerate(1034, "1.034 kHz");
252 test_samplerate(1004, "1.004 kHz");
ba253f2b 253 test_samplerate(1230, "1.23 kHz");
79bb0e97
UH
254
255 /* Again, but now using SR_KHZ(). */
256 test_samplerate(SR_KHZ(1), "1 kHz");
257 test_samplerate(SR_KHZ(99), "99 kHz");
258 test_samplerate(SR_KHZ(225), "225 kHz");
259 test_samplerate(SR_KHZ(1.234), "1.234 kHz");
260 test_samplerate(SR_KHZ(12.345), "12.345 kHz");
261 test_samplerate(SR_KHZ(123.456), "123.456 kHz");
262 test_samplerate(SR_KHZ(1.204), "1.204 kHz");
263 test_samplerate(SR_KHZ(1.034), "1.034 kHz");
264 test_samplerate(SR_KHZ(1.004), "1.004 kHz");
ba253f2b 265 test_samplerate(SR_KHZ(1.230), "1.23 kHz");
79bb0e97
UH
266}
267END_TEST
268
269START_TEST(test_mhz)
270{
271 test_samplerate(1000000, "1 MHz");
272 test_samplerate(28000000, "28 MHz");
273 test_samplerate(775000000, "775 MHz");
274 test_samplerate(1234567, "1.234567 MHz");
275 test_samplerate(12345678, "12.345678 MHz");
276 test_samplerate(123456789, "123.456789 MHz");
277 test_samplerate(1230007, "1.230007 MHz");
278 test_samplerate(1034567, "1.034567 MHz");
279 test_samplerate(1000007, "1.000007 MHz");
ba253f2b 280 test_samplerate(1234000, "1.234 MHz");
79bb0e97
UH
281
282 /* Again, but now using SR_MHZ(). */
283 test_samplerate(SR_MHZ(1), "1 MHz");
284 test_samplerate(SR_MHZ(28), "28 MHz");
285 test_samplerate(SR_MHZ(775), "775 MHz");
286 test_samplerate(SR_MHZ(1.234567), "1.234567 MHz");
287 test_samplerate(SR_MHZ(12.345678), "12.345678 MHz");
288 test_samplerate(SR_MHZ(123.456789), "123.456789 MHz");
289 test_samplerate(SR_MHZ(1.230007), "1.230007 MHz");
290 test_samplerate(SR_MHZ(1.034567), "1.034567 MHz");
291 test_samplerate(SR_MHZ(1.000007), "1.000007 MHz");
ba253f2b 292 test_samplerate(SR_MHZ(1.234000), "1.234 MHz");
79bb0e97
UH
293}
294END_TEST
295
296START_TEST(test_ghz)
297{
d9b716fc
UH
298 test_samplerate(UINT64_C(1000000000), "1 GHz");
299 test_samplerate(UINT64_C(5000000000), "5 GHz");
300 test_samplerate(UINT64_C(72000000000), "72 GHz");
301 test_samplerate(UINT64_C(388000000000), "388 GHz");
302 test_samplerate(UINT64_C(4417594444), "4.417594444 GHz");
303 test_samplerate(UINT64_C(44175944444), "44.175944444 GHz");
304 test_samplerate(UINT64_C(441759444441), "441.759444441 GHz");
305 test_samplerate(UINT64_C(441759000001), "441.759000001 GHz");
306 test_samplerate(UINT64_C(441050000000), "441.05 GHz");
307 test_samplerate(UINT64_C(441000000005), "441.000000005 GHz");
308 test_samplerate(UINT64_C(441500000000), "441.5 GHz");
79bb0e97
UH
309
310 /* Again, but now using SR_GHZ(). */
311 test_samplerate(SR_GHZ(1), "1 GHz");
312 test_samplerate(SR_GHZ(5), "5 GHz");
313 test_samplerate(SR_GHZ(72), "72 GHz");
314 test_samplerate(SR_GHZ(388), "388 GHz");
315 test_samplerate(SR_GHZ(4.417594444), "4.417594444 GHz");
316 test_samplerate(SR_GHZ(44.175944444), "44.175944444 GHz");
317 test_samplerate(SR_GHZ(441.759444441), "441.759444441 GHz");
318 test_samplerate(SR_GHZ(441.759000001), "441.759000001 GHz");
319 test_samplerate(SR_GHZ(441.050000000), "441.05 GHz");
320 test_samplerate(SR_GHZ(441.000000005), "441.000000005 GHz");
ba253f2b 321 test_samplerate(SR_GHZ(441.500000000), "441.5 GHz");
79bb0e97
UH
322
323 /* Now check the biggest-possible samplerate (2^64 Hz). */
d9b716fc
UH
324 // test_samplerate(UINT64_C(18446744073709551615), "18446744073.709551615 GHz");
325 // test_samplerate(SR_GHZ(UINT64_C(18446744073)), "18446744073 GHz");
79bb0e97
UH
326}
327END_TEST
328
5223412e
SB
329START_TEST(test_hz_period)
330{
6984cfb2
SA
331 test_period(1, 1, "1 s");
332 test_period(1, 5, "200 ms");
333 test_period(1, 72, "13.889 ms");
334 test_period(1, 388, "2.577 ms");
335 test_period(10, 1000, "10 ms");
5223412e
SB
336
337 /* Again, but now using SR_HZ(). */
6984cfb2
SA
338 test_period(1, SR_HZ(1), "1 s");
339 test_period(1, SR_HZ(5), "200 ms");
340 test_period(1, SR_HZ(72), "13.889 ms");
341 test_period(1, SR_HZ(388), "2.577 ms");
342 test_period(10, SR_HZ(100), "100 ms");
5223412e
SB
343}
344END_TEST
345
346START_TEST(test_ghz_period)
347{
d9b716fc
UH
348 test_period(1, UINT64_C(1000000000), "1 ns");
349 test_period(1, UINT64_C(5000000000), "200 ps");
350 test_period(1, UINT64_C(72000000000), "13.889 ps");
351 test_period(1, UINT64_C(388000000000), "2.577 ps");
352 test_period(10, UINT64_C(1000000000000), "10 ps");
353 test_period(200, UINT64_C(1000000000000), "200 ps");
5223412e
SB
354
355 /* Again, but now using SR_GHZ(). */
6984cfb2
SA
356 test_period(1, SR_GHZ(1), "1 ns");
357 test_period(1, SR_GHZ(5), "200 ps");
358 test_period(1, SR_GHZ(72), "13.889 ps");
359 test_period(1, SR_GHZ(388), "2.577 ps");
360 test_period(10, SR_GHZ(1), "10 ns");
361 test_period(200, SR_GHZ(1000), "200 ps");
5223412e
SB
362}
363END_TEST
364
c911599d
UH
365START_TEST(test_volt)
366{
b5df922e
UH
367 test_voltage(34, 1, "34 V");
368 test_voltage(34, 2, "17 V");
369 test_voltage(1, 1, "1 V");
370 test_voltage(1, 5, "0.2 V");
371 test_voltage(200, 1000, "200 mV");
372 test_voltage(1, 72, "0.0138889 V");
373 test_voltage(1, 388, "0.00257732 V");
374 test_voltage(10, 1000, "10 mV");
c911599d
UH
375}
376END_TEST
377
5ec172d7
SB
378START_TEST(test_integral)
379{
380 test_rational("1", (struct sr_rational){1, 1});
381 test_rational("2", (struct sr_rational){2, 1});
382 test_rational("10", (struct sr_rational){10, 1});
383 test_rational("-255", (struct sr_rational){-255, 1});
384}
385END_TEST
386
387START_TEST(test_fractional)
388{
389 test_rational("0.1", (struct sr_rational){1, 10});
390 test_rational("1.0", (struct sr_rational){10, 10});
391 test_rational("1.2", (struct sr_rational){12, 10});
392 test_rational("12.34", (struct sr_rational){1234, 100});
393 test_rational("-12.34", (struct sr_rational){-1234, 100});
394 test_rational("10.00", (struct sr_rational){1000, 100});
41c47f2c
SB
395 test_rational(".1", (struct sr_rational){1, 10});
396 test_rational("+0.1", (struct sr_rational){1, 10});
397 test_rational("+.1", (struct sr_rational){1, 10});
398 test_rational("-0.1", (struct sr_rational){-1, 10});
399 test_rational("-.1", (struct sr_rational){-1, 10});
74d91533
GS
400 test_rational(".1", (struct sr_rational){1, 10});
401 test_rational(".123", (struct sr_rational){123, 1000});
402 test_rational("1.", (struct sr_rational){1, 1});
403 test_rational("123.", (struct sr_rational){123, 1});
404 test_rational("-.1", (struct sr_rational){-1, 10});
405 test_rational(" .1", (struct sr_rational){1, 10});
406 test_rational("+.1", (struct sr_rational){1, 10});
081aaebf
GS
407 test_rational_fail(".");
408 test_rational_fail(".e");
409 test_rational_fail(".e1");
5ec172d7
SB
410}
411END_TEST
412
413START_TEST(test_exponent)
414{
415 test_rational("1e0", (struct sr_rational){1, 1});
416 test_rational("1E0", (struct sr_rational){1, 1});
417 test_rational("1E1", (struct sr_rational){10, 1});
418 test_rational("1e-1", (struct sr_rational){1, 10});
419 test_rational("-1.234e-0", (struct sr_rational){-1234, 1000});
420 test_rational("-1.234e3", (struct sr_rational){-1234, 1});
421 test_rational("-1.234e-3", (struct sr_rational){-1234, 1000000});
422 test_rational("0.001e3", (struct sr_rational){1, 1});
423 test_rational("0.001e0", (struct sr_rational){1, 1000});
424 test_rational("0.001e-3", (struct sr_rational){1, 1000000});
41c47f2c
SB
425 test_rational("43.737E-3", (struct sr_rational){43737, 1000000});
426 test_rational("-0.1e-2", (struct sr_rational){-1, 1000});
427 test_rational("-.1e-2", (struct sr_rational){-1, 1000});
428 test_rational("-.0e-2", (struct sr_rational){0, 1000});
429 test_rational("+.0e-2", (struct sr_rational){0, 1000});
5ec172d7
SB
430}
431END_TEST
432
79bb0e97
UH
433Suite *suite_strutil(void)
434{
435 Suite *s;
436 TCase *tc;
437
438 s = suite_create("strutil");
439
440 tc = tcase_create("sr_samplerate_string");
98de0c78 441 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
16e88c6b 442 tcase_add_test(tc, test_locale);
79bb0e97
UH
443 tcase_add_test(tc, test_hz);
444 tcase_add_test(tc, test_khz);
445 tcase_add_test(tc, test_mhz);
446 tcase_add_test(tc, test_ghz);
5223412e
SB
447 tcase_add_test(tc, test_hz_period);
448 tcase_add_test(tc, test_ghz_period);
c911599d 449 tcase_add_test(tc, test_volt);
5ec172d7
SB
450 tcase_add_test(tc, test_integral);
451 tcase_add_test(tc, test_fractional);
452 tcase_add_test(tc, test_exponent);
79bb0e97
UH
453 suite_add_tcase(s, tc);
454
455 return s;
456}