onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 1 | #include "doctest.h" |
| 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 | be46017 | 2018-05-30 18:15:58 +0300 | [diff] [blame] | 34 | TEST_CASE_TEMPLATE_INSTANTIATE(test_id, signed char, short, int); |
| 35 | TEST_CASE_TEMPLATE_INSTANTIATE(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 | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 37 | // ================================================================================================= |
| 38 | // MULTIPLE TYPES AS PARAMETERS |
| 39 | // ================================================================================================= |
| 40 | |
| 41 | template <typename first, typename second> |
| 42 | struct TypePair |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 43 | { |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 44 | typedef first A; |
| 45 | typedef second B; |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 46 | }; |
| 47 | |
onqtam | be46017 | 2018-05-30 18:15:58 +0300 | [diff] [blame] | 48 | #define pairs \ |
| 49 | TypePair<int, char>, \ |
| 50 | TypePair<char, int>, \ |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 51 | TypePair<bool, int> |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 52 | |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 53 | TEST_CASE_TEMPLATE("multiple types", T, pairs) { |
| 54 | typedef typename T::A T1; |
| 55 | typedef typename T::B T2; |
onqtam | f715325 | 2017-04-19 21:41:31 +0300 | [diff] [blame] | 56 | T1 t1 = T1(); |
| 57 | T2 t2 = T2(); |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 58 | // use T1 and T2 types |
onqtam | c79b754 | 2017-04-19 20:47:13 +0300 | [diff] [blame] | 59 | CHECK(t1 == T1()); |
| 60 | CHECK(t2 != T2()); |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 61 | } |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 62 | |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 63 | // currently the string result will be "int_pair" instead of "TypePair<int, int>" because of the way the type stringification works |
| 64 | typedef TypePair<int, int> int_pair; |
| 65 | TYPE_TO_STRING(int_pair); |
| 66 | |
onqtam | be46017 | 2018-05-30 18:15:58 +0300 | [diff] [blame] | 67 | TEST_CASE_TEMPLATE("bad stringification of type pair", T, int_pair) { |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 68 | typedef typename T::A T1; |
| 69 | typedef typename T::B T2; |
onqtam | f715325 | 2017-04-19 21:41:31 +0300 | [diff] [blame] | 70 | T1 t1 = T1(); |
| 71 | T2 t2 = T2(); |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 72 | // use T1 and T2 types |
onqtam | c79b754 | 2017-04-19 20:47:13 +0300 | [diff] [blame] | 73 | CHECK(t1 == T1()); |
| 74 | CHECK(t2 != T2()); |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 75 | } |