]> sigrok.org Git - libsigrok.git/blame - tests/strutil.c
output/csv: use intermediate time_t var, silence compiler warning
[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 123 fail_unless((expected.p == rational.p) && (expected.q == rational.q),
11cd0ff1 124 "Invalid result for '%s': %" PRIi64 "/%" PRIu64 "'.",
5ec172d7
SB
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
4db61ed4
GS
433START_TEST(test_text_line)
434{
435 /*
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.
441 */
442#define EOL "\n"
443
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?"
448
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"
453
454#define TEXT_INPUT TEXT_LINE_1 TEXT_LINE_2 TEXT_LINE_3 TEXT_LINE_4
455
456 char *input_text, *read_pos, *next_pos, *line;
457 size_t input_len, taken;
458
459 input_text = g_strdup(TEXT_INPUT);
460 read_pos = input_text;
461 input_len = strlen(input_text);
462
463 /* Cover first line in tests. */
464 taken = 0;
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");
473 read_pos = next_pos;
474 input_len -= taken;
475 taken = 0;
476
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;
488 read_pos = next_pos;
489
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;
500 read_pos = next_pos;
501 taken = 0;
502
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");
511 input_len -= taken;
512 read_pos = next_pos;
513
514 /* All input must have been consumed. */
515 fail_unless(!read_pos);
516 fail_unless(!input_len);
517
518 g_free(input_text);
519}
520END_TEST
521
522/*
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).
526 */
527static const struct {
528 const char *line;
529 const char **words;
530} word_cases[] = {
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, }, },
537 { "one two three ",
538 (const char *[]){ "one", "two", "three", NULL, },
539 },
540};
541
542START_TEST(test_text_word)
543{
544 size_t case_idx, word_idx;
545 char *line;
546 const char **words, *want;
547 char *read_pos, *next_pos, *have;
548
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;
552 word_idx = 0;
553
554 read_pos = line;
555 while (read_pos) {
556 want = words[word_idx];
557 have = sr_text_next_word(read_pos, &next_pos);
558 if (!want) {
559 fail_unless(!have, "word found, unexpected");
560 fail_unless(!next_pos, "next found after end");
561 break;
562 }
563 word_idx++;
564 read_pos = next_pos;
565 fail_unless(have, "word not found");
566 fail_unless(strcmp(have, want) == 0,
567 "unexpected word found");
568 }
569 fail_unless(!words[word_idx], "missed expected words");
570
571 g_free(line);
572 }
573}
574END_TEST
575
96b85562
GS
576static const struct power_case_t {
577 size_t value;
578 size_t want_bits;
579 size_t want_power;
580} power_cases[] = {
87e738be 581 { 0, 1, 1, },
96b85562
GS
582 { 1, 1, 2, },
583 { 2, 2, 4, },
584 { 3, 2, 4, },
585 { 4, 3, 8, },
586 { 5, 3, 8, },
587 { 6, 3, 8, },
588 { 7, 3, 8, },
589 { 8, 4, 16, },
590 { 15, 4, 16, },
591 { 16, 5, 32, },
592 { 31, 5, 32, },
593};
594
595START_TEST(test_calc_power_of_two)
596{
597 size_t case_idx, bits, power;
598 const struct power_case_t *tcase;
599 int ret;
600
96b85562
GS
601 for (case_idx = 0; case_idx < ARRAY_SIZE(power_cases); case_idx++) {
602 tcase = &power_cases[case_idx];
603 ret = sr_next_power_of_two(tcase->value, &bits, &power);
604 fail_unless(ret == SR_OK, "bits count not found");
605 fail_unless(bits == tcase->want_bits, "bits count differs");
606 fail_unless(power == tcase->want_power, "power differs");
607 }
608}
609END_TEST
610
79bb0e97
UH
611Suite *suite_strutil(void)
612{
613 Suite *s;
614 TCase *tc;
615
616 s = suite_create("strutil");
617
618 tc = tcase_create("sr_samplerate_string");
98de0c78 619 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
16e88c6b 620 tcase_add_test(tc, test_locale);
79bb0e97
UH
621 tcase_add_test(tc, test_hz);
622 tcase_add_test(tc, test_khz);
623 tcase_add_test(tc, test_mhz);
624 tcase_add_test(tc, test_ghz);
5223412e
SB
625 tcase_add_test(tc, test_hz_period);
626 tcase_add_test(tc, test_ghz_period);
c911599d 627 tcase_add_test(tc, test_volt);
5ec172d7
SB
628 tcase_add_test(tc, test_integral);
629 tcase_add_test(tc, test_fractional);
630 tcase_add_test(tc, test_exponent);
79bb0e97
UH
631 suite_add_tcase(s, tc);
632
4db61ed4
GS
633 tc = tcase_create("text");
634 tcase_add_test(tc, test_text_line);
635 tcase_add_test(tc, test_text_word);
636 suite_add_tcase(s, tc);
637
96b85562
GS
638 tc = tcase_create("calc");
639 tcase_add_test(tc, test_calc_power_of_two);
640 suite_add_tcase(s, tc);
641
79bb0e97
UH
642 return s;
643}