selftest.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* A self-testing framework, for use by -fself-test.
  2. Copyright (C) 2015-2019 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_val1, const char *desc_val2,
  48. const char *val1, const char *val2);
  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. /* Implementation detail of ASSERT_STR_STARTSWITH. */
  56. extern void assert_str_startswith (const location &loc,
  57. const char *desc_str,
  58. const char *desc_prefix,
  59. const char *val_str,
  60. const char *val_prefix);
  61. /* A named temporary file for use in selftests.
  62. Usable for writing out files, and as the base class for
  63. temp_source_file.
  64. The file is unlinked in the destructor. */
  65. class named_temp_file
  66. {
  67. public:
  68. named_temp_file (const char *suffix);
  69. ~named_temp_file ();
  70. const char *get_filename () const { return m_filename; }
  71. private:
  72. char *m_filename;
  73. };
  74. /* A class for writing out a temporary sourcefile for use in selftests
  75. of input handling. */
  76. class temp_source_file : public named_temp_file
  77. {
  78. public:
  79. temp_source_file (const location &loc, const char *suffix,
  80. const char *content);
  81. };
  82. /* RAII-style class for avoiding introducing locale-specific differences
  83. in strings containing localized quote marks, by temporarily overriding
  84. the "open_quote" and "close_quote" globals to something hardcoded.
  85. Specifically, the C locale's values are used:
  86. - open_quote becomes "`"
  87. - close_quote becomes "'"
  88. for the lifetime of the object. */
  89. class auto_fix_quotes
  90. {
  91. public:
  92. auto_fix_quotes ();
  93. ~auto_fix_quotes ();
  94. private:
  95. const char *m_saved_open_quote;
  96. const char *m_saved_close_quote;
  97. };
  98. /* Various selftests involving location-handling require constructing a
  99. line table and one or more line maps within it.
  100. For maximum test coverage we want to run these tests with a variety
  101. of situations:
  102. - line_table->default_range_bits: some frontends use a non-zero value
  103. and others use zero
  104. - the fallback modes within line-map.c: there are various threshold
  105. values for location_t beyond line-map.c changes
  106. behavior (disabling of the range-packing optimization, disabling
  107. of column-tracking). We can exercise these by starting the line_table
  108. at interesting values at or near these thresholds.
  109. The following struct describes a particular case within our test
  110. matrix. */
  111. struct line_table_case;
  112. /* A class for overriding the global "line_table" within a selftest,
  113. restoring its value afterwards. At most one instance of this
  114. class can exist at once, due to the need to keep the old value
  115. of line_table as a GC root. */
  116. class line_table_test
  117. {
  118. public:
  119. /* Default constructor. Override "line_table", using sane defaults
  120. for the temporary line_table. */
  121. line_table_test ();
  122. /* Constructor. Override "line_table", using the case described by C. */
  123. line_table_test (const line_table_case &c);
  124. /* Destructor. Restore the saved line_table. */
  125. ~line_table_test ();
  126. };
  127. /* Run TESTCASE multiple times, once for each case in our test matrix. */
  128. extern void
  129. for_each_line_table_case (void (*testcase) (const line_table_case &));
  130. /* Read the contents of PATH into memory, returning a 0-terminated buffer
  131. that must be freed by the caller.
  132. Fail (and abort) if there are any problems, with LOC as the reported
  133. location of the failure. */
  134. extern char *read_file (const location &loc, const char *path);
  135. /* A helper function for writing tests that interact with the
  136. garbage collector. */
  137. extern void forcibly_ggc_collect ();
  138. /* Convert a path relative to SRCDIR/gcc/testsuite/selftests
  139. to a real path (either absolute, or relative to pwd).
  140. The result should be freed by the caller. */
  141. extern char *locate_file (const char *path);
  142. /* The path of SRCDIR/testsuite/selftests. */
  143. extern const char *path_to_selftest_files;
  144. /* selftest::test_runner is an implementation detail of selftest::run_tests,
  145. exposed here to allow plugins to run their own suites of tests. */
  146. class test_runner
  147. {
  148. public:
  149. test_runner (const char *name);
  150. ~test_runner ();
  151. private:
  152. const char *m_name;
  153. long m_start_time;
  154. };
  155. /* Declarations for specific families of tests (by source file), in
  156. alphabetical order. */
  157. extern void attribute_c_tests ();
  158. extern void bitmap_c_tests ();
  159. extern void cgraph_c_tests ();
  160. extern void convert_c_tests ();
  161. extern void diagnostic_c_tests ();
  162. extern void diagnostic_show_locus_c_tests ();
  163. extern void dumpfile_c_tests ();
  164. extern void edit_context_c_tests ();
  165. extern void et_forest_c_tests ();
  166. extern void fibonacci_heap_c_tests ();
  167. extern void fold_const_c_tests ();
  168. extern void function_tests_c_tests ();
  169. extern void ggc_tests_c_tests ();
  170. extern void gimple_c_tests ();
  171. extern void hash_map_tests_c_tests ();
  172. extern void hash_set_tests_c_tests ();
  173. extern void input_c_tests ();
  174. extern void json_cc_tests ();
  175. extern void opt_problem_cc_tests ();
  176. extern void optinfo_emit_json_cc_tests ();
  177. extern void predict_c_tests ();
  178. extern void pretty_print_c_tests ();
  179. extern void read_rtl_function_c_tests ();
  180. extern void rtl_tests_c_tests ();
  181. extern void sbitmap_c_tests ();
  182. extern void selftest_c_tests ();
  183. extern void simplify_rtx_c_tests ();
  184. extern void spellcheck_c_tests ();
  185. extern void spellcheck_tree_c_tests ();
  186. extern void sreal_c_tests ();
  187. extern void store_merging_c_tests ();
  188. extern void tree_c_tests ();
  189. extern void tree_cfg_c_tests ();
  190. extern void typed_splay_tree_c_tests ();
  191. extern void unique_ptr_tests_cc_tests ();
  192. extern void vec_c_tests ();
  193. extern void vec_perm_indices_c_tests ();
  194. extern void wide_int_cc_tests ();
  195. extern void opt_proposer_c_tests ();
  196. extern int num_passes;
  197. } /* end of namespace selftest. */
  198. /* Macros for writing tests. */
  199. /* Evaluate EXPR and coerce to bool, calling
  200. ::selftest::pass if it is true,
  201. ::selftest::fail if it false. */
  202. #define ASSERT_TRUE(EXPR) \
  203. ASSERT_TRUE_AT (SELFTEST_LOCATION, (EXPR))
  204. /* Like ASSERT_TRUE, but treat LOC as the effective location of the
  205. selftest. */
  206. #define ASSERT_TRUE_AT(LOC, EXPR) \
  207. SELFTEST_BEGIN_STMT \
  208. const char *desc_ = "ASSERT_TRUE (" #EXPR ")"; \
  209. bool actual_ = (EXPR); \
  210. if (actual_) \
  211. ::selftest::pass ((LOC), desc_); \
  212. else \
  213. ::selftest::fail ((LOC), desc_); \
  214. SELFTEST_END_STMT
  215. /* Evaluate EXPR and coerce to bool, calling
  216. ::selftest::pass if it is false,
  217. ::selftest::fail if it true. */
  218. #define ASSERT_FALSE(EXPR) \
  219. ASSERT_FALSE_AT (SELFTEST_LOCATION, (EXPR))
  220. /* Like ASSERT_FALSE, but treat LOC as the effective location of the
  221. selftest. */
  222. #define ASSERT_FALSE_AT(LOC, EXPR) \
  223. SELFTEST_BEGIN_STMT \
  224. const char *desc_ = "ASSERT_FALSE (" #EXPR ")"; \
  225. bool actual_ = (EXPR); \
  226. if (actual_) \
  227. ::selftest::fail ((LOC), desc_); \
  228. else \
  229. ::selftest::pass ((LOC), desc_); \
  230. SELFTEST_END_STMT
  231. /* Evaluate VAL1 and VAL2 and compare them with ==, calling
  232. ::selftest::pass if they are equal,
  233. ::selftest::fail if they are non-equal. */
  234. #define ASSERT_EQ(VAL1, VAL2) \
  235. ASSERT_EQ_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
  236. /* Like ASSERT_EQ, but treat LOC as the effective location of the
  237. selftest. */
  238. #define ASSERT_EQ_AT(LOC, VAL1, VAL2) \
  239. SELFTEST_BEGIN_STMT \
  240. const char *desc_ = "ASSERT_EQ (" #VAL1 ", " #VAL2 ")"; \
  241. if ((VAL1) == (VAL2)) \
  242. ::selftest::pass ((LOC), desc_); \
  243. else \
  244. ::selftest::fail ((LOC), desc_); \
  245. SELFTEST_END_STMT
  246. /* Evaluate VAL1 and VAL2 and compare them with known_eq, calling
  247. ::selftest::pass if they are always equal,
  248. ::selftest::fail if they might be non-equal. */
  249. #define ASSERT_KNOWN_EQ(VAL1, VAL2) \
  250. ASSERT_KNOWN_EQ_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
  251. /* Like ASSERT_KNOWN_EQ, but treat LOC as the effective location of the
  252. selftest. */
  253. #define ASSERT_KNOWN_EQ_AT(LOC, VAL1, VAL2) \
  254. SELFTEST_BEGIN_STMT \
  255. const char *desc = "ASSERT_KNOWN_EQ (" #VAL1 ", " #VAL2 ")"; \
  256. if (known_eq (VAL1, VAL2)) \
  257. ::selftest::pass ((LOC), desc); \
  258. else \
  259. ::selftest::fail ((LOC), desc); \
  260. SELFTEST_END_STMT
  261. /* Evaluate VAL1 and VAL2 and compare them with !=, calling
  262. ::selftest::pass if they are non-equal,
  263. ::selftest::fail if they are equal. */
  264. #define ASSERT_NE(VAL1, VAL2) \
  265. SELFTEST_BEGIN_STMT \
  266. const char *desc_ = "ASSERT_NE (" #VAL1 ", " #VAL2 ")"; \
  267. if ((VAL1) != (VAL2)) \
  268. ::selftest::pass (SELFTEST_LOCATION, desc_); \
  269. else \
  270. ::selftest::fail (SELFTEST_LOCATION, desc_); \
  271. SELFTEST_END_STMT
  272. /* Evaluate VAL1 and VAL2 and compare them with maybe_ne, calling
  273. ::selftest::pass if they might be non-equal,
  274. ::selftest::fail if they are known to be equal. */
  275. #define ASSERT_MAYBE_NE(VAL1, VAL2) \
  276. ASSERT_MAYBE_NE_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
  277. /* Like ASSERT_MAYBE_NE, but treat LOC as the effective location of the
  278. selftest. */
  279. #define ASSERT_MAYBE_NE_AT(LOC, VAL1, VAL2) \
  280. SELFTEST_BEGIN_STMT \
  281. const char *desc = "ASSERT_MAYBE_NE (" #VAL1 ", " #VAL2 ")"; \
  282. if (maybe_ne (VAL1, VAL2)) \
  283. ::selftest::pass ((LOC), desc); \
  284. else \
  285. ::selftest::fail ((LOC), desc); \
  286. SELFTEST_END_STMT
  287. /* Evaluate LHS and RHS and compare them with >, calling
  288. ::selftest::pass if LHS > RHS,
  289. ::selftest::fail otherwise. */
  290. #define ASSERT_GT(LHS, RHS) \
  291. ASSERT_GT_AT ((SELFTEST_LOCATION), (LHS), (RHS))
  292. /* Like ASSERT_GT, but treat LOC as the effective location of the
  293. selftest. */
  294. #define ASSERT_GT_AT(LOC, LHS, RHS) \
  295. SELFTEST_BEGIN_STMT \
  296. const char *desc_ = "ASSERT_GT (" #LHS ", " #RHS ")"; \
  297. if ((LHS) > (RHS)) \
  298. ::selftest::pass ((LOC), desc_); \
  299. else \
  300. ::selftest::fail ((LOC), desc_); \
  301. SELFTEST_END_STMT
  302. /* Evaluate LHS and RHS and compare them with <, calling
  303. ::selftest::pass if LHS < RHS,
  304. ::selftest::fail otherwise. */
  305. #define ASSERT_LT(LHS, RHS) \
  306. ASSERT_LT_AT ((SELFTEST_LOCATION), (LHS), (RHS))
  307. /* Like ASSERT_LT, but treat LOC as the effective location of the
  308. selftest. */
  309. #define ASSERT_LT_AT(LOC, LHS, RHS) \
  310. SELFTEST_BEGIN_STMT \
  311. const char *desc_ = "ASSERT_LT (" #LHS ", " #RHS ")"; \
  312. if ((LHS) < (RHS)) \
  313. ::selftest::pass ((LOC), desc_); \
  314. else \
  315. ::selftest::fail ((LOC), desc_); \
  316. SELFTEST_END_STMT
  317. /* Evaluate VAL1 and VAL2 and compare them with strcmp, calling
  318. ::selftest::pass if they are equal (and both are non-NULL),
  319. ::selftest::fail if they are non-equal, or are both NULL. */
  320. #define ASSERT_STREQ(VAL1, VAL2) \
  321. SELFTEST_BEGIN_STMT \
  322. ::selftest::assert_streq (SELFTEST_LOCATION, #VAL1, #VAL2, \
  323. (VAL1), (VAL2)); \
  324. SELFTEST_END_STMT
  325. /* Like ASSERT_STREQ, but treat LOC as the effective location of the
  326. selftest. */
  327. #define ASSERT_STREQ_AT(LOC, VAL1, VAL2) \
  328. SELFTEST_BEGIN_STMT \
  329. ::selftest::assert_streq ((LOC), #VAL1, #VAL2, \
  330. (VAL1), (VAL2)); \
  331. SELFTEST_END_STMT
  332. /* Evaluate HAYSTACK and NEEDLE and use strstr to determine if NEEDLE
  333. is within HAYSTACK.
  334. ::selftest::pass if NEEDLE is found.
  335. ::selftest::fail if it is not found. */
  336. #define ASSERT_STR_CONTAINS(HAYSTACK, NEEDLE) \
  337. SELFTEST_BEGIN_STMT \
  338. ::selftest::assert_str_contains (SELFTEST_LOCATION, #HAYSTACK, #NEEDLE, \
  339. (HAYSTACK), (NEEDLE)); \
  340. SELFTEST_END_STMT
  341. /* Like ASSERT_STR_CONTAINS, but treat LOC as the effective location of the
  342. selftest. */
  343. #define ASSERT_STR_CONTAINS_AT(LOC, HAYSTACK, NEEDLE) \
  344. SELFTEST_BEGIN_STMT \
  345. ::selftest::assert_str_contains (LOC, #HAYSTACK, #NEEDLE, \
  346. (HAYSTACK), (NEEDLE)); \
  347. SELFTEST_END_STMT
  348. /* Evaluate STR and PREFIX and determine if STR starts with PREFIX.
  349. ::selftest::pass if STR does start with PREFIX.
  350. ::selftest::fail if does not, or either is NULL. */
  351. #define ASSERT_STR_STARTSWITH(STR, PREFIX) \
  352. SELFTEST_BEGIN_STMT \
  353. ::selftest::assert_str_startswith (SELFTEST_LOCATION, #STR, #PREFIX, \
  354. (STR), (PREFIX)); \
  355. SELFTEST_END_STMT
  356. /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
  357. ::selftest::fail if it is false. */
  358. #define ASSERT_PRED1(PRED1, VAL1) \
  359. SELFTEST_BEGIN_STMT \
  360. const char *desc_ = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
  361. bool actual_ = (PRED1) (VAL1); \
  362. if (actual_) \
  363. ::selftest::pass (SELFTEST_LOCATION, desc_); \
  364. else \
  365. ::selftest::fail (SELFTEST_LOCATION, desc_); \
  366. SELFTEST_END_STMT
  367. #define SELFTEST_BEGIN_STMT do {
  368. #define SELFTEST_END_STMT } while (0)
  369. #endif /* #if CHECKING_P */
  370. #endif /* GCC_SELFTEST_H */