blob: 34489da52e0f8ca83c6c258e2e272452d9efbf05 [file] [log] [blame]
ncihnegnc5458f22019-01-28 05:08:18 -08001#include <doctest/doctest.h>
onqtam3ac4c3f2018-06-01 15:31:06 +03002
Stefandb758e02022-06-06 03:42:02 +02003#include "header.h"
onqtam3ac4c3f2018-06-01 15:31:06 +03004
5DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
6#include <ostream>
7#include <sstream>
Stefan8acee4f2022-01-11 19:35:14 +01008#include <stdexcept>
onqtam3ac4c3f2018-06-01 15:31:06 +03009DOCTEST_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
17TEST_CASE("exercising tricky code paths of doctest") {
18 using namespace doctest;
19
20 // trigger code path for comparing the file in "operator<" of SubcaseSignature
onqtamdca8b662019-09-22 17:34:50 +030021 CHECK(SubcaseSignature{"", "a.cpp", 0} < SubcaseSignature{"", "b.cpp", 0});
onqtam3ac4c3f2018-06-01 15:31:06 +030022 // 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');
onqtam8d5eafa2019-03-21 12:19:25 +020041 heap_str = "";
onqtam3ac4c3f2018-06-01 15:31:06 +030042
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) //
Stefane8ba7712022-04-28 02:47:24 +020054 + toString(0u) //
onqtam3ac4c3f2018-06-01 15:31:06 +030055 + toString('c') //
56 + toString(static_cast<signed char>('c')) //
57 + toString(static_cast<unsigned char>(1)) //
58 + toString(static_cast<short>(1)) //
Stefane8ba7712022-04-28 02:47:24 +020059 + toString(1L) //
60 + toString(1UL) //
onqtam3ac4c3f2018-06-01 15:31:06 +030061 + toString(static_cast<unsigned short>(1)) //
Stefane8ba7712022-04-28 02:47:24 +020062 + toString(1LL) //
63 + toString(1ULL);
onqtam3ac4c3f2018-06-01 15:31:06 +030064
65 std::ostringstream oss;
66
onqtam3ac4c3f2018-06-01 15:31:06 +030067 // 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
Stefan8acee4f2022-01-11 19:35:14 +010070#ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
71 try {
72 assertString(static_cast<assertType::Enum>(3));
73 } catch (const std::logic_error&) { }
74#endif
onqtam3ac4c3f2018-06-01 15:31:06 +030075 str += oss.str().c_str();
onqtamacbcd122019-03-02 21:24:18 +020076 str += failureString(assertType::is_normal);
Stefane8ba7712022-04-28 02:47:24 +020077 CHECK(str == "omgomgomgaaanullptrtrue099991111111"
78 "omgomgomgaaanullptrtrue099991111111");
onqtam3ac4c3f2018-06-01 15:31:06 +030079 // trigger code path for rawMemoryToString
80 bool isThereAnything = str.size() > 0u;
onqtam3ac4c3f2018-06-01 15:31:06 +030081 String unknown = toString(skip()); // trigger code path for "{?}"
82 str = unknown; // trigger code path for deleting memory in operator=
Stefane8ba7712022-04-28 02:47:24 +020083 CHECK_FALSE_MESSAGE(isThereAnything, "should fail");
onqtam3ac4c3f2018-06-01 15:31:06 +030084
85 Approx a(5);
86 a.scale(4);
87 Approx b = a(7);
88
Stefane8ba7712022-04-28 02:47:24 +020089 CHECK(b == 7);
90 CHECK(b != 6);
91 CHECK(b > 6);
92 CHECK(b < 8);
93 CHECK(b >= 7);
94 CHECK(b <= 7);
onqtam3ac4c3f2018-06-01 15:31:06 +030095
Stefane8ba7712022-04-28 02:47:24 +020096 CHECK(5 == a);
onqtam3ac4c3f2018-06-01 15:31:06 +030097 CHECK(6 != a);
98 CHECK(6 > a);
Stefane8ba7712022-04-28 02:47:24 +020099 CHECK(4 < a);
100 CHECK(5 >= a);
101 CHECK(5 <= a);
onqtam3ac4c3f2018-06-01 15:31:06 +0300102
onqtamacbcd122019-03-02 21:24:18 +0200103 // trigger another single line of code... lol
Stefandb758e02022-06-06 03:42:02 +0200104 // NOLINTBEGIN(cppcoreguidelines-pro-type-const-cast)
onqtamacbcd122019-03-02 21:24:18 +0200105 auto oldVal = const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames;
106 const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames = false;
onqtam15ce2662019-03-15 18:43:17 +0200107 CHECK(String(skipPathFromFilename("")) == "");
onqtamacbcd122019-03-02 21:24:18 +0200108 const_cast<ContextOptions*>(getContextOptions())->no_path_in_filenames = oldVal;
Stefandb758e02022-06-06 03:42:02 +0200109 // NOLINTEND(cppcoreguidelines-pro-type-const-cast)
onqtamacbcd122019-03-02 21:24:18 +0200110
onqtam3ac4c3f2018-06-01 15:31:06 +0300111 // 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
115TEST_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
onqtam3ac4c3f2018-06-01 15:31:06 +0300127#endif // DOCTEST_CONFIG_DISABLE