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
|
#include <cmath>
#include <functional>
#include <iostream>
#include <sstream>
#include "bag.h"
template <typename T>
void test(std::string, std::function<T()>, T, bool = false);
int main() {
main_savitch_3::bag bag;
main_savitch_3::bag bag_2;
test<int>(
"size", [&bag]() { return bag.size(); }, 0);
test<int>(
"insert",
[&bag, &bag_2]() {
bag.insert(1);
bag_2.insert(3);
bag_2.insert(3);
bag_2.insert(7);
return bag.size();
},
1);
test<int>(
"+=",
[&bag, &bag_2]() {
bag += bag_2;
return bag.size();
},
4);
test<int>(
"count", [&bag]() { return bag.count(3); }, 2);
test<int>(
"erase_one",
[&bag]() {
bag.erase_one(3);
return bag.count(3);
},
1);
test<int>(
"erase",
[&bag]() {
bag.insert(3);
bag.erase(3);
return bag.count(3);
},
0);
test<int>(
"smallest", [&bag]() { return bag.smallest(); }, 1);
test<int>(
"average", [&bag]() { return bag.average(); },
static_cast<int>((1 + 7) / 2));
test<int>(
"sort",
[&bag]() {
std::stringstream sort_buffer;
std::streambuf *original_cout_buffer = std::cout.rdbuf();
std::cout.rdbuf(sort_buffer.rdbuf());
bag.insert(3);
bag.sort();
std::cout.rdbuf(original_cout_buffer);
return sort_buffer.str() == "1\n3\n7\n";
},
true);
test<int>(
"==",
[&bag]() {
main_savitch_3::bag bag_test;
bag_test.insert(7);
bag_test.insert(1);
bag_test.insert(3);
return bag == bag_test;
},
true);
test<int>(
"==",
[&bag]() {
main_savitch_3::bag bag_test;
// Slightly different test values that fail, as to prove validity of
// test suite.
// I mark the expected value as `false` for this `test` to treat an
// intentional fail expectation as a pass.
bag_test.insert(7);
bag_test.insert(1);
bag_test.insert(9);
return bag == bag_test;
},
false);
// This test is purposefully set to fail as to prove the validity of `test`.
test<int>(
"test", [&bag]() { return bag.smallest(); }, 0, true);
return 0;
}
template <typename T>
void test(std::string name, std::function<T()> evaluate, T expected,
bool should_fail) {
T evaluated = evaluate();
if (should_fail)
evaluated = !evaluated;
std::cout << (evaluated == expected ? "pass(" : "FAIL(") << name
<< "): expected " << expected << ", got " << evaluated << std::endl;
}
|