]> sigrok.org Git - libsigrok.git/blame - tests/analog.c
rigol-ds: free memory that was allocated by SCPI get routines
[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
6205515c
GS
27/*
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.
31 */
32
33static int host_be;
34
35static void get_host_endianess(void)
36{
37 int x;
38 uint8_t *p;
39
40 p = (void *)&x;
41 x = 1;
42 host_be = *p ? 0 : 1;
43}
44
45static void swap_bytes(uint8_t *buff, size_t blen)
46{
47 size_t idx;
48 uint8_t tmp;
49
50 for (idx = 0; idx < blen / 2; idx++) {
51 tmp = buff[blen - 1 - idx];
52 buff[blen - 1 - idx] = buff[idx];
53 buff[idx] = tmp;
54 }
55}
56
6b71bf1b
UH
57static 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,
61 int digits)
62{
63 memset(analog, 0, sizeof(*analog));
64 memset(encoding, 0, sizeof(*encoding));
65 memset(meaning, 0, sizeof(*meaning));
66 memset(spec, 0, sizeof(*spec));
67
68 analog->encoding = encoding;
69 analog->meaning = meaning;
70 analog->spec = spec;
71
72 encoding->unitsize = sizeof(float);
73 encoding->is_float = TRUE;
74#ifdef WORDS_BIGENDIAN
75 encoding->is_bigendian = TRUE;
76#else
77 encoding->is_bigendian = FALSE;
78#endif
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;
85
86 spec->spec_digits = digits;
87
88 return SR_OK;
89}
90
91START_TEST(test_analog_to_float)
92{
93 int ret;
94 unsigned int i;
95 float f, fout;
96 struct sr_channel ch;
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};
102
103 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
104 analog.num_samples = 1;
105 analog.data = &f;
106 meaning.channels = g_slist_append(NULL, &ch);
107
108 for (i = 0; i < ARRAY_SIZE(v); i++) {
109 fout = 19;
110 f = 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);
114 }
115}
116END_TEST
117
118START_TEST(test_analog_to_float_null)
119{
120 int ret;
121 float f, fout;
122 struct sr_datafeed_analog analog;
123 struct sr_analog_encoding encoding;
124 struct sr_analog_meaning meaning;
125 struct sr_analog_spec spec;
126
127 f = G_PI;
128 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
129 analog.num_samples = 1;
130 analog.data = &f;
131
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);
138
139 analog.data = NULL;
140 ret = sr_analog_to_float(&analog, &fout);
141 fail_unless(ret == SR_ERR_ARG);
142 analog.data = &f;
143
144 analog.meaning = NULL;
145 ret = sr_analog_to_float(&analog, &fout);
146 fail_unless(ret == SR_ERR_ARG);
147 analog.meaning = &meaning;
148
149 analog.encoding = NULL;
150 ret = sr_analog_to_float(&analog, &fout);
151 fail_unless(ret == SR_ERR_ARG);
152 analog.encoding = &encoding;
153}
154END_TEST
155
6205515c
GS
156START_TEST(test_analog_to_float_conv)
157{
158 static const int with_diag = 0;
159
160 struct {
161 const char *desc;
162 void *bytes;
163 size_t nums, unit;
164 int is_fp, is_sign, is_be;
165 int scale, offset;
166 float *want;
167 } *item, items[] = {
168 /* Test to cover multiple values in an array, odd numbers. */
169 {
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, },
176 },
177 /* Tests to cover floating point input data conversion. */
178 {
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, },
185 },
186 {
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, },
193 },
194 {
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, },
201 },
202 {
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, },
209 },
210 {
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,
215 .want = NULL,
216 },
217 {
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,
222 .want = NULL,
223 },
224 /* Tests to cover integer input data conversion. */
225 {
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, },
232 },
233 {
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, },
240 },
241 {
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, },
248 },
249 {
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, },
256 },
257 {
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, },
264 },
265 {
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, },
272 },
273 {
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, },
280 },
281 {
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, },
288 },
289 {
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, },
296 },
297 {
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, },
304 },
305 {
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,
310 .want = NULL,
311 },
312 /* Tests to cover scale/offset calculation. */
313 {
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, },
320 },
321 {
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, },
328 },
329 };
330 const size_t max_floats = 6;
331 struct sr_channel ch = {
332 .index = 0,
333 .enabled = TRUE,
334 .type = SR_CHANNEL_LOGIC,
335 .name = "input",
336 };
337
338 size_t item_idx;
339 char item_text[32];
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];
347 int ret;
348 float want, have;
349
350 for (item_idx = 0; item_idx < ARRAY_SIZE(items); item_idx++) {
351 item = &items[item_idx];
352
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");
358 if (with_diag) {
359 fprintf(stderr, "%s -- %s", item_text, item->desc);
360 fflush(stderr);
361 }
362
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) {
367 byte_ptr = &f_in[0];
368 for (value_idx = 0; value_idx < item->nums; value_idx++) {
369 swap_bytes(byte_ptr, item->unit);
370 byte_ptr += item->unit;
371 }
372 }
373 if (with_diag) {
374 fprintf(stderr, " -- bytes:");
375 for (value_idx = 0; value_idx < byte_count; value_idx++)
376 fprintf(stderr, " %02x", f_in[value_idx]);
377 fflush(stderr);
378 }
379
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);
391
392 /* Convert to an array of single precision float values. */
393 ret = sr_analog_to_float(&analog, &f_out[0]);
394 if (!item->want) {
395 fail_if(ret == SR_OK,
396 "%s: sr_analog_to_float() passed", item_text);
397 if (with_diag) {
398 fprintf(stderr, " -- expected fail, OK\n");
399 fflush(stderr);
400 }
401 continue;
402 }
403 fail_unless(ret == SR_OK,
404 "%s: sr_analog_to_float() failed: %d", item_text, ret);
405 if (with_diag) {
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");
410 fflush(stderr);
411 }
412
413 /*
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.
419 */
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);
426 }
427 }
428}
429END_TEST
430
962172e4
AJ
431START_TEST(test_analog_si_prefix)
432{
433 struct {
434 float input_value;
435 int input_digits;
436 float output_value;
437 int output_digits;
438 const char *output_si_prefix;
439 } v[] = {
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, "µ" },
461 };
462
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);
467
468 fail_unless(fabs(value - v[i].output_value) <= 0.00001,
469 "sr_analog_si_prefix() unexpected output value %f (i=%d).",
470 value , i);
471 fail_unless(digits == v[i].output_digits,
472 "sr_analog_si_prefix() unexpected output digits %d (i=%d).",
473 digits, i);
474 fail_unless(!strcmp(si_prefix, v[i].output_si_prefix),
475 "sr_analog_si_prefix() unexpected output prefix \"%s\" (i=%d).",
476 si_prefix, i);
477 }
478}
479END_TEST
480
481START_TEST(test_analog_si_prefix_null)
482{
483 float value = 1.23;
484 int digits = 1;
485 const char *si_prefix;
486
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, ""));
493}
494END_TEST
495
6b71bf1b
UH
496START_TEST(test_analog_unit_to_string)
497{
498 int ret;
499 unsigned int i;
500 char *result;
501 struct sr_datafeed_analog analog;
502 struct sr_analog_encoding encoding;
503 struct sr_analog_meaning meaning;
504 struct sr_analog_spec spec;
32054b09
UH
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"};
6b71bf1b
UH
508
509 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
510
72cb20ed 511 for (i = 0; i < ARRAY_SIZE(r); i++) {
32054b09
UH
512 meaning.unit = u[i];
513 meaning.mqflags = f[i];
6b71bf1b
UH
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]);
518 g_free(result);
519 }
520}
521END_TEST
522
523START_TEST(test_analog_unit_to_string_null)
524{
525 int ret;
526 char *result;
527 struct sr_datafeed_analog analog;
528 struct sr_analog_encoding encoding;
529 struct sr_analog_meaning meaning;
530 struct sr_analog_spec spec;
531
532 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
533
534 meaning.unit = SR_UNIT_VOLT;
535 meaning.mqflags = SR_MQFLAG_RMS;
536
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);
543
544 analog.meaning = NULL;
545 ret = sr_analog_unit_to_string(&analog, &result);
546 fail_unless(ret == SR_ERR_ARG);
547}
548END_TEST
549
550START_TEST(test_set_rational)
551{
552 unsigned int i;
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};
556
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]);
560 }
561}
562END_TEST
563
564START_TEST(test_set_rational_null)
565{
566 sr_rational_set(NULL, 5, 7);
567}
568END_TEST
569
bdba3626
SB
570START_TEST(test_cmp_rational)
571{
572 const struct sr_rational r[] = { { 1, 1 },
573 { 2, 2 },
574 { 1000, 1000 },
575 { INT64_MAX, INT64_MAX },
576 { 1, 4 },
577 { 2, 8 },
578 { INT64_MAX, UINT64_MAX },
579 { INT64_MIN, UINT64_MAX },
580 };
581
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);
587
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);
591
592 fail_unless(sr_rational_eq(&r[6], &r[6]) == 1);
593 fail_unless(sr_rational_eq(&r[7], &r[7]) == 1);
594
595 fail_unless(sr_rational_eq(&r[1], &r[4]) == 0);
596}
597END_TEST
598
ee1b6054
SB
599START_TEST(test_mult_rational)
600{
601 const struct sr_rational r[][3] = {
602 /* a * b = c */
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 }},
610 /* Test reduction */
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 }},
616
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 }},
623
624 { { -10000*3, 4 }, { 80000*3, 1 }, { -200000000*9, 1 }},
625 { { 10000*3, 4 }, { -80000*3, 1 }, { -200000000*9, 1 }},
626 };
627
628 for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
629 struct sr_rational res;
630
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);
636 }
637}
638END_TEST
639
17d5a11c
SB
640START_TEST(test_div_rational)
641{
642 const struct sr_rational r[][3] = {
643 /* a * b = c */
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 }},
651 /* Test reduction */
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 }},
657
658 { { 10000*3, 4 }, { 1, 80000*3 }, { 200000000*9, 1 }},
659 { { 4, 10000*3 }, { 80000*3, 1 }, { 1, 200000000*9 }},
660
661 { { -10000*3, 4 }, { 1, 80000*3 }, { -200000000*9, 1 }},
662 { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }},
663 };
664
665 for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
666 struct sr_rational res;
667
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);
673 }
674
675 {
676 struct sr_rational res;
677 int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 }));
678
679 fail_unless(rc == SR_ERR_ARG);
680 }
681}
682END_TEST
683
6b71bf1b
UH
684Suite *suite_analog(void)
685{
686 Suite *s;
687 TCase *tc;
688
6205515c
GS
689 get_host_endianess();
690
6b71bf1b
UH
691 s = suite_create("analog");
692
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);
6205515c
GS
696 tcase_add_test(tc, test_analog_to_float_conv);
697 suite_add_tcase(s, tc);
698
699 tc = tcase_create("analog_si_unit");
962172e4
AJ
700 tcase_add_test(tc, test_analog_si_prefix);
701 tcase_add_test(tc, test_analog_si_prefix_null);
6b71bf1b
UH
702 tcase_add_test(tc, test_analog_unit_to_string);
703 tcase_add_test(tc, test_analog_unit_to_string_null);
6205515c
GS
704 suite_add_tcase(s, tc);
705
706 tc = tcase_create("analog_rational");
6b71bf1b
UH
707 tcase_add_test(tc, test_set_rational);
708 tcase_add_test(tc, test_set_rational_null);
bdba3626 709 tcase_add_test(tc, test_cmp_rational);
ee1b6054 710 tcase_add_test(tc, test_mult_rational);
17d5a11c 711 tcase_add_test(tc, test_div_rational);
6b71bf1b
UH
712 suite_add_tcase(s, tc);
713
714 return s;
715}