]> sigrok.org Git - libsigrok.git/blame - tests/check_input_binary.c
A few more random 'probe' to 'channel' changes.
[libsigrok.git] / tests / check_input_binary.c
CommitLineData
71185b48
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 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 <check.h>
22#include <glib/gstdio.h>
23#include "../libsigrok.h"
24#include "lib.h"
25
26#define FILENAME "foo.dat"
27#define MAX_FILESIZE (1 * 1000000)
28
29#define CHECK_ALL_LOW 0
30#define CHECK_ALL_HIGH 1
31#define CHECK_HELLO_WORLD 2
32
33static struct sr_context *sr_ctx;
34
35static uint64_t df_packet_counter = 0, sample_counter = 0;
36static gboolean have_seen_df_end = FALSE;
fca75cbb 37static GArray *logic_channellist = NULL;
71185b48
UH
38static int check_to_perform;
39static uint64_t expected_samples;
40static uint64_t *expected_samplerate;
41
42static void setup(void)
43{
44 int ret;
45
46 ret = sr_init(&sr_ctx);
47 fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
48}
49
50static void teardown(void)
51{
52 int ret;
53
54 ret = sr_exit(sr_ctx);
55 fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
56}
57
58static void check_all_low(const struct sr_datafeed_logic *logic)
59{
60 uint64_t i;
61 uint8_t *data;
62
63 for (i = 0; i < logic->length; i++) {
64 data = logic->data;
65 if (data[i * logic->unitsize] != 0)
66 fail("Logic data was not all-0x00.");
67 }
68}
69
70static void check_all_high(const struct sr_datafeed_logic *logic)
71{
72 uint64_t i;
73 uint8_t *data;
74
75 for (i = 0; i < logic->length; i++) {
76 data = logic->data;
77 if (data[i * logic->unitsize] != 0xff)
78 fail("Logic data was not all-0xff.");
79 }
80}
81
82static void check_hello_world(const struct sr_datafeed_logic *logic)
83{
84 uint64_t i;
85 uint8_t *data, b;
86 const char *h = "Hello world";
87
88 for (i = 0; i < logic->length; i++) {
89 data = logic->data;
90 b = data[sample_counter + i];
91 if (b != h[sample_counter + i])
92 fail("Logic data was not 'Hello world'.");
93 }
94}
95
96static void datafeed_in(const struct sr_dev_inst *sdi,
97 const struct sr_datafeed_packet *packet, void *cb_data)
98{
99 const struct sr_datafeed_meta *meta;
100 const struct sr_datafeed_logic *logic;
101 struct sr_config *src;
102 uint64_t samplerate, sample_interval;
103 GSList *l;
104 const void *p;
105
106 (void)cb_data;
107
108 fail_unless(sdi != NULL);
109 fail_unless(packet != NULL);
110
111 if (df_packet_counter++ == 0)
112 fail_unless(packet->type == SR_DF_HEADER,
113 "The first packet must be an SR_DF_HEADER.");
114
115 if (have_seen_df_end)
116 fail("There must be no packets after an SR_DF_END, but we "
117 "received a packet of type %d.", packet->type);
118
119 p = packet->payload;
120
121 switch (packet->type) {
122 case SR_DF_HEADER:
123 // g_debug("Received SR_DF_HEADER.");
124 // fail_unless(p != NULL, "SR_DF_HEADER payload was NULL.");
125
fca75cbb
UH
126 logic_channellist = srtest_get_enabled_logic_channels(sdi);
127 fail_unless(logic_channellist != NULL);
128 fail_unless(logic_channellist->len != 0);
129 // g_debug("Enabled channels: %d.", logic_channellist->len);
71185b48
UH
130 break;
131 case SR_DF_META:
132 // g_debug("Received SR_DF_META.");
133
134 meta = packet->payload;
135 fail_unless(p != NULL, "SR_DF_META payload was NULL.");
136
137 for (l = meta->config; l; l = l->next) {
138 src = l->data;
139 // g_debug("Got meta key: %d.", src->key);
140 switch (src->key) {
141 case SR_CONF_SAMPLERATE:
142 samplerate = g_variant_get_uint64(src->data);
143 if (!expected_samplerate)
144 break;
145 fail_unless(samplerate == *expected_samplerate,
146 "Expected samplerate=%" PRIu64 ", "
147 "got %" PRIu64 "", samplerate,
148 *expected_samplerate);
149 // g_debug("samplerate = %" PRIu64 " Hz.",
150 // samplerate);
151 break;
152 case SR_CONF_SAMPLE_INTERVAL:
153 sample_interval = g_variant_get_uint64(src->data);
154 (void)sample_interval;
155 // g_debug("sample interval = %" PRIu64 " ms.",
156 // sample_interval);
157 break;
158 default:
159 /* Unknown metadata is not an error. */
160 g_debug("Got unknown meta key: %d.", src->key);
161 break;
162 }
163 }
164 break;
165 case SR_DF_LOGIC:
166 logic = packet->payload;
167 fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL.");
168
169 // g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, "
170 // "unitsize %d).", logic->length, logic->unitsize);
171
172 if (check_to_perform == CHECK_ALL_LOW)
173 check_all_low(logic);
174 else if (check_to_perform == CHECK_ALL_HIGH)
175 check_all_high(logic);
176 else if (check_to_perform == CHECK_HELLO_WORLD)
177 check_hello_world(logic);
178
179 sample_counter += logic->length / logic->unitsize;
180
181 break;
182 case SR_DF_END:
183 // g_debug("Received SR_DF_END.");
184 // fail_unless(p != NULL, "SR_DF_END payload was NULL.");
185 have_seen_df_end = TRUE;
186 if (sample_counter != expected_samples)
187 fail("Expected %" PRIu64 " samples, got %" PRIu64 "",
188 expected_samples, sample_counter);
189 break;
190 default:
191 /*
192 * Note: The binary input format doesn't support SR_DF_TRIGGER
193 * and some other types, those should yield an error.
194 */
195 fail("Invalid packet type: %d.", packet->type);
196 break;
197 }
198}
199
200static void check_buf(const char *filename, GHashTable *param,
201 const uint8_t *buf, int check, uint64_t samples,
202 uint64_t *samplerate)
203{
204 int ret;
205 struct sr_input *in;
206 struct sr_input_format *in_format;
207
208 /* Initialize global variables for this run. */
209 df_packet_counter = sample_counter = 0;
210 have_seen_df_end = FALSE;
fca75cbb 211 logic_channellist = NULL;
71185b48
UH
212 check_to_perform = check;
213 expected_samples = samples;
214 expected_samplerate = samplerate;
215
216 in_format = srtest_input_get("binary");
217
218 in = g_try_malloc0(sizeof(struct sr_input));
219 fail_unless(in != NULL);
220
221 in->format = in_format;
222 in->param = param;
223
224 srtest_buf_to_file(filename, buf, samples); /* Create a file. */
225
226 ret = in->format->init(in, filename);
227 fail_unless(ret == SR_OK, "Input format init error: %d", ret);
228
229 sr_session_new();
230 sr_session_datafeed_callback_add(datafeed_in, NULL);
231 sr_session_dev_add(in->sdi);
232 in_format->loadfile(in, filename);
233 sr_session_destroy();
234
235 g_unlink(filename); /* Delete file again. */
236}
237
238START_TEST(test_input_binary_all_low)
239{
240 uint64_t i, samplerate;
241 uint8_t *buf;
242 GHashTable *param;
243
244 buf = g_try_malloc0(MAX_FILESIZE);
245 fail_unless(buf != NULL);
246
247 param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
248 fail_unless(param != NULL);
249 g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250"));
250 samplerate = SR_HZ(1250);
251
252 /* Check various filesizes, with/without specifying a samplerate. */
253 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL);
254 check_buf(FILENAME, param, buf, CHECK_ALL_LOW, 0, &samplerate);
255 for (i = 1; i < MAX_FILESIZE; i *= 3) {
256 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, i, NULL);
257 check_buf(FILENAME, param, buf, CHECK_ALL_LOW, i, &samplerate);
258
259 }
260
261 g_hash_table_destroy(param);
262 g_free(buf);
263}
264END_TEST
265
266START_TEST(test_input_binary_all_high)
267{
268 uint64_t i;
269 uint8_t *buf;
270
271 buf = g_try_malloc(MAX_FILESIZE);
272 memset(buf, 0xff, MAX_FILESIZE);
273
274 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL);
275 for (i = 1; i < MAX_FILESIZE; i *= 3)
276 check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, i, NULL);
277
278 g_free(buf);
279}
280END_TEST
281
282START_TEST(test_input_binary_all_high_loop)
283{
284 uint8_t *buf;
285
286 /* Note: _i is the loop variable from tcase_add_loop_test(). */
287
288 buf = g_try_malloc((_i * 10) + 1);
289 memset(buf, 0xff, _i * 10);
290
291 check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, _i * 10, NULL);
292
293 g_free(buf);
294}
295END_TEST
296
297START_TEST(test_input_binary_hello_world)
298{
299 uint64_t samplerate;
300 uint8_t *buf;
301 GHashTable *param;
302
303 buf = (uint8_t *)g_strdup("Hello world");
304
305 param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
306 fail_unless(param != NULL);
307 g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250"));
308 samplerate = SR_HZ(1250);
309
310 /* Check with and without specifying a samplerate. */
311 check_buf(FILENAME, NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
312 check_buf(FILENAME, param, buf, CHECK_HELLO_WORLD, 11, &samplerate);
313
314 g_hash_table_destroy(param);
315 g_free(buf);
316}
317END_TEST
318
319Suite *suite_input_binary(void)
320{
321 Suite *s;
322 TCase *tc;
323
324 s = suite_create("input-binary");
325
326 tc = tcase_create("basic");
327 tcase_add_checked_fixture(tc, setup, teardown);
328 tcase_add_test(tc, test_input_binary_all_low);
329 tcase_add_test(tc, test_input_binary_all_high);
330 tcase_add_loop_test(tc, test_input_binary_all_high_loop, 0, 10);
331 tcase_add_test(tc, test_input_binary_hello_world);
332 suite_add_tcase(s, tc);
333
334 return s;
335}