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