selftest.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* A self-testing framework, for use by -fself-test.
  2. Copyright (C) 2015-2018 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_SELFTEST_H
  16. #define GCC_SELFTEST_H
  17. /* The selftest code should entirely disappear in a production
  18. configuration, hence we guard all of it with #if CHECKING_P. */
  19. #if CHECKING_P
  20. namespace selftest {
  21. /* A struct describing the source-location of a selftest, to make it
  22. easier to track down failing tests. */
  23. struct location
  24. {
  25. location (const char *file, int line, const char *function)
  26. : m_file (file), m_line (line), m_function (function) {}
  27. const char *m_file;
  28. int m_line;
  29. const char *m_function;
  30. };
  31. /* A macro for use in selftests and by the ASSERT_ macros below,
  32. constructing a selftest::location for the current source location. */
  33. #define SELFTEST_LOCATION \
  34. (::selftest::location (__FILE__, __LINE__, __FUNCTION__))
  35. /* The entrypoint for running all tests. */
  36. extern void run_tests ();
  37. /* Record the successful outcome of some aspect of the test. */
  38. extern void pass (const location &loc, const char *msg);
  39. /* Report the failed outcome of some aspect of the test and abort. */
  40. extern void fail (const location &loc, const char *msg)
  41. ATTRIBUTE_NORETURN;
  42. /* As "fail", but using printf-style formatted output. */
  43. extern void fail_formatted (const location &loc, const char *fmt, ...)
  44. ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
  45. /* Implementation detail of ASSERT_STREQ. */
  46. extern void assert_streq (const location &loc,
  47. const char *desc_expected, const char *desc_actual,
  48. const char *val_expected, const char *val_actual);
  49. /* Implementation detail of ASSERT_STR_CONTAINS. */
  50. extern void assert_str_contains (const location &loc,
  51. const char *desc_haystack,
  52. const char *desc_needle,
  53. const char *val_haystack,
  54. const char *val_needle);
  55. /* A named temporary file for use in selftests.
  56. Usable for writing out files, and as the base class for
  57. temp_source_file.
  58. The file is unlinked in the destructor. */
  59. class named_temp_file
  60. {
  61. public:
  62. named_temp_file (const char *suffix);
  63. ~named_temp_file ();
  64. const char *get_filename () const { return m_filename; }
  65. private:
  66. char *m_filename;
  67. };
  68. /* A class for writing out a temporary sourcefile for use in selftests
  69. of input handling. */
  70. class temp_source_file : public named_temp_file
  71. {
  72. public:
  73. temp_source_file (const location &loc, const char *suffix,
  74. const char *content);
  75. };
  76. /* Various selftests involving location-handling require constructing a
  77. line table and one or more line maps within it.
  78. For maximum test coverage we want to run these tests with a variety
  79. of situations:
  80. - line_table->default_range_bits: some frontends use a non-zero value
  81. and others use zero
  82. - the fallback modes within line-map.c: there are various threshold
  83. values for source_location/location_t beyond line-map.c changes
  84. behavior (disabling of the range-packing optimization, disabling
  85. of column-tracking). We can exercise these by starting the line_table
  86. at interesting values at or near these thresholds.
  87. The following struct describes a particular case within our test
  88. matrix. */
  89. struct line_table_case;
  90. /* A class for overriding the global "line_table" within a selftest,
  91. restoring its value afterwards. At most one instance of this
  92. class can exist at once, due to the need to keep the old value
  93. of line_table as a GC root. */
  94. class line_table_test
  95. {
  96. public:
  97. /* Default constructor. Override "line_table", using sane defaults
  98. for the temporary line_table. */
  99. line_table_test ();
  100. /* Constructor. Override "line_table", using the case described by C. */
  101. line_table_test (const line_table_case &c);
  102. /* Destructor. Restore the saved line_table. */
  103. ~line_table_test ();
  104. };
  105. /* Run TESTCASE multiple times, once for each case in our test matrix. */
  106. extern void
  107. for_each_line_table_case (void (*testcase) (const line_table_case &));
  108. /* Read the contents of PATH into memory, returning a 0-terminated buffer
  109. that must be freed by the caller.
  110. Fail (and abort) if there are any problems, with LOC as the reported
  111. location of the failure. */
  112. extern char *read_file (const location &loc, const char *path);
  113. /* A helper function for writing tests that interact with the
  114. garbage collector. */
  115. extern void forcibly_ggc_collect ();
  116. /* Convert a path relative to SRCDIR/gcc/testsuite/selftests
  117. to a real path (either absolute, or relative to pwd).
  118. The result should be freed by the caller. */
  119. extern char *locate_file (const char *path);
  120. /* The path of SRCDIR/testsuite/selftests. */
  121. extern const char *path_to_selftest_files;
  122. /* selftest::test_runner is an implementation detail of selftest::run_tests,
  123. exposed here to allow plugins to run their own suites of tests. */
  124. class test_runner
  125. {
  126. public:
  127. test_runner (const char *name);
  128. ~test_runner ();
  129. private:
  130. const char *m_name;
  131. long m_start_time;
  132. };
  133. /* Declarations for specific families of tests (by source file), in
  134. alphabetical order. */
  135. extern void attribute_c_tests ();
  136. extern void bitmap_c_tests ();
  137. extern void sbitmap_c_tests ();
  138. extern void diagnostic_c_tests ();
  139. extern void diagnostic_show_locus_c_tests ();
  140. extern void edit_context_c_tests ();
  141. extern void et_forest_c_tests ();
  142. extern void fold_const_c_tests ();
  143. extern void fibonacci_heap_c_tests ();
  144. extern void function_tests_c_tests ();
  145. extern void gimple_c_tests ();
  146. extern void ggc_tests_c_tests ();
  147. extern void hash_map_tests_c_tests ();
  148. extern void hash_set_tests_c_tests ();
  149. extern void input_c_tests ();
  150. extern void pretty_print_c_tests ();
  151. extern void read_rtl_function_c_tests ();
  152. extern void rtl_tests_c_tests ();
  153. extern void selftest_c_tests ();
  154. extern void spellcheck_c_tests ();
  155. extern void spellcheck_tree_c_tests ();
  156. extern void sreal_c_tests ();
  157. extern void store_merging_c_tests ();
  158. extern void typed_splay_tree_c_tests ();
  159. extern void tree_c_tests ();
  160. extern void tree_cfg_c_tests ();
  161. extern void unique_ptr_tests_cc_tests ();
  162. extern void vec_c_tests ();
  163. extern void wide_int_cc_tests ();
  164. extern void predict_c_tests ();
  165. extern void simplify_rtx_c_tests ();
  166. extern void vec_perm_indices_c_tests ();
  167. extern int num_passes;
  168. } /* end of namespace selftest. */
  169. /* Macros for writing tests. */
  170. /* Evaluate EXPR and coerce to bool, calling
  171. ::selftest::pass if it is true,
  172. ::selftest::fail if it false. */
  173. #define ASSERT_TRUE(EXPR) \
  174. ASSERT_TRUE_AT (SELFTEST_LOCATION, (EXPR))
  175. /* Like ASSERT_TRUE, but treat LOC as the effective location of the
  176. selftest. */
  177. #define ASSERT_TRUE_AT(LOC, EXPR) \
  178. SELFTEST_BEGIN_STMT \
  179. const char *desc_ = "ASSERT_TRUE (" #EXPR ")"; \
  180. bool actual_ = (EXPR); \
  181. if (actual_) \
  182. ::selftest::pass ((LOC), desc_); \
  183. else \
  184. ::selftest::fail ((LOC), desc_); \
  185. SELFTEST_END_STMT
  186. /* Evaluate EXPR and coerce to bool, calling
  187. ::selftest::pass if it is false,
  188. ::selftest::fail if it true. */
  189. #define ASSERT_FALSE(EXPR) \
  190. ASSERT_FALSE_AT (SELFTEST_LOCATION, (EXPR))
  191. /* Like ASSERT_FALSE, but treat LOC as the effective location of the
  192. selftest. */
  193. #define ASSERT_FALSE_AT(LOC, EXPR) \
  194. SELFTEST_BEGIN_STMT \
  195. const char *desc_ = "ASSERT_FALSE (" #EXPR ")"; \
  196. bool actual_ = (EXPR); \
  197. if (actual_) \
  198. ::selftest::fail ((LOC), desc_); \
  199. else \
  200. ::selftest::pass ((LOC), desc_); \
  201. SELFTEST_END_STMT
  202. /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
  203. ::selftest::pass if they are equal,
  204. ::selftest::fail if they are non-equal. */
  205. #define ASSERT_EQ(EXPECTED, ACTUAL) \
  206. ASSERT_EQ_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))
  207. /* Like ASSERT_EQ, but treat LOC as the effective location of the
  208. selftest. */
  209. #define ASSERT_EQ_AT(LOC, EXPECTED, ACTUAL) \
  210. SELFTEST_BEGIN_STMT \
  211. const char *desc_ = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
  212. if ((EXPECTED) == (ACTUAL)) \
  213. ::selftest::pass ((LOC), desc_); \
  214. else \
  215. ::selftest::fail ((LOC), desc_); \
  216. SELFTEST_END_STMT
  217. /* Evaluate EXPECTED and ACTUAL and compare them with known_eq, calling
  218. ::selftest::pass if they are always equal,
  219. ::selftest::fail if they might be non-equal. */
  220. #define ASSERT_KNOWN_EQ(EXPECTED, ACTUAL) \
  221. ASSERT_KNOWN_EQ_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))
  222. /* Like ASSERT_KNOWN_EQ, but treat LOC as the effective location of the
  223. selftest. */
  224. #define ASSERT_KNOWN_EQ_AT(LOC, EXPECTED, ACTUAL) \
  225. SELFTEST_BEGIN_STMT \
  226. const char *desc = "ASSERT_KNOWN_EQ (" #EXPECTED ", " #ACTUAL ")"; \
  227. if (known_eq (EXPECTED, ACTUAL)) \
  228. ::selftest::pass ((LOC), desc); \
  229. else \
  230. ::selftest::fail ((LOC), desc); \
  231. SELFTEST_END_STMT
  232. /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
  233. ::selftest::pass if they are non-equal,
  234. ::selftest::fail if they are equal. */
  235. #define ASSERT_NE(EXPECTED, ACTUAL) \
  236. SELFTEST_BEGIN_STMT \
  237. const char *desc_ = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
  238. if ((EXPECTED) != (ACTUAL)) \
  239. ::selftest::pass (SELFTEST_LOCATION, desc_); \
  240. else \
  241. ::selftest::fail (SELFTEST_LOCATION, desc_); \
  242. SELFTEST_END_STMT
  243. /* Evaluate EXPECTED and ACTUAL and compare them with maybe_ne, calling
  244. ::selftest::pass if they might be non-equal,
  245. ::selftest::fail if they are known to be equal. */
  246. #define ASSERT_MAYBE_NE(EXPECTED, ACTUAL) \
  247. ASSERT_MAYBE_NE_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))
  248. /* Like ASSERT_MAYBE_NE, but treat LOC as the effective location of the
  249. selftest. */
  250. #define ASSERT_MAYBE_NE_AT(LOC, EXPECTED, ACTUAL) \
  251. SELFTEST_BEGIN_STMT \
  252. const char *desc = "ASSERT_MAYBE_NE (" #EXPECTED ", " #ACTUAL ")"; \
  253. if (maybe_ne (EXPECTED, ACTUAL)) \
  254. ::selftest::pass ((LOC), desc); \
  255. else \
  256. ::selftest::fail ((LOC), desc); \
  257. SELFTEST_END_STMT
  258. /* Evaluate LHS and RHS and compare them with >, calling
  259. ::selftest::pass if LHS > RHS,
  260. ::selftest::fail otherwise. */
  261. #define ASSERT_GT(LHS, RHS) \
  262. ASSERT_GT_AT ((SELFTEST_LOCATION), (LHS), (RHS))
  263. /* Like ASSERT_GT, but treat LOC as the effective location of the
  264. selftest. */
  265. #define ASSERT_GT_AT(LOC, LHS, RHS) \
  266. SELFTEST_BEGIN_STMT \
  267. const char *desc_ = "ASSERT_GT (" #LHS ", " #RHS ")"; \
  268. if ((LHS) > (RHS)) \
  269. ::selftest::pass ((LOC), desc_); \
  270. else \
  271. ::selftest::fail ((LOC), desc_); \
  272. SELFTEST_END_STMT
  273. /* Evaluate LHS and RHS and compare them with <, calling
  274. ::selftest::pass if LHS < RHS,
  275. ::selftest::fail otherwise. */
  276. #define ASSERT_LT(LHS, RHS) \
  277. ASSERT_LT_AT ((SELFTEST_LOCATION), (LHS), (RHS))
  278. /* Like ASSERT_LT, but treat LOC as the effective location of the
  279. selftest. */
  280. #define ASSERT_LT_AT(LOC, LHS, RHS) \
  281. SELFTEST_BEGIN_STMT \
  282. const char *desc_ = "ASSERT_LT (" #LHS ", " #RHS ")"; \
  283. if ((LHS) < (RHS)) \
  284. ::selftest::pass ((LOC), desc_); \
  285. else \
  286. ::selftest::fail ((LOC), desc_); \
  287. SELFTEST_END_STMT
  288. /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
  289. ::selftest::pass if they are equal,
  290. ::selftest::fail if they are non-equal. */
  291. #define ASSERT_STREQ(EXPECTED, ACTUAL) \
  292. SELFTEST_BEGIN_STMT \
  293. ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
  294. (EXPECTED), (ACTUAL)); \
  295. SELFTEST_END_STMT
  296. /* Like ASSERT_STREQ, but treat LOC as the effective location of the
  297. selftest. */
  298. #define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL) \
  299. SELFTEST_BEGIN_STMT \
  300. ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL, \
  301. (EXPECTED), (ACTUAL)); \
  302. SELFTEST_END_STMT
  303. /* Evaluate HAYSTACK and NEEDLE and use strstr to determine if NEEDLE
  304. is within HAYSTACK.
  305. ::selftest::pass if NEEDLE is found.
  306. ::selftest::fail if it is not found. */
  307. #define ASSERT_STR_CONTAINS(HAYSTACK, NEEDLE) \
  308. SELFTEST_BEGIN_STMT \
  309. ::selftest::assert_str_contains (SELFTEST_LOCATION, #HAYSTACK, #NEEDLE, \
  310. (HAYSTACK), (NEEDLE)); \
  311. SELFTEST_END_STMT
  312. /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
  313. ::selftest::fail if it is false. */
  314. #define ASSERT_PRED1(PRED1, VAL1) \
  315. SELFTEST_BEGIN_STMT \
  316. const char *desc_ = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
  317. bool actual_ = (PRED1) (VAL1); \
  318. if (actual_) \
  319. ::selftest::pass (SELFTEST_LOCATION, desc_); \
  320. else \
  321. ::selftest::fail (SELFTEST_LOCATION, desc_); \
  322. SELFTEST_END_STMT
  323. #define SELFTEST_BEGIN_STMT do {
  324. #define SELFTEST_END_STMT } while (0)
  325. #endif /* #if CHECKING_P */
  326. #endif /* GCC_SELFTEST_H */