-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathSeqTests.cpp
More file actions
177 lines (144 loc) · 5.2 KB
/
Copy pathSeqTests.cpp
File metadata and controls
177 lines (144 loc) · 5.2 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <catch2/catch.hpp>
#include <rapidcheck/catch.h>
#include "rapidcheck/Seq.h"
#include "util/Generators.h"
#include "util/TemplateProps.h"
#include "util/Logger.h"
using namespace rc;
using namespace rc::test;
namespace {
class LoggingSeqImpl : public Logger {
public:
LoggingSeqImpl(std::string theId)
: Logger(std::move(theId)) {}
Maybe<std::pair<std::string, std::vector<std::string>>> operator()() {
return {{id, log}};
}
};
using LoggingSeq = Seq<std::pair<std::string, std::vector<std::string>>>;
}
TEST_CASE("Seq") {
SECTION("default constructed Seq is empty") {
REQUIRE_FALSE(Seq<int>().next());
}
SECTION("Nothing constructed Seq is empty") {
REQUIRE_FALSE(Seq<int>(Nothing).next());
}
SECTION("calls operator()() of the implementation object") {
bool nextCalled = false;
Seq<int> seq = Seq<int>([&] {
nextCalled = true;
return Maybe<int>(1337);
});
REQUIRE(*seq.next() == 1337);
REQUIRE(nextCalled);
}
SECTION("self assignment leaves value unchanged") {
const auto seq = seq::just(1, 2);
auto x = seq;
auto &ref = x;
x = ref;
REQUIRE(x == seq);
}
SECTION("copies implementation if constructed from lvalue") {
LoggingSeqImpl impl("foobar");
LoggingSeq seq(impl);
const auto value = seq.next();
std::vector<std::string> expectedLog{"constructed as foobar",
"copy constructed"};
REQUIRE(value->first == "foobar");
REQUIRE(value->second == expectedLog);
}
SECTION("moves implementation if constructed from rvalue") {
LoggingSeq seq(LoggingSeqImpl("foobar"));
const auto value = seq.next();
std::vector<std::string> expectedLog{"constructed as foobar",
"move constructed"};
REQUIRE(value->first == "foobar");
REQUIRE(value->second == expectedLog);
}
SECTION("copy construction copies the implementation object") {
LoggingSeq original(LoggingSeqImpl("foobar"));
auto copy(original);
const auto value = copy.next();
std::vector<std::string> expectedLog{
"constructed as foobar", "move constructed", "copy constructed"};
REQUIRE(value->first == "foobar");
REQUIRE(value->second == expectedLog);
}
SECTION("copy assignment copies the implementation object") {
LoggingSeq original(LoggingSeqImpl("foobar"));
LoggingSeq copy;
copy = original;
const auto value = copy.next();
std::vector<std::string> expectedLog{
"constructed as foobar", "move constructed", "copy constructed"};
REQUIRE(value->first == "foobar");
REQUIRE(value->second == expectedLog);
}
SECTION("move construction neither moves nor copies") {
LoggingSeq original(LoggingSeqImpl("foobar"));
LoggingSeq moved(std::move(original));
const auto value = moved.next();
std::vector<std::string> expectedLog{"constructed as foobar",
"move constructed"};
REQUIRE(value->first == "foobar");
REQUIRE(value->second == expectedLog);
}
SECTION("move assignment neither moves nor copies") {
LoggingSeq original(LoggingSeqImpl("foobar"));
LoggingSeq moved;
moved = std::move(original);
const auto value = moved.next();
std::vector<std::string> expectedLog{"constructed as foobar",
"move constructed"};
REQUIRE(value->first == "foobar");
REQUIRE(value->second == expectedLog);
}
SECTION("if exception is throw on next(), Seq ends immediately") {
auto x = 0;
const auto seq = Seq<int>([x]() mutable -> Maybe<int> {
if (x == 3) {
throw std::string("foobar");
}
return ++x;
});
REQUIRE(seq == seq::just(1, 2, 3));
}
SECTION("operator==/operator!=") {
propConformsToEquals<Seq<std::string>>();
SECTION("empty sequences are equal") { REQUIRE(Seq<int>() == Seq<int>()); }
SECTION("an exhausted sequence equals an originally empty sequence") {
auto seq = seq::just(1, 2, 3);
seq.next();
seq.next();
seq.next();
REQUIRE(seq == Seq<int>());
}
prop("sequences with different implementation classes can be equal",
[](const std::string &a, const std::string &b, const std::string &c) {
auto seqJust = seq::just(a, b, c);
std::vector<std::string> vec{a, b, c};
auto seqContainer = seq::fromContainer(vec);
RC_ASSERT(seqJust == seqContainer);
});
prop("changing a single element leads to inequal sequences",
[] {
const auto elements1 = *gen::nonEmpty<std::vector<std::string>>();
auto elements2 = elements1;
const auto i = *gen::inRange<std::size_t>(0, elements2.size());
elements2[i] = *gen::distinctFrom(elements2[i]);
RC_ASSERT(seq::fromContainer(elements1) !=
seq::fromContainer(elements2));
});
}
SECTION("makeSeq") {
SECTION("constructs implementation object in place") {
auto seq = makeSeq<LoggingSeqImpl>("foobar");
const auto value = seq.next();
REQUIRE(value->first == "foobar");
std::vector<std::string> expectedLog{"constructed as foobar"};
REQUIRE(value->second == expectedLog);
}
}
}