forked from ad-freiburg/qlever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceptionTest.cpp
173 lines (151 loc) · 5.61 KB
/
ExceptionTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2023, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Johannes Kalmbach <[email protected]>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "./util/GTestHelpers.h"
#include "util/Algorithm.h"
#include "util/Exception.h"
using namespace ad_utility;
namespace {
auto makeMatcher = [](const std::string& condition,
ad_utility::source_location l =
ad_utility::source_location::current()) {
using namespace ::testing;
auto line = l.line();
auto e = [](size_t l) { return EndsWith(std::to_string(l)); };
auto approximateLineMatcher =
AnyOf(e(line - 5), e(line - 4), e(line - 3), e(line - 2), e(line - 1),
e(line), e(line + 1), e(line + 2));
return AllOf(HasSubstr(condition), HasSubstr(l.file_name()),
approximateLineMatcher);
};
void checkContains(const std::exception& e, std::string_view substring) {
ASSERT_TRUE(ad_utility::contains(std::string_view{e.what()}, substring));
}
} // namespace
TEST(Exception, AbortException) {
AbortException a{std::runtime_error{"errorA"}};
ASSERT_STREQ(a.what(), "errorA");
AbortException b{"errorB"};
ASSERT_STREQ(b.what(), "errorB");
}
TEST(Exception, Exception) {
ad_utility::source_location l = ad_utility::source_location::current();
Exception e{"exceptionE"};
ASSERT_THAT(e.what(), ::testing::StartsWith("exceptionE"));
ASSERT_THAT(e.what(), ::testing::EndsWith(std::to_string(l.line() + 1)));
checkContains(e, l.file_name());
}
TEST(Exception, AD_THROW) {
ad_utility::source_location l;
try {
l = ad_utility::source_location::current();
AD_THROW("anError");
FAIL() << "No exception was thrown, but one was expected";
} catch (const Exception& e) {
ASSERT_THAT(e.what(), ::testing::StartsWith("anError"));
ASSERT_THAT(e.what(), ::testing::EndsWith(std::to_string(l.line() + 1)));
checkContains(e, l.file_name());
}
}
TEST(Exception, AD_CONTRACT_CHECK) {
ASSERT_NO_THROW(AD_CONTRACT_CHECK(3 < 5));
std::vector<int> v;
ASSERT_NO_THROW(AD_CONTRACT_CHECK(v.empty()));
auto failCheck = [] { AD_CONTRACT_CHECK(3 > 5); };
AD_EXPECT_THROW_WITH_MESSAGE(failCheck(), makeMatcher("3 > 5"));
}
TEST(Exception, contractCheckWithMessage) {
ASSERT_NO_THROW(AD_CONTRACT_CHECK(3 < 5, "some message"));
ASSERT_NO_THROW(
AD_CONTRACT_CHECK(3 < 5, [x = "someMessage"]() { return x; }));
std::vector<int> v;
v.push_back(27);
auto failCheck = [&v] { AD_CONTRACT_CHECK(v.empty(), "`v` must be empty"); };
AD_EXPECT_THROW_WITH_MESSAGE(
(failCheck()),
makeMatcher("Assertion `v.empty()` failed. `v` must be empty. Please"));
auto failCheck2 = [&v] {
AD_CONTRACT_CHECK(v.empty(),
[&v] { return absl::StrCat("`v` has size ", v.size()); });
};
AD_EXPECT_THROW_WITH_MESSAGE(
(failCheck2()),
makeMatcher("Assertion `v.empty()` failed. `v` has size 1. Please"));
auto failCheck3 = [&v] {
AD_CONTRACT_CHECK(v.empty(), "`v` has size ", v.size(), " and not 0");
};
AD_EXPECT_THROW_WITH_MESSAGE(
(failCheck3()),
makeMatcher(
"Assertion `v.empty()` failed. `v` has size 1 and not 0. Please"));
// No additional message, check that no additional full stops or spaces are
// inserted.
auto failCheck4 = [&v] { AD_CONTRACT_CHECK(v.empty()); };
AD_EXPECT_THROW_WITH_MESSAGE(
(failCheck4()),
makeMatcher("Assertion `v.empty()` failed. Please report"));
}
TEST(Exception, AD_CORRECTNESS_CHECK) {
ASSERT_NO_THROW(AD_CORRECTNESS_CHECK(3 < 5));
std::vector<int> v;
ASSERT_NO_THROW(AD_CORRECTNESS_CHECK(v.empty()));
v.push_back(27);
auto failCheck = [&v] { AD_CONTRACT_CHECK(v.empty()); };
AD_EXPECT_THROW_WITH_MESSAGE(failCheck(), makeMatcher("v.empty()"));
}
TEST(Exception, correctnessCheckWithMessage) {
ASSERT_NO_THROW(AD_CORRECTNESS_CHECK(3 < 5, "some message"));
ASSERT_NO_THROW(
AD_CORRECTNESS_CHECK(3 < 5, [x = "someMessage"]() { return x; }));
std::vector<int> v;
v.push_back(27);
auto failCheck = [&v] {
AD_CORRECTNESS_CHECK(v.empty(), "`v` must be empty");
};
AD_EXPECT_THROW_WITH_MESSAGE(
(failCheck()),
makeMatcher("Assertion `v.empty()` failed. `v` must be empty. Please"));
auto failCheck2 = [&v] {
AD_CORRECTNESS_CHECK(
v.empty(), [&v] { return absl::StrCat("`v` has size ", v.size()); });
};
AD_EXPECT_THROW_WITH_MESSAGE(
(failCheck2()),
makeMatcher("Assertion `v.empty()` failed. `v` has size 1. Please"));
auto failCheck3 = [&v] {
AD_CORRECTNESS_CHECK(v.empty(), "`v` has size ", v.size(), " and not 0");
};
AD_EXPECT_THROW_WITH_MESSAGE(
(failCheck3()),
makeMatcher(
"Assertion `v.empty()` failed. `v` has size 1 and not 0. Please"));
// No additional message, check that no additional full stops or spaces are
// inserted.
auto failCheck4 = [&v] { AD_CORRECTNESS_CHECK(v.empty()); };
AD_EXPECT_THROW_WITH_MESSAGE(
(failCheck4()),
makeMatcher("Assertion `v.empty()` failed. Please report"));
}
TEST(Exception, AD_FAIL) {
try {
AD_FAIL();
FAIL() << "No exception was thrown, but one was expected";
} catch (const Exception& e) {
ASSERT_THAT(e.what(),
::testing::StartsWith("This code should be unreachable."));
}
}
TEST(Exception, AD_EXPENSIVE_CHECK) {
#if (!defined(NDEBUG) || defined(AD_ENABLE_EXPENSIVE_CHECKS))
ASSERT_ANY_THROW(AD_EXPENSIVE_CHECK(3 > 5));
#else
ASSERT_NO_THROW(AD_EXPENSIVE_CHECK(3 > 5));
#endif
if (ad_utility::areExpensiveChecksEnabled) {
ASSERT_ANY_THROW(AD_EXPENSIVE_CHECK(3 > 5));
} else {
ASSERT_NO_THROW(AD_EXPENSIVE_CHECK(3 > 5));
}
}