core.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <check.h>
  22. #include <libsigrok/libsigrok.h>
  23. #include "lib.h"
  24. /*
  25. * Check various basic init related things.
  26. *
  27. * - Check whether an sr_init() call with a proper sr_ctx works.
  28. * If it returns != SR_OK (or segfaults) this test will fail.
  29. * The sr_init() call (among other things) also runs sanity checks on
  30. * all libsigrok hardware drivers and errors out upon issues.
  31. *
  32. * - Check whether a subsequent sr_exit() with that sr_ctx works.
  33. * If it returns != SR_OK (or segfaults) this test will fail.
  34. */
  35. START_TEST(test_init_exit)
  36. {
  37. int ret;
  38. struct sr_context *sr_ctx;
  39. ret = sr_init(&sr_ctx);
  40. fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
  41. ret = sr_exit(sr_ctx);
  42. fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
  43. }
  44. END_TEST
  45. /*
  46. * Check whether two nested sr_init() and sr_exit() calls work.
  47. * The two functions have two different contexts.
  48. * If any function returns != SR_OK (or segfaults) this test will fail.
  49. */
  50. START_TEST(test_init_exit_2)
  51. {
  52. int ret;
  53. struct sr_context *sr_ctx1, *sr_ctx2;
  54. ret = sr_init(&sr_ctx1);
  55. fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
  56. ret = sr_init(&sr_ctx2);
  57. fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
  58. ret = sr_exit(sr_ctx2);
  59. fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
  60. ret = sr_exit(sr_ctx1);
  61. fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
  62. }
  63. END_TEST
  64. /*
  65. * Same as above, but sr_exit() in the "wrong" order.
  66. * This should work fine, it's not a bug to do this.
  67. */
  68. START_TEST(test_init_exit_2_reverse)
  69. {
  70. int ret;
  71. struct sr_context *sr_ctx1, *sr_ctx2;
  72. ret = sr_init(&sr_ctx1);
  73. fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
  74. ret = sr_init(&sr_ctx2);
  75. fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
  76. ret = sr_exit(sr_ctx1);
  77. fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
  78. ret = sr_exit(sr_ctx2);
  79. fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
  80. }
  81. END_TEST
  82. /*
  83. * Check whether three nested sr_init() and sr_exit() calls work.
  84. * The three functions have three different contexts.
  85. * If any function returns != SR_OK (or segfaults) this test will fail.
  86. */
  87. START_TEST(test_init_exit_3)
  88. {
  89. int ret;
  90. struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
  91. ret = sr_init(&sr_ctx1);
  92. fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
  93. ret = sr_init(&sr_ctx2);
  94. fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
  95. ret = sr_init(&sr_ctx3);
  96. fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
  97. ret = sr_exit(sr_ctx3);
  98. fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
  99. ret = sr_exit(sr_ctx2);
  100. fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
  101. ret = sr_exit(sr_ctx1);
  102. fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
  103. }
  104. END_TEST
  105. /*
  106. * Same as above, but sr_exit() in the "wrong" order.
  107. * This should work fine, it's not a bug to do this.
  108. */
  109. START_TEST(test_init_exit_3_reverse)
  110. {
  111. int ret;
  112. struct sr_context *sr_ctx1, *sr_ctx2, *sr_ctx3;
  113. ret = sr_init(&sr_ctx1);
  114. fail_unless(ret == SR_OK, "sr_init() 1 failed: %d.", ret);
  115. ret = sr_init(&sr_ctx2);
  116. fail_unless(ret == SR_OK, "sr_init() 2 failed: %d.", ret);
  117. ret = sr_init(&sr_ctx3);
  118. fail_unless(ret == SR_OK, "sr_init() 3 failed: %d.", ret);
  119. ret = sr_exit(sr_ctx1);
  120. fail_unless(ret == SR_OK, "sr_exit() 1 failed: %d.", ret);
  121. ret = sr_exit(sr_ctx2);
  122. fail_unless(ret == SR_OK, "sr_exit() 2 failed: %d.", ret);
  123. ret = sr_exit(sr_ctx3);
  124. fail_unless(ret == SR_OK, "sr_exit() 3 failed: %d.", ret);
  125. }
  126. END_TEST
  127. /* Check whether sr_init(NULL) fails as it should. */
  128. START_TEST(test_init_null)
  129. {
  130. int ret;
  131. ret = sr_log_loglevel_set(SR_LOG_NONE);
  132. fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
  133. ret = sr_init(NULL);
  134. fail_unless(ret != SR_OK, "sr_init(NULL) should have failed.");
  135. }
  136. END_TEST
  137. /* Check whether sr_exit(NULL) fails as it should. */
  138. START_TEST(test_exit_null)
  139. {
  140. int ret;
  141. ret = sr_log_loglevel_set(SR_LOG_NONE);
  142. fail_unless(ret == SR_OK, "sr_log_loglevel_set() failed: %d.", ret);
  143. ret = sr_exit(NULL);
  144. fail_unless(ret != SR_OK, "sr_exit(NULL) should have failed.");
  145. }
  146. END_TEST
  147. Suite *suite_core(void)
  148. {
  149. Suite *s;
  150. TCase *tc;
  151. s = suite_create("core");
  152. tc = tcase_create("init_exit");
  153. tcase_add_test(tc, test_init_exit);
  154. tcase_add_test(tc, test_init_exit_2);
  155. tcase_add_test(tc, test_init_exit_2_reverse);
  156. tcase_add_test(tc, test_init_exit_3);
  157. tcase_add_test(tc, test_init_exit_3_reverse);
  158. tcase_add_test(tc, test_init_null);
  159. tcase_add_test(tc, test_exit_null);
  160. suite_add_tcase(s, tc);
  161. return s;
  162. }