]>
Commit | Line | Data |
---|---|---|
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
79bb0e97 | 22 | #include <check.h> |
4960aeb0 | 23 | #include <libsigrok/libsigrok.h> |
17794067 | 24 | #include "lib.h" |
79bb0e97 | 25 | |
79bb0e97 UH |
26 | static void test_samplerate(uint64_t samplerate, const char *expected) |
27 | { | |
28 | char *s; | |
29 | ||
30 | s = sr_samplerate_string(samplerate); | |
31 | fail_unless(s != NULL); | |
32 | fail_unless(!strcmp(s, expected), | |
33 | "Invalid result for '%s': %s.", expected, s); | |
34 | g_free(s); | |
35 | } | |
36 | ||
5ec172d7 SB |
37 | static void test_rational(const char *input, struct sr_rational expected) |
38 | { | |
39 | int ret; | |
40 | struct sr_rational rational; | |
41 | ||
42 | ret = sr_parse_rational(input, &rational); | |
43 | fail_unless(ret == SR_OK); | |
44 | fail_unless((expected.p == rational.p) && (expected.q == rational.q), | |
45 | "Invalid result for '%s': %ld/%ld'.", | |
46 | input, rational.p, rational.q); | |
47 | } | |
48 | ||
79bb0e97 UH |
49 | /* |
50 | * Check various inputs for sr_samplerate_string(): | |
51 | * | |
52 | * - One, two, or three digit results (e.g. 5/55/555 MHz). | |
53 | * - Results which contain commas (e.g. 1.234 / 12.34 / 123.4 kHz). | |
54 | * - Results with zeroes right after the comma (e.g. 1.034 Hz). | |
55 | * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=73 | |
79bb0e97 UH |
56 | * - Results with zeroes in the middle (e.g. 1.204 kHz). |
57 | * - All of the above, but using SR_MHZ() and friends. | |
58 | * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=72 | |
59 | * | |
60 | * All of the above tests are done for the Hz/kHz/MHz/GHz ranges. | |
61 | */ | |
62 | ||
63 | START_TEST(test_hz) | |
64 | { | |
65 | test_samplerate(0, "0 Hz"); | |
66 | test_samplerate(1, "1 Hz"); | |
67 | test_samplerate(23, "23 Hz"); | |
68 | test_samplerate(644, "644 Hz"); | |
69 | test_samplerate(604, "604 Hz"); | |
70 | test_samplerate(550, "550 Hz"); | |
71 | ||
72 | /* Again, but now using SR_HZ(). */ | |
73 | test_samplerate(SR_HZ(0), "0 Hz"); | |
74 | test_samplerate(SR_HZ(1), "1 Hz"); | |
75 | test_samplerate(SR_HZ(23), "23 Hz"); | |
76 | test_samplerate(SR_HZ(644), "644 Hz"); | |
77 | test_samplerate(SR_HZ(604), "604 Hz"); | |
78 | test_samplerate(SR_HZ(550), "550 Hz"); | |
79 | } | |
80 | END_TEST | |
81 | ||
82 | START_TEST(test_khz) | |
83 | { | |
84 | test_samplerate(1000, "1 kHz"); | |
85 | test_samplerate(99000, "99 kHz"); | |
86 | test_samplerate(225000, "225 kHz"); | |
87 | test_samplerate(1234, "1.234 kHz"); | |
88 | test_samplerate(12345, "12.345 kHz"); | |
89 | test_samplerate(123456, "123.456 kHz"); | |
90 | test_samplerate(1034, "1.034 kHz"); | |
91 | test_samplerate(1004, "1.004 kHz"); | |
ba253f2b | 92 | test_samplerate(1230, "1.23 kHz"); |
79bb0e97 UH |
93 | |
94 | /* Again, but now using SR_KHZ(). */ | |
95 | test_samplerate(SR_KHZ(1), "1 kHz"); | |
96 | test_samplerate(SR_KHZ(99), "99 kHz"); | |
97 | test_samplerate(SR_KHZ(225), "225 kHz"); | |
98 | test_samplerate(SR_KHZ(1.234), "1.234 kHz"); | |
99 | test_samplerate(SR_KHZ(12.345), "12.345 kHz"); | |
100 | test_samplerate(SR_KHZ(123.456), "123.456 kHz"); | |
101 | test_samplerate(SR_KHZ(1.204), "1.204 kHz"); | |
102 | test_samplerate(SR_KHZ(1.034), "1.034 kHz"); | |
103 | test_samplerate(SR_KHZ(1.004), "1.004 kHz"); | |
ba253f2b | 104 | test_samplerate(SR_KHZ(1.230), "1.23 kHz"); |
79bb0e97 UH |
105 | } |
106 | END_TEST | |
107 | ||
108 | START_TEST(test_mhz) | |
109 | { | |
110 | test_samplerate(1000000, "1 MHz"); | |
111 | test_samplerate(28000000, "28 MHz"); | |
112 | test_samplerate(775000000, "775 MHz"); | |
113 | test_samplerate(1234567, "1.234567 MHz"); | |
114 | test_samplerate(12345678, "12.345678 MHz"); | |
115 | test_samplerate(123456789, "123.456789 MHz"); | |
116 | test_samplerate(1230007, "1.230007 MHz"); | |
117 | test_samplerate(1034567, "1.034567 MHz"); | |
118 | test_samplerate(1000007, "1.000007 MHz"); | |
ba253f2b | 119 | test_samplerate(1234000, "1.234 MHz"); |
79bb0e97 UH |
120 | |
121 | /* Again, but now using SR_MHZ(). */ | |
122 | test_samplerate(SR_MHZ(1), "1 MHz"); | |
123 | test_samplerate(SR_MHZ(28), "28 MHz"); | |
124 | test_samplerate(SR_MHZ(775), "775 MHz"); | |
125 | test_samplerate(SR_MHZ(1.234567), "1.234567 MHz"); | |
126 | test_samplerate(SR_MHZ(12.345678), "12.345678 MHz"); | |
127 | test_samplerate(SR_MHZ(123.456789), "123.456789 MHz"); | |
128 | test_samplerate(SR_MHZ(1.230007), "1.230007 MHz"); | |
129 | test_samplerate(SR_MHZ(1.034567), "1.034567 MHz"); | |
130 | test_samplerate(SR_MHZ(1.000007), "1.000007 MHz"); | |
ba253f2b | 131 | test_samplerate(SR_MHZ(1.234000), "1.234 MHz"); |
79bb0e97 UH |
132 | } |
133 | END_TEST | |
134 | ||
135 | START_TEST(test_ghz) | |
136 | { | |
137 | /* Note: Numbers > 2^32 need a ULL suffix. */ | |
138 | ||
139 | test_samplerate(1000000000, "1 GHz"); | |
140 | test_samplerate(5000000000ULL, "5 GHz"); | |
141 | test_samplerate(72000000000ULL, "72 GHz"); | |
142 | test_samplerate(388000000000ULL, "388 GHz"); | |
143 | test_samplerate(4417594444ULL, "4.417594444 GHz"); | |
144 | test_samplerate(44175944444ULL, "44.175944444 GHz"); | |
145 | test_samplerate(441759444441ULL, "441.759444441 GHz"); | |
146 | test_samplerate(441759000001ULL, "441.759000001 GHz"); | |
147 | test_samplerate(441050000000ULL, "441.05 GHz"); | |
148 | test_samplerate(441000000005ULL, "441.000000005 GHz"); | |
ba253f2b | 149 | test_samplerate(441500000000ULL, "441.5 GHz"); |
79bb0e97 UH |
150 | |
151 | /* Again, but now using SR_GHZ(). */ | |
152 | test_samplerate(SR_GHZ(1), "1 GHz"); | |
153 | test_samplerate(SR_GHZ(5), "5 GHz"); | |
154 | test_samplerate(SR_GHZ(72), "72 GHz"); | |
155 | test_samplerate(SR_GHZ(388), "388 GHz"); | |
156 | test_samplerate(SR_GHZ(4.417594444), "4.417594444 GHz"); | |
157 | test_samplerate(SR_GHZ(44.175944444), "44.175944444 GHz"); | |
158 | test_samplerate(SR_GHZ(441.759444441), "441.759444441 GHz"); | |
159 | test_samplerate(SR_GHZ(441.759000001), "441.759000001 GHz"); | |
160 | test_samplerate(SR_GHZ(441.050000000), "441.05 GHz"); | |
161 | test_samplerate(SR_GHZ(441.000000005), "441.000000005 GHz"); | |
ba253f2b | 162 | test_samplerate(SR_GHZ(441.500000000), "441.5 GHz"); |
79bb0e97 UH |
163 | |
164 | /* Now check the biggest-possible samplerate (2^64 Hz). */ | |
059f3632 UH |
165 | // test_samplerate(18446744073709551615ULL, "18446744073.709551615 GHz"); |
166 | // test_samplerate(SR_GHZ(18446744073ULL), "18446744073 GHz"); | |
79bb0e97 UH |
167 | } |
168 | END_TEST | |
169 | ||
5ec172d7 SB |
170 | START_TEST(test_integral) |
171 | { | |
172 | test_rational("1", (struct sr_rational){1, 1}); | |
173 | test_rational("2", (struct sr_rational){2, 1}); | |
174 | test_rational("10", (struct sr_rational){10, 1}); | |
175 | test_rational("-255", (struct sr_rational){-255, 1}); | |
176 | } | |
177 | END_TEST | |
178 | ||
179 | START_TEST(test_fractional) | |
180 | { | |
181 | test_rational("0.1", (struct sr_rational){1, 10}); | |
182 | test_rational("1.0", (struct sr_rational){10, 10}); | |
183 | test_rational("1.2", (struct sr_rational){12, 10}); | |
184 | test_rational("12.34", (struct sr_rational){1234, 100}); | |
185 | test_rational("-12.34", (struct sr_rational){-1234, 100}); | |
186 | test_rational("10.00", (struct sr_rational){1000, 100}); | |
187 | } | |
188 | END_TEST | |
189 | ||
190 | START_TEST(test_exponent) | |
191 | { | |
192 | test_rational("1e0", (struct sr_rational){1, 1}); | |
193 | test_rational("1E0", (struct sr_rational){1, 1}); | |
194 | test_rational("1E1", (struct sr_rational){10, 1}); | |
195 | test_rational("1e-1", (struct sr_rational){1, 10}); | |
196 | test_rational("-1.234e-0", (struct sr_rational){-1234, 1000}); | |
197 | test_rational("-1.234e3", (struct sr_rational){-1234, 1}); | |
198 | test_rational("-1.234e-3", (struct sr_rational){-1234, 1000000}); | |
199 | test_rational("0.001e3", (struct sr_rational){1, 1}); | |
200 | test_rational("0.001e0", (struct sr_rational){1, 1000}); | |
201 | test_rational("0.001e-3", (struct sr_rational){1, 1000000}); | |
202 | } | |
203 | END_TEST | |
204 | ||
79bb0e97 UH |
205 | Suite *suite_strutil(void) |
206 | { | |
207 | Suite *s; | |
208 | TCase *tc; | |
209 | ||
210 | s = suite_create("strutil"); | |
211 | ||
212 | tc = tcase_create("sr_samplerate_string"); | |
98de0c78 | 213 | tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown); |
79bb0e97 UH |
214 | tcase_add_test(tc, test_hz); |
215 | tcase_add_test(tc, test_khz); | |
216 | tcase_add_test(tc, test_mhz); | |
217 | tcase_add_test(tc, test_ghz); | |
5ec172d7 SB |
218 | tcase_add_test(tc, test_integral); |
219 | tcase_add_test(tc, test_fractional); | |
220 | tcase_add_test(tc, test_exponent); | |
79bb0e97 UH |
221 | suite_add_tcase(s, tc); |
222 | ||
223 | return s; | |
224 | } |