]>
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 | ||
21 | #include <check.h> | |
22 | #include "../libsigrok.h" | |
23 | ||
24 | struct sr_context *sr_ctx; | |
25 | ||
26 | static void setup(void) | |
27 | { | |
28 | int ret; | |
29 | ||
30 | ret = sr_init(&sr_ctx); | |
31 | fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret); | |
32 | } | |
33 | ||
34 | static void teardown(void) | |
35 | { | |
36 | int ret; | |
37 | ||
38 | ret = sr_exit(sr_ctx); | |
39 | fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret); | |
40 | } | |
41 | ||
42 | static void test_samplerate(uint64_t samplerate, const char *expected) | |
43 | { | |
44 | char *s; | |
45 | ||
46 | s = sr_samplerate_string(samplerate); | |
47 | fail_unless(s != NULL); | |
48 | fail_unless(!strcmp(s, expected), | |
49 | "Invalid result for '%s': %s.", expected, s); | |
50 | g_free(s); | |
51 | } | |
52 | ||
53 | /* | |
54 | * Check various inputs for sr_samplerate_string(): | |
55 | * | |
56 | * - One, two, or three digit results (e.g. 5/55/555 MHz). | |
57 | * - Results which contain commas (e.g. 1.234 / 12.34 / 123.4 kHz). | |
58 | * - Results with zeroes right after the comma (e.g. 1.034 Hz). | |
59 | * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=73 | |
60 | * - Results with trailing zeroes (e.g. 1.230 kHz). | |
61 | * (This is currently allowed, but might be changed later) | |
62 | * - Results with zeroes in the middle (e.g. 1.204 kHz). | |
63 | * - All of the above, but using SR_MHZ() and friends. | |
64 | * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=72 | |
65 | * | |
66 | * All of the above tests are done for the Hz/kHz/MHz/GHz ranges. | |
67 | */ | |
68 | ||
69 | START_TEST(test_hz) | |
70 | { | |
71 | test_samplerate(0, "0 Hz"); | |
72 | test_samplerate(1, "1 Hz"); | |
73 | test_samplerate(23, "23 Hz"); | |
74 | test_samplerate(644, "644 Hz"); | |
75 | test_samplerate(604, "604 Hz"); | |
76 | test_samplerate(550, "550 Hz"); | |
77 | ||
78 | /* Again, but now using SR_HZ(). */ | |
79 | test_samplerate(SR_HZ(0), "0 Hz"); | |
80 | test_samplerate(SR_HZ(1), "1 Hz"); | |
81 | test_samplerate(SR_HZ(23), "23 Hz"); | |
82 | test_samplerate(SR_HZ(644), "644 Hz"); | |
83 | test_samplerate(SR_HZ(604), "604 Hz"); | |
84 | test_samplerate(SR_HZ(550), "550 Hz"); | |
85 | } | |
86 | END_TEST | |
87 | ||
88 | START_TEST(test_khz) | |
89 | { | |
90 | test_samplerate(1000, "1 kHz"); | |
91 | test_samplerate(99000, "99 kHz"); | |
92 | test_samplerate(225000, "225 kHz"); | |
93 | test_samplerate(1234, "1.234 kHz"); | |
94 | test_samplerate(12345, "12.345 kHz"); | |
95 | test_samplerate(123456, "123.456 kHz"); | |
96 | test_samplerate(1034, "1.034 kHz"); | |
97 | test_samplerate(1004, "1.004 kHz"); | |
98 | test_samplerate(1230, "1.230 kHz"); | |
99 | ||
100 | /* Again, but now using SR_KHZ(). */ | |
101 | test_samplerate(SR_KHZ(1), "1 kHz"); | |
102 | test_samplerate(SR_KHZ(99), "99 kHz"); | |
103 | test_samplerate(SR_KHZ(225), "225 kHz"); | |
104 | test_samplerate(SR_KHZ(1.234), "1.234 kHz"); | |
105 | test_samplerate(SR_KHZ(12.345), "12.345 kHz"); | |
106 | test_samplerate(SR_KHZ(123.456), "123.456 kHz"); | |
107 | test_samplerate(SR_KHZ(1.204), "1.204 kHz"); | |
108 | test_samplerate(SR_KHZ(1.034), "1.034 kHz"); | |
109 | test_samplerate(SR_KHZ(1.004), "1.004 kHz"); | |
110 | test_samplerate(SR_KHZ(1.230), "1.230 kHz"); | |
111 | } | |
112 | END_TEST | |
113 | ||
114 | START_TEST(test_mhz) | |
115 | { | |
116 | test_samplerate(1000000, "1 MHz"); | |
117 | test_samplerate(28000000, "28 MHz"); | |
118 | test_samplerate(775000000, "775 MHz"); | |
119 | test_samplerate(1234567, "1.234567 MHz"); | |
120 | test_samplerate(12345678, "12.345678 MHz"); | |
121 | test_samplerate(123456789, "123.456789 MHz"); | |
122 | test_samplerate(1230007, "1.230007 MHz"); | |
123 | test_samplerate(1034567, "1.034567 MHz"); | |
124 | test_samplerate(1000007, "1.000007 MHz"); | |
125 | test_samplerate(1234000, "1.234000 MHz"); | |
126 | ||
127 | /* Again, but now using SR_MHZ(). */ | |
128 | test_samplerate(SR_MHZ(1), "1 MHz"); | |
129 | test_samplerate(SR_MHZ(28), "28 MHz"); | |
130 | test_samplerate(SR_MHZ(775), "775 MHz"); | |
131 | test_samplerate(SR_MHZ(1.234567), "1.234567 MHz"); | |
132 | test_samplerate(SR_MHZ(12.345678), "12.345678 MHz"); | |
133 | test_samplerate(SR_MHZ(123.456789), "123.456789 MHz"); | |
134 | test_samplerate(SR_MHZ(1.230007), "1.230007 MHz"); | |
135 | test_samplerate(SR_MHZ(1.034567), "1.034567 MHz"); | |
136 | test_samplerate(SR_MHZ(1.000007), "1.000007 MHz"); | |
137 | test_samplerate(SR_MHZ(1.234000), "1.234000 MHz"); | |
138 | } | |
139 | END_TEST | |
140 | ||
141 | START_TEST(test_ghz) | |
142 | { | |
143 | /* Note: Numbers > 2^32 need a ULL suffix. */ | |
144 | ||
145 | test_samplerate(1000000000, "1 GHz"); | |
146 | test_samplerate(5000000000ULL, "5 GHz"); | |
147 | test_samplerate(72000000000ULL, "72 GHz"); | |
148 | test_samplerate(388000000000ULL, "388 GHz"); | |
149 | test_samplerate(4417594444ULL, "4.417594444 GHz"); | |
150 | test_samplerate(44175944444ULL, "44.175944444 GHz"); | |
151 | test_samplerate(441759444441ULL, "441.759444441 GHz"); | |
152 | test_samplerate(441759000001ULL, "441.759000001 GHz"); | |
153 | test_samplerate(441050000000ULL, "441.05 GHz"); | |
154 | test_samplerate(441000000005ULL, "441.000000005 GHz"); | |
155 | test_samplerate(441500000000ULL, "441.500000000 GHz"); | |
156 | ||
157 | /* Again, but now using SR_GHZ(). */ | |
158 | test_samplerate(SR_GHZ(1), "1 GHz"); | |
159 | test_samplerate(SR_GHZ(5), "5 GHz"); | |
160 | test_samplerate(SR_GHZ(72), "72 GHz"); | |
161 | test_samplerate(SR_GHZ(388), "388 GHz"); | |
162 | test_samplerate(SR_GHZ(4.417594444), "4.417594444 GHz"); | |
163 | test_samplerate(SR_GHZ(44.175944444), "44.175944444 GHz"); | |
164 | test_samplerate(SR_GHZ(441.759444441), "441.759444441 GHz"); | |
165 | test_samplerate(SR_GHZ(441.759000001), "441.759000001 GHz"); | |
166 | test_samplerate(SR_GHZ(441.050000000), "441.05 GHz"); | |
167 | test_samplerate(SR_GHZ(441.000000005), "441.000000005 GHz"); | |
168 | test_samplerate(SR_GHZ(441.500000000), "441.500000000 GHz"); | |
169 | ||
170 | /* Now check the biggest-possible samplerate (2^64 Hz). */ | |
171 | test_samplerate(18446744073709551615ULL, "18446744073.709551615 GHz"); | |
172 | test_samplerate(SR_GHZ(18446744073ULL), "18446744073 GHz"); | |
173 | } | |
174 | END_TEST | |
175 | ||
176 | Suite *suite_strutil(void) | |
177 | { | |
178 | Suite *s; | |
179 | TCase *tc; | |
180 | ||
181 | s = suite_create("strutil"); | |
182 | ||
183 | tc = tcase_create("sr_samplerate_string"); | |
184 | tcase_add_checked_fixture(tc, setup, teardown); | |
185 | tcase_add_test(tc, test_hz); | |
186 | tcase_add_test(tc, test_khz); | |
187 | tcase_add_test(tc, test_mhz); | |
188 | tcase_add_test(tc, test_ghz); | |
189 | suite_add_tcase(s, tc); | |
190 | ||
191 | return s; | |
192 | } |