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

coverage_maxout.cpp (4770B)


      1 #include <doctest/doctest.h>
      2 
      3 #include "header.h"
      4 
      5 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
      6 #include <ostream>
      7 #include <sstream>
      8 #include <stdexcept>
      9 DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
     10 
     11 #ifndef DOCTEST_CONFIG_DISABLE
     12 
     13 // =================================================================================================
     14 // !!! THESE ARE NOT PROPER EXAMPLES OF LIBRARY USAGE !!! THESE ARE MEANT FOR CODE COVERAGE ONLY !!!
     15 // =================================================================================================
     16 
     17 TEST_CASE("exercising tricky code paths of doctest") {
     18     using namespace doctest;
     19 
     20     // trigger code path for comparing the file in "operator<" of SubcaseSignature
     21     CHECK(SubcaseSignature{"", "a.cpp", 0} < SubcaseSignature{"", "b.cpp", 0});
     22     // same for String
     23     CHECK(String("a.cpp") < String("b.cpp"));
     24 
     25     // trigger code path for string with nullptr
     26     String       str;
     27     const String const_str("omgomgomg");
     28     str = const_str.c_str();
     29     CHECK(const_str[0] == 'o');
     30     CHECK(str.capacity() == 24);
     31     CHECK(str.size() == const_str.size());
     32     CHECK_MESSAGE(str.compare(const_str, true) != 0, "should fail");
     33     CHECK_MESSAGE(str.compare("omgomgomg", false) != 0, "should fail");
     34 
     35     String heap_str("012345678901234567890123456789");
     36     CHECK(heap_str.capacity() == heap_str.size() + 1); // on heap with maxed capacity
     37     heap_str += "0123456789";
     38     CHECK(heap_str.capacity() > heap_str.size() + 1);
     39     heap_str += "0123456789"; // triggers path in +=
     40     CHECK(heap_str[heap_str.size() - 1] == '9');
     41     heap_str = "";
     42 
     43     CHECK(String("abc") == "abc");
     44     CHECK(String("abc") > "aaa");
     45     CHECK(String("abc") >= "aaa");
     46     CHECK(String("abc") < "bbb");
     47     CHECK(String("abc") <= "bbb");
     48     CHECK(String("abc")[0] == 'a');
     49 
     50     // toString
     51     str += toString("aaa")                            //
     52            + toString(nullptr)                        //
     53            + toString(true)                           //
     54            + toString(0u)                             //
     55            + toString('c')                            //
     56            + toString(static_cast<signed char>('c'))  //
     57            + toString(static_cast<unsigned char>(1))  //
     58            + toString(static_cast<short>(1))          //
     59            + toString(1L)                             //
     60            + toString(1UL)                            //
     61            + toString(static_cast<unsigned short>(1)) //
     62            + toString(1LL)                            //
     63            + toString(1ULL);
     64 
     65     std::ostringstream oss;
     66 
     67     // trigger code path for String to ostream through operator<<
     68     oss << str;
     69     // trigger code path for assert string of a non-existent assert type
     70 #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
     71     try {
     72         assertString(static_cast<assertType::Enum>(3));
     73     } catch (const std::logic_error&) { }
     74 #endif
     75     str += oss.str().c_str();
     76     str += failureString(assertType::is_normal);
     77     CHECK(str == "omgomgomgaaanullptrtrue099991111111"
     78                  "omgomgomgaaanullptrtrue099991111111");
     79     // trigger code path for rawMemoryToString
     80     bool   isThereAnything = str.size() > 0u;
     81     String unknown         = toString(skip()); // trigger code path for "{?}"
     82     str                    = unknown;          // trigger code path for deleting memory in operator=
     83     CHECK_FALSE_MESSAGE(isThereAnything, "should fail");
     84 
     85     Approx a(5);
     86     a.scale(4);
     87     Approx b = a(7);
     88 
     89     CHECK(b == 7);
     90     CHECK(b != 6);
     91     CHECK(b > 6);
     92     CHECK(b < 8);
     93     CHECK(b >= 7);
     94     CHECK(b <= 7);
     95 
     96     CHECK(5 == a);
     97     CHECK(6 != a);
     98     CHECK(6 > a);
     99     CHECK(4 < a);
    100     CHECK(5 >= a);
    101     CHECK(5 <= a);
    102 
    103     // trigger another single line of code... lol
    104     // NOLINTBEGIN(cppcoreguidelines-pro-type-const-cast)
    105     auto oldVal = const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames;
    106     const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames = false;
    107     CHECK(String(skipPathFromFilename("")) == "");
    108     const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames = oldVal;
    109     // NOLINTEND(cppcoreguidelines-pro-type-const-cast)
    110 
    111     // a hack to trigger a bug in doctest: currently a 0 cannot be successfully parsed for an int option!
    112     Context().setOption("last", 0);
    113 }
    114 
    115 TEST_SUITE("will be overridden by a decorator" * doctest::test_suite("exception related")) {
    116     TEST_CASE("will end from a std::string exception") {
    117         throw_if(true, std::string("std::string!"));
    118     }
    119 
    120     TEST_CASE("will end from a const char* exception") { throw_if(true, "const char*!"); }
    121 
    122     TEST_CASE("will end from an unknown exception") {
    123         throw_if(true, doctest::String("unknown :("));
    124     }
    125 }
    126 
    127 #endif // DOCTEST_CONFIG_DISABLE