doctest

FORK: The fastest feature-rich C++11/14/17/20 single-header testing framework
git clone https://git.neptards.moe/neptards/doctest.git
Log | Files | Refs | README

templated_test_cases.cpp (2630B)


      1 #include <doctest/doctest.h>
      2 
      3 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
      4 #include <vector>
      5 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
      6 
      7 // =================================================================================================
      8 // NORMAL TEMPLATED TEST CASES
      9 // =================================================================================================
     10 
     11 TYPE_TO_STRING_AS("SHORT!!!", short);
     12 
     13 TEST_CASE_TEMPLATE("signed integers stuff", T, signed char, short, int) {
     14     T var = T();
     15     --var;
     16     CHECK(var == -1);
     17 }
     18 
     19 // teach the library how to stringify this type - otherwise <> will be used
     20 TYPE_TO_STRING(std::vector<int>);
     21 
     22 TEST_CASE_TEMPLATE("vector stuff", T, std::vector<int>) {
     23     T vec(10);
     24     CHECK(vec.size() == 20); // will fail
     25 }
     26 
     27 // =================================================================================================
     28 // NAMED TEMPLATED TEST CASES WITH DEFERRED INSTANTIATION
     29 // =================================================================================================
     30 
     31 TEST_CASE_TEMPLATE_DEFINE("default construction", T, test_id) {
     32     T var = T();
     33     CHECK(doctest::Approx(var) == T());
     34 }
     35 
     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
     38 
     39 TEST_CASE_TEMPLATE_APPLY(test_id, std::tuple<unsigned char, char>);
     40 
     41 // =================================================================================================
     42 // MULTIPLE TYPES AS PARAMETERS
     43 // =================================================================================================
     44 
     45 template <typename first, typename second>
     46 struct TypePair
     47 {
     48     using A = first;
     49     using B = second;
     50 };
     51 
     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 
     56 TEST_CASE_TEMPLATE("multiple types", T, TypePair<int, char>, TypePair<char, int>, TypePair<bool, int>) {
     57     using T1 = typename T::A;
     58     using T2 = typename T::B;
     59     T1 t1 = T1();
     60     T2 t2 = T2();
     61     // use T1 and T2 types
     62     CHECK(t1 == T1());
     63     CHECK(t2 != T2());
     64 }
     65 
     66 // currently the string result will be "int_pair" instead of "TypePair<int, int>" because of the way the type stringification works
     67 using int_pair = TypePair<int, int>;
     68 TYPE_TO_STRING(int_pair);
     69 
     70 TEST_CASE_TEMPLATE("bad stringification of type pair", T, int_pair) {
     71     using T1 = typename T::A;
     72     using T2 = typename T::B;
     73     T1 t1 = T1();
     74     T2 t2 = T2();
     75     // use T1 and T2 types
     76     CHECK(t1 == T1());
     77     CHECK(t2 != T2());
     78 }