]> sigrok.org Git - libsigrok.git/blame - tests/analog.c
analog: improve output readability by using SI prefix
[libsigrok.git] / tests / analog.c
CommitLineData
6b71bf1b
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 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 <config.h>
22#include <stdlib.h>
23#include <math.h>
24#include <check.h>
25#include <libsigrok/libsigrok.h>
26#include "lib.h"
27
28static int sr_analog_init_(struct sr_datafeed_analog *analog,
29 struct sr_analog_encoding *encoding,
30 struct sr_analog_meaning *meaning,
31 struct sr_analog_spec *spec,
32 int digits)
33{
34 memset(analog, 0, sizeof(*analog));
35 memset(encoding, 0, sizeof(*encoding));
36 memset(meaning, 0, sizeof(*meaning));
37 memset(spec, 0, sizeof(*spec));
38
39 analog->encoding = encoding;
40 analog->meaning = meaning;
41 analog->spec = spec;
42
43 encoding->unitsize = sizeof(float);
44 encoding->is_float = TRUE;
45#ifdef WORDS_BIGENDIAN
46 encoding->is_bigendian = TRUE;
47#else
48 encoding->is_bigendian = FALSE;
49#endif
50 encoding->digits = digits;
51 encoding->is_digits_decimal = TRUE;
52 encoding->scale.p = 1;
53 encoding->scale.q = 1;
54 encoding->offset.p = 0;
55 encoding->offset.q = 1;
56
57 spec->spec_digits = digits;
58
59 return SR_OK;
60}
61
62START_TEST(test_analog_to_float)
63{
64 int ret;
65 unsigned int i;
66 float f, fout;
67 struct sr_channel ch;
68 struct sr_datafeed_analog analog;
69 struct sr_analog_encoding encoding;
70 struct sr_analog_meaning meaning;
71 struct sr_analog_spec spec;
72 const float v[] = {-12.9, -333.999, 0, 3.1415, 29.7, 989898.121212};
73
74 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
75 analog.num_samples = 1;
76 analog.data = &f;
77 meaning.channels = g_slist_append(NULL, &ch);
78
79 for (i = 0; i < ARRAY_SIZE(v); i++) {
80 fout = 19;
81 f = v[i];
82 ret = sr_analog_to_float(&analog, &fout);
83 fail_unless(ret == SR_OK, "sr_analog_to_float() failed: %d.", ret);
84 fail_unless(fabs(f - fout) <= 0.001, "%f != %f", f, fout);
85 }
86}
87END_TEST
88
89START_TEST(test_analog_to_float_null)
90{
91 int ret;
92 float f, fout;
93 struct sr_datafeed_analog analog;
94 struct sr_analog_encoding encoding;
95 struct sr_analog_meaning meaning;
96 struct sr_analog_spec spec;
97
98 f = G_PI;
99 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
100 analog.num_samples = 1;
101 analog.data = &f;
102
103 ret = sr_analog_to_float(NULL, &fout);
104 fail_unless(ret == SR_ERR_ARG);
105 ret = sr_analog_to_float(&analog, NULL);
106 fail_unless(ret == SR_ERR_ARG);
107 ret = sr_analog_to_float(NULL, NULL);
108 fail_unless(ret == SR_ERR_ARG);
109
110 analog.data = NULL;
111 ret = sr_analog_to_float(&analog, &fout);
112 fail_unless(ret == SR_ERR_ARG);
113 analog.data = &f;
114
115 analog.meaning = NULL;
116 ret = sr_analog_to_float(&analog, &fout);
117 fail_unless(ret == SR_ERR_ARG);
118 analog.meaning = &meaning;
119
120 analog.encoding = NULL;
121 ret = sr_analog_to_float(&analog, &fout);
122 fail_unless(ret == SR_ERR_ARG);
123 analog.encoding = &encoding;
124}
125END_TEST
126
962172e4
AJ
127START_TEST(test_analog_si_prefix)
128{
129 struct {
130 float input_value;
131 int input_digits;
132 float output_value;
133 int output_digits;
134 const char *output_si_prefix;
135 } v[] = {
136 { 12.0 , 0, 12.0 , 0, "" },
137 { 12.0 , 1, 12.0 , 1, "" },
138 { 12.0 , -1, 0.012, 2, "k" },
139 { 1024.0 , 0, 1.024, 3, "k" },
140 { 1024.0 , -1, 1.024, 2, "k" },
141 { 1024.0 , -3, 1.024, 0, "k" },
142 { 12.0e5 , 0, 1.2, 6, "M" },
143 { 0.123456, 0, 0.123456, 0, "" },
144 { 0.123456, 1, 0.123456, 1, "" },
145 { 0.123456, 2, 0.123456, 2, "" },
146 { 0.123456, 3, 123.456, 0, "m" },
147 { 0.123456, 4, 123.456, 1, "m" },
148 { 0.123456, 5, 123.456, 2, "m" },
149 { 0.123456, 6, 123.456, 3, "m" },
150 { 0.123456, 7, 123.456, 4, "m" },
151 { 0.0123 , 4, 12.3, 1, "m" },
152 { 0.00123 , 5, 1.23, 2, "m" },
153 { 0.000123, 4, 0.123, 1, "m" },
154 { 0.000123, 5, 0.123, 2, "m" },
155 { 0.000123, 6, 123.0, 0, "µ" },
156 { 0.000123, 7, 123.0, 1, "µ" },
157 };
158
159 for (unsigned int i = 0; i < ARRAY_SIZE(v); i++) {
160 float value = v[i].input_value;
161 int digits = v[i].input_digits;
162 const char *si_prefix = sr_analog_si_prefix(&value, &digits);
163
164 fail_unless(fabs(value - v[i].output_value) <= 0.00001,
165 "sr_analog_si_prefix() unexpected output value %f (i=%d).",
166 value , i);
167 fail_unless(digits == v[i].output_digits,
168 "sr_analog_si_prefix() unexpected output digits %d (i=%d).",
169 digits, i);
170 fail_unless(!strcmp(si_prefix, v[i].output_si_prefix),
171 "sr_analog_si_prefix() unexpected output prefix \"%s\" (i=%d).",
172 si_prefix, i);
173 }
174}
175END_TEST
176
177START_TEST(test_analog_si_prefix_null)
178{
179 float value = 1.23;
180 int digits = 1;
181 const char *si_prefix;
182
183 si_prefix = sr_analog_si_prefix(NULL, &digits);
184 fail_unless(!strcmp(si_prefix, ""));
185 si_prefix = sr_analog_si_prefix(&value, NULL);
186 fail_unless(!strcmp(si_prefix, ""));
187 si_prefix = sr_analog_si_prefix(NULL, NULL);
188 fail_unless(!strcmp(si_prefix, ""));
189}
190END_TEST
191
6b71bf1b
UH
192START_TEST(test_analog_unit_to_string)
193{
194 int ret;
195 unsigned int i;
196 char *result;
197 struct sr_datafeed_analog analog;
198 struct sr_analog_encoding encoding;
199 struct sr_analog_meaning meaning;
200 struct sr_analog_spec spec;
201 const char *r[] = {" V RMS"};
202
203 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
204
205 for (i = -1; i < ARRAY_SIZE(r); i++) {
206 meaning.unit = SR_UNIT_VOLT;
207 meaning.mqflags = SR_MQFLAG_RMS;
208 ret = sr_analog_unit_to_string(&analog, &result);
209 fail_unless(ret == SR_OK);
210 fail_unless(result != NULL);
211 fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
212 g_free(result);
213 }
214}
215END_TEST
216
217START_TEST(test_analog_unit_to_string_null)
218{
219 int ret;
220 char *result;
221 struct sr_datafeed_analog analog;
222 struct sr_analog_encoding encoding;
223 struct sr_analog_meaning meaning;
224 struct sr_analog_spec spec;
225
226 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
227
228 meaning.unit = SR_UNIT_VOLT;
229 meaning.mqflags = SR_MQFLAG_RMS;
230
231 ret = sr_analog_unit_to_string(NULL, &result);
232 fail_unless(ret == SR_ERR_ARG);
233 ret = sr_analog_unit_to_string(&analog, NULL);
234 fail_unless(ret == SR_ERR_ARG);
235 ret = sr_analog_unit_to_string(NULL, NULL);
236 fail_unless(ret == SR_ERR_ARG);
237
238 analog.meaning = NULL;
239 ret = sr_analog_unit_to_string(&analog, &result);
240 fail_unless(ret == SR_ERR_ARG);
241}
242END_TEST
243
244START_TEST(test_set_rational)
245{
246 unsigned int i;
247 struct sr_rational r;
248 const int64_t p[] = {0, 1, -5, INT64_MAX};
249 const uint64_t q[] = {0, 2, 7, UINT64_MAX};
250
251 for (i = 0; i < ARRAY_SIZE(p); i++) {
252 sr_rational_set(&r, p[i], q[i]);
253 fail_unless(r.p == p[i] && r.q == q[i]);
254 }
255}
256END_TEST
257
258START_TEST(test_set_rational_null)
259{
260 sr_rational_set(NULL, 5, 7);
261}
262END_TEST
263
bdba3626
SB
264START_TEST(test_cmp_rational)
265{
266 const struct sr_rational r[] = { { 1, 1 },
267 { 2, 2 },
268 { 1000, 1000 },
269 { INT64_MAX, INT64_MAX },
270 { 1, 4 },
271 { 2, 8 },
272 { INT64_MAX, UINT64_MAX },
273 { INT64_MIN, UINT64_MAX },
274 };
275
276 fail_unless(sr_rational_eq(&r[0], &r[0]) == 1);
277 fail_unless(sr_rational_eq(&r[0], &r[1]) == 1);
278 fail_unless(sr_rational_eq(&r[1], &r[2]) == 1);
279 fail_unless(sr_rational_eq(&r[2], &r[3]) == 1);
280 fail_unless(sr_rational_eq(&r[3], &r[3]) == 1);
281
282 fail_unless(sr_rational_eq(&r[4], &r[4]) == 1);
283 fail_unless(sr_rational_eq(&r[4], &r[5]) == 1);
284 fail_unless(sr_rational_eq(&r[5], &r[5]) == 1);
285
286 fail_unless(sr_rational_eq(&r[6], &r[6]) == 1);
287 fail_unless(sr_rational_eq(&r[7], &r[7]) == 1);
288
289 fail_unless(sr_rational_eq(&r[1], &r[4]) == 0);
290}
291END_TEST
292
ee1b6054
SB
293START_TEST(test_mult_rational)
294{
295 const struct sr_rational r[][3] = {
296 /* a * b = c */
297 { { 1, 1 }, { 1, 1 }, { 1, 1 }},
298 { { 2, 1 }, { 3, 1 }, { 6, 1 }},
299 { { 1, 2 }, { 2, 1 }, { 1, 1 }},
300 /* Test negative numbers */
301 { { -1, 2 }, { 2, 1 }, { -1, 1 }},
302 { { -1, 2 }, { -2, 1 }, { 1, 1 }},
303 { { -(1ll<<20), (1ll<<10) }, { -(1ll<<20), 1 }, { (1ll<<30), 1 }},
304 /* Test reduction */
305 { { INT32_MAX, (1ll<<12) }, { (1<<2), 1 }, { INT32_MAX, (1ll<<10) }},
306 { { INT64_MAX, (1ll<<63) }, { (1<<3), 1 }, { INT64_MAX, (1ll<<60) }},
307 /* Test large numbers */
308 { { (1ll<<40), (1ll<<10) }, { (1ll<<30), 1 }, { (1ll<<60), 1 }},
309 { { -(1ll<<40), (1ll<<10) }, { -(1ll<<30), 1 }, { (1ll<<60), 1 }},
310
311 { { 1000, 1 }, { 8000, 1 }, { 8000000, 1 }},
312 { { 10000, 1 }, { 80000, 1 }, { 800000000, 1 }},
313 { { 10000*3, 4 }, { 80000*3, 1 }, { 200000000*9, 1 }},
314 { { 1, 1000 }, { 1, 8000 }, { 1, 8000000 }},
315 { { 1, 10000 }, { 1, 80000 }, { 1, 800000000 }},
316 { { 4, 10000*3 }, { 1, 80000*3 }, { 1, 200000000*9 }},
317
318 { { -10000*3, 4 }, { 80000*3, 1 }, { -200000000*9, 1 }},
319 { { 10000*3, 4 }, { -80000*3, 1 }, { -200000000*9, 1 }},
320 };
321
322 for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
323 struct sr_rational res;
324
325 int rc = sr_rational_mult(&res, &r[i][0], &r[i][1]);
326 fail_unless(rc == SR_OK);
327 fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
328 "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
329 i, res.p, res.q, r[i][2].p, r[i][2].q);
330 }
331}
332END_TEST
333
17d5a11c
SB
334START_TEST(test_div_rational)
335{
336 const struct sr_rational r[][3] = {
337 /* a * b = c */
338 { { 1, 1 }, { 1, 1 }, { 1, 1 }},
339 { { 2, 1 }, { 1, 3 }, { 6, 1 }},
340 { { 1, 2 }, { 1, 2 }, { 1, 1 }},
341 /* Test negative numbers */
342 { { -1, 2 }, { 1, 2 }, { -1, 1 }},
343 { { -1, 2 }, { -1, 2 }, { 1, 1 }},
344 { { -(1ll<<20), (1ll<<10) }, { -1, (1ll<<20) }, { (1ll<<30), 1 }},
345 /* Test reduction */
346 { { INT32_MAX, (1ll<<12) }, { 1, (1<<2) }, { INT32_MAX, (1ll<<10) }},
347 { { INT64_MAX, (1ll<<63) }, { 1, (1<<3) }, { INT64_MAX, (1ll<<60) }},
348 /* Test large numbers */
349 { { (1ll<<40), (1ll<<10) }, { 1, (1ll<<30) }, { (1ll<<60), 1 }},
350 { { -(1ll<<40), (1ll<<10) }, { -1, (1ll<<30) }, { (1ll<<60), 1 }},
351
352 { { 10000*3, 4 }, { 1, 80000*3 }, { 200000000*9, 1 }},
353 { { 4, 10000*3 }, { 80000*3, 1 }, { 1, 200000000*9 }},
354
355 { { -10000*3, 4 }, { 1, 80000*3 }, { -200000000*9, 1 }},
356 { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }},
357 };
358
359 for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
360 struct sr_rational res;
361
362 int rc = sr_rational_div(&res, &r[i][0], &r[i][1]);
363 fail_unless(rc == SR_OK);
364 fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
365 "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
366 i, res.p, res.q, r[i][2].p, r[i][2].q);
367 }
368
369 {
370 struct sr_rational res;
371 int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 }));
372
373 fail_unless(rc == SR_ERR_ARG);
374 }
375}
376END_TEST
377
6b71bf1b
UH
378Suite *suite_analog(void)
379{
380 Suite *s;
381 TCase *tc;
382
383 s = suite_create("analog");
384
385 tc = tcase_create("analog_to_float");
386 tcase_add_test(tc, test_analog_to_float);
387 tcase_add_test(tc, test_analog_to_float_null);
962172e4
AJ
388 tcase_add_test(tc, test_analog_si_prefix);
389 tcase_add_test(tc, test_analog_si_prefix_null);
6b71bf1b
UH
390 tcase_add_test(tc, test_analog_unit_to_string);
391 tcase_add_test(tc, test_analog_unit_to_string_null);
392 tcase_add_test(tc, test_set_rational);
393 tcase_add_test(tc, test_set_rational_null);
bdba3626 394 tcase_add_test(tc, test_cmp_rational);
ee1b6054 395 tcase_add_test(tc, test_mult_rational);
17d5a11c 396 tcase_add_test(tc, test_div_rational);
6b71bf1b
UH
397 suite_add_tcase(s, tc);
398
399 return s;
400}