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