]> sigrok.org Git - libsigrok.git/blob - tests/analog.c
3e8fd494c2c80616b012e99aed9c4a81c40aba18
[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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <config.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <check.h>
25 #include <libsigrok/libsigrok.h>
26 #include "lib.h"
27
28 static int sr_analog_init_(struct sr_datafeed_analog *analog,
29                 struct sr_analog_encoding *encoding,
30                 struct sr_analog_meaning *meaning,
31                 struct sr_analog_spec *spec,
32                 int digits)
33 {
34         memset(analog, 0, sizeof(*analog));
35         memset(encoding, 0, sizeof(*encoding));
36         memset(meaning, 0, sizeof(*meaning));
37         memset(spec, 0, sizeof(*spec));
38
39         analog->encoding = encoding;
40         analog->meaning = meaning;
41         analog->spec = spec;
42
43         encoding->unitsize = sizeof(float);
44         encoding->is_float = TRUE;
45 #ifdef WORDS_BIGENDIAN
46         encoding->is_bigendian = TRUE;
47 #else
48         encoding->is_bigendian = FALSE;
49 #endif
50         encoding->digits = digits;
51         encoding->is_digits_decimal = TRUE;
52         encoding->scale.p = 1;
53         encoding->scale.q = 1;
54         encoding->offset.p = 0;
55         encoding->offset.q = 1;
56
57         spec->spec_digits = digits;
58
59         return SR_OK;
60 }
61
62 START_TEST(test_analog_to_float)
63 {
64         int ret;
65         unsigned int i;
66         float f, fout;
67         struct sr_channel ch;
68         struct sr_datafeed_analog analog;
69         struct sr_analog_encoding encoding;
70         struct sr_analog_meaning meaning;
71         struct sr_analog_spec spec;
72         const float v[] = {-12.9, -333.999, 0, 3.1415, 29.7, 989898.121212};
73
74         sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
75         analog.num_samples = 1;
76         analog.data = &f;
77         meaning.channels = g_slist_append(NULL, &ch);
78
79         for (i = 0; i < ARRAY_SIZE(v); i++) {
80                 fout = 19;
81                 f = v[i];
82                 ret = sr_analog_to_float(&analog, &fout);
83                 fail_unless(ret == SR_OK, "sr_analog_to_float() failed: %d.", ret);
84                 fail_unless(fabs(f - fout) <= 0.001, "%f != %f", f, fout);
85         }
86 }
87 END_TEST
88
89 START_TEST(test_analog_to_float_null)
90 {
91         int ret;
92         float f, fout;
93         struct sr_datafeed_analog analog;
94         struct sr_analog_encoding encoding;
95         struct sr_analog_meaning meaning;
96         struct sr_analog_spec spec;
97
98         f = G_PI;
99         sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
100         analog.num_samples = 1;
101         analog.data = &f;
102
103         ret = sr_analog_to_float(NULL, &fout);
104         fail_unless(ret == SR_ERR_ARG);
105         ret = sr_analog_to_float(&analog, NULL);
106         fail_unless(ret == SR_ERR_ARG);
107         ret = sr_analog_to_float(NULL, NULL);
108         fail_unless(ret == SR_ERR_ARG);
109
110         analog.data = NULL;
111         ret = sr_analog_to_float(&analog, &fout);
112         fail_unless(ret == SR_ERR_ARG);
113         analog.data = &f;
114
115         analog.meaning = NULL;
116         ret = sr_analog_to_float(&analog, &fout);
117         fail_unless(ret == SR_ERR_ARG);
118         analog.meaning = &meaning;
119
120         analog.encoding = NULL;
121         ret = sr_analog_to_float(&analog, &fout);
122         fail_unless(ret == SR_ERR_ARG);
123         analog.encoding = &encoding;
124 }
125 END_TEST
126
127 START_TEST(test_analog_unit_to_string)
128 {
129         int ret;
130         unsigned int i;
131         char *result;
132         struct sr_datafeed_analog analog;
133         struct sr_analog_encoding encoding;
134         struct sr_analog_meaning meaning;
135         struct sr_analog_spec spec;
136         const char *r[] = {" V RMS"};
137
138         sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
139
140         for (i = -1; i < ARRAY_SIZE(r); i++) {
141                 meaning.unit = SR_UNIT_VOLT;
142                 meaning.mqflags = SR_MQFLAG_RMS;
143                 ret = sr_analog_unit_to_string(&analog, &result);
144                 fail_unless(ret == SR_OK);
145                 fail_unless(result != NULL);
146                 fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
147                 g_free(result);
148         }
149 }
150 END_TEST
151
152 START_TEST(test_analog_unit_to_string_null)
153 {
154         int ret;
155         char *result;
156         struct sr_datafeed_analog analog;
157         struct sr_analog_encoding encoding;
158         struct sr_analog_meaning meaning;
159         struct sr_analog_spec spec;
160
161         sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
162
163         meaning.unit = SR_UNIT_VOLT;
164         meaning.mqflags = SR_MQFLAG_RMS;
165
166         ret = sr_analog_unit_to_string(NULL, &result);
167         fail_unless(ret == SR_ERR_ARG);
168         ret = sr_analog_unit_to_string(&analog, NULL);
169         fail_unless(ret == SR_ERR_ARG);
170         ret = sr_analog_unit_to_string(NULL, NULL);
171         fail_unless(ret == SR_ERR_ARG);
172
173         analog.meaning = NULL;
174         ret = sr_analog_unit_to_string(&analog, &result);
175         fail_unless(ret == SR_ERR_ARG);
176 }
177 END_TEST
178
179 START_TEST(test_set_rational)
180 {
181         unsigned int i;
182         struct sr_rational r;
183         const int64_t p[] = {0, 1, -5, INT64_MAX};
184         const uint64_t q[] = {0, 2, 7, UINT64_MAX};
185
186         for (i = 0; i < ARRAY_SIZE(p); i++) {
187                 sr_rational_set(&r, p[i], q[i]);
188                 fail_unless(r.p == p[i] && r.q == q[i]);
189         }
190 }
191 END_TEST
192
193 START_TEST(test_set_rational_null)
194 {
195         sr_rational_set(NULL, 5, 7);
196 }
197 END_TEST
198
199 START_TEST(test_cmp_rational)
200 {
201         const struct sr_rational r[] = { { 1, 1 },
202                 { 2, 2 },
203                 { 1000, 1000 },
204                 { INT64_MAX, INT64_MAX },
205                 { 1, 4 },
206                 { 2, 8 },
207                 { INT64_MAX, UINT64_MAX },
208                 { INT64_MIN, UINT64_MAX },
209         };
210
211         fail_unless(sr_rational_eq(&r[0], &r[0]) == 1);
212         fail_unless(sr_rational_eq(&r[0], &r[1]) == 1);
213         fail_unless(sr_rational_eq(&r[1], &r[2]) == 1);
214         fail_unless(sr_rational_eq(&r[2], &r[3]) == 1);
215         fail_unless(sr_rational_eq(&r[3], &r[3]) == 1);
216
217         fail_unless(sr_rational_eq(&r[4], &r[4]) == 1);
218         fail_unless(sr_rational_eq(&r[4], &r[5]) == 1);
219         fail_unless(sr_rational_eq(&r[5], &r[5]) == 1);
220
221         fail_unless(sr_rational_eq(&r[6], &r[6]) == 1);
222         fail_unless(sr_rational_eq(&r[7], &r[7]) == 1);
223
224         fail_unless(sr_rational_eq(&r[1], &r[4]) == 0);
225 }
226 END_TEST
227
228 START_TEST(test_mult_rational)
229 {
230         const struct sr_rational r[][3] = {
231                 /*   a    *    b    =    c   */
232                 { { 1, 1 }, { 1, 1 }, { 1, 1 }},
233                 { { 2, 1 }, { 3, 1 }, { 6, 1 }},
234                 { { 1, 2 }, { 2, 1 }, { 1, 1 }},
235                 /* Test negative numbers */
236                 { { -1, 2 }, { 2, 1 }, { -1, 1 }},
237                 { { -1, 2 }, { -2, 1 }, { 1, 1 }},
238                 { { -(1ll<<20), (1ll<<10) }, { -(1ll<<20), 1 }, { (1ll<<30), 1 }},
239                 /* Test reduction */
240                 { { INT32_MAX, (1ll<<12) }, { (1<<2), 1 }, { INT32_MAX, (1ll<<10) }},
241                 { { INT64_MAX, (1ll<<63) }, { (1<<3), 1 }, { INT64_MAX, (1ll<<60) }},
242                 /* Test large numbers */
243                 { {  (1ll<<40), (1ll<<10) }, {  (1ll<<30), 1 }, { (1ll<<60), 1 }},
244                 { { -(1ll<<40), (1ll<<10) }, { -(1ll<<30), 1 }, { (1ll<<60), 1 }},
245
246                 { { 1000, 1 }, { 8000, 1 }, { 8000000, 1 }},
247                 { { 10000, 1 }, { 80000, 1 }, { 800000000, 1 }},
248                 { { 10000*3, 4 }, { 80000*3, 1 }, { 200000000*9, 1 }},
249                 { { 1, 1000 }, { 1, 8000 }, { 1, 8000000 }},
250                 { { 1, 10000 }, { 1, 80000 }, { 1, 800000000 }},
251                 { { 4, 10000*3 }, { 1, 80000*3 }, { 1, 200000000*9 }},
252
253                 { { -10000*3, 4 }, { 80000*3, 1 }, { -200000000*9, 1 }},
254                 { { 10000*3, 4 }, { -80000*3, 1 }, { -200000000*9, 1 }},
255         };
256
257         for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
258                 struct sr_rational res;
259
260                 int rc = sr_rational_mult(&res, &r[i][0], &r[i][1]);
261                 fail_unless(rc == SR_OK);
262                 fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
263                         "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
264                         i, res.p, res.q, r[i][2].p, r[i][2].q);
265         }
266 }
267 END_TEST
268
269 START_TEST(test_div_rational)
270 {
271         const struct sr_rational r[][3] = {
272                 /*   a    *    b    =    c   */
273                 { { 1, 1 }, { 1, 1 }, { 1, 1 }},
274                 { { 2, 1 }, { 1, 3 }, { 6, 1 }},
275                 { { 1, 2 }, { 1, 2 }, { 1, 1 }},
276                 /* Test negative numbers */
277                 { { -1, 2 }, { 1, 2 }, { -1, 1 }},
278                 { { -1, 2 }, { -1, 2 }, { 1, 1 }},
279                 { { -(1ll<<20), (1ll<<10) }, { -1, (1ll<<20) }, { (1ll<<30), 1 }},
280                 /* Test reduction */
281                 { { INT32_MAX, (1ll<<12) }, { 1, (1<<2) }, { INT32_MAX, (1ll<<10) }},
282                 { { INT64_MAX, (1ll<<63) }, { 1, (1<<3) }, { INT64_MAX, (1ll<<60) }},
283                 /* Test large numbers */
284                 { {  (1ll<<40), (1ll<<10) }, {  1, (1ll<<30) }, { (1ll<<60), 1 }},
285                 { { -(1ll<<40), (1ll<<10) }, { -1, (1ll<<30) }, { (1ll<<60), 1 }},
286
287                 { { 10000*3, 4 }, { 1, 80000*3 }, { 200000000*9, 1 }},
288                 { { 4, 10000*3 }, { 80000*3, 1 }, { 1, 200000000*9 }},
289
290                 { { -10000*3, 4 }, { 1, 80000*3 }, { -200000000*9, 1 }},
291                 { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }},
292         };
293
294         for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
295                 struct sr_rational res;
296
297                 int rc = sr_rational_div(&res, &r[i][0], &r[i][1]);
298                 fail_unless(rc == SR_OK);
299                 fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
300                         "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
301                         i, res.p, res.q, r[i][2].p, r[i][2].q);
302         }
303
304         {
305                 struct sr_rational res;
306                 int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 }));
307
308                 fail_unless(rc == SR_ERR_ARG);
309         }
310 }
311 END_TEST
312
313 Suite *suite_analog(void)
314 {
315         Suite *s;
316         TCase *tc;
317
318         s = suite_create("analog");
319
320         tc = tcase_create("analog_to_float");
321         tcase_add_test(tc, test_analog_to_float);
322         tcase_add_test(tc, test_analog_to_float_null);
323         tcase_add_test(tc, test_analog_unit_to_string);
324         tcase_add_test(tc, test_analog_unit_to_string_null);
325         tcase_add_test(tc, test_set_rational);
326         tcase_add_test(tc, test_set_rational_null);
327         tcase_add_test(tc, test_cmp_rational);
328         tcase_add_test(tc, test_mult_rational);
329         tcase_add_test(tc, test_div_rational);
330         suite_add_tcase(s, tc);
331
332         return s;
333 }