check_check_selective.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Check: a unit test framework for C
  3. * Copyright (C) 2001, 2002 Arien Malec
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include "../lib/libcompat.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <check.h>
  24. #include "check_check.h"
  25. static SRunner *sr;
  26. static int test_tc11_executed;
  27. static int test_tc12_executed;
  28. static int test_tc21_executed;
  29. static void reset_executed (void)
  30. {
  31. test_tc11_executed = 0;
  32. test_tc12_executed = 0;
  33. test_tc21_executed = 0;
  34. }
  35. START_TEST(test_tc11)
  36. {
  37. test_tc11_executed = 1;
  38. }
  39. END_TEST
  40. START_TEST(test_tc12)
  41. {
  42. test_tc12_executed = 1;
  43. }
  44. END_TEST
  45. START_TEST(test_tc21)
  46. {
  47. test_tc21_executed = 1;
  48. }
  49. END_TEST
  50. static void selective_setup (void)
  51. {
  52. Suite *s1, *s2;
  53. TCase *tc11, *tc12, *tc21;
  54. /*
  55. * Create a test suite 'suite1' with two test cases 'tcase11' and
  56. * 'tcase12' containing a single test each.
  57. */
  58. s1 = suite_create ("suite1");
  59. tc11 = tcase_create ("tcase11");
  60. tcase_add_test (tc11, test_tc11);
  61. tc12 = tcase_create ("tcase12");
  62. tcase_add_test (tc12, test_tc12);
  63. suite_add_tcase (s1, tc11);
  64. suite_add_tcase (s1, tc12);
  65. /* This line intentionally attempts to add an already
  66. * added test case twice, to ensure it is not added
  67. * again. If it was added again, when the test cases
  68. * are freed a double-free failure will occur. */
  69. suite_add_tcase (s1, tc12);
  70. /*
  71. * Create a test suite 'suite2' with one test case 'test21'
  72. * containing two tests.
  73. */
  74. s2 = suite_create ("suite2");
  75. tc21 = tcase_create ("tcase21");
  76. tcase_add_test (tc21, test_tc21);
  77. suite_add_tcase (s2, tc21);
  78. sr = srunner_create (s1);
  79. srunner_add_suite (sr, s2);
  80. srunner_set_fork_status (sr, CK_NOFORK);
  81. }
  82. static void selective_teardown (void)
  83. {
  84. srunner_free (sr);
  85. }
  86. START_TEST(test_srunner_run_run_all)
  87. {
  88. /* This test exercises the srunner_run function for the case where
  89. both sname and tcname are NULL. That means to run all the test
  90. cases in all the defined suites. */
  91. srunner_run (sr,
  92. NULL, /* NULL tsuite name. */
  93. NULL, /* NULL tcase name. */
  94. CK_VERBOSE);
  95. ck_assert_msg (srunner_ntests_run(sr) == 3,
  96. "Not all tests were executed.");
  97. reset_executed ();
  98. }
  99. END_TEST
  100. START_TEST(test_srunner_run_suite)
  101. {
  102. /* This test makes the srunner_run function to run all the test
  103. cases of a existing suite. */
  104. srunner_run (sr,
  105. "suite1",
  106. NULL, /* NULL tcase name. */
  107. CK_VERBOSE);
  108. ck_assert_msg (test_tc11_executed
  109. && test_tc12_executed
  110. && !test_tc21_executed,
  111. "Expected tests were not executed.");
  112. reset_executed ();
  113. }
  114. END_TEST
  115. START_TEST(test_srunner_run_no_suite)
  116. {
  117. /* This test makes the srunner_run function to run all the test
  118. cases of a non-existing suite. */
  119. srunner_run (sr,
  120. "non-existing-suite",
  121. NULL, /* NULL tcase name. */
  122. CK_VERBOSE);
  123. ck_assert_msg (!(test_tc11_executed
  124. || test_tc12_executed
  125. || test_tc21_executed),
  126. "An unexpected test was executed.");
  127. reset_executed ();
  128. }
  129. END_TEST
  130. START_TEST(test_srunner_run_tcase)
  131. {
  132. /* This test makes the srunner_run function to run an specific
  133. existing test case. */
  134. srunner_run (sr,
  135. NULL, /* NULL suite name. */
  136. "tcase12",
  137. CK_VERBOSE);
  138. ck_assert_msg (!test_tc11_executed
  139. && test_tc12_executed
  140. && !test_tc21_executed,
  141. "Expected tests were not executed.");
  142. reset_executed ();
  143. }
  144. END_TEST
  145. START_TEST(test_srunner_no_tcase)
  146. {
  147. /* This test makes the srunner_run function to run a non-existant
  148. test case. */
  149. srunner_run (sr,
  150. NULL, /* NULL suite name. */
  151. "non-existant-test-case",
  152. CK_VERBOSE);
  153. ck_assert_msg (!(test_tc11_executed
  154. || test_tc12_executed
  155. || test_tc21_executed),
  156. "An unexpected test was executed.");
  157. reset_executed ();
  158. }
  159. END_TEST
  160. START_TEST(test_srunner_suite_tcase)
  161. {
  162. /* This test makes the srunner_run function to run a specific test
  163. case of a specific test suite. */
  164. srunner_run (sr,
  165. "suite2",
  166. "tcase21",
  167. CK_VERBOSE);
  168. ck_assert_msg (!test_tc11_executed
  169. && !test_tc12_executed
  170. && test_tc21_executed,
  171. "Expected tests were not executed.");
  172. reset_executed ();
  173. }
  174. END_TEST
  175. START_TEST(test_srunner_suite_no_tcase)
  176. {
  177. /* This test makes the srunner_run function to run a non existant
  178. test case of a specific test suite. */
  179. srunner_run (sr,
  180. "suite1",
  181. "non-existant-test-case",
  182. CK_VERBOSE);
  183. ck_assert_msg (!(test_tc11_executed
  184. || test_tc12_executed
  185. || test_tc21_executed),
  186. "An unexpected test was executed.");
  187. reset_executed ();
  188. }
  189. END_TEST
  190. #if HAVE_DECL_SETENV
  191. START_TEST(test_srunner_run_suite_env)
  192. {
  193. /* This test makes the srunner_run_all function to run all the test
  194. cases of a existing suite. */
  195. setenv ("CK_RUN_SUITE", "suite1", 1);
  196. srunner_run_all (sr, CK_VERBOSE);
  197. ck_assert_msg (test_tc11_executed
  198. && test_tc12_executed
  199. && !test_tc21_executed,
  200. "Expected tests were not executed.");
  201. reset_executed ();
  202. unsetenv ("CK_RUN_SUITE");
  203. }
  204. END_TEST
  205. START_TEST(test_srunner_run_no_suite_env)
  206. {
  207. /* This test makes the srunner_run_all function to run all the test
  208. cases of a non-existing suite. */
  209. setenv ("CK_RUN_SUITE", "non-existing-suite", 1);
  210. srunner_run_all (sr, CK_VERBOSE);
  211. ck_assert_msg (!(test_tc11_executed
  212. || test_tc12_executed
  213. || test_tc21_executed),
  214. "An unexpected test was executed.");
  215. reset_executed ();
  216. unsetenv ("CK_RUN_SUITE");
  217. }
  218. END_TEST
  219. START_TEST(test_srunner_run_tcase_env)
  220. {
  221. /* This test makes the srunner_run_all function to run an specific
  222. existing test case. */
  223. setenv ("CK_RUN_CASE", "tcase12", 1);
  224. srunner_run_all (sr, CK_VERBOSE);
  225. ck_assert_msg (!test_tc11_executed
  226. && test_tc12_executed
  227. && !test_tc21_executed,
  228. "Expected tests were not executed.");
  229. reset_executed ();
  230. unsetenv ("CK_RUN_CASE");
  231. }
  232. END_TEST
  233. START_TEST(test_srunner_no_tcase_env)
  234. {
  235. /* This test makes the srunner_run_all function to run a
  236. non-existant test case. */
  237. setenv ("CK_RUN_CASE", "non-existant-test-case", 1);
  238. srunner_run_all (sr, CK_VERBOSE);
  239. ck_assert_msg (!(test_tc11_executed
  240. || test_tc12_executed
  241. || test_tc21_executed),
  242. "An unexpected test was executed.");
  243. reset_executed ();
  244. unsetenv ("CK_RUN_CASE");
  245. }
  246. END_TEST
  247. START_TEST(test_srunner_suite_tcase_env)
  248. {
  249. /* This test makes the srunner_run_all function to run a specific test
  250. case of a specific test suite. */
  251. setenv ("CK_RUN_SUITE", "suite2", 1);
  252. setenv ("CK_RUN_CASE", "tcase21", 1);
  253. srunner_run_all (sr, CK_VERBOSE);
  254. ck_assert_msg (!test_tc11_executed
  255. && !test_tc12_executed
  256. && test_tc21_executed,
  257. "Expected tests were not executed.");
  258. reset_executed ();
  259. unsetenv ("CK_RUN_SUITE");
  260. unsetenv ("CK_RUN_CASE");
  261. }
  262. END_TEST
  263. START_TEST(test_srunner_suite_no_tcase_env)
  264. {
  265. /* This test makes the srunner_run_all function to run a non
  266. existant test case of a specific test suite. */
  267. setenv ("CK_RUN_SUITE", "suite1", 1);
  268. setenv ("CK_RUN_CASE", "non-existant-test-case", 1);
  269. srunner_run_all (sr, CK_VERBOSE);
  270. ck_assert_msg (!(test_tc11_executed
  271. || test_tc12_executed
  272. || test_tc21_executed),
  273. "An unexpected test was executed.");
  274. reset_executed ();
  275. unsetenv ("CK_RUN_SUITE");
  276. unsetenv ("CK_RUN_CASE");
  277. }
  278. END_TEST
  279. #endif /* HAVE_DECL_SETENV */
  280. Suite *make_selective_suite (void)
  281. {
  282. Suite *s = suite_create ("SelectiveTesting");
  283. TCase *tc = tcase_create ("Core");
  284. suite_add_tcase (s, tc);
  285. tcase_add_test (tc, test_srunner_run_run_all);
  286. tcase_add_test (tc, test_srunner_run_suite);
  287. tcase_add_test (tc, test_srunner_run_no_suite);
  288. tcase_add_test (tc, test_srunner_run_tcase);
  289. tcase_add_test (tc, test_srunner_no_tcase);
  290. tcase_add_test (tc, test_srunner_suite_tcase);
  291. tcase_add_test (tc, test_srunner_suite_no_tcase);
  292. #if HAVE_DECL_SETENV
  293. tcase_add_test (tc, test_srunner_run_suite_env);
  294. tcase_add_test (tc, test_srunner_run_no_suite_env);
  295. tcase_add_test (tc, test_srunner_run_tcase_env);
  296. tcase_add_test (tc, test_srunner_no_tcase_env);
  297. tcase_add_test (tc, test_srunner_suite_tcase_env);
  298. tcase_add_test (tc, test_srunner_suite_no_tcase_env);
  299. #endif /* HAVE_DECL_SETENV */
  300. tcase_add_unchecked_fixture (tc,
  301. selective_setup,
  302. selective_teardown);
  303. return s;
  304. }