-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjust_simple_test.cpp
More file actions
150 lines (119 loc) · 3.92 KB
/
just_simple_test.cpp
File metadata and controls
150 lines (119 loc) · 3.92 KB
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
#include "simple_test.h"
#include <cassert>
#include <vector>
#include <cxxabi.h>
TEST(should_fail, vector_capacity) {
std::vector<int> xs;
xs.reserve(100500);
xs.clear();
EXPECT_CMP(xs.capacity(), ==, 0ul) << " ahaha ? ahaha!";
ASSERT_CMP(xs.size(), ==, 1ul) << " ahaha ? ahaha!";
}
TEST(should_fail, lots_of_failed_expectations) {
EXPECT_CMP(123, ==, 456);
EXPECT_BOOL(true, false);
EXPECT_STRCMP("hello", >, "world");
EXPECT_STRCMP("hello\0one", !=, "hello\0two");
std::cout << "some went wrong (or not...)" << std::endl;
}
TEST(should_fail, some_assertion_failed) {
ASSERT_CMP(123, ==, 456);
assert(false); // unreachable
}
TEST(should_fail, raised_exception) {
throw std::runtime_error("ooo");
assert(false); // unreachable
}
TEST(MUST_SKIP, some_disabled, false) {
assert(false); // unreachable
}
TEST(should_fail, some_fault_1) {
ASSERTION_FAULT() << "comment " << 123 << " goes here";
assert(false); // unreachable
}
TEST(should_fail, some_fault_2) {
ASSERTION_FAULT();
assert(false); // unreachable
}
TEST(simple_test, some_correct) {
const char h1[] = "hello";
const char h2[] = "hello";
EXPECT_CMP(h1, !=, h2); // different addresses
EXPECT_STRCMP(h1, ==, h2); // equal content
EXPECT_STRCMP("hello", <, "world");
EXPECT_STRCMP("hello\0one", ==, "hello\0two"); // difference beyond trailing zero
std::cout << "all right!" << std::endl;
}
TEST(simple_test, do_not_eval_twice) {
int counter = 0;
ASSERT_CMP(counter++, ==, 0);
ASSERT_CMP(++counter, ==, 2);
ASSERT_CMP(2, ==, counter++);
ASSERT_CMP(4, ==, ++counter);
}
struct D {
static const int TRASH = 123456789;
static int g_x;
explicit D(int x) { g_x = x; }
D(const D&) = delete;
void operator=(const D&) = delete;
~D() { g_x = TRASH; }
const int& cref() const { return g_x; }
};
int D::g_x = D::TRASH;
TEST(simple_test, do_not_make_dangling_references) {
EXPECT_CMP(D(123).cref(), ==, 123);
const int& d = D(456).cref();
EXPECT_CMP(d, !=, 456);
}
TEST(simple_test, compare_floats_explicitly) {
EXPECT_CMP(123.456, ==, simple_test::nearly_abs(123.4, 0.1));
EXPECT_CMP(123.456, ==, simple_test::nearly_abs(123.5, 0.1));
EXPECT_CMP(123.456, <, simple_test::nearly_abs(123.6, 0.1));
EXPECT_CMP(123.456, >, simple_test::nearly_abs(123.3, 0.1));
EXPECT_CMP(123.456, <=, simple_test::nearly_abs(123.4, 0.1));
EXPECT_CMP(123.456, >=, simple_test::nearly_abs(123.5, 0.1));
}
TEST(simple_test, compare_floats_implicitly) {
const auto pivot = 123.4;
const auto epsilon = 0.1;
const auto tiny = 0.001;
EXPECT_FLOATCMP(pivot, ==, pivot - epsilon, epsilon);
EXPECT_FLOATCMP(pivot, ==, pivot + epsilon, epsilon);
EXPECT_FLOATCMP(pivot, !=, pivot - epsilon - tiny, epsilon);
EXPECT_FLOATCMP(pivot, !=, pivot + epsilon + tiny, epsilon);
EXPECT_FLOATCMP(pivot, >, pivot - epsilon - tiny, epsilon);
EXPECT_FLOATCMP(pivot, <, pivot + epsilon + tiny, epsilon);
EXPECT_FLOATCMP(pivot, <=, pivot - epsilon, epsilon);
EXPECT_FLOATCMP(pivot, >=, pivot + epsilon, epsilon);
}
TEST(gtest_like, variety) {
EXPECT_EQ(123, 123);
EXPECT_STREQ("aaa\0bbb", "aaa\0ccc");
EXPECT_TRUE(1 == 1);
EXPECT_FALSE(1 == 2);
EXPECT_NEAR(123.4, 123.5, 0.1);
}
TEST(should_fail, strings) {
EXPECT_EQ("aaa\x11", "aaa\x12") << simple_print::verbose("bbb\x13");
}
SHOW_GREEN_ASSERTIONS(true); // global flag
TEST(green, visible_1) {
EXPECT_TRUE("You must see this") << "and this";
}
TEST(green, visible_2) {
EXPECT_TRUE("You must see this") << "and this";
}
SHOW_GREEN_ASSERTIONS(false); // global flag
TEST(green, invisible_3) {
EXPECT_TRUE("You must NOT see this") << "nor this";
SHOW_GREEN_ASSERTIONS(true);
EXPECT_TRUE("You must see this") << "and this";
SHOW_GREEN_ASSERTIONS(false);
EXPECT_TRUE("You must NOT see this") << "nor this";
SHOW_GREEN_ASSERTIONS(true); // local flag is ignored outside the test
}
TEST(green, invisible_4) {
EXPECT_TRUE("You must NOT see this") << "nor this";
}
TESTING_MAIN()