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>
28 * This test sequence cannot use internal helpers, since it's limited
29 * to the library's public API (by design). That is why there are local
30 * helper routines for endianess handling.
35 static void get_host_endianess(void)
45 static void swap_bytes(uint8_t *buff, size_t blen)
50 for (idx = 0; idx < blen / 2; idx++) {
51 tmp = buff[blen - 1 - idx];
52 buff[blen - 1 - idx] = buff[idx];
57 static int sr_analog_init_(struct sr_datafeed_analog *analog,
58 struct sr_analog_encoding *encoding,
59 struct sr_analog_meaning *meaning,
60 struct sr_analog_spec *spec,
63 memset(analog, 0, sizeof(*analog));
64 memset(encoding, 0, sizeof(*encoding));
65 memset(meaning, 0, sizeof(*meaning));
66 memset(spec, 0, sizeof(*spec));
68 analog->encoding = encoding;
69 analog->meaning = meaning;
72 encoding->unitsize = sizeof(float);
73 encoding->is_float = TRUE;
74 #ifdef WORDS_BIGENDIAN
75 encoding->is_bigendian = TRUE;
77 encoding->is_bigendian = FALSE;
79 encoding->digits = digits;
80 encoding->is_digits_decimal = TRUE;
81 encoding->scale.p = 1;
82 encoding->scale.q = 1;
83 encoding->offset.p = 0;
84 encoding->offset.q = 1;
86 spec->spec_digits = digits;
91 START_TEST(test_analog_to_float)
97 struct sr_datafeed_analog analog;
98 struct sr_analog_encoding encoding;
99 struct sr_analog_meaning meaning;
100 struct sr_analog_spec spec;
101 const float v[] = {-12.9, -333.999, 0, 3.1415, 29.7, 989898.121212};
103 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
104 analog.num_samples = 1;
106 meaning.channels = g_slist_append(NULL, &ch);
108 for (i = 0; i < ARRAY_SIZE(v); i++) {
111 ret = sr_analog_to_float(&analog, &fout);
112 fail_unless(ret == SR_OK, "sr_analog_to_float() failed: %d.", ret);
113 fail_unless(fabs(f - fout) <= 0.001, "%f != %f", f, fout);
118 START_TEST(test_analog_to_float_null)
122 struct sr_datafeed_analog analog;
123 struct sr_analog_encoding encoding;
124 struct sr_analog_meaning meaning;
125 struct sr_analog_spec spec;
128 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
129 analog.num_samples = 1;
132 ret = sr_analog_to_float(NULL, &fout);
133 fail_unless(ret == SR_ERR_ARG);
134 ret = sr_analog_to_float(&analog, NULL);
135 fail_unless(ret == SR_ERR_ARG);
136 ret = sr_analog_to_float(NULL, NULL);
137 fail_unless(ret == SR_ERR_ARG);
140 ret = sr_analog_to_float(&analog, &fout);
141 fail_unless(ret == SR_ERR_ARG);
144 analog.meaning = NULL;
145 ret = sr_analog_to_float(&analog, &fout);
146 fail_unless(ret == SR_ERR_ARG);
147 analog.meaning = &meaning;
149 analog.encoding = NULL;
150 ret = sr_analog_to_float(&analog, &fout);
151 fail_unless(ret == SR_ERR_ARG);
152 analog.encoding = &encoding;
156 START_TEST(test_analog_to_float_conv)
158 static const int with_diag = 0;
164 int is_fp, is_sign, is_be;
168 /* Test to cover multiple values in an array, odd numbers. */
170 .desc = "float single input, native, value array",
171 .bytes = (float[]){ -12.9, -333.999, 0, 3.14, 29.7, 9898.12, },
172 .nums = 6, .unit = sizeof(float),
173 .is_fp = TRUE, .is_sign = FALSE, .is_be = host_be,
174 .scale = 1, .offset = 0,
175 .want = (float[]){ -12.9, -333.999, 0, 3.14, 29.7, 9898.12, },
177 /* Tests to cover floating point input data conversion. */
179 .desc = "float single input, native",
180 .bytes = (float[]){ 1.0, 2.0, 3.0, 4.0, },
181 .nums = 4, .unit = sizeof(float),
182 .is_fp = TRUE, .is_sign = FALSE, .is_be = host_be,
183 .scale = 1, .offset = 0,
184 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
187 .desc = "float single input, big endian",
188 .bytes = (float[]){ 1.0, 2.0, 3.0, 4.0, },
189 .nums = 4, .unit = sizeof(float),
190 .is_fp = TRUE, .is_sign = FALSE, .is_be = TRUE,
191 .scale = 1, .offset = 0,
192 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
195 .desc = "float single input, little endian",
196 .bytes = (float[]){ 1.0, 2.0, 3.0, 4.0, },
197 .nums = 4, .unit = sizeof(float),
198 .is_fp = TRUE, .is_sign = FALSE, .is_be = FALSE,
199 .scale = 1, .offset = 0,
200 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
203 .desc = "float double input, native",
204 .bytes = (double[]){ 1.0, 2.0, 3.0, 4.0, },
205 .nums = 4, .unit = sizeof(double),
206 .is_fp = TRUE, .is_sign = FALSE, .is_be = host_be,
207 .scale = 1, .offset = 0,
208 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
211 .desc = "float half input, unsupported, fake bytes",
212 .bytes = (uint16_t[]){ 0x1234, 0x5678, },
213 .nums = 2, .unit = sizeof(uint16_t),
214 .is_fp = TRUE, .is_sign = FALSE, .is_be = host_be,
218 .desc = "float quad input, unsupported, fake bytes",
219 .bytes = (uint64_t[]){ 0x0, 0x0, },
220 .nums = 1, .unit = 2 * sizeof(uint64_t),
221 .is_fp = TRUE, .is_sign = FALSE, .is_be = host_be,
224 /* Tests to cover integer input data conversion. */
226 .desc = "int u8 input",
227 .bytes = (uint8_t[]){ 1, 2, 3, 4, },
228 .nums = 4, .unit = sizeof(uint8_t),
229 .is_fp = FALSE, .is_sign = FALSE, .is_be = host_be,
230 .scale = 1, .offset = 0,
231 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
234 .desc = "int i8 input",
235 .bytes = (int8_t[]){ -1, 2, -3, 4, },
236 .nums = 4, .unit = sizeof(int8_t),
237 .is_fp = FALSE, .is_sign = TRUE, .is_be = host_be,
238 .scale = 1, .offset = 0,
239 .want = (float[]){ -1.0, 2.0, -3.0, 4.0, },
242 .desc = "int u16 input, big endian",
243 .bytes = (uint16_t[]){ 1, 2, 3, 4, },
244 .nums = 4, .unit = sizeof(uint16_t),
245 .is_fp = FALSE, .is_sign = FALSE, .is_be = TRUE,
246 .scale = 1, .offset = 0,
247 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
250 .desc = "int u16 input, little endian",
251 .bytes = (uint16_t[]){ 1, 2, 3, 4, },
252 .nums = 4, .unit = sizeof(uint16_t),
253 .is_fp = FALSE, .is_sign = FALSE, .is_be = FALSE,
254 .scale = 1, .offset = 0,
255 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
258 .desc = "int i16 input, big endian",
259 .bytes = (int16_t[]){ 1, -2, 3, -4, },
260 .nums = 4, .unit = sizeof(int16_t),
261 .is_fp = FALSE, .is_sign = TRUE, .is_be = TRUE,
262 .scale = 1, .offset = 0,
263 .want = (float[]){ 1.0, -2.0, 3.0, -4.0, },
266 .desc = "int i16 input, little endian",
267 .bytes = (int16_t[]){ 1, -2, 3, -4, },
268 .nums = 4, .unit = sizeof(int16_t),
269 .is_fp = FALSE, .is_sign = TRUE, .is_be = FALSE,
270 .scale = 1, .offset = 0,
271 .want = (float[]){ 1.0, -2.0, 3.0, -4.0, },
274 .desc = "int u32 input, big endian",
275 .bytes = (uint32_t[]){ 1, 2, 3, 4, },
276 .nums = 4, .unit = sizeof(uint32_t),
277 .is_fp = FALSE, .is_sign = FALSE, .is_be = TRUE,
278 .scale = 1, .offset = 0,
279 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
282 .desc = "int u32 input, little endian",
283 .bytes = (uint32_t[]){ 1, 2, 3, 4, },
284 .nums = 4, .unit = sizeof(uint32_t),
285 .is_fp = FALSE, .is_sign = FALSE, .is_be = FALSE,
286 .scale = 1, .offset = 0,
287 .want = (float[]){ 1.0, 2.0, 3.0, 4.0, },
290 .desc = "int i32 input, big endian",
291 .bytes = (int32_t[]){ 1, 2, -3, -4, },
292 .nums = 4, .unit = sizeof(int32_t),
293 .is_fp = FALSE, .is_sign = TRUE, .is_be = TRUE,
294 .scale = 1, .offset = 0,
295 .want = (float[]){ 1.0, 2.0, -3.0, -4.0, },
298 .desc = "int i32 input, little endian",
299 .bytes = (int32_t[]){ 1, 2, -3, -4, },
300 .nums = 4, .unit = sizeof(int32_t),
301 .is_fp = FALSE, .is_sign = TRUE, .is_be = FALSE,
302 .scale = 1, .offset = 0,
303 .want = (float[]){ 1.0, 2.0, -3.0, -4.0, },
306 .desc = "int u64 input, unsupported",
307 .bytes = (uint64_t[]){ 1, 2, 3, 4, },
308 .nums = 4, .unit = sizeof(uint64_t),
309 .is_fp = FALSE, .is_sign = FALSE, .is_be = TRUE,
312 /* Tests to cover scale/offset calculation. */
314 .desc = "float single input, scale + offset",
315 .bytes = (float[]){ 1.0, 2.0, 3.0, 4.0, },
316 .nums = 4, .unit = sizeof(float),
317 .is_fp = TRUE, .is_sign = FALSE, .is_be = host_be,
318 .scale = 3, .offset = 2,
319 .want = (float[]){ 5.0, 8.0, 11.0, 14.0, },
322 .desc = "int u8 input, scale + offset",
323 .bytes = (uint8_t[]){ 1, 2, 3, 4, },
324 .nums = 4, .unit = sizeof(uint8_t),
325 .is_fp = FALSE, .is_sign = FALSE, .is_be = TRUE,
326 .scale = 3, .offset = 2,
327 .want = (float[]){ 5.0, 8.0, 11.0, 14.0, },
330 const size_t max_floats = 6;
331 struct sr_channel ch = {
334 .type = SR_CHANNEL_LOGIC,
340 struct sr_datafeed_analog analog;
341 struct sr_analog_encoding encoding;
342 struct sr_analog_meaning meaning;
343 struct sr_analog_spec spec;
344 size_t byte_count, value_idx;
345 uint8_t f_in[max_floats * sizeof(double)], *byte_ptr;
346 float f_out[max_floats];
350 for (item_idx = 0; item_idx < ARRAY_SIZE(items); item_idx++) {
351 item = &items[item_idx];
353 /* Construct "4x u32le" style test item identification. */
354 snprintf(item_text, sizeof(item_text), "%zu: %zux %c%zu%s",
355 item_idx, item->nums,
356 item->is_fp ? 'f' : item->is_sign ? 'i' : 'u',
357 item->unit * 8, item->is_be ? "be" : "le");
359 fprintf(stderr, "%s -- %s", item_text, item->desc);
363 /* Copy input data bytes, optionally adjust endianess. */
364 byte_count = item->nums * item->unit;
365 memcpy(f_in, item->bytes, byte_count);
366 if (item->is_be != host_be) {
368 for (value_idx = 0; value_idx < item->nums; value_idx++) {
369 swap_bytes(byte_ptr, item->unit);
370 byte_ptr += item->unit;
374 fprintf(stderr, " -- bytes:");
375 for (value_idx = 0; value_idx < byte_count; value_idx++)
376 fprintf(stderr, " %02x", f_in[value_idx]);
380 /* Setup the analog feed description. */
381 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
382 analog.num_samples = item->nums;
383 analog.data = &f_in[0];
384 encoding.unitsize = item->unit;
385 encoding.is_float = item->is_fp;
386 encoding.is_signed = item->is_sign;
387 encoding.is_bigendian = item->is_be;
388 encoding.scale.p = item->scale ? item->scale : 1;
389 encoding.offset.p = item->offset;
390 meaning.channels = g_slist_append(NULL, &ch);
392 /* Convert to an array of single precision float values. */
393 ret = sr_analog_to_float(&analog, &f_out[0]);
395 fail_if(ret == SR_OK,
396 "%s: sr_analog_to_float() passed", item_text);
398 fprintf(stderr, " -- expected fail, OK\n");
403 fail_unless(ret == SR_OK,
404 "%s: sr_analog_to_float() failed: %d", item_text, ret);
406 fprintf(stderr, " -- float:");
407 for (value_idx = 0; value_idx < item->nums; value_idx++)
408 fprintf(stderr, " %f", f_out[value_idx]);
409 fprintf(stderr, "\n");
414 * Compare result data to the expectation. No tolerance
415 * is required here due to the input set's values. This
416 * test concentrates on endianess / data type / bit count
417 * conversion and simple scale/offset calculation, neither
418 * on precision nor rounding nor truncation.
420 for (value_idx = 0; value_idx < item->nums; value_idx++) {
421 want = item->want[value_idx];
422 have = f_out[value_idx];
423 fail_unless(want == have,
424 "%s: input %f != output %f",
425 item_text, want, have);
431 START_TEST(test_analog_si_prefix)
438 const char *output_si_prefix;
440 { 12.0 , 0, 12.0 , 0, "" },
441 { 12.0 , 1, 12.0 , 1, "" },
442 { 12.0 , -1, 0.012, 2, "k" },
443 { 1024.0 , 0, 1.024, 3, "k" },
444 { 1024.0 , -1, 1.024, 2, "k" },
445 { 1024.0 , -3, 1.024, 0, "k" },
446 { 12.0e5 , 0, 1.2, 6, "M" },
447 { 0.123456, 0, 0.123456, 0, "" },
448 { 0.123456, 1, 0.123456, 1, "" },
449 { 0.123456, 2, 0.123456, 2, "" },
450 { 0.123456, 3, 123.456, 0, "m" },
451 { 0.123456, 4, 123.456, 1, "m" },
452 { 0.123456, 5, 123.456, 2, "m" },
453 { 0.123456, 6, 123.456, 3, "m" },
454 { 0.123456, 7, 123.456, 4, "m" },
455 { 0.0123 , 4, 12.3, 1, "m" },
456 { 0.00123 , 5, 1.23, 2, "m" },
457 { 0.000123, 4, 0.123, 1, "m" },
458 { 0.000123, 5, 0.123, 2, "m" },
459 { 0.000123, 6, 123.0, 0, "µ" },
460 { 0.000123, 7, 123.0, 1, "µ" },
463 for (unsigned int i = 0; i < ARRAY_SIZE(v); i++) {
464 float value = v[i].input_value;
465 int digits = v[i].input_digits;
466 const char *si_prefix = sr_analog_si_prefix(&value, &digits);
468 fail_unless(fabs(value - v[i].output_value) <= 0.00001,
469 "sr_analog_si_prefix() unexpected output value %f (i=%d).",
471 fail_unless(digits == v[i].output_digits,
472 "sr_analog_si_prefix() unexpected output digits %d (i=%d).",
474 fail_unless(!strcmp(si_prefix, v[i].output_si_prefix),
475 "sr_analog_si_prefix() unexpected output prefix \"%s\" (i=%d).",
481 START_TEST(test_analog_si_prefix_null)
485 const char *si_prefix;
487 si_prefix = sr_analog_si_prefix(NULL, &digits);
488 fail_unless(!strcmp(si_prefix, ""));
489 si_prefix = sr_analog_si_prefix(&value, NULL);
490 fail_unless(!strcmp(si_prefix, ""));
491 si_prefix = sr_analog_si_prefix(NULL, NULL);
492 fail_unless(!strcmp(si_prefix, ""));
496 START_TEST(test_analog_unit_to_string)
501 struct sr_datafeed_analog analog;
502 struct sr_analog_encoding encoding;
503 struct sr_analog_meaning meaning;
504 struct sr_analog_spec spec;
505 const int u[] = {SR_UNIT_VOLT, SR_UNIT_AMPERE, SR_UNIT_CELSIUS};
506 const int f[] = {SR_MQFLAG_RMS, 0, 0};
507 const char *r[] = {"V RMS", "A", "°C"};
509 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
511 for (i = 0; i < ARRAY_SIZE(r); i++) {
513 meaning.mqflags = f[i];
514 ret = sr_analog_unit_to_string(&analog, &result);
515 fail_unless(ret == SR_OK);
516 fail_unless(result != NULL);
517 fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
523 START_TEST(test_analog_unit_to_string_null)
527 struct sr_datafeed_analog analog;
528 struct sr_analog_encoding encoding;
529 struct sr_analog_meaning meaning;
530 struct sr_analog_spec spec;
532 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
534 meaning.unit = SR_UNIT_VOLT;
535 meaning.mqflags = SR_MQFLAG_RMS;
537 ret = sr_analog_unit_to_string(NULL, &result);
538 fail_unless(ret == SR_ERR_ARG);
539 ret = sr_analog_unit_to_string(&analog, NULL);
540 fail_unless(ret == SR_ERR_ARG);
541 ret = sr_analog_unit_to_string(NULL, NULL);
542 fail_unless(ret == SR_ERR_ARG);
544 analog.meaning = NULL;
545 ret = sr_analog_unit_to_string(&analog, &result);
546 fail_unless(ret == SR_ERR_ARG);
550 START_TEST(test_set_rational)
553 struct sr_rational r;
554 const int64_t p[] = {0, 1, -5, INT64_MAX};
555 const uint64_t q[] = {0, 2, 7, UINT64_MAX};
557 for (i = 0; i < ARRAY_SIZE(p); i++) {
558 sr_rational_set(&r, p[i], q[i]);
559 fail_unless(r.p == p[i] && r.q == q[i]);
564 START_TEST(test_set_rational_null)
566 sr_rational_set(NULL, 5, 7);
570 START_TEST(test_cmp_rational)
572 const struct sr_rational r[] = { { 1, 1 },
575 { INT64_MAX, INT64_MAX },
578 { INT64_MAX, UINT64_MAX },
579 { INT64_MIN, UINT64_MAX },
582 fail_unless(sr_rational_eq(&r[0], &r[0]) == 1);
583 fail_unless(sr_rational_eq(&r[0], &r[1]) == 1);
584 fail_unless(sr_rational_eq(&r[1], &r[2]) == 1);
585 fail_unless(sr_rational_eq(&r[2], &r[3]) == 1);
586 fail_unless(sr_rational_eq(&r[3], &r[3]) == 1);
588 fail_unless(sr_rational_eq(&r[4], &r[4]) == 1);
589 fail_unless(sr_rational_eq(&r[4], &r[5]) == 1);
590 fail_unless(sr_rational_eq(&r[5], &r[5]) == 1);
592 fail_unless(sr_rational_eq(&r[6], &r[6]) == 1);
593 fail_unless(sr_rational_eq(&r[7], &r[7]) == 1);
595 fail_unless(sr_rational_eq(&r[1], &r[4]) == 0);
599 START_TEST(test_mult_rational)
601 const struct sr_rational r[][3] = {
603 { { 1, 1 }, { 1, 1 }, { 1, 1 }},
604 { { 2, 1 }, { 3, 1 }, { 6, 1 }},
605 { { 1, 2 }, { 2, 1 }, { 1, 1 }},
606 /* Test negative numbers */
607 { { -1, 2 }, { 2, 1 }, { -1, 1 }},
608 { { -1, 2 }, { -2, 1 }, { 1, 1 }},
609 { { -(1ll<<20), (1ll<<10) }, { -(1ll<<20), 1 }, { (1ll<<30), 1 }},
611 { { INT32_MAX, (1ll<<12) }, { (1<<2), 1 }, { INT32_MAX, (1ll<<10) }},
612 { { INT64_MAX, (1ll<<63) }, { (1<<3), 1 }, { INT64_MAX, (1ll<<60) }},
613 /* Test large numbers */
614 { { (1ll<<40), (1ll<<10) }, { (1ll<<30), 1 }, { (1ll<<60), 1 }},
615 { { -(1ll<<40), (1ll<<10) }, { -(1ll<<30), 1 }, { (1ll<<60), 1 }},
617 { { 1000, 1 }, { 8000, 1 }, { 8000000, 1 }},
618 { { 10000, 1 }, { 80000, 1 }, { 800000000, 1 }},
619 { { 10000*3, 4 }, { 80000*3, 1 }, { 200000000*9, 1 }},
620 { { 1, 1000 }, { 1, 8000 }, { 1, 8000000 }},
621 { { 1, 10000 }, { 1, 80000 }, { 1, 800000000 }},
622 { { 4, 10000*3 }, { 1, 80000*3 }, { 1, 200000000*9 }},
624 { { -10000*3, 4 }, { 80000*3, 1 }, { -200000000*9, 1 }},
625 { { 10000*3, 4 }, { -80000*3, 1 }, { -200000000*9, 1 }},
628 for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
629 struct sr_rational res;
631 int rc = sr_rational_mult(&res, &r[i][0], &r[i][1]);
632 fail_unless(rc == SR_OK);
633 fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
634 "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
635 i, res.p, res.q, r[i][2].p, r[i][2].q);
640 START_TEST(test_div_rational)
642 const struct sr_rational r[][3] = {
644 { { 1, 1 }, { 1, 1 }, { 1, 1 }},
645 { { 2, 1 }, { 1, 3 }, { 6, 1 }},
646 { { 1, 2 }, { 1, 2 }, { 1, 1 }},
647 /* Test negative numbers */
648 { { -1, 2 }, { 1, 2 }, { -1, 1 }},
649 { { -1, 2 }, { -1, 2 }, { 1, 1 }},
650 { { -(1ll<<20), (1ll<<10) }, { -1, (1ll<<20) }, { (1ll<<30), 1 }},
652 { { INT32_MAX, (1ll<<12) }, { 1, (1<<2) }, { INT32_MAX, (1ll<<10) }},
653 { { INT64_MAX, (1ll<<63) }, { 1, (1<<3) }, { INT64_MAX, (1ll<<60) }},
654 /* Test large numbers */
655 { { (1ll<<40), (1ll<<10) }, { 1, (1ll<<30) }, { (1ll<<60), 1 }},
656 { { -(1ll<<40), (1ll<<10) }, { -1, (1ll<<30) }, { (1ll<<60), 1 }},
658 { { 10000*3, 4 }, { 1, 80000*3 }, { 200000000*9, 1 }},
659 { { 4, 10000*3 }, { 80000*3, 1 }, { 1, 200000000*9 }},
661 { { -10000*3, 4 }, { 1, 80000*3 }, { -200000000*9, 1 }},
662 { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }},
665 for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
666 struct sr_rational res;
668 int rc = sr_rational_div(&res, &r[i][0], &r[i][1]);
669 fail_unless(rc == SR_OK);
670 fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
671 "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
672 i, res.p, res.q, r[i][2].p, r[i][2].q);
676 struct sr_rational res;
677 int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 }));
679 fail_unless(rc == SR_ERR_ARG);
684 Suite *suite_analog(void)
689 get_host_endianess();
691 s = suite_create("analog");
693 tc = tcase_create("analog_to_float");
694 tcase_add_test(tc, test_analog_to_float);
695 tcase_add_test(tc, test_analog_to_float_null);
696 tcase_add_test(tc, test_analog_to_float_conv);
697 suite_add_tcase(s, tc);
699 tc = tcase_create("analog_si_unit");
700 tcase_add_test(tc, test_analog_si_prefix);
701 tcase_add_test(tc, test_analog_si_prefix_null);
702 tcase_add_test(tc, test_analog_unit_to_string);
703 tcase_add_test(tc, test_analog_unit_to_string_null);
704 suite_add_tcase(s, tc);
706 tc = tcase_create("analog_rational");
707 tcase_add_test(tc, test_set_rational);
708 tcase_add_test(tc, test_set_rational_null);
709 tcase_add_test(tc, test_cmp_rational);
710 tcase_add_test(tc, test_mult_rational);
711 tcase_add_test(tc, test_div_rational);
712 suite_add_tcase(s, tc);