-
Notifications
You must be signed in to change notification settings - Fork 6
/
plf_stack_test_suite.cpp
507 lines (357 loc) · 11.1 KB
/
plf_stack_test_suite.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__)
#if _MSC_VER >= 1600
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#endif
#if _MSC_VER >= 1700
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#endif
#if _MSC_VER >= 1800
#define PLF_TEST_VARIADICS_SUPPORT // Variadics, in this context, means both variadic templates and variadic macros are supported
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#if defined(_MSVC_LANG) && (_MSVC_LANG >= 202002L) && _MSC_VER >= 1929
#define PLF_TEST_CPP20_SUPPORT
#endif
#elif defined(__cplusplus) && __cplusplus >= 201103L // C++11 support, at least
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && !defined(__clang__) // If compiler is GCC/G++
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4 // 4.2 and below do not support variadic templates
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#define PLF_TEST_VARIADICS_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4 // 4.3 and below do not support initializer lists
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#if __GNUC__ >= 5 // GCC v4.9 and below do not support std::is_trivially_copyable
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#endif
#elif defined(__clang__) && !defined(__GLIBCXX__) && !defined(_LIBCPP_CXX03_LANG)
#if __clang_major__ >= 3 // clang versions < 3 don't support __has_feature() or traits
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#if __has_feature(cxx_rvalue_references) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#endif
#if __has_feature(cxx_variadic_templates) && !defined(_LIBCPP_HAS_NO_VARIADICS)
#define PLF_TEST_VARIADICS_SUPPORT
#endif
#if (__clang_major__ == 3 && __clang_minor__ >= 1) || __clang_major__ > 3
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#endif
#elif defined(__GLIBCXX__)
#if __GLIBCXX__ >= 20080606
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#define PLF_TEST_VARIADICS_SUPPORT
#endif
#if __GLIBCXX__ >= 20090421
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#if __GLIBCXX__ >= 20150422
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#endif
#elif !(defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) || defined(_LIBCPP_HAS_NO_VARIADICS))
// Assume full support for other compilers and standard libraries
#define PLF_TEST_VARIADICS_SUPPORT
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#if __cplusplus > 201704L && ((((defined(__clang__) && !defined(__APPLE_CC__) && __clang_major__ >= 14) || (defined(__GNUC__) && (__GNUC__ > 11 || (__GNUC__ == 11 && __GNUC_MINOR__ > 0)))) && ((defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 14) || (defined(__GLIBCXX__) && __GLIBCXX__ >= 201806L))) || (!defined(__clang__) && !defined(__GNUC__)))
#define PLF_TEST_CPP20_SUPPORT
#endif
#endif
#include <cstdio> // log redirection
#include <cstdlib> // abort
#ifdef PLF_TEST_MOVE_SEMANTICS_SUPPORT
#include <utility> // std::move
#endif
#include "plf_stack.h"
void title1(const char *title_text)
{
printf("\n\n\n*** %s ***\n", title_text);
printf("===========================================\n\n\n");
}
void title2(const char *title_text)
{
printf("\n\n--- %s ---\n\n", title_text);
}
void failpass(const char *test_type, bool condition)
{
printf("%s: ", test_type);
if (condition)
{
printf("Pass\n");
}
else
{
printf("Fail\n");
getchar();
abort();
}
}
#ifdef PLF_TEST_VARIADICS_SUPPORT
struct perfect_forwarding_test
{
const bool success;
perfect_forwarding_test(int && /*perfect1*/, int& perfect2)
: success(true)
{
perfect2 = 1;
}
template <typename T, typename U>
perfect_forwarding_test(T&& /*imperfect1*/, U&& /*imperfect2*/)
: success(false)
{}
};
#endif
int main()
{
freopen("error.log","w", stderr);
using namespace std;
using namespace plf;
unsigned int looper = 0;
while (++looper != 50)
{
{
title1("Test basics");
stack<unsigned int> i_stack(50);
for (unsigned int temp = 0; temp != 250000; ++temp)
{
i_stack.push(10);
}
failpass("Multipush test", i_stack.size() == 250000);
stack<unsigned int> i_stack2;
i_stack2 = i_stack;
stack<unsigned int> i_stack3(i_stack);
failpass("Copy constructor test", i_stack3.size() == 250000);
stack<unsigned int> i_stack6(i_stack, i_stack3.get_allocator());
failpass("Allocator-extended copy constructor test", i_stack6.size() == 250000);
i_stack3.reserve(400000);
failpass("Reserve test", i_stack3.size() == 250000);
stack<unsigned int> i_stack7(50, 50);
for (unsigned int temp = 0; temp != 449; ++temp)
{
i_stack7.push(10);
}
failpass("Max limit test", i_stack7.capacity() == 450);
i_stack7.reshape(100, 100);
failpass("Reshape test", i_stack7.capacity() == 500);
#ifdef PLF_TEST_MOVE_SEMANTICS_SUPPORT
stack<unsigned int> i_stack4;
i_stack4 = std::move(i_stack3);
failpass("Move equality operator test", i_stack2 == i_stack4);
stack<unsigned int> i_stack5(std::move(i_stack4), i_stack3.get_allocator());
failpass("Allocator-extended move-construct test", i_stack5.size() == 250000);
i_stack3 = std::move(i_stack5);
#else
failpass("Equality operator test", i_stack2 == i_stack3);
#endif
failpass("Copy test", i_stack2.size() == 250000);
failpass("Equality operator test 2", i_stack == i_stack2);
i_stack2.push(5);
failpass("Inequality operator test", i_stack != i_stack2);
i_stack2.swap(i_stack3);
failpass("Swap test", i_stack2.size() == i_stack3.size() - 1);
swap(i_stack2, i_stack3);
failpass("Swap test 2", i_stack3.size() == i_stack2.size() - 1);
failpass("max_size() test", i_stack2.max_size() > i_stack2.size());
unsigned int total = 0;
const unsigned int temp_capacity = static_cast<unsigned int>(i_stack.capacity());
for (unsigned int temp = 0; temp != 200000; ++temp)
{
total += i_stack.top();
i_stack.pop();
}
failpass("Multipop test", i_stack.size() == 50000);
failpass("top() test", total == 2000000);
i_stack.shrink_to_fit();
failpass("shrink_to_fit() test", temp_capacity != i_stack.capacity());
do
{
if ((rand() & 3) == 0)
{
i_stack.push(10);
}
else
{
i_stack.pop();
}
} while (!i_stack.empty());;
failpass("Randomly pop/push till empty test", i_stack.empty());
#ifdef PLF_TEST_VARIADICS_SUPPORT
i_stack.emplace(20);
failpass("Emplace test", i_stack.size() == 1);
#endif
}
{
title2("Stack Special Case Tests");
stack<int> i_stack(50, 100);
for (int temp = 0; temp != 256; ++temp)
{
i_stack.push(10);
}
stack<int> i_stack_copy(i_stack);
int temp2 = 0;
for (int temp = 0; temp != 256; ++temp)
{
temp2 += i_stack_copy.top();
i_stack_copy.pop();
}
failpass("Stack copy special case test", temp2 == 2560);
}
{
title2("stack expansion test");
stack<int> i_stack;
int push_total = 0, pop_total = 0;
for (int counter = 0; counter != 500; ++counter)
{
i_stack.push(counter);
push_total += counter;
}
do
{
pop_total += i_stack.top();
i_stack.pop();
} while (!i_stack.empty());
failpass("stack expansion test 1", pop_total == push_total);
push_total = 0;
pop_total = 0;
for (int counter = 0; counter != 50; ++counter)
{
i_stack.push(counter);
push_total += counter;
}
do
{
if ((rand() & 3) == 0)
{
pop_total += i_stack.top();
i_stack.pop();
}
else
{
i_stack.push(10);
push_total += 10;
}
} while (!i_stack.empty() && i_stack.capacity() < 5000);
do
{
if ((rand() & 3) == 0)
{
i_stack.push(10);
push_total += 10;
}
else
{
pop_total += i_stack.top();
i_stack.pop();
}
} while (!i_stack.empty());
failpass("random stack expansion test", pop_total == push_total);
}
#ifdef PLF_TEST_VARIADICS_SUPPORT
{
title2("Perfect Forwarding tests");
stack<perfect_forwarding_test> pf_stack;
int lvalue = 0;
int &lvalueref = lvalue;
pf_stack.emplace(7, lvalueref);
failpass("Perfect forwarding test", pf_stack.top().success);
failpass("Perfect forwarding test 2", lvalueref == 1);
}
#endif
{
title2("append tests");
{
stack<int> stack1, stack2;
for(int number = 0; number != 20; ++number)
{
stack1.push(number);
stack2.push(number + 20);
}
stack1.append(stack2);
int check_number = 0;
while(!stack1.empty())
{
check_number += stack1.top();
stack1.pop();
}
failpass("Small append test 1", check_number == 780);
}
{
stack<unsigned int> stack1, stack2;
for(unsigned int number = 0; number != 100000; ++number)
{
stack1.push(number);
stack2.push(number + 100000);
}
stack1.append(stack2);
unsigned int check_number = 0;
while(!stack1.empty())
{
check_number += stack1.top();
stack1.pop();
}
failpass("Large append test 1", check_number == 2820030816u);
}
{
stack<int> stack1, stack2;
for(int number = 150; number != 250; ++number)
{
stack1.push(number);
}
for(int number = 0; number != 150; ++number)
{
stack2.push(number);
}
stack1.append(stack2);
int check_number = 0;
while(!stack1.empty())
{
check_number += stack1.top();
stack1.pop();
}
failpass("Unequal size append test 1", check_number == 31125);
}
}
{
title1("Iterator tests");
stack<int> istack;
int total = 0;
for (int temp = 0; temp != 1000; ++temp)
{
istack.push(1);
++total;
}
int number_of_elements = static_cast<int>(istack.size());
for (stack<int>::iterator current = istack.begin(); current != istack.end(); ++current)
{
total -= *current;
--number_of_elements;
}
failpass("Iterator test 1", total == 0 && number_of_elements == 0);
istack.pop();
istack.pop();
istack.pop();
istack.pop();
istack.pop();
number_of_elements = static_cast<int>(istack.size());
for (stack<int>::iterator current = istack.begin(); current != istack.end(); ++current)
{
--number_of_elements;
}
failpass("Iterator test 2", number_of_elements == 0);
istack.push(1);
istack.push(1);
number_of_elements = static_cast<int>(istack.size());
for (stack<int>::reverse_iterator current = istack.rbegin(); current != istack.rend(); ++current)
{
--number_of_elements;
}
failpass("Reverse Iterator test 1", number_of_elements == 0);
}
}
title1("Test Suite PASS - Press ENTER to Exit");
getchar();
return 0;
}