2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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/>.
24 #include <libsigrok/libsigrok.h>
28 static void test_vsnprintf(const char *expected, char *format, ...)
35 s = g_malloc0(len + 1);
37 va_start(args, format);
38 len = vsnprintf(s, len, format, args);
41 fail_unless(s != NULL,
42 "Invalid result for '%s': len = %i.", expected, len);
43 fail_unless(!strcmp(s, expected),
44 "Invalid result for '%s': %s.", expected, s);
49 static void test_sr_vsnprintf_ascii(const char *expected, char *format, ...)
56 s = g_malloc0(len + 1);
58 va_start(args, format);
59 len = sr_vsnprintf_ascii(s, len, format, args);
62 fail_unless(s != NULL,
63 "Invalid result for '%s': len = %i.", expected, len);
64 fail_unless(!strcmp(s, expected),
65 "Invalid result for '%s': %s.", expected, s);
69 static void test_sr_vsprintf_ascii(const char *expected, char *format, ...)
71 va_list args, args_copy;
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);
81 /* Allocate buffer and write out command. */
82 s = g_malloc0(len + 1);
83 len = sr_vsprintf_ascii(s, format, args_copy);
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);
93 static void test_samplerate(uint64_t samplerate, const char *expected)
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);
104 static void test_period(uint64_t v_p, uint64_t v_q, const char *expected)
108 s = sr_period_string(v_p, v_q);
109 fail_unless(s != NULL);
110 fail_unless(!strcmp(s, expected),
111 "Invalid result for '%s': %s.", expected, s);
115 static void test_rational(const char *input, struct sr_rational expected)
118 struct sr_rational rational;
120 ret = sr_parse_rational(input, &rational);
121 fail_unless(ret == SR_OK, "Unexpected rc for '%s': %d, errno %d.",
123 fail_unless((expected.p == rational.p) && (expected.q == rational.q),
124 "Invalid result for '%s': %" PRIi64 "/%" PRIu64 "'.",
125 input, rational.p, rational.q);
128 static void test_rational_fail(const char *input)
131 struct sr_rational rational;
133 ret = sr_parse_rational(input, &rational);
134 fail_unless(ret != SR_OK, "Unexpected success for '%s'.", input);
137 static void test_voltage(uint64_t v_p, uint64_t v_q, const char *expected)
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);
148 START_TEST(test_locale)
150 char *old_locale, *saved_locale;
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);
157 ck_assert(saved_locale != NULL);
161 * See: https://msdn.microsoft.com/en-us/library/cc233982.aspx
162 * Doesn't work! Locale is not set!
164 setlocale(LC_NUMERIC, "de-DE");
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.
171 * Find all your available system locales with "locale -a".
173 setlocale(LC_NUMERIC, "de_DE.UTF-8");
175 fprintf(stderr, "New locale = %s\n", setlocale(LC_NUMERIC, NULL));
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);
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);
193 * These tests can be used to tell on which platforms the printf()
194 * functions are locale-dependent (i.e. these tests will fail).
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);
204 /* Restore the original locale. */
205 setlocale(LC_NUMERIC, saved_locale);
206 g_free(saved_locale);
211 * Check various inputs for sr_samplerate_string():
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
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
221 * All of the above tests are done for the Hz/kHz/MHz/GHz ranges.
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");
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");
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");
253 test_samplerate(1230, "1.23 kHz");
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");
265 test_samplerate(SR_KHZ(1.230), "1.23 kHz");
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");
280 test_samplerate(1234000, "1.234 MHz");
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");
292 test_samplerate(SR_MHZ(1.234000), "1.234 MHz");
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");
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");
321 test_samplerate(SR_GHZ(441.500000000), "441.5 GHz");
323 /* Now check the biggest-possible samplerate (2^64 Hz). */
324 // test_samplerate(UINT64_C(18446744073709551615), "18446744073.709551615 GHz");
325 // test_samplerate(SR_GHZ(UINT64_C(18446744073)), "18446744073 GHz");
329 START_TEST(test_hz_period)
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");
337 /* Again, but now using SR_HZ(). */
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");
346 START_TEST(test_ghz_period)
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");
355 /* Again, but now using SR_GHZ(). */
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");
365 START_TEST(test_volt)
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");
378 START_TEST(test_integral)
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});
387 START_TEST(test_fractional)
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});
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});
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});
407 test_rational_fail(".");
408 test_rational_fail(".e");
409 test_rational_fail(".e1");
413 START_TEST(test_exponent)
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});
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});
433 START_TEST(test_text_line)
436 * Covers text line splitting as used in input modules. Accepts
437 * input with differing end-of-line conventions, accepts leading
438 * and trailing whitespace. Isolates "the core" of a text line.
439 * Supports repeated calls which accumulate what later needs to
440 * get discarded after input data got processed in pieces.
444 #define TEXT_CORE_1 "Need to provide"
445 #define TEXT_CORE_2 "an input text"
446 #define TEXT_CORE_3 ""
447 #define TEXT_CORE_4 "with empty lines and funny spacing perhaps?"
449 #define TEXT_LINE_1 TEXT_CORE_1 " \n"
450 #define TEXT_LINE_2 " " TEXT_CORE_2 "\n"
451 #define TEXT_LINE_3 TEXT_CORE_3 "\r\n"
452 #define TEXT_LINE_4 TEXT_CORE_4 "\n"
454 #define TEXT_INPUT TEXT_LINE_1 TEXT_LINE_2 TEXT_LINE_3 TEXT_LINE_4
456 char *input_text, *read_pos, *next_pos, *line;
457 size_t input_len, taken;
459 input_text = g_strdup(TEXT_INPUT);
460 read_pos = input_text;
461 input_len = strlen(input_text);
463 /* Cover first line in tests. */
465 line = sr_text_next_line(read_pos, input_len, &next_pos, &taken);
466 fail_unless(line, "Text line not found");
467 fail_unless(strcmp(line, TEXT_CORE_1) == 0, "Unexpected line content");
468 fail_unless(next_pos, "No next line found");
469 fail_unless(strncmp(next_pos, TEXT_LINE_2, strlen(TEXT_LINE_2)) == 0,
470 "Unexpected next line content");
471 fail_unless(taken == strlen(TEXT_LINE_1),
472 "Unexpected consumed count");
477 /* Cover second line in tests. DO NOT void 'taken' yet. */
478 line = sr_text_next_line(read_pos, input_len, &next_pos, &taken);
479 fail_unless(line, "Text line not found");
480 fail_unless(strcmp(line, TEXT_CORE_2) == 0,
481 "Unexpected text line content");
482 fail_unless(next_pos, "No next line found");
483 fail_unless(strncmp(next_pos, TEXT_LINE_3, strlen(TEXT_LINE_3)) == 0,
484 "Unexpected next line content");
485 fail_unless(taken == strlen(TEXT_LINE_2),
486 "Unexpected consumed count");
487 input_len -= next_pos - read_pos;
490 /* Cover third line in tests. Accumulates 'taken'. */
491 line = sr_text_next_line(read_pos, input_len, &next_pos, &taken);
492 fail_unless(line, "Text line not found");
493 fail_unless(strcmp(line, TEXT_CORE_3) == 0, "Unexpected line content");
494 fail_unless(next_pos, "No next line found");
495 fail_unless(strncmp(next_pos, TEXT_LINE_4, strlen(TEXT_LINE_4)) == 0,
496 "Unexpected next line content");
497 fail_unless(taken == strlen(TEXT_LINE_2) + strlen(TEXT_LINE_3),
498 "Unexpected consumed count (totalled)");
499 input_len -= next_pos - read_pos;
503 /* Cover last line in tests. */
504 line = sr_text_next_line(read_pos, input_len, &next_pos, &taken);
505 fail_unless(line, "Text line not found");
506 fail_unless(strcmp(line, TEXT_CORE_4) == 0,
507 "Unexpected text line content");
508 fail_unless(!next_pos, "Next line found, unexpected");
509 fail_unless(taken == strlen(TEXT_LINE_4),
510 "Unexpected consumed count");
514 /* All input must have been consumed. */
515 fail_unless(!read_pos);
516 fail_unless(!input_len);
523 * TODO Ideally this table of test cases should reside within the
524 * test_text_word() routine. But compilation fails when it's put there
525 * (initializers are said to not be constant, cause is yet uncertain).
527 static const struct {
531 { "", (const char *[]){ NULL, }, },
532 { " ", (const char *[]){ NULL, }, },
533 { "one", (const char *[]){ "one", NULL, }, },
534 { "one ", (const char *[]){ "one", NULL, }, },
535 { " one ", (const char *[]){ "one", NULL, }, },
536 { " one two ", (const char *[]){ "one", "two", NULL, }, },
538 (const char *[]){ "one", "two", "three", NULL, },
542 START_TEST(test_text_word)
544 size_t case_idx, word_idx;
546 const char **words, *want;
547 char *read_pos, *next_pos, *have;
549 for (case_idx = 0; case_idx < ARRAY_SIZE(word_cases); case_idx++) {
550 line = g_strdup(word_cases[case_idx].line);
551 words = word_cases[case_idx].words;
556 want = words[word_idx];
557 have = sr_text_next_word(read_pos, &next_pos);
559 fail_unless(!have, "word found, unexpected");
560 fail_unless(!next_pos, "next found after end");
565 fail_unless(have, "word not found");
566 fail_unless(strcmp(have, want) == 0,
567 "unexpected word found");
569 fail_unless(!words[word_idx], "missed expected words");
576 Suite *suite_strutil(void)
581 s = suite_create("strutil");
583 tc = tcase_create("sr_samplerate_string");
584 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
585 tcase_add_test(tc, test_locale);
586 tcase_add_test(tc, test_hz);
587 tcase_add_test(tc, test_khz);
588 tcase_add_test(tc, test_mhz);
589 tcase_add_test(tc, test_ghz);
590 tcase_add_test(tc, test_hz_period);
591 tcase_add_test(tc, test_ghz_period);
592 tcase_add_test(tc, test_volt);
593 tcase_add_test(tc, test_integral);
594 tcase_add_test(tc, test_fractional);
595 tcase_add_test(tc, test_exponent);
596 suite_add_tcase(s, tc);
598 tc = tcase_create("text");
599 tcase_add_test(tc, test_text_line);
600 tcase_add_test(tc, test_text_word);
601 suite_add_tcase(s, tc);