]> sigrok.org Git - libsigrok.git/blame - tests/analog.c
Drop unneeded sr_analog_float_to_string().
[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
6b71bf1b
UH
127START_TEST(test_analog_float_to_string_null)
128{
129 int ret;
130
131 ret = sr_analog_float_to_string(0, 0, NULL);
132 fail_unless(ret == SR_ERR_ARG);
133}
134END_TEST
135
136START_TEST(test_analog_unit_to_string)
137{
138 int ret;
139 unsigned int i;
140 char *result;
141 struct sr_datafeed_analog analog;
142 struct sr_analog_encoding encoding;
143 struct sr_analog_meaning meaning;
144 struct sr_analog_spec spec;
145 const char *r[] = {" V RMS"};
146
147 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
148
149 for (i = -1; i < ARRAY_SIZE(r); i++) {
150 meaning.unit = SR_UNIT_VOLT;
151 meaning.mqflags = SR_MQFLAG_RMS;
152 ret = sr_analog_unit_to_string(&analog, &result);
153 fail_unless(ret == SR_OK);
154 fail_unless(result != NULL);
155 fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
156 g_free(result);
157 }
158}
159END_TEST
160
161START_TEST(test_analog_unit_to_string_null)
162{
163 int ret;
164 char *result;
165 struct sr_datafeed_analog analog;
166 struct sr_analog_encoding encoding;
167 struct sr_analog_meaning meaning;
168 struct sr_analog_spec spec;
169
170 sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
171
172 meaning.unit = SR_UNIT_VOLT;
173 meaning.mqflags = SR_MQFLAG_RMS;
174
175 ret = sr_analog_unit_to_string(NULL, &result);
176 fail_unless(ret == SR_ERR_ARG);
177 ret = sr_analog_unit_to_string(&analog, NULL);
178 fail_unless(ret == SR_ERR_ARG);
179 ret = sr_analog_unit_to_string(NULL, NULL);
180 fail_unless(ret == SR_ERR_ARG);
181
182 analog.meaning = NULL;
183 ret = sr_analog_unit_to_string(&analog, &result);
184 fail_unless(ret == SR_ERR_ARG);
185}
186END_TEST
187
188START_TEST(test_set_rational)
189{
190 unsigned int i;
191 struct sr_rational r;
192 const int64_t p[] = {0, 1, -5, INT64_MAX};
193 const uint64_t q[] = {0, 2, 7, UINT64_MAX};
194
195 for (i = 0; i < ARRAY_SIZE(p); i++) {
196 sr_rational_set(&r, p[i], q[i]);
197 fail_unless(r.p == p[i] && r.q == q[i]);
198 }
199}
200END_TEST
201
202START_TEST(test_set_rational_null)
203{
204 sr_rational_set(NULL, 5, 7);
205}
206END_TEST
207
208Suite *suite_analog(void)
209{
210 Suite *s;
211 TCase *tc;
212
213 s = suite_create("analog");
214
215 tc = tcase_create("analog_to_float");
216 tcase_add_test(tc, test_analog_to_float);
217 tcase_add_test(tc, test_analog_to_float_null);
6b71bf1b
UH
218 tcase_add_test(tc, test_analog_float_to_string_null);
219 tcase_add_test(tc, test_analog_unit_to_string);
220 tcase_add_test(tc, test_analog_unit_to_string_null);
221 tcase_add_test(tc, test_set_rational);
222 tcase_add_test(tc, test_set_rational_null);
223 suite_add_tcase(s, tc);
224
225 return s;
226}