blob: a417091b3f243f44d96f0cb7db3fd82944ad72df [file] [log] [blame]
onqtam3ac4c3f2018-06-01 15:31:06 +03001#include "doctest.h"
2
3// helper for throwing exceptions
4template <typename T>
5int throw_if(bool in, const T& ex) {
6 if(in)
7#ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
8 throw ex;
9#else // DOCTEST_CONFIG_NO_EXCEPTIONS
10 ((void)ex);
11#endif // DOCTEST_CONFIG_NO_EXCEPTIONS
12 return 42;
13}
14
15DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
16#include <ostream>
17#include <sstream>
18DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
19
20#ifndef DOCTEST_CONFIG_DISABLE
21
22// =================================================================================================
23// !!! THESE ARE NOT PROPER EXAMPLES OF LIBRARY USAGE !!! THESE ARE MEANT FOR CODE COVERAGE ONLY !!!
24// =================================================================================================
25
26TEST_CASE("exercising tricky code paths of doctest") {
27 using namespace doctest;
28
29 // trigger code path for comparing the file in "operator<" of SubcaseSignature
30 CHECK(SubcaseSignature("", "a.cpp", 0) < SubcaseSignature("", "b.cpp", 0));
31 // same for String
32 CHECK(String("a.cpp") < String("b.cpp"));
33
34 // trigger code path for string with nullptr
35 String str;
36 const String const_str("omgomgomg");
37 str = const_str.c_str();
38 CHECK(const_str[0] == 'o');
39 CHECK(str.capacity() == 24);
40 CHECK(str.size() == const_str.size());
41 CHECK_MESSAGE(str.compare(const_str, true) != 0, "should fail");
42 CHECK_MESSAGE(str.compare("omgomgomg", false) != 0, "should fail");
43
44 String heap_str("012345678901234567890123456789");
45 CHECK(heap_str.capacity() == heap_str.size() + 1); // on heap with maxed capacity
46 heap_str += "0123456789";
47 CHECK(heap_str.capacity() > heap_str.size() + 1);
48 heap_str += "0123456789"; // triggers path in +=
49 CHECK(heap_str[heap_str.size() - 1] == '9');
50
51 CHECK(String("abc") == "abc");
52 CHECK(String("abc") > "aaa");
53 CHECK(String("abc") >= "aaa");
54 CHECK(String("abc") < "bbb");
55 CHECK(String("abc") <= "bbb");
56 CHECK(String("abc")[0] == 'a');
57
58 // toString
59 str += toString("aaa") //
60 + toString(nullptr) //
61 + toString(true) //
62 + toString(static_cast<unsigned int>(0)) //
63 + toString(0.5f) //
64 + toString(0.5) //
65 + toString(static_cast<long double>(0.1)) //
66 + toString('c') //
67 + toString(static_cast<signed char>('c')) //
68 + toString(static_cast<unsigned char>(1)) //
69 + toString(static_cast<short>(1)) //
70 + toString(static_cast<long>(1)) //
71 + toString(static_cast<unsigned long>(1)) //
72 + toString(static_cast<unsigned short>(1)) //
73 + toString(static_cast<long long>(1)) //
74 + toString(static_cast<unsigned long long>(1));
75
76 std::ostringstream oss;
77
78 // toStream
79 detail::toStream(&oss, true);
80 detail::toStream(&oss, 0.5f);
81 detail::toStream(&oss, 0.5);
82 detail::toStream(&oss, static_cast<long double>(0.1));
83 detail::toStream(&oss, 'c');
84 detail::toStream(&oss, static_cast<signed char>('c'));
85 detail::toStream(&oss, static_cast<unsigned char>(1));
86 detail::toStream(&oss, static_cast<short>(1));
87 detail::toStream(&oss, static_cast<long>(1));
88 detail::toStream(&oss, static_cast<unsigned long>(1));
89 detail::toStream(&oss, static_cast<unsigned short>(1));
90 detail::toStream(&oss, static_cast<long long>(1));
91 detail::toStream(&oss, static_cast<unsigned long long>(1));
92
93 // trigger code path for String to ostream through operator<<
94 oss << str;
95 // trigger code path for assert string of a non-existent assert type
96 oss << assertString(static_cast<assertType::Enum>(3));
97 str += oss.str().c_str();
Thomas Bleheraf39bae2018-09-26 09:22:20 +020098 CHECK(str == "omgomgomgaaaNULLtrue00.5f0.50.199991111111true0.50.50.1cc"
99 "111111omgomgomgaaaNULLtrue00.5f0.50.199991111111");
onqtam3ac4c3f2018-06-01 15:31:06 +0300100 // trigger code path for rawMemoryToString
101 bool isThereAnything = str.size() > 0u;
102 bool len_is_zero = detail::rawMemoryToString(isThereAnything).size() == 0u;
103 String unknown = toString(skip()); // trigger code path for "{?}"
104 str = unknown; // trigger code path for deleting memory in operator=
105 CHECK_MESSAGE(len_is_zero, "should fail");
106
107 Approx a(5);
108 a.scale(4);
109 Approx b = a(7);
110
111 CHECK(b == 5);
112 CHECK(b != 5);
113 CHECK(b > 5);
114 CHECK(b < 5);
115 CHECK(b >= 5);
116 CHECK(b <= 5);
117
118 CHECK(6 == a);
119 CHECK(6 != a);
120 CHECK(6 > a);
121 CHECK(6 < a);
122 CHECK(6 >= a);
123 CHECK(6 <= a);
124
125 // a hack to trigger a bug in doctest: currently a 0 cannot be successfully parsed for an int option!
126 Context().setOption("last", 0);
127}
128
129TEST_SUITE("will be overridden by a decorator" * doctest::test_suite("exception related")) {
130 TEST_CASE("will end from a std::string exception") {
131 throw_if(true, std::string("std::string!"));
132 }
133
134 TEST_CASE("will end from a const char* exception") { throw_if(true, "const char*!"); }
135
136 TEST_CASE("will end from an unknown exception") {
137 throw_if(true, doctest::String("unknown :("));
138 }
139}
140
onqtam3ac4c3f2018-06-01 15:31:06 +0300141#endif // DOCTEST_CONFIG_DISABLE