Skip to content

Commit 0172f42

Browse files
committed
Add unit test on ranges of move only values
1 parent e8c849a commit 0172f42

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

unit/util/range.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,95 @@ SCENARIO("range tests", "[core][util][range]")
6565
}
6666
}
6767
}
68+
69+
class move_onlyt
70+
{
71+
public:
72+
move_onlyt(move_onlyt &&) = default;
73+
move_onlyt &operator=(move_onlyt &&) = default;
74+
move_onlyt(const move_onlyt &) = delete;
75+
move_onlyt &operator=(const move_onlyt &) = delete;
76+
77+
explicit move_onlyt(int value) : value{value} {};
78+
int value = 0;
79+
};
80+
81+
bool is_odd(const move_onlyt &move_only) {
82+
return move_only.value % 2 != 0;
83+
}
84+
85+
SCENARIO(
86+
"Range tests, with collections of move only typed values.",
87+
"[core][util][range]")
88+
{
89+
GIVEN("A vector of move only typed values.")
90+
{
91+
std::vector<move_onlyt> input1;
92+
for(int i = 1; i <= 10; ++i)
93+
input1.emplace_back(i);
94+
std::vector<move_onlyt> input2;
95+
for(int i = 1; i <= 10; ++i)
96+
input2.emplace_back(i);
97+
THEN("Values from a range of made from the vector can be moved.")
98+
{
99+
auto input_range = make_range(input1);
100+
move_onlyt destination{std::move(*input_range.begin())};
101+
REQUIRE(destination.value == 1);
102+
}
103+
THEN("A range of made from the vector can be filtered.")
104+
{
105+
auto odds_filter = make_range(input1).filter(is_odd);
106+
std::size_t total = 0;
107+
for(const auto &move_only : odds_filter)
108+
total += 1;
109+
REQUIRE(total == 5);
110+
auto iterator = odds_filter.begin();
111+
REQUIRE((iterator++)->value == 1);
112+
REQUIRE((iterator++)->value == 3);
113+
REQUIRE((iterator++)->value == 5);
114+
REQUIRE((iterator++)->value == 7);
115+
REQUIRE((iterator++)->value == 9);
116+
}
117+
THEN("Values from a filtered range made from the vector can be moved.")
118+
{
119+
std::vector<move_onlyt> odds;
120+
for(move_onlyt &odd : make_range(input1).filter(is_odd))
121+
odds.emplace_back(std::move(odd));
122+
123+
REQUIRE(odds.size() == 5);
124+
REQUIRE(odds[0].value == 1);
125+
REQUIRE(odds[1].value == 3);
126+
REQUIRE(odds[2].value == 5);
127+
REQUIRE(odds[3].value == 7);
128+
REQUIRE(odds[4].value == 9);
129+
}
130+
THEN("Values from concatenated ranges made from the vector can be moved.")
131+
{
132+
std::vector<move_onlyt> both_inputs;
133+
for(move_onlyt &input : make_range(input1).concat(make_range(input2)))
134+
both_inputs.emplace_back(std::move(input));
135+
136+
REQUIRE(both_inputs.size() == 20);
137+
REQUIRE(both_inputs[0].value == 1);
138+
REQUIRE(both_inputs[1].value == 2);
139+
REQUIRE(both_inputs[2].value == 3);
140+
REQUIRE(both_inputs[3].value == 4);
141+
REQUIRE(both_inputs[4].value == 5);
142+
REQUIRE(both_inputs[5].value == 6);
143+
REQUIRE(both_inputs[6].value == 7);
144+
REQUIRE(both_inputs[7].value == 8);
145+
REQUIRE(both_inputs[8].value == 9);
146+
REQUIRE(both_inputs[9].value == 10);
147+
REQUIRE(both_inputs[10].value == 1);
148+
REQUIRE(both_inputs[11].value == 2);
149+
REQUIRE(both_inputs[12].value == 3);
150+
REQUIRE(both_inputs[13].value == 4);
151+
REQUIRE(both_inputs[14].value == 5);
152+
REQUIRE(both_inputs[15].value == 6);
153+
REQUIRE(both_inputs[16].value == 7);
154+
REQUIRE(both_inputs[17].value == 8);
155+
REQUIRE(both_inputs[18].value == 9);
156+
REQUIRE(both_inputs[19].value == 10);
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)