onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 1 | #include "doctest.h" |
| 2 | |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 3 | #include <vector> |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 4 | |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 5 | // typedefs are required if variadic macro support is not available (otherwise the commas are a problem) |
| 6 | typedef doctest::Types<char, short, int> int_types; |
onqtam | eca6585 | 2017-08-11 14:58:00 +0300 | [diff] [blame] | 7 | typedef doctest::Types<double, double> float_types; // note that types won't be filtered for uniqueness |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 8 | |
| 9 | // ================================================================================================= |
| 10 | // NORMAL TEMPLATED TEST CASES |
| 11 | // ================================================================================================= |
| 12 | |
| 13 | TEST_CASE_TEMPLATE("signed integers stuff", T, int_types) { |
| 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 | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 22 | TEST_CASE_TEMPLATE("vector stuff", T, doctest::Types<std::vector<int> >) { |
| 23 | T vec(10); |
| 24 | CHECK(vec.size() == 20); // fill 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 | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 36 | TEST_CASE_TEMPLATE_INSTANTIATE(test_id, int_types); |
| 37 | TEST_CASE_TEMPLATE_INSTANTIATE(test_id, float_types); |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [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 | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 50 | typedef doctest::Types< |
| 51 | TypePair<int, char>, |
| 52 | TypePair<char, int>, |
| 53 | TypePair<bool, int> |
| 54 | > pairs; |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 55 | |
onqtam | d320ac2 | 2017-04-17 11:44:32 +0300 | [diff] [blame] | 56 | TEST_CASE_TEMPLATE("multiple types", T, pairs) { |
| 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 | |
| 66 | // if variadic macros are supported then "TypePair<int, int>" can be passed directly to the macro (otherwise the commas are a problem) |
| 67 | // currently the string result will be "int_pair" instead of "TypePair<int, int>" because of the way the type stringification works |
| 68 | typedef TypePair<int, int> int_pair; |
| 69 | TYPE_TO_STRING(int_pair); |
| 70 | |
| 71 | TEST_CASE_TEMPLATE("bad stringification of type pair", T, doctest::Types<int_pair>) { |
| 72 | typedef typename T::A T1; |
| 73 | typedef typename T::B T2; |
onqtam | f715325 | 2017-04-19 21:41:31 +0300 | [diff] [blame] | 74 | T1 t1 = T1(); |
| 75 | T2 t2 = T2(); |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 76 | // use T1 and T2 types |
onqtam | c79b754 | 2017-04-19 20:47:13 +0300 | [diff] [blame] | 77 | CHECK(t1 == T1()); |
| 78 | CHECK(t2 != T2()); |
onqtam | 8c31176 | 2017-04-17 23:58:56 +0300 | [diff] [blame] | 79 | } |