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/>.
22 #include <libsigrok/libsigrok.h>
25 static void test_samplerate(uint64_t samplerate, const char *expected)
29 s = sr_samplerate_string(samplerate);
30 fail_unless(s != NULL);
31 fail_unless(!strcmp(s, expected),
32 "Invalid result for '%s': %s.", expected, s);
36 static void test_period(uint64_t frequency, const char *expected)
40 s = sr_period_string(frequency);
41 fail_unless(s != NULL);
42 fail_unless(!strcmp(s, expected),
43 "Invalid result for '%s': %s.", expected, s);
47 static void test_rational(const char *input, struct sr_rational expected)
50 struct sr_rational rational;
52 ret = sr_parse_rational(input, &rational);
53 fail_unless(ret == SR_OK);
54 fail_unless((expected.p == rational.p) && (expected.q == rational.q),
55 "Invalid result for '%s': %ld/%ld'.",
56 input, rational.p, rational.q);
60 * Check various inputs for sr_samplerate_string():
62 * - One, two, or three digit results (e.g. 5/55/555 MHz).
63 * - Results which contain commas (e.g. 1.234 / 12.34 / 123.4 kHz).
64 * - Results with zeroes right after the comma (e.g. 1.034 Hz).
65 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=73
66 * - Results with zeroes in the middle (e.g. 1.204 kHz).
67 * - All of the above, but using SR_MHZ() and friends.
68 * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=72
70 * All of the above tests are done for the Hz/kHz/MHz/GHz ranges.
75 test_samplerate(0, "0 Hz");
76 test_samplerate(1, "1 Hz");
77 test_samplerate(23, "23 Hz");
78 test_samplerate(644, "644 Hz");
79 test_samplerate(604, "604 Hz");
80 test_samplerate(550, "550 Hz");
82 /* Again, but now using SR_HZ(). */
83 test_samplerate(SR_HZ(0), "0 Hz");
84 test_samplerate(SR_HZ(1), "1 Hz");
85 test_samplerate(SR_HZ(23), "23 Hz");
86 test_samplerate(SR_HZ(644), "644 Hz");
87 test_samplerate(SR_HZ(604), "604 Hz");
88 test_samplerate(SR_HZ(550), "550 Hz");
94 test_samplerate(1000, "1 kHz");
95 test_samplerate(99000, "99 kHz");
96 test_samplerate(225000, "225 kHz");
97 test_samplerate(1234, "1.234 kHz");
98 test_samplerate(12345, "12.345 kHz");
99 test_samplerate(123456, "123.456 kHz");
100 test_samplerate(1034, "1.034 kHz");
101 test_samplerate(1004, "1.004 kHz");
102 test_samplerate(1230, "1.23 kHz");
104 /* Again, but now using SR_KHZ(). */
105 test_samplerate(SR_KHZ(1), "1 kHz");
106 test_samplerate(SR_KHZ(99), "99 kHz");
107 test_samplerate(SR_KHZ(225), "225 kHz");
108 test_samplerate(SR_KHZ(1.234), "1.234 kHz");
109 test_samplerate(SR_KHZ(12.345), "12.345 kHz");
110 test_samplerate(SR_KHZ(123.456), "123.456 kHz");
111 test_samplerate(SR_KHZ(1.204), "1.204 kHz");
112 test_samplerate(SR_KHZ(1.034), "1.034 kHz");
113 test_samplerate(SR_KHZ(1.004), "1.004 kHz");
114 test_samplerate(SR_KHZ(1.230), "1.23 kHz");
120 test_samplerate(1000000, "1 MHz");
121 test_samplerate(28000000, "28 MHz");
122 test_samplerate(775000000, "775 MHz");
123 test_samplerate(1234567, "1.234567 MHz");
124 test_samplerate(12345678, "12.345678 MHz");
125 test_samplerate(123456789, "123.456789 MHz");
126 test_samplerate(1230007, "1.230007 MHz");
127 test_samplerate(1034567, "1.034567 MHz");
128 test_samplerate(1000007, "1.000007 MHz");
129 test_samplerate(1234000, "1.234 MHz");
131 /* Again, but now using SR_MHZ(). */
132 test_samplerate(SR_MHZ(1), "1 MHz");
133 test_samplerate(SR_MHZ(28), "28 MHz");
134 test_samplerate(SR_MHZ(775), "775 MHz");
135 test_samplerate(SR_MHZ(1.234567), "1.234567 MHz");
136 test_samplerate(SR_MHZ(12.345678), "12.345678 MHz");
137 test_samplerate(SR_MHZ(123.456789), "123.456789 MHz");
138 test_samplerate(SR_MHZ(1.230007), "1.230007 MHz");
139 test_samplerate(SR_MHZ(1.034567), "1.034567 MHz");
140 test_samplerate(SR_MHZ(1.000007), "1.000007 MHz");
141 test_samplerate(SR_MHZ(1.234000), "1.234 MHz");
147 /* Note: Numbers > 2^32 need a ULL suffix. */
149 test_samplerate(1000000000, "1 GHz");
150 test_samplerate(5000000000ULL, "5 GHz");
151 test_samplerate(72000000000ULL, "72 GHz");
152 test_samplerate(388000000000ULL, "388 GHz");
153 test_samplerate(4417594444ULL, "4.417594444 GHz");
154 test_samplerate(44175944444ULL, "44.175944444 GHz");
155 test_samplerate(441759444441ULL, "441.759444441 GHz");
156 test_samplerate(441759000001ULL, "441.759000001 GHz");
157 test_samplerate(441050000000ULL, "441.05 GHz");
158 test_samplerate(441000000005ULL, "441.000000005 GHz");
159 test_samplerate(441500000000ULL, "441.5 GHz");
161 /* Again, but now using SR_GHZ(). */
162 test_samplerate(SR_GHZ(1), "1 GHz");
163 test_samplerate(SR_GHZ(5), "5 GHz");
164 test_samplerate(SR_GHZ(72), "72 GHz");
165 test_samplerate(SR_GHZ(388), "388 GHz");
166 test_samplerate(SR_GHZ(4.417594444), "4.417594444 GHz");
167 test_samplerate(SR_GHZ(44.175944444), "44.175944444 GHz");
168 test_samplerate(SR_GHZ(441.759444441), "441.759444441 GHz");
169 test_samplerate(SR_GHZ(441.759000001), "441.759000001 GHz");
170 test_samplerate(SR_GHZ(441.050000000), "441.05 GHz");
171 test_samplerate(SR_GHZ(441.000000005), "441.000000005 GHz");
172 test_samplerate(SR_GHZ(441.500000000), "441.5 GHz");
174 /* Now check the biggest-possible samplerate (2^64 Hz). */
175 // test_samplerate(18446744073709551615ULL, "18446744073.709551615 GHz");
176 // test_samplerate(SR_GHZ(18446744073ULL), "18446744073 GHz");
180 START_TEST(test_hz_period)
182 test_period(1, "1000 ms");
183 test_period(5, "200 ms");
184 test_period(72, "13 ms");
185 test_period(388, "2 ms");
187 /* Again, but now using SR_HZ(). */
188 test_period(SR_HZ(1), "1000 ms");
189 test_period(SR_HZ(5), "200 ms");
190 test_period(SR_HZ(72), "13 ms");
191 test_period(SR_HZ(388), "2 ms");
195 START_TEST(test_ghz_period)
197 /* Note: Numbers > 2^32 need a ULL suffix. */
199 test_period(1000000000, "1000 ps");
200 test_period(5000000000ULL, "200 ps");
201 test_period(72000000000ULL, "13 ps");
202 test_period(388000000000ULL, "2 ps");
204 /* Again, but now using SR_GHZ(). */
205 test_period(SR_GHZ(1), "1000 ps");
206 test_period(SR_GHZ(5), "200 ps");
207 test_period(SR_GHZ(72), "13 ps");
208 test_period(SR_GHZ(388), "2 ps");
212 START_TEST(test_integral)
214 test_rational("1", (struct sr_rational){1, 1});
215 test_rational("2", (struct sr_rational){2, 1});
216 test_rational("10", (struct sr_rational){10, 1});
217 test_rational("-255", (struct sr_rational){-255, 1});
221 START_TEST(test_fractional)
223 test_rational("0.1", (struct sr_rational){1, 10});
224 test_rational("1.0", (struct sr_rational){10, 10});
225 test_rational("1.2", (struct sr_rational){12, 10});
226 test_rational("12.34", (struct sr_rational){1234, 100});
227 test_rational("-12.34", (struct sr_rational){-1234, 100});
228 test_rational("10.00", (struct sr_rational){1000, 100});
232 START_TEST(test_exponent)
234 test_rational("1e0", (struct sr_rational){1, 1});
235 test_rational("1E0", (struct sr_rational){1, 1});
236 test_rational("1E1", (struct sr_rational){10, 1});
237 test_rational("1e-1", (struct sr_rational){1, 10});
238 test_rational("-1.234e-0", (struct sr_rational){-1234, 1000});
239 test_rational("-1.234e3", (struct sr_rational){-1234, 1});
240 test_rational("-1.234e-3", (struct sr_rational){-1234, 1000000});
241 test_rational("0.001e3", (struct sr_rational){1, 1});
242 test_rational("0.001e0", (struct sr_rational){1, 1000});
243 test_rational("0.001e-3", (struct sr_rational){1, 1000000});
247 Suite *suite_strutil(void)
252 s = suite_create("strutil");
254 tc = tcase_create("sr_samplerate_string");
255 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
256 tcase_add_test(tc, test_hz);
257 tcase_add_test(tc, test_khz);
258 tcase_add_test(tc, test_mhz);
259 tcase_add_test(tc, test_ghz);
260 tcase_add_test(tc, test_hz_period);
261 tcase_add_test(tc, test_ghz_period);
262 tcase_add_test(tc, test_integral);
263 tcase_add_test(tc, test_fractional);
264 tcase_add_test(tc, test_exponent);
265 suite_add_tcase(s, tc);