-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_tests_Stack.hpp
168 lines (125 loc) · 3.62 KB
/
unit_tests_Stack.hpp
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
#include <cassert>
#include <iostream>
#include "Stack.hpp"
class unit_tests_Stack
{
private:
static void test_Get()
{
int* a = new int[6];
for(int i = 0; i < 6; i++) a[i] = i;
Stack<int>* s = new Stack<int>(a, 6);
assert(s->GetLength() == 6);
assert(s->Pop() == 5);
assert(s->GetLength() == 5);
for(int i = s->GetLength() - 1; i >= 0; i--) assert(s->Pop() == i);
delete [] a;
delete s;
}
static void test_Set()
{
int* a = new int[6];
for(int i = 0; i < 6; i++) a[i] = i;
Stack<int>* s = new Stack<int>(a, 6);
s->Push(-1);
assert(s->GetLength() == 7);
assert(s->Pop() == -1);
delete [] a;
delete s;
}
static int Double(int n) { return n * 2; }
static void test_Map()
{
int* a = new int[6];
for(int i = 0; i < 6; i++) a[i] = i;
Stack<int>* s = new Stack<int>(a, 6);
Stack<int>* mapped = s->Map(Double);
for(int i = 5; i >= 0; i--) assert(s->Pop() == i);
for(int i = 5; i >= 0; i--) assert(mapped->Pop() == i * 2);
delete [] a;
delete s;
delete mapped;
}
static bool isOdd(int n) { return (n % 2) ? true : false; }
static void test_Where()
{
int* a = new int[6];
for(int i = 0; i < 6; i++) a[i] = i;
Stack<int>* s = new Stack<int>(a, 6);
Stack<int>* whered = s->Where(isOdd);
for(int i = 5; i >= 0; i--) {
assert(whered->Pop() == i % 2);
}
delete [] a;
delete s;
delete whered;
}
static int reduceF(int n1, int n2) { return 2*n1 + 3*n2; }
static void test_Reduce()
{
int a[3] = {1, 2, 3};
Stack<int>* s = new Stack<int>(a, 3);
int b = s->Reduce(reduceF, 4);
assert(b == 144);
delete s;
}
static void test_GetSubsequence()
{
int* a = new int[6];
for(int i = 0; i < 6; i++) a[i] = i;
Stack<int>* s = new Stack<int>(a, 6);
Stack<int>* sub = s->GetSubsequence(1, 4);
for(int i = 2; i >= 0; i--) assert(sub->Pop() == i + 1);
for(int i = 5; i >= 0; i--) assert(s->Pop() == i);
delete [] a;
delete s;
delete sub;
}
static void test_FindSubsequence()
{
int* a = new int[6];
for(int i = 0; i < 6; i++) a[i] = i;
Stack<int>* s = new Stack<int>(a, 6);
int b[3] = {2, 3, 4};
bool f = s->FindSubsequence(b, 3);
assert(f == true);
b[1] = 5;
assert(s->FindSubsequence(b, 3) == false);
assert(s->FindSubsequence(b, 2) == false);
delete [] a;
delete s;
}
static void test_Concat()
{
int* a = new int[6];
for(int i = 0; i < 6; i++) a[i] = i;
int* b = new int[3];
for(int i = 0; i < 3; i++) b[i] = i + 6;
Stack<int>* s = new Stack<int>(a, 6);
Stack<int>* s2 = new Stack<int>(b, 3);
Stack<int>* cr = (Stack<int>*) s->Concat(s2);
assert(cr->GetLength() == 9);
for(int i = 5; i >= 0; i--) assert(s->Pop() == i);
for(int i = 8; i >= 0; i--) assert(cr->Pop() == i);
assert(s2->GetLength() == 0);
delete [] a;
delete [] b;
delete s;
delete s2;
delete cr;
}
public:
static void Run()
{
std::cout << "\nYou'll need to choose sequence type multiple times due to stack constructor is called often during test. Choose the same type.\n\n";
test_Get();
test_Set();
test_Map();
test_Where();
test_Reduce();
test_GetSubsequence();
test_FindSubsequence();
test_Concat();
std::cout << "Stack tests finished successfully. \n";
}
};