]> sigrok.org Git - libsigrok.git/blob - tests/analog.c
tests: Drop another obsolete sr_analog_float_to_string() 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, 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 Suite *suite_analog(void)
200 {
201         Suite *s;
202         TCase *tc;
203
204         s = suite_create("analog");
205
206         tc = tcase_create("analog_to_float");
207         tcase_add_test(tc, test_analog_to_float);
208         tcase_add_test(tc, test_analog_to_float_null);
209         tcase_add_test(tc, test_analog_unit_to_string);
210         tcase_add_test(tc, test_analog_unit_to_string_null);
211         tcase_add_test(tc, test_set_rational);
212         tcase_add_test(tc, test_set_rational_null);
213         suite_add_tcase(s, tc);
214
215         return s;
216 }