2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 #include <libsigrok/libsigrok.h>
27 /* Test lots of triggers/stages/matches/channels */
28 #define NUM_TRIGGERS 70
30 #define NUM_MATCHES 70
31 #define NUM_CHANNELS NUM_MATCHES
33 /* Check whether creating/freeing triggers with valid names works. */
34 START_TEST(test_trigger_new_free)
37 struct sr_trigger *t[NUM_TRIGGERS];
40 /* Create a few triggers with a valid name. */
41 for (i = 0; i < NUM_TRIGGERS; i++) {
42 sprintf((char *)&name, "T%d", i);
43 t[i] = sr_trigger_new((const char *)&name);
44 fail_unless(t[i] != NULL);
45 fail_unless(!strcmp(t[i]->name, (const char *)&name));
46 fail_unless(t[i]->stages == NULL);
49 /* Free the triggers again (must not segfault). */
50 for (i = 0; i < NUM_TRIGGERS; i++)
51 sr_trigger_free(t[i]);
55 /* Check whether creating/freeing triggers with NULL names works. */
56 START_TEST(test_trigger_new_free_null)
59 struct sr_trigger *t[NUM_TRIGGERS];
61 /* Create a few triggers with a NULL name (which is allowed). */
62 for (i = 0; i < NUM_TRIGGERS; i++) {
63 t[i] = sr_trigger_new(NULL);
64 fail_unless(t[i] != NULL);
65 fail_unless(t[i]->name == NULL);
66 fail_unless(t[i]->stages == NULL);
69 /* Free the triggers again (must not segfault). */
70 for (i = 0; i < NUM_TRIGGERS; i++)
71 sr_trigger_free(t[i]);
75 /* Check whether sr_trigger_free(NULL) works without segfaulting. */
76 START_TEST(test_trigger_free_null)
78 sr_trigger_free(NULL);
82 /* Check whether creating/freeing triggers with stages works. */
83 START_TEST(test_trigger_stage_add)
86 struct sr_trigger *t[NUM_TRIGGERS];
87 struct sr_trigger_stage *s[NUM_STAGES];
89 /* Create a few triggers with a valid name. */
90 for (i = 0; i < NUM_TRIGGERS; i++) {
91 t[i] = sr_trigger_new("T");
93 /* Add a bunch of trigger stages to this trigger. */
94 for (j = 0; j < NUM_STAGES; j++) {
95 s[j] = sr_trigger_stage_add(t[i]);
96 fail_unless(s[j] != NULL);
97 fail_unless(t[i]->stages != NULL);
98 fail_unless((int)g_slist_length(t[i]->stages) == (j + 1));
99 fail_unless(s[j]->stage == j);
100 fail_unless(s[j]->matches == NULL);
104 /* Free the triggers again (must not segfault). */
105 for (i = 0; i < NUM_TRIGGERS; i++)
106 sr_trigger_free(t[i]);
110 /* Check whether creating NULL trigger stages fails (as it should). */
111 START_TEST(test_trigger_stage_add_null)
113 /* Should not segfault, but rather return NULL. */
114 fail_unless(sr_trigger_stage_add(NULL) == NULL);
118 /* Check whether creating/freeing triggers with matches works. */
119 START_TEST(test_trigger_match_add)
121 int i, j, k, tm, ret;
122 struct sr_trigger *t[NUM_TRIGGERS];
123 struct sr_trigger_stage *s[NUM_STAGES];
124 struct sr_channel *chl[NUM_CHANNELS];
125 struct sr_channel *cha[NUM_CHANNELS];
128 /* Create a bunch of logic and analog channels. */
129 for (i = 0; i < NUM_CHANNELS; i++) {
130 sprintf((char *)&name, "L%d", i);
131 chl[i] = g_malloc0(sizeof(struct sr_channel));
133 chl[i]->type = SR_CHANNEL_LOGIC;
134 chl[i]->enabled = TRUE;
135 chl[i]->name = g_strdup((const char *)&name);
137 sprintf((char *)&name, "A%d", i);
138 cha[i] = g_malloc0(sizeof(struct sr_channel));
140 cha[i]->type = SR_CHANNEL_ANALOG;
141 cha[i]->enabled = TRUE;
142 cha[i]->name = g_strdup((const char *)&name);
145 /* Create a few triggers with a valid name. */
146 for (i = 0; i < NUM_TRIGGERS; i++) {
147 t[i] = sr_trigger_new("T");
149 /* Add a bunch of trigger stages to this trigger. */
150 for (j = 0; j < NUM_STAGES; j++) {
151 s[j] = sr_trigger_stage_add(t[i]);
153 /* Add a bunch of matches to this stage. */
154 for (k = 0; k < NUM_MATCHES; k++) {
155 /* Logic channel matches. */
156 tm = 1 + (k % 5); /* *_ZERO .. *_EDGE */
157 ret = sr_trigger_match_add(s[j], chl[k], tm, 0);
158 fail_unless(ret == SR_OK);
160 /* Analog channel matches. */
161 tm = 3 + (k % 4); /* *_RISING .. *_UNDER */
162 ret = sr_trigger_match_add(s[j], cha[k],
163 tm, ((rand() % 500) - 500) * 1.739);
164 fail_unless(ret == SR_OK);
169 /* Free the triggers again (must not segfault). */
170 for (i = 0; i < NUM_TRIGGERS; i++)
171 sr_trigger_free(t[i]);
173 /* Free the channels. */
174 for (i = 0; i < NUM_CHANNELS; i++) {
175 g_free(chl[i]->name);
177 g_free(cha[i]->name);
183 /* Check whether trigger_match_add() copes well with incorrect input. */
184 START_TEST(test_trigger_match_add_bogus)
187 struct sr_trigger *t;
188 struct sr_trigger_stage *s, *sl;
189 struct sr_channel *chl, *cha;
191 t = sr_trigger_new("T");
192 s = sr_trigger_stage_add(t);
193 chl = g_malloc0(sizeof(struct sr_channel));
195 chl->type = SR_CHANNEL_LOGIC;
197 chl->name = g_strdup("L0");
198 cha = g_malloc0(sizeof(struct sr_channel));
200 cha->type = SR_CHANNEL_ANALOG;
202 cha->name = g_strdup("A0");
204 /* Initially we have no matches at all. */
205 sl = t->stages->data;
206 fail_unless(g_slist_length(sl->matches) == 0);
209 ret = sr_trigger_match_add(NULL, chl, SR_TRIGGER_ZERO, 0);
210 fail_unless(ret == SR_ERR_ARG);
211 fail_unless(g_slist_length(sl->matches) == 0);
214 ret = sr_trigger_match_add(s, NULL, SR_TRIGGER_ZERO, 0);
215 fail_unless(ret == SR_ERR_ARG);
216 fail_unless(g_slist_length(sl->matches) == 0);
218 /* Invalid trigger matches for logic channels. */
219 ret = sr_trigger_match_add(s, chl, SR_TRIGGER_OVER, 0);
220 fail_unless(ret == SR_ERR_ARG);
221 fail_unless(g_slist_length(sl->matches) == 0);
222 ret = sr_trigger_match_add(s, chl, SR_TRIGGER_UNDER, 0);
223 fail_unless(ret == SR_ERR_ARG);
224 fail_unless(g_slist_length(sl->matches) == 0);
226 /* Invalid trigger matches for analog channels. */
227 ret = sr_trigger_match_add(s, cha, SR_TRIGGER_ZERO, 9.4);
228 fail_unless(ret == SR_ERR_ARG);
229 fail_unless(g_slist_length(sl->matches) == 0);
230 ret = sr_trigger_match_add(s, cha, SR_TRIGGER_ONE, -9.4);
231 fail_unless(ret == SR_ERR_ARG);
232 fail_unless(g_slist_length(sl->matches) == 0);
234 /* Invalid channel type. */
236 ret = sr_trigger_match_add(s, chl, SR_TRIGGER_ZERO, 0);
237 fail_unless(ret == SR_ERR_ARG);
238 fail_unless(g_slist_length(sl->matches) == 0);
240 ret = sr_trigger_match_add(s, chl, SR_TRIGGER_ZERO, 0);
241 fail_unless(ret == SR_ERR_ARG);
242 fail_unless(g_slist_length(sl->matches) == 0);
252 Suite *suite_trigger(void)
257 s = suite_create("trigger");
259 tc = tcase_create("new_free");
260 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
261 tcase_add_test(tc, test_trigger_new_free);
262 tcase_add_test(tc, test_trigger_new_free_null);
263 tcase_add_test(tc, test_trigger_free_null);
264 suite_add_tcase(s, tc);
266 tc = tcase_create("stage");
267 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
268 tcase_add_test(tc, test_trigger_stage_add);
269 tcase_add_test(tc, test_trigger_stage_add_null);
270 suite_add_tcase(s, tc);
272 tc = tcase_create("match");
273 tcase_set_timeout(tc, 0);
274 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
275 tcase_add_test(tc, test_trigger_match_add);
276 tcase_add_test(tc, test_trigger_match_add_bogus);
277 suite_add_tcase(s, tc);