-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson_6_ut.cpp
257 lines (204 loc) · 6.88 KB
/
lesson_6_ut.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <string>
#include <vector>
#include <list>
#include <sstream>
#include <thread>
#include <mutex>
#include <memory>
#include <deque>
#include <condition_variable>
#include <chrono>
#include <atomic>
#include "scalgorithm"
#include <gtest/gtest.h>
namespace lesson_6_ns {
namespace detail {
// handle conversion of non-strings to `std::string`
template <typename T>
std::string convert_to_string(T&& t) {
return std::to_string(std::forward<T>(t));
}
// overload to forward mutable c strings
std::string convert_to_string(char* s) {
return std::string(s);
}
// overload to forward const c strings
std::string convert_to_string(const char* s) {
return std::string(s);
}
// overload to forward `std::string`s without converting
std::string convert_to_string(std::string&& s) {
return std::move(s);
}
// lvalue overloads explicitly return a reference to avoid copying
std::string& convert_to_string(std::string& s) {
return s;
}
const std::string& convert_to_string(const std::string& s) {
return s;
}
// Final invocation when no more arguments to concatenate.
std::string concatenate(std::stringstream& ss) {
return ss.str();
}
// handle concatenating an element at a time
template <typename A, typename... As>
std::string concatenate(std::stringstream& ss, A&& a, As&&... as) {
ss << convert_to_string(std::forward<A>(a));
return concatenate(ss, std::forward<As>(as)...); // pass the rest to further calls
}
}
template <typename... As>
std::string concatenate(As&&... as) {
std::stringstream ss; // create a stringstream to be used in detail calls
return detail::concatenate(ss, std::forward<As>(as)...);
}
}
TEST(lesson_6, concatenate) {
using namespace lesson_6_ns;
// concatenate 3 rvalue `std::string`s
{
auto s = concatenate(std::string("foo"), std::string(" "), std::string("faa"));
EXPECT_EQ(std::string("foo faa"), s);
}
// concatenate 2 rvalue `std::string`s and an lvalue `std::string`
{
std::string third("faa");
auto s = concatenate(std::string("foo"), std::string(" "), third);
EXPECT_EQ(std::string("foo faa"), s);
}
// concatenate 1 rvalue `std::string`s, an lvalue `std::string`, and
// a const lvalue `std::string`
{
const std::string first("foo");
std::string third("faa");
auto s = concatenate(first, std::string(" "), third);
EXPECT_EQ(std::string("foo faa"), s);
}
// concatenate 2 `std::string`s and a c-string
{
auto s = concatenate(std::string("foo"), std::string(" "), "faa");
EXPECT_EQ(std::string("foo faa"), s);
}
// concatenate 1 `std::string`s and 2 c-string
{
auto s = concatenate("foo", std::string(" "), "faa");
EXPECT_EQ(std::string("foo faa"), s);
}
// concatenate 3 c-strings
{
auto s = concatenate("foo", " ", "faa");
EXPECT_EQ(std::string("foo faa"), s);
}
// concatenate a mutable c string and a const c string
{
char mutable_c_str[10];
memset(mutable_c_str, 0, sizeof(mutable_c_str));
strncpy(mutable_c_str, "hello", sizeof(mutable_c_str) - 1);
auto s = concatenate(mutable_c_str, " world");
EXPECT_EQ(std::string("hello world"), s);
}
// concatenate an std::string and a number
{
auto s = concatenate(std::string("number "), 3);
EXPECT_EQ(std::string("number 3"), s);
}
// concatenate a number and std::string
{
auto s = concatenate(3, std::string(" is a number"));
EXPECT_EQ(std::string("3 is a number"), s);
}
}
TEST(lesson_6, detail_advance_group) {
std::vector<int> v1{1,2,3};
std::vector<int> v2{4,5,6};
std::vector<int> v3{7,8,9};
auto cur_v1 = v1.begin();
auto cur_v2 = v2.begin();
auto cur_v3 = v3.begin();
EXPECT_EQ(1, *cur_v1);
EXPECT_EQ(4, *cur_v2);
EXPECT_EQ(7, *cur_v3);
sca::detail::advance_group(cur_v1, cur_v2, cur_v3);
EXPECT_EQ(2, *cur_v1);
EXPECT_EQ(5, *cur_v2);
EXPECT_EQ(8, *cur_v3);
sca::detail::advance_group(cur_v1, cur_v2, cur_v3);
EXPECT_EQ(3, *cur_v1);
EXPECT_EQ(6, *cur_v2);
EXPECT_EQ(9, *cur_v3);
sca::detail::advance_group(cur_v1, cur_v2, cur_v3);
EXPECT_EQ(v1.end(), cur_v1);
EXPECT_EQ(v2.end(), cur_v2);
EXPECT_EQ(v3.end(), cur_v3);
}
TEST(lesson_6, each) {
std::vector<int> v1{1,2,3};
std::vector<int> v2{4,5,6};
const std::vector<int> expect{5,7,9};
std::vector<int> out(sca::size(v1));
auto out_it = out.begin();
auto add = [&out_it](int a, int b) {
*out_it = a + b;
++out_it;
};
sca::detail::each(add, v1.begin(), v1.end(), v2.begin());
EXPECT_EQ(expect, out);
out = std::vector<int>(sca::size(v1)); // reset our out vector
out_it = out.begin(); // reset our iterator
// completed algorithm `sca::each()` abstracts the argument iterators
sca::each(add, v1, v2);
EXPECT_EQ(expect, out);
out = std::vector<int>(); // reset and resize our out vector
// don't use iterator in this case
auto add_v2 = [&out](int a, int b) {
out.push_back(a + b);
};
sca::each(add_v2, v1, v2);
EXPECT_EQ(expect, out);
}
TEST(lesson_6, detail_map) {
std::vector<int> v1{1,2,3};
std::vector<int> v2{4,5,6};
{
std::vector<int> out(sca::size(v1));
auto add = [](int a, int b) { return a + b; };
// internal algorithm `sca::detail::map` is similar to
// `std::transform()` except that it can accept iterators to more than 1
// container
sca::detail::map(add, out.begin(), v1.begin(), v1.end(), v2.begin());
std::vector<int> expect{5,7,9};
EXPECT_EQ(expect, out);
}
{
std::vector<std::string> out(sca::size(v1));
auto add_and_stringify = [](int a, int b) { return std::to_string(a + b); };
// internal algorithm `sca::detail::map` is similar to
// `std::transform()` except that it can accept iterators to more than 1
// container
sca::detail::map(add_and_stringify, out.begin(), v1.begin(), v1.end(), v2.begin());
std::vector<std::string> expect{"5","7","9"};
EXPECT_EQ(expect, out);
}
}
TEST(lesson_6, detail_fold) {
std::vector<int> v1{1,2,3};
std::vector<int> v2{4,5,6};
{
// sum 1 vector at a time
auto sum = [](int cur_sum, int new_value) {
return cur_sum + new_value;
};
auto out = sca::detail::fold(sum, 0, v1.begin(), v1.end());
out = sca::detail::fold(sum, out, v2.begin(), v2.end());
EXPECT_EQ(21, out);
}
{
// sum 2 vectors simultaneously
auto sum = [](int cur_sum, int new_value_1, int new_value_2) {
return cur_sum + new_value_1 + new_value_2;
};
auto out = sca::detail::fold(sum, 0, v1.begin(), v1.end(), v2.begin());
EXPECT_EQ(21, out);
}
}