-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.cpp
More file actions
122 lines (95 loc) · 3.17 KB
/
main.cpp
File metadata and controls
122 lines (95 loc) · 3.17 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
#include "adjacent.hpp"
#include "cartesian_product.hpp"
#include "transform.hpp"
#include "zip.hpp"
#include "algorithm.hpp"
#include "variadic.hpp"
#include <iostream>
#include <sstream>
#include <vector>
namespace
{
int multiplyBy2(int i)
{
return i * 2;
}
bool testTransform()
{
std::vector<int> input = {1, 2, 3, 4, 5};
std::vector<int> expected = { 2, 4, 6, 8, 10 };
std::vector<int> result;
auto doubledNumbers = input | ranges::view::transform(multiplyBy2);
push_back(result, doubledNumbers);
return result == expected;
}
std::string letterPlusNumber(char line, int column)
{
std::ostringstream position;
position << line << column;
return position.str();
}
bool testZip()
{
std::vector<char> input1 = {'A', 'B', 'C', 'D', 'E'};
std::vector<int> input2 = {1, 2, 3, 4, 5};
std::vector<std::string> expected = { "A1", "B2", "C3", "D4", "E5" };
std::vector<std::string> result;
push_back(result, ranges::view::zip(input1, input2) | ranges::view::transform(tupled_args(letterPlusNumber)));
return result == expected;
}
bool testCartesianProduct()
{
std::vector<char> input1 = {'A', 'B', 'C', 'D', 'E'};
std::vector<int> input2 = {1, 2, 3, 4, 5};
std::vector<std::string> expected = { "A1", "A2", "A3", "A4", "A5",
"B1", "B2", "B3", "B4", "B5",
"C1", "C2", "C3", "C4", "C5",
"D1", "D2", "D3", "D4", "D5",
"E1", "E2", "E3", "E4", "E5" };
std::vector<std::string> result;
push_back(result, ranges::view::cartesian_product(input1, input2) | ranges::view::transform(tupled_args(letterPlusNumber)));
return result == expected;
}
std::string letterToNext(char letter, char next)
{
std::ostringstream result;
result << letter << '-' << next;
return result.str();
}
bool testAdjacentAdaptor()
{
std::vector<char> input = { 'A', 'B', 'C', 'D', 'E', 'F' };
std::vector<std::string> expected = { "A-B", "B-C", "C-D", "D-E", "E-F" };
std::vector<std::string> result;
push_back(result, input | ranges::view::adjacent | ranges::view::transform(paired_args(letterToNext)));
return result == expected;
}
bool testConsecutiveFunction()
{
std::vector<char> input = { 'A', 'B', 'C', 'D', 'E', 'F' };
std::vector<std::string> expected = { "A-B", "B-C", "C-D", "D-E", "E-F" };
std::vector<std::string> result;
push_back(result, ranges::view::consecutive(input) | ranges::view::transform(paired_args(letterToNext)));
return result == expected;
}
bool testAdjacent()
{
return testAdjacentAdaptor() && testConsecutiveFunction();
}
template <typename Function>
std::string testResult(Function test)
{
return test() ? "PASSED" : "FAILED";
}
void launchTests()
{
std::cout << "TEST TRANSFORM\t\t" << testResult(testTransform) << std::endl;
std::cout << "TEST ZIP\t\t" << testResult(testZip) << std::endl;
std::cout << "TEST CARTESIAN_PRODUCT\t\t" << testResult(testCartesianProduct) << std::endl;
std::cout << "TEST ADJACENT\t\t" << testResult(testAdjacent) << std::endl;
}
}
int main()
{
launchTests();
}