-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_for_each.cpp
More file actions
56 lines (46 loc) · 1.37 KB
/
Copy pathtest_for_each.cpp
File metadata and controls
56 lines (46 loc) · 1.37 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
#include "util/printing.hpp"
#include <catch2/catch_test_macros.hpp>
#include <iterator>
#include <memory>
#include <string>
#include <string_view>
#include <tuplet/tuple.hpp>
TEST_CASE("Test that for_each works", "[for_each]") {
using tuplet::tuple;
std::string str;
auto ins = std::back_inserter(str);
tuple tup {
"Hello world!",
std::string("This is a test..."),
10,
'x',
20,
3.141592};
tup.for_each([&](auto& value) { fmt::format_to(ins, "{}\n", value); });
REQUIRE(
str
== "Hello world!\n" //
"This is a test...\n" //
"10\n" //
"x\n" //
"20\n" //
"3.141592\n");
}
TEST_CASE(
"Test that for_each moves values when given moved-from tuple",
"[for_each]") {
using tuplet::tuple;
auto tup = tuple {
std::make_unique<int>(10),
std::make_unique<int>(20),
std::make_unique<int>(30)};
SECTION("Check that the tuple is valid") {
REQUIRE(tup.all([](auto& val) { return bool(val); }));
}
int sum = 0;
std::move(tup).for_each([&](auto value) { sum += *value; });
SECTION("check that the tuple was moved from and the sum is 60") {
REQUIRE(tup.all([](auto& val) { return bool(val) == false; }));
REQUIRE(sum == 60);
}
}