-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_reduce.cpp
More file actions
100 lines (77 loc) · 3.19 KB
/
test_reduce.cpp
File metadata and controls
100 lines (77 loc) · 3.19 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
#include <stdio.h>
#include <enumerable/enumerable.h>
#include <vector>
#include <string>
#include <gtest/gtest.h>
TEST(Reduce, SeedFromParameter)
{
int array[5] = { 1,2,3,4,5 };
EXPECT_EQ(15, Enumerable(array).reduce([](int i, int sum) { return i + sum; }, 0));
EXPECT_EQ(25, Enumerable(array).reduce([](int i, int sum) { return i + sum; }, 10));
}
TEST(Reduce, SeedFromLambdaDefaultParameter)
{
int array[5] = { 1,2,3,4,5 };
EXPECT_EQ(15, Enumerable(array).reduce([](int i, int sum = 0) { return i + sum; }));
EXPECT_EQ(25, Enumerable(array).reduce([](int i, int sum = 0) { return i + sum; }, 10));
}
TEST(Reduce, NoSeed)
{
int array[5] = { 1,2,3,4,5 };
EXPECT_EQ(15, Enumerable(array).reduce([](int i, int sum) { return i + sum; }));
}
TEST(Reduce, StringReduceTestLambda)
{
std::vector<std::string> stringVector = {
"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"
};
// Using normal seed
auto reducedWhitespace = Enumerable(stringVector).reduce([](auto str, std::string sum) { return sum + " " + str; }, "");
EXPECT_EQ(std::string(" The quick brown fox jumps over the lazy dog"), reducedWhitespace);
// Using seed from lambda default
auto reducedComma = Enumerable(stringVector).reduce([](std::string str, std::string sum="") { return sum + "," + str; });
EXPECT_EQ(std::string(",The,quick,brown,fox,jumps,over,the,lazy,dog"), reducedComma);
// First element is seed
auto reducedWhitespaceNoSeed = Enumerable(stringVector).reduce([](auto str, std::string sum) { return sum + " " + str; });
EXPECT_EQ(std::string("The quick brown fox jumps over the lazy dog"), reducedWhitespaceNoSeed);
}
std::string ReduceStringWhitespace(std::string str, std::string sum)
{
return sum + " " + str;
}
std::string ReduceStringComma(std::string str, std::string sum = "")
{
return sum + "," + str;
}
TEST(Reduce, StringReduceTestFixedFunc)
{
std::vector<std::string> stringVector = {
"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"
};
// Using normal seed
auto reducedWhitespace = Enumerable(stringVector).reduce(ReduceStringWhitespace, "");
EXPECT_EQ(std::string(" The quick brown fox jumps over the lazy dog"), reducedWhitespace);
// Seed is used from first element, even though the function has a default argument.
auto reducedComma = Enumerable(stringVector).reduce(ReduceStringComma);
EXPECT_EQ(std::string("The,quick,brown,fox,jumps,over,the,lazy,dog"), reducedComma);
// First element is seed
auto reducedWhitespaceNoSeed = Enumerable(stringVector).reduce(ReduceStringWhitespace);
EXPECT_EQ(std::string("The quick brown fox jumps over the lazy dog"), reducedWhitespaceNoSeed);
}
TEST(Reduce, EmptySequenceExceptionTest)
{
std::vector<std::string> stringVector = {
};
// Using normal seed
ASSERT_THROW(
Enumerable(stringVector).reduce([](auto str, std::string sum) { return sum + " " + str; }, "")
, std::out_of_range);
// Using seed from lambda default
ASSERT_THROW(
Enumerable(stringVector).reduce([](std::string str, std::string sum="") { return sum + "," + str; })
, std::out_of_range);
// First element is seed
ASSERT_THROW(
Enumerable(stringVector).reduce([](auto str, std::string sum) { return sum + " " + str; })
, std::out_of_range);
}