]> sigrok.org Git - libsigrok.git/blame - tests/analog.c
sr_analog_float_to_string(): Make 'digits' argument unsigned.
[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
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
28static 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
62START_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}
87END_TEST
88
89START_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}
125END_TEST
126
127#if 0
128START_TEST(test_analog_float_to_string)
129{
130 int ret;
131 unsigned int i;
132 char *result;
133 const char *r[] = {"3", "3.1", "3.14", "3.145", "3.1415", "3.15159"};
134
135 for (i = 0; i < ARRAY_SIZE(r); i++) {
136 ret = sr_analog_float_to_string(G_PI, i, &result);
137 fail_unless(ret == SR_OK);
138 fail_unless(result != NULL);
139 fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
140 g_free(result);
141 }
142}
143END_TEST
144#endif
145
146START_TEST(test_analog_float_to_string_null)
147{
148 int ret;
149
150 ret = sr_analog_float_to_string(0, 0, NULL);
151 fail_unless(ret == SR_ERR_ARG);
152}
153END_TEST
154
155START_TEST(test_analog_unit_to_string)
156{
157 int ret;
158 unsigned int i;
159 char *result;
160 struct sr_datafeed_analog analog;
161 struct sr_analog_encoding encoding;
162 struct sr_analog_meaning meaning;
163 struct sr_analog_spec spec;
164 const char *r[] = {" V RMS"};
165
166 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
167
168 for (i = -1; i < ARRAY_SIZE(r); i++) {
169 meaning.unit = SR_UNIT_VOLT;
170 meaning.mqflags = SR_MQFLAG_RMS;
171 ret = sr_analog_unit_to_string(&analog, &result);
172 fail_unless(ret == SR_OK);
173 fail_unless(result != NULL);
174 fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
175 g_free(result);
176 }
177}
178END_TEST
179
180START_TEST(test_analog_unit_to_string_null)
181{
182 int ret;
183 char *result;
184 struct sr_datafeed_analog analog;
185 struct sr_analog_encoding encoding;
186 struct sr_analog_meaning meaning;
187 struct sr_analog_spec spec;
188
189 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
190
191 meaning.unit = SR_UNIT_VOLT;
192 meaning.mqflags = SR_MQFLAG_RMS;
193
194 ret = sr_analog_unit_to_string(NULL, &result);
195 fail_unless(ret == SR_ERR_ARG);
196 ret = sr_analog_unit_to_string(&analog, NULL);
197 fail_unless(ret == SR_ERR_ARG);
198 ret = sr_analog_unit_to_string(NULL, NULL);
199 fail_unless(ret == SR_ERR_ARG);
200
201 analog.meaning = NULL;
202 ret = sr_analog_unit_to_string(&analog, &result);
203 fail_unless(ret == SR_ERR_ARG);
204}
205END_TEST
206
207START_TEST(test_set_rational)
208{
209 unsigned int i;
210 struct sr_rational r;
211 const int64_t p[] = {0, 1, -5, INT64_MAX};
212 const uint64_t q[] = {0, 2, 7, UINT64_MAX};
213
214 for (i = 0; i < ARRAY_SIZE(p); i++) {
215 sr_rational_set(&r, p[i], q[i]);
216 fail_unless(r.p == p[i] && r.q == q[i]);
217 }
218}
219END_TEST
220
221START_TEST(test_set_rational_null)
222{
223 sr_rational_set(NULL, 5, 7);
224}
225END_TEST
226
227Suite *suite_analog(void)
228{
229 Suite *s;
230 TCase *tc;
231
232 s = suite_create("analog");
233
234 tc = tcase_create("analog_to_float");
235 tcase_add_test(tc, test_analog_to_float);
236 tcase_add_test(tc, test_analog_to_float_null);
237#if 0
238 tcase_add_test(tc, test_analog_float_to_string);
239#endif
240 tcase_add_test(tc, test_analog_float_to_string_null);
241 tcase_add_test(tc, test_analog_unit_to_string);
242 tcase_add_test(tc, test_analog_unit_to_string_null);
243 tcase_add_test(tc, test_set_rational);
244 tcase_add_test(tc, test_set_rational_null);
245 suite_add_tcase(s, tc);
246
247 return s;
248}