-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdStream_Test.cpp
More file actions
295 lines (254 loc) · 8.08 KB
/
Copy pathStdStream_Test.cpp
File metadata and controls
295 lines (254 loc) · 8.08 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* \file main.cpp
* \brief
*/
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include "StdStream.h"
//-------------------------------------------------------------------------------------------------
#define TEST(expr) \
do { \
if (!(expr)) { \
std::cerr << "TEST failed: " #expr \
<< "\nFile: " << __FILE__ \
<< "\nLine: " << __LINE__ << '\n'; \
std::abort(); \
} \
} while (false)
//-------------------------------------------------------------------------------------------------
template<typename T>
class TestAllocator
{
public:
using value_type = T;
TestAllocator() = default;
template<typename OtherT>
TestAllocator(const TestAllocator<OtherT> &)
{
}
T * allocate(std::size_t count)
{
return std::allocator<T>().allocate(count);
}
void deallocate(T *ptr, std::size_t count)
{
std::allocator<T>().deallocate(ptr, count);
}
};
template<typename T1, typename T2>
bool operator == (const TestAllocator<T1> &, const TestAllocator<T2> &)
{
return true;
}
template<typename T1, typename T2>
bool operator != (const TestAllocator<T1> &, const TestAllocator<T2> &)
{
return false;
}
//-------------------------------------------------------------------------------------------------
struct IntHash
{
std::size_t operator () (int value) const
{
return std::hash<int>()(value);
}
};
struct IntEqual
{
bool operator () (int lhs, int rhs) const
{
return lhs == rhs;
}
};
//-------------------------------------------------------------------------------------------------
template<typename ValueT>
std::string toString(const ValueT &value)
{
std::ostringstream os;
os << value;
return os.str();
}
void testContains(const std::string &value, const std::string &part)
{
TEST(value.find(part) != std::string::npos);
}
//-------------------------------------------------------------------------------------------------
void testPair()
{
const std::pair<std::string, std::string> emptyValue;
TEST(toString(emptyValue) == "std::pair: {,}");
const std::pair<std::string, std::string> value {"aaa", "111"};
TEST(toString(value) == "std::pair: {aaa,111}");
}
//-------------------------------------------------------------------------------------------------
void testSequenceContainers()
{
{
std::vector<std::string> emptyValue;
TEST(toString(emptyValue) == "std::vector (size=0): {}");
std::vector<std::string, TestAllocator<std::string>> value {"aaa", "bbb", "ccc"};
TEST(toString(value) == "std::vector (size=3): {aaa,bbb,ccc}");
}
{
std::list<std::string, TestAllocator<std::string>> value {"aaa1", "bbb2", "ccc3"};
TEST(toString(value) == "std::list (size=3): {aaa1,bbb2,ccc3}");
}
{
std::deque<std::string, TestAllocator<std::string>> value {"aaa", "bbb", "ccc"};
TEST(toString(value) == "std::deque (size=3): {aaa,bbb,ccc}");
}
{
std::forward_list<std::string, TestAllocator<std::string>> value {"aaa", "bbb", "ccc"};
TEST(toString(value) == "std::forward_list (size=3): {aaa,bbb,ccc}");
}
{
std::array<std::string, 0> emptyValue;
TEST(toString(emptyValue) == "std::array (size=0): {}");
std::array<std::string, 3> value {"a", "b", "c"};
TEST(toString(value) == "std::array (size=3): {a,b,c}");
}
}
//-------------------------------------------------------------------------------------------------
void testOrderedAssociativeContainers()
{
{
std::set<int, std::greater<int>, TestAllocator<int>> value {1, 2, 2, 3};
TEST(toString(value) == "std::set (size=3): {3,2,1}");
}
{
std::multiset<int, std::greater<int>, TestAllocator<int>> value {1, 2, 2, 3};
TEST(toString(value) == "std::multiset (size=4): {3,2,2,1}");
}
{
using ValueT = std::pair<const int, int>;
std::map<int, int, std::greater<int>, TestAllocator<ValueT>> value {{1, 10}, {2, 20}};
TEST(toString(value) == "std::map (size=2): {std::pair: {2,20}\nstd::pair: {1,10}}");
}
{
using ValueT = std::pair<const int, int>;
std::multimap<int, int, std::greater<int>, TestAllocator<ValueT>> value {{1, 10}, {2, 20}, {2, 22}};
TEST(toString(value) == "std::multimap (size=3): {std::pair: {2,20}\nstd::pair: {2,22}\nstd::pair: {1,10}}");
}
}
//-------------------------------------------------------------------------------------------------
void testUnorderedAssociativeContainers()
{
{
using ValueT = std::pair<const int, int>;
std::unordered_map<int, int, IntHash, IntEqual, TestAllocator<ValueT>> value {{1, 10}, {2, 20}};
const std::string out = toString(value);
testContains(out, "std::unordered_map (size=2): {");
testContains(out, "std::pair: {1,10}");
testContains(out, "std::pair: {2,20}");
}
{
using ValueT = std::pair<const int, int>;
std::unordered_multimap<int, int, IntHash, IntEqual, TestAllocator<ValueT>> value {{1, 10}, {2, 20}, {2, 22}};
const std::string out = toString(value);
testContains(out, "std::unordered_multimap (size=3): {");
testContains(out, "std::pair: {1,10}");
testContains(out, "std::pair: {2,20}");
testContains(out, "std::pair: {2,22}");
}
{
std::unordered_set<int, IntHash, IntEqual, TestAllocator<int>> value {1, 2, 2, 3};
const std::string out = toString(value);
testContains(out, "std::unordered_set (size=3): {");
testContains(out, "1");
testContains(out, "2");
testContains(out, "3");
}
{
std::unordered_multiset<int, IntHash, IntEqual, TestAllocator<int>> value {1, 2, 2, 3};
const std::string out = toString(value);
testContains(out, "std::unordered_multiset (size=4): {");
testContains(out, "1");
testContains(out, "2");
testContains(out, "3");
}
}
//-------------------------------------------------------------------------------------------------
void testContainerAdaptors()
{
{
std::queue<std::string, std::list<std::string>> value;
value.push("aaa");
value.push("bbb");
value.push("ccc");
TEST(toString(value) == "std::queue (size=3): {aaa,bbb,ccc}");
TEST(value.size() == 3);
TEST(value.front() == "aaa");
TEST(value.back() == "ccc");
}
{
std::priority_queue<int, std::vector<int>, std::greater<int>> value;
value.push(3);
value.push(1);
value.push(2);
TEST(toString(value) == "std::priority_queue (size=3): {1,2,3}");
TEST(value.size() == 3);
TEST(value.top() == 1);
}
{
std::stack<std::string, std::vector<std::string>> value;
value.push("aaa");
value.push("bbb");
value.push("ccc");
TEST(toString(value) == "std::stack (size=3): {aaa,bbb,ccc}");
TEST(value.size() == 3);
TEST(value.top() == "ccc");
}
}
//-------------------------------------------------------------------------------------------------
void testTuple()
{
const std::tuple<std::string, double, int, char> value = std::make_tuple("test", 3.1, 14, 'y');
TEST(toString(value) == "std::tuple (size=4): {test,3.1,14,y}");
const std::tuple<> emptyValue;
TEST(toString(emptyValue) == "std::tuple (size=0): {}");
}
//-------------------------------------------------------------------------------------------------
void testTracePtr()
{
{
std::string *value {};
std::ostringstream os;
os << STD_TRACE_PTR(value);
const std::string out = os.str();
testContains(out, "value: {");
testContains(out, "nullptr");
}
{
std::string data {"abc"};
std::string *value {&data};
std::ostringstream os;
os << STD_TRACE_PTR(value);
const std::string out = os.str();
testContains(out, "value: {");
testContains(out, ", abc}");
}
{
volatile int data {7};
volatile int *value {&data};
std::ostringstream os;
os << STD_TRACE_PTR(value);
const std::string out = os.str();
testContains(out, "value: {");
testContains(out, ", 7}");
}
}
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
testPair();
testSequenceContainers();
testOrderedAssociativeContainers();
testUnorderedAssociativeContainers();
testContainerAdaptors();
testTuple();
testTracePtr();
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------