ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 2 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 3 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 4 | #include <vector> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 5 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 6 | |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 7 | // ================================================================================================= |
| 8 | // NORMAL TEMPLATED TEST CASES |
| 9 | // ================================================================================================= |
| 10 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 11 | TYPE_TO_STRING_AS("SHORT!!!", short); |
| 12 | |
onqtam | be46017 | 2018-05-30 18:15:58 +0300 | [diff] [blame] | 13 | TEST_CASE_TEMPLATE("signed integers stuff", T, signed char, short, int) { |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 14 | T var = T(); |
| 15 | --var; |
| 16 | CHECK(var == -1); |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 17 | } |
| 18 | |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 19 | // teach the library how to stringify this type - otherwise <> will be used |
| 20 | TYPE_TO_STRING(std::vector<int>); |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 21 | |
onqtam | be46017 | 2018-05-30 18:15:58 +0300 | [diff] [blame] | 22 | TEST_CASE_TEMPLATE("vector stuff", T, std::vector<int>) { |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 23 | T vec(10); |
Viktor Kirilov | 09f55ca | 2018-03-05 19:34:25 +0700 | [diff] [blame] | 24 | CHECK(vec.size() == 20); // will fail |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 25 | } |
| 26 | |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 27 | // ================================================================================================= |
| 28 | // NAMED TEMPLATED TEST CASES WITH DEFERRED INSTANTIATION |
| 29 | // ================================================================================================= |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 30 | |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 31 | TEST_CASE_TEMPLATE_DEFINE("default construction", T, test_id) { |
| 32 | T var = T(); |
| 33 | CHECK(doctest::Approx(var) == T()); |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 34 | } |
| 35 | |
onqtam | 88911e3 | 2019-03-20 21:00:05 +0200 | [diff] [blame] | 36 | TEST_CASE_TEMPLATE_INVOKE(test_id, signed char, short, int); |
| 37 | TEST_CASE_TEMPLATE_INVOKE(test_id, double, double); // note that types won't be filtered for uniqueness |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 38 | |
onqtam | 88911e3 | 2019-03-20 21:00:05 +0200 | [diff] [blame] | 39 | TEST_CASE_TEMPLATE_APPLY(test_id, std::tuple<unsigned char, char>); |
zhihaoy | 73ff612 | 2019-03-20 12:37:17 -0500 | [diff] [blame] | 40 | |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 41 | // ================================================================================================= |
| 42 | // MULTIPLE TYPES AS PARAMETERS |
| 43 | // ================================================================================================= |
| 44 | |
| 45 | template <typename first, typename second> |
| 46 | struct TypePair |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 47 | { |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 48 | typedef first A; |
| 49 | typedef second B; |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 50 | }; |
| 51 | |
Stefan | e8ba771 | 2022-04-28 02:47:24 +0200 | [diff] [blame] | 52 | TYPE_TO_STRING_AS("Custom name test", TypePair<int, char>); |
| 53 | TYPE_TO_STRING_AS("Other custom name", TypePair<char, int>); |
| 54 | TYPE_TO_STRING(TypePair<bool, int>); |
| 55 | |
onqtam | 4317c82 | 2018-05-30 19:18:55 +0300 | [diff] [blame] | 56 | TEST_CASE_TEMPLATE("multiple types", T, TypePair<int, char>, TypePair<char, int>, TypePair<bool, int>) { |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 57 | typedef typename T::A T1; |
| 58 | typedef typename T::B T2; |
onqtam | f715325 | 2017-04-19 21:41:31 +0300 | [diff] [blame] | 59 | T1 t1 = T1(); |
| 60 | T2 t2 = T2(); |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 61 | // use T1 and T2 types |
onqtam | c79b754 | 2017-04-19 20:47:13 +0300 | [diff] [blame] | 62 | CHECK(t1 == T1()); |
| 63 | CHECK(t2 != T2()); |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 64 | } |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 65 | |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 66 | // currently the string result will be "int_pair" instead of "TypePair<int, int>" because of the way the type stringification works |
| 67 | typedef TypePair<int, int> int_pair; |
| 68 | TYPE_TO_STRING(int_pair); |
| 69 | |
onqtam | be46017 | 2018-05-30 18:15:58 +0300 | [diff] [blame] | 70 | TEST_CASE_TEMPLATE("bad stringification of type pair", T, int_pair) { |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 71 | typedef typename T::A T1; |
| 72 | typedef typename T::B T2; |
onqtam | f715325 | 2017-04-19 21:41:31 +0300 | [diff] [blame] | 73 | T1 t1 = T1(); |
| 74 | T2 t2 = T2(); |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 75 | // use T1 and T2 types |
onqtam | c79b754 | 2017-04-19 20:47:13 +0300 | [diff] [blame] | 76 | CHECK(t1 == T1()); |
| 77 | CHECK(t2 != T2()); |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 78 | } |