trigger.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <check.h>
  23. #include <libsigrok/libsigrok.h>
  24. #include "lib.h"
  25. /* Test lots of triggers/stages/matches/channels */
  26. #define NUM_TRIGGERS 70
  27. #define NUM_STAGES 30
  28. #define NUM_MATCHES 70
  29. #define NUM_CHANNELS NUM_MATCHES
  30. /* Check whether creating/freeing triggers with valid names works. */
  31. START_TEST(test_trigger_new_free)
  32. {
  33. int i;
  34. struct sr_trigger *t[NUM_TRIGGERS];
  35. char name[10];
  36. /* Create a few triggers with a valid name. */
  37. for (i = 0; i < NUM_TRIGGERS; i++) {
  38. sprintf((char *)&name, "T%d", i);
  39. t[i] = sr_trigger_new((const char *)&name);
  40. fail_unless(t[i] != NULL);
  41. fail_unless(!strcmp(t[i]->name, (const char *)&name));
  42. fail_unless(t[i]->stages == NULL);
  43. }
  44. /* Free the triggers again (must not segfault). */
  45. for (i = 0; i < NUM_TRIGGERS; i++)
  46. sr_trigger_free(t[i]);
  47. }
  48. END_TEST
  49. /* Check whether creating/freeing triggers with NULL names works. */
  50. START_TEST(test_trigger_new_free_null)
  51. {
  52. int i;
  53. struct sr_trigger *t[NUM_TRIGGERS];
  54. /* Create a few triggers with a NULL name (which is allowed). */
  55. for (i = 0; i < NUM_TRIGGERS; i++) {
  56. t[i] = sr_trigger_new(NULL);
  57. fail_unless(t[i] != NULL);
  58. fail_unless(t[i]->name == NULL);
  59. fail_unless(t[i]->stages == NULL);
  60. }
  61. /* Free the triggers again (must not segfault). */
  62. for (i = 0; i < NUM_TRIGGERS; i++)
  63. sr_trigger_free(t[i]);
  64. }
  65. END_TEST
  66. /* Check whether sr_trigger_free(NULL) works without segfaulting. */
  67. START_TEST(test_trigger_free_null)
  68. {
  69. sr_trigger_free(NULL);
  70. }
  71. END_TEST
  72. /* Check whether creating/freeing triggers with stages works. */
  73. START_TEST(test_trigger_stage_add)
  74. {
  75. int i, j;
  76. struct sr_trigger *t[NUM_TRIGGERS];
  77. struct sr_trigger_stage *s[NUM_STAGES];
  78. /* Create a few triggers with a valid name. */
  79. for (i = 0; i < NUM_TRIGGERS; i++) {
  80. t[i] = sr_trigger_new("T");
  81. /* Add a bunch of trigger stages to this trigger. */
  82. for (j = 0; j < NUM_STAGES; j++) {
  83. s[j] = sr_trigger_stage_add(t[i]);
  84. fail_unless(s[j] != NULL);
  85. fail_unless(t[i]->stages != NULL);
  86. fail_unless((int)g_slist_length(t[i]->stages) == (j + 1));
  87. fail_unless(s[j]->stage == j);
  88. fail_unless(s[j]->matches == NULL);
  89. }
  90. }
  91. /* Free the triggers again (must not segfault). */
  92. for (i = 0; i < NUM_TRIGGERS; i++)
  93. sr_trigger_free(t[i]);
  94. }
  95. END_TEST
  96. /* Check whether creating NULL trigger stages fails (as it should). */
  97. START_TEST(test_trigger_stage_add_null)
  98. {
  99. /* Should not segfault, but rather return NULL. */
  100. fail_unless(sr_trigger_stage_add(NULL) == NULL);
  101. }
  102. END_TEST
  103. /* Check whether creating/freeing triggers with matches works. */
  104. START_TEST(test_trigger_match_add)
  105. {
  106. int i, j, k, tm, ret;
  107. struct sr_trigger *t[NUM_TRIGGERS];
  108. struct sr_trigger_stage *s[NUM_STAGES];
  109. struct sr_channel *chl[NUM_CHANNELS];
  110. struct sr_channel *cha[NUM_CHANNELS];
  111. char name[10];
  112. /* Create a bunch of logic and analog channels. */
  113. for (i = 0; i < NUM_CHANNELS; i++) {
  114. sprintf((char *)&name, "L%d", i);
  115. chl[i] = g_malloc0(sizeof(struct sr_channel));
  116. chl[i]->index = i;
  117. chl[i]->type = SR_CHANNEL_LOGIC;
  118. chl[i]->enabled = TRUE;
  119. chl[i]->name = g_strdup((const char *)&name);
  120. sprintf((char *)&name, "A%d", i);
  121. cha[i] = g_malloc0(sizeof(struct sr_channel));
  122. cha[i]->index = i;
  123. cha[i]->type = SR_CHANNEL_ANALOG;
  124. cha[i]->enabled = TRUE;
  125. cha[i]->name = g_strdup((const char *)&name);
  126. }
  127. /* Create a few triggers with a valid name. */
  128. for (i = 0; i < NUM_TRIGGERS; i++) {
  129. t[i] = sr_trigger_new("T");
  130. /* Add a bunch of trigger stages to this trigger. */
  131. for (j = 0; j < NUM_STAGES; j++) {
  132. s[j] = sr_trigger_stage_add(t[i]);
  133. /* Add a bunch of matches to this stage. */
  134. for (k = 0; k < NUM_MATCHES; k++) {
  135. /* Logic channel matches. */
  136. tm = 1 + (k % 5); /* *_ZERO .. *_EDGE */
  137. ret = sr_trigger_match_add(s[j], chl[k], tm, 0);
  138. fail_unless(ret == SR_OK);
  139. /* Analog channel matches. */
  140. tm = 3 + (k % 4); /* *_RISING .. *_UNDER */
  141. ret = sr_trigger_match_add(s[j], cha[k],
  142. tm, ((rand() % 500) - 500) * 1.739);
  143. fail_unless(ret == SR_OK);
  144. }
  145. }
  146. }
  147. /* Free the triggers again (must not segfault). */
  148. for (i = 0; i < NUM_TRIGGERS; i++)
  149. sr_trigger_free(t[i]);
  150. /* Free the channels. */
  151. for (i = 0; i < NUM_CHANNELS; i++) {
  152. g_free(chl[i]->name);
  153. g_free(chl[i]);
  154. g_free(cha[i]->name);
  155. g_free(cha[i]);
  156. }
  157. }
  158. END_TEST
  159. /* Check whether trigger_match_add() copes well with incorrect input. */
  160. START_TEST(test_trigger_match_add_bogus)
  161. {
  162. int ret;
  163. struct sr_trigger *t;
  164. struct sr_trigger_stage *s, *sl;
  165. struct sr_channel *chl, *cha;
  166. t = sr_trigger_new("T");
  167. s = sr_trigger_stage_add(t);
  168. chl = g_malloc0(sizeof(struct sr_channel));
  169. chl->index = 0;
  170. chl->type = SR_CHANNEL_LOGIC;
  171. chl->enabled = TRUE;
  172. chl->name = g_strdup("L0");
  173. cha = g_malloc0(sizeof(struct sr_channel));
  174. cha->index = 1;
  175. cha->type = SR_CHANNEL_ANALOG;
  176. cha->enabled = TRUE;
  177. cha->name = g_strdup("A0");
  178. /* Initially we have no matches at all. */
  179. sl = t->stages->data;
  180. fail_unless(g_slist_length(sl->matches) == 0);
  181. /* NULL stage */
  182. ret = sr_trigger_match_add(NULL, chl, SR_TRIGGER_ZERO, 0);
  183. fail_unless(ret == SR_ERR_ARG);
  184. fail_unless(g_slist_length(sl->matches) == 0);
  185. /* NULL channel */
  186. ret = sr_trigger_match_add(s, NULL, SR_TRIGGER_ZERO, 0);
  187. fail_unless(ret == SR_ERR_ARG);
  188. fail_unless(g_slist_length(sl->matches) == 0);
  189. /* Invalid trigger matches for logic channels. */
  190. ret = sr_trigger_match_add(s, chl, SR_TRIGGER_OVER, 0);
  191. fail_unless(ret == SR_ERR_ARG);
  192. fail_unless(g_slist_length(sl->matches) == 0);
  193. ret = sr_trigger_match_add(s, chl, SR_TRIGGER_UNDER, 0);
  194. fail_unless(ret == SR_ERR_ARG);
  195. fail_unless(g_slist_length(sl->matches) == 0);
  196. /* Invalid trigger matches for analog channels. */
  197. ret = sr_trigger_match_add(s, cha, SR_TRIGGER_ZERO, 9.4);
  198. fail_unless(ret == SR_ERR_ARG);
  199. fail_unless(g_slist_length(sl->matches) == 0);
  200. ret = sr_trigger_match_add(s, cha, SR_TRIGGER_ONE, -9.4);
  201. fail_unless(ret == SR_ERR_ARG);
  202. fail_unless(g_slist_length(sl->matches) == 0);
  203. /* Invalid channel type. */
  204. chl->type = -1;
  205. ret = sr_trigger_match_add(s, chl, SR_TRIGGER_ZERO, 0);
  206. fail_unless(ret == SR_ERR_ARG);
  207. fail_unless(g_slist_length(sl->matches) == 0);
  208. chl->type = 270;
  209. ret = sr_trigger_match_add(s, chl, SR_TRIGGER_ZERO, 0);
  210. fail_unless(ret == SR_ERR_ARG);
  211. fail_unless(g_slist_length(sl->matches) == 0);
  212. sr_trigger_free(t);
  213. g_free(chl->name);
  214. g_free(chl);
  215. g_free(cha->name);
  216. g_free(cha);
  217. }
  218. END_TEST
  219. Suite *suite_trigger(void)
  220. {
  221. Suite *s;
  222. TCase *tc;
  223. s = suite_create("trigger");
  224. tc = tcase_create("new_free");
  225. tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
  226. tcase_add_test(tc, test_trigger_new_free);
  227. tcase_add_test(tc, test_trigger_new_free_null);
  228. tcase_add_test(tc, test_trigger_free_null);
  229. suite_add_tcase(s, tc);
  230. tc = tcase_create("stage");
  231. tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
  232. tcase_add_test(tc, test_trigger_stage_add);
  233. tcase_add_test(tc, test_trigger_stage_add_null);
  234. suite_add_tcase(s, tc);
  235. tc = tcase_create("match");
  236. tcase_set_timeout(tc, 0);
  237. tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
  238. tcase_add_test(tc, test_trigger_match_add);
  239. tcase_add_test(tc, test_trigger_match_add_bogus);
  240. suite_add_tcase(s, tc);
  241. return s;
  242. }