2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 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/>.
24 #include <libsigrok/libsigrok.h>
27 static 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,
33 memset(analog, 0, sizeof(*analog));
34 memset(encoding, 0, sizeof(*encoding));
35 memset(meaning, 0, sizeof(*meaning));
36 memset(spec, 0, sizeof(*spec));
38 analog->encoding = encoding;
39 analog->meaning = meaning;
42 encoding->unitsize = sizeof(float);
43 encoding->is_float = TRUE;
44 #ifdef WORDS_BIGENDIAN
45 encoding->is_bigendian = TRUE;
47 encoding->is_bigendian = FALSE;
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;
56 spec->spec_digits = digits;
61 START_TEST(test_analog_to_float)
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};
73 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
74 analog.num_samples = 1;
76 meaning.channels = g_slist_append(NULL, &ch);
78 for (i = 0; i < ARRAY_SIZE(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);
88 START_TEST(test_analog_to_float_null)
92 struct sr_datafeed_analog analog;
93 struct sr_analog_encoding encoding;
94 struct sr_analog_meaning meaning;
95 struct sr_analog_spec spec;
98 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
99 analog.num_samples = 1;
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);
110 ret = sr_analog_to_float(&analog, &fout);
111 fail_unless(ret == SR_ERR_ARG);
114 analog.meaning = NULL;
115 ret = sr_analog_to_float(&analog, &fout);
116 fail_unless(ret == SR_ERR_ARG);
117 analog.meaning = &meaning;
119 analog.encoding = NULL;
120 ret = sr_analog_to_float(&analog, &fout);
121 fail_unless(ret == SR_ERR_ARG);
122 analog.encoding = &encoding;
126 START_TEST(test_analog_si_prefix)
133 const char *output_si_prefix;
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, "µ" },
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);
163 fail_unless(fabs(value - v[i].output_value) <= 0.00001,
164 "sr_analog_si_prefix() unexpected output value %f (i=%d).",
166 fail_unless(digits == v[i].output_digits,
167 "sr_analog_si_prefix() unexpected output digits %d (i=%d).",
169 fail_unless(!strcmp(si_prefix, v[i].output_si_prefix),
170 "sr_analog_si_prefix() unexpected output prefix \"%s\" (i=%d).",
176 START_TEST(test_analog_si_prefix_null)
180 const char *si_prefix;
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, ""));
191 START_TEST(test_analog_unit_to_string)
196 struct sr_datafeed_analog analog;
197 struct sr_analog_encoding encoding;
198 struct sr_analog_meaning meaning;
199 struct sr_analog_spec spec;
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"};
204 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
206 for (i = 0; i < ARRAY_SIZE(r); i++) {
208 meaning.mqflags = f[i];
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]);
218 START_TEST(test_analog_unit_to_string_null)
222 struct sr_datafeed_analog analog;
223 struct sr_analog_encoding encoding;
224 struct sr_analog_meaning meaning;
225 struct sr_analog_spec spec;
227 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
229 meaning.unit = SR_UNIT_VOLT;
230 meaning.mqflags = SR_MQFLAG_RMS;
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);
239 analog.meaning = NULL;
240 ret = sr_analog_unit_to_string(&analog, &result);
241 fail_unless(ret == SR_ERR_ARG);
245 START_TEST(test_set_rational)
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};
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]);
259 START_TEST(test_set_rational_null)
261 sr_rational_set(NULL, 5, 7);
265 START_TEST(test_cmp_rational)
267 const struct sr_rational r[] = { { 1, 1 },
270 { INT64_MAX, INT64_MAX },
273 { INT64_MAX, UINT64_MAX },
274 { INT64_MIN, UINT64_MAX },
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);
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);
287 fail_unless(sr_rational_eq(&r[6], &r[6]) == 1);
288 fail_unless(sr_rational_eq(&r[7], &r[7]) == 1);
290 fail_unless(sr_rational_eq(&r[1], &r[4]) == 0);
294 START_TEST(test_mult_rational)
296 const struct sr_rational r[][3] = {
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 }},
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 }},
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 }},
319 { { -10000*3, 4 }, { 80000*3, 1 }, { -200000000*9, 1 }},
320 { { 10000*3, 4 }, { -80000*3, 1 }, { -200000000*9, 1 }},
323 for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
324 struct sr_rational res;
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);
335 START_TEST(test_div_rational)
337 const struct sr_rational r[][3] = {
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 }},
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 }},
353 { { 10000*3, 4 }, { 1, 80000*3 }, { 200000000*9, 1 }},
354 { { 4, 10000*3 }, { 80000*3, 1 }, { 1, 200000000*9 }},
356 { { -10000*3, 4 }, { 1, 80000*3 }, { -200000000*9, 1 }},
357 { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }},
360 for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
361 struct sr_rational res;
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);
371 struct sr_rational res;
372 int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 }));
374 fail_unless(rc == SR_ERR_ARG);
379 Suite *suite_analog(void)
384 s = suite_create("analog");
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);
389 tcase_add_test(tc, test_analog_si_prefix);
390 tcase_add_test(tc, test_analog_si_prefix_null);
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);
395 tcase_add_test(tc, test_cmp_rational);
396 tcase_add_test(tc, test_mult_rational);
397 tcase_add_test(tc, test_div_rational);
398 suite_add_tcase(s, tc);