]> sigrok.org Git - libsigrok.git/blob - tests/analog.c
tests: Fix test_analog_unit_to_string unit test.
[libsigrok.git] / tests / analog.c
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, see <http://www.gnu.org/licenses/>.
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
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,
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
61 START_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 }
86 END_TEST
87
88 START_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 }
124 END_TEST
125
126 START_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 }
174 END_TEST
175
176 START_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 }
189 END_TEST
190
191 START_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;
200         const char *r[] = {"V RMS"};
201
202         sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
203
204         for (i = 0; i < ARRAY_SIZE(r); i++) {
205                 meaning.unit = SR_UNIT_VOLT;
206                 meaning.mqflags = SR_MQFLAG_RMS;
207                 ret = sr_analog_unit_to_string(&analog, &result);
208                 fail_unless(ret == SR_OK);
209                 fail_unless(result != NULL);
210                 fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
211                 g_free(result);
212         }
213 }
214 END_TEST
215
216 START_TEST(test_analog_unit_to_string_null)
217 {
218         int ret;
219         char *result;
220         struct sr_datafeed_analog analog;
221         struct sr_analog_encoding encoding;
222         struct sr_analog_meaning meaning;
223         struct sr_analog_spec spec;
224
225         sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
226
227         meaning.unit = SR_UNIT_VOLT;
228         meaning.mqflags = SR_MQFLAG_RMS;
229
230         ret = sr_analog_unit_to_string(NULL, &result);
231         fail_unless(ret == SR_ERR_ARG);
232         ret = sr_analog_unit_to_string(&analog, NULL);
233         fail_unless(ret == SR_ERR_ARG);
234         ret = sr_analog_unit_to_string(NULL, NULL);
235         fail_unless(ret == SR_ERR_ARG);
236
237         analog.meaning = NULL;
238         ret = sr_analog_unit_to_string(&analog, &result);
239         fail_unless(ret == SR_ERR_ARG);
240 }
241 END_TEST
242
243 START_TEST(test_set_rational)
244 {
245         unsigned int i;
246         struct sr_rational r;
247         const int64_t p[] = {0, 1, -5, INT64_MAX};
248         const uint64_t q[] = {0, 2, 7, UINT64_MAX};
249
250         for (i = 0; i < ARRAY_SIZE(p); i++) {
251                 sr_rational_set(&r, p[i], q[i]);
252                 fail_unless(r.p == p[i] && r.q == q[i]);
253         }
254 }
255 END_TEST
256
257 START_TEST(test_set_rational_null)
258 {
259         sr_rational_set(NULL, 5, 7);
260 }
261 END_TEST
262
263 START_TEST(test_cmp_rational)
264 {
265         const struct sr_rational r[] = { { 1, 1 },
266                 { 2, 2 },
267                 { 1000, 1000 },
268                 { INT64_MAX, INT64_MAX },
269                 { 1, 4 },
270                 { 2, 8 },
271                 { INT64_MAX, UINT64_MAX },
272                 { INT64_MIN, UINT64_MAX },
273         };
274
275         fail_unless(sr_rational_eq(&r[0], &r[0]) == 1);
276         fail_unless(sr_rational_eq(&r[0], &r[1]) == 1);
277         fail_unless(sr_rational_eq(&r[1], &r[2]) == 1);
278         fail_unless(sr_rational_eq(&r[2], &r[3]) == 1);
279         fail_unless(sr_rational_eq(&r[3], &r[3]) == 1);
280
281         fail_unless(sr_rational_eq(&r[4], &r[4]) == 1);
282         fail_unless(sr_rational_eq(&r[4], &r[5]) == 1);
283         fail_unless(sr_rational_eq(&r[5], &r[5]) == 1);
284
285         fail_unless(sr_rational_eq(&r[6], &r[6]) == 1);
286         fail_unless(sr_rational_eq(&r[7], &r[7]) == 1);
287
288         fail_unless(sr_rational_eq(&r[1], &r[4]) == 0);
289 }
290 END_TEST
291
292 START_TEST(test_mult_rational)
293 {
294         const struct sr_rational r[][3] = {
295                 /*   a    *    b    =    c   */
296                 { { 1, 1 }, { 1, 1 }, { 1, 1 }},
297                 { { 2, 1 }, { 3, 1 }, { 6, 1 }},
298                 { { 1, 2 }, { 2, 1 }, { 1, 1 }},
299                 /* Test negative numbers */
300                 { { -1, 2 }, { 2, 1 }, { -1, 1 }},
301                 { { -1, 2 }, { -2, 1 }, { 1, 1 }},
302                 { { -(1ll<<20), (1ll<<10) }, { -(1ll<<20), 1 }, { (1ll<<30), 1 }},
303                 /* Test reduction */
304                 { { INT32_MAX, (1ll<<12) }, { (1<<2), 1 }, { INT32_MAX, (1ll<<10) }},
305                 { { INT64_MAX, (1ll<<63) }, { (1<<3), 1 }, { INT64_MAX, (1ll<<60) }},
306                 /* Test large numbers */
307                 { {  (1ll<<40), (1ll<<10) }, {  (1ll<<30), 1 }, { (1ll<<60), 1 }},
308                 { { -(1ll<<40), (1ll<<10) }, { -(1ll<<30), 1 }, { (1ll<<60), 1 }},
309
310                 { { 1000, 1 }, { 8000, 1 }, { 8000000, 1 }},
311                 { { 10000, 1 }, { 80000, 1 }, { 800000000, 1 }},
312                 { { 10000*3, 4 }, { 80000*3, 1 }, { 200000000*9, 1 }},
313                 { { 1, 1000 }, { 1, 8000 }, { 1, 8000000 }},
314                 { { 1, 10000 }, { 1, 80000 }, { 1, 800000000 }},
315                 { { 4, 10000*3 }, { 1, 80000*3 }, { 1, 200000000*9 }},
316
317                 { { -10000*3, 4 }, { 80000*3, 1 }, { -200000000*9, 1 }},
318                 { { 10000*3, 4 }, { -80000*3, 1 }, { -200000000*9, 1 }},
319         };
320
321         for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
322                 struct sr_rational res;
323
324                 int rc = sr_rational_mult(&res, &r[i][0], &r[i][1]);
325                 fail_unless(rc == SR_OK);
326                 fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
327                         "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
328                         i, res.p, res.q, r[i][2].p, r[i][2].q);
329         }
330 }
331 END_TEST
332
333 START_TEST(test_div_rational)
334 {
335         const struct sr_rational r[][3] = {
336                 /*   a    *    b    =    c   */
337                 { { 1, 1 }, { 1, 1 }, { 1, 1 }},
338                 { { 2, 1 }, { 1, 3 }, { 6, 1 }},
339                 { { 1, 2 }, { 1, 2 }, { 1, 1 }},
340                 /* Test negative numbers */
341                 { { -1, 2 }, { 1, 2 }, { -1, 1 }},
342                 { { -1, 2 }, { -1, 2 }, { 1, 1 }},
343                 { { -(1ll<<20), (1ll<<10) }, { -1, (1ll<<20) }, { (1ll<<30), 1 }},
344                 /* Test reduction */
345                 { { INT32_MAX, (1ll<<12) }, { 1, (1<<2) }, { INT32_MAX, (1ll<<10) }},
346                 { { INT64_MAX, (1ll<<63) }, { 1, (1<<3) }, { INT64_MAX, (1ll<<60) }},
347                 /* Test large numbers */
348                 { {  (1ll<<40), (1ll<<10) }, {  1, (1ll<<30) }, { (1ll<<60), 1 }},
349                 { { -(1ll<<40), (1ll<<10) }, { -1, (1ll<<30) }, { (1ll<<60), 1 }},
350
351                 { { 10000*3, 4 }, { 1, 80000*3 }, { 200000000*9, 1 }},
352                 { { 4, 10000*3 }, { 80000*3, 1 }, { 1, 200000000*9 }},
353
354                 { { -10000*3, 4 }, { 1, 80000*3 }, { -200000000*9, 1 }},
355                 { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }},
356         };
357
358         for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
359                 struct sr_rational res;
360
361                 int rc = sr_rational_div(&res, &r[i][0], &r[i][1]);
362                 fail_unless(rc == SR_OK);
363                 fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
364                         "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
365                         i, res.p, res.q, r[i][2].p, r[i][2].q);
366         }
367
368         {
369                 struct sr_rational res;
370                 int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 }));
371
372                 fail_unless(rc == SR_ERR_ARG);
373         }
374 }
375 END_TEST
376
377 Suite *suite_analog(void)
378 {
379         Suite *s;
380         TCase *tc;
381
382         s = suite_create("analog");
383
384         tc = tcase_create("analog_to_float");
385         tcase_add_test(tc, test_analog_to_float);
386         tcase_add_test(tc, test_analog_to_float_null);
387         tcase_add_test(tc, test_analog_si_prefix);
388         tcase_add_test(tc, test_analog_si_prefix_null);
389         tcase_add_test(tc, test_analog_unit_to_string);
390         tcase_add_test(tc, test_analog_unit_to_string_null);
391         tcase_add_test(tc, test_set_rational);
392         tcase_add_test(tc, test_set_rational_null);
393         tcase_add_test(tc, test_cmp_rational);
394         tcase_add_test(tc, test_mult_rational);
395         tcase_add_test(tc, test_div_rational);
396         suite_add_tcase(s, tc);
397
398         return s;
399 }