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