]> sigrok.org Git - libsigrok.git/blob - tests/check_input_binary.c
make check: Update to new session API.
[libsigrok.git] / tests / check_input_binary.c
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 "../include/libsigrok/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
33 static struct sr_context *sr_ctx;
34
35 static uint64_t df_packet_counter = 0, sample_counter = 0;
36 static gboolean have_seen_df_end = FALSE;
37 static GArray *logic_channellist = NULL;
38 static int check_to_perform;
39 static uint64_t expected_samples;
40 static uint64_t *expected_samplerate;
41
42 static 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
50 static 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
58 static 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
70 static 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
82 static 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
96 static 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
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);
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
200 static 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         struct sr_session *session;
208
209         /* Initialize global variables for this run. */
210         df_packet_counter = sample_counter = 0;
211         have_seen_df_end = FALSE;
212         logic_channellist = NULL;
213         check_to_perform = check;
214         expected_samples = samples;
215         expected_samplerate = samplerate;
216
217         in_format = srtest_input_get("binary");
218
219         in = g_try_malloc0(sizeof(struct sr_input));
220         fail_unless(in != NULL);
221
222         in->format = in_format;
223         in->param = param;
224
225         srtest_buf_to_file(filename, buf, samples); /* Create a file. */
226
227         ret = in->format->init(in, filename);
228         fail_unless(ret == SR_OK, "Input format init error: %d", ret);
229         
230         sr_session_new(&session);
231         sr_session_datafeed_callback_add(session, datafeed_in, NULL);
232         sr_session_dev_add(session, in->sdi);
233         in_format->loadfile(in, filename);
234         sr_session_destroy(session);
235
236         g_unlink(filename); /* Delete file again. */
237 }
238
239 START_TEST(test_input_binary_all_low)
240 {
241         uint64_t i, samplerate;
242         uint8_t *buf;
243         GHashTable *param;
244
245         buf = g_try_malloc0(MAX_FILESIZE);
246         fail_unless(buf != NULL);
247
248         param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
249         fail_unless(param != NULL);
250         g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250"));
251         samplerate = SR_HZ(1250);
252
253         /* Check various filesizes, with/without specifying a samplerate. */
254         check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL);
255         check_buf(FILENAME, param, buf, CHECK_ALL_LOW, 0, &samplerate);
256         for (i = 1; i < MAX_FILESIZE; i *= 3) {
257                 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, i, NULL);
258                 check_buf(FILENAME, param, buf, CHECK_ALL_LOW, i, &samplerate);
259
260         }
261
262         g_hash_table_destroy(param);
263         g_free(buf);
264 }
265 END_TEST
266
267 START_TEST(test_input_binary_all_high)
268 {
269         uint64_t i;
270         uint8_t *buf;
271
272         buf = g_try_malloc(MAX_FILESIZE);
273         memset(buf, 0xff, MAX_FILESIZE);
274
275         check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL);
276         for (i = 1; i < MAX_FILESIZE; i *= 3)
277                 check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, i, NULL);
278
279         g_free(buf);
280 }
281 END_TEST
282
283 START_TEST(test_input_binary_all_high_loop)
284 {
285         uint8_t *buf;
286
287         /* Note: _i is the loop variable from tcase_add_loop_test(). */
288
289         buf = g_try_malloc((_i * 10) + 1);
290         memset(buf, 0xff, _i * 10);
291
292         check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, _i * 10, NULL);
293
294         g_free(buf);
295 }
296 END_TEST
297
298 START_TEST(test_input_binary_hello_world)
299 {
300         uint64_t samplerate;
301         uint8_t *buf;
302         GHashTable *param;
303
304         buf = (uint8_t *)g_strdup("Hello world");
305
306         param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
307         fail_unless(param != NULL);
308         g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250"));
309         samplerate = SR_HZ(1250);
310
311         /* Check with and without specifying a samplerate. */
312         check_buf(FILENAME, NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
313         check_buf(FILENAME, param, buf, CHECK_HELLO_WORLD, 11, &samplerate);
314
315         g_hash_table_destroy(param);
316         g_free(buf);
317 }
318 END_TEST
319
320 Suite *suite_input_binary(void)
321 {
322         Suite *s;
323         TCase *tc;
324
325         s = suite_create("input-binary");
326
327         tc = tcase_create("basic");
328         tcase_add_checked_fixture(tc, setup, teardown);
329         tcase_add_test(tc, test_input_binary_all_low);
330         tcase_add_test(tc, test_input_binary_all_high);
331         tcase_add_loop_test(tc, test_input_binary_all_high_loop, 0, 10);
332         tcase_add_test(tc, test_input_binary_hello_world);
333         suite_add_tcase(s, tc);
334
335         return s;
336 }