-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_stack_lib_test.cc
213 lines (194 loc) · 6.08 KB
/
template_stack_lib_test.cc
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
#include "complex.hh"
#include "template_stack.hh"
#include <iostream>
#include <vector>
#include "gtest/gtest.h"
using namespace std;
using namespace complex;
namespace template_stack {
namespace testing {
constexpr char alphalist[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
constexpr char alphalist2[] = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r',
'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i',
'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'};
TEST(CharStackTest, DefaultCtorTest) {
TemplateStack<char> charstack;
EXPECT_TRUE(charstack.empty());
for (char c : alphalist) {
if (!charstack.full()) {
charstack.push(c);
EXPECT_EQ(c, charstack.top_of());
}
}
EXPECT_FALSE(charstack.full());
EXPECT_FALSE(charstack.empty());
cout << charstack << endl;
// int i = 0;
for (char c : alphalist2) {
if (!charstack.empty()) {
// operator[] overload not found: why?
// error: no match for ‘operator==’ (operand types are ‘const char’ and
// ‘const template_stack::TemplateStack<char>’)
// EXPECT_EQ(c, charstack[i++]);
EXPECT_EQ(c, charstack.pop());
}
}
EXPECT_TRUE(charstack.empty());
}
TEST(CharStackTest, RValRefArrayCtorTest) {
char arr[5]{'a', 'b', 'c', 'd', 'e'};
TemplateStack<char> charstack2(arr, 5);
ostringstream out;
out << charstack2;
EXPECT_EQ("e, d, c, b, a", out.str());
cout << charstack2 << endl;
EXPECT_FALSE(charstack2.empty());
EXPECT_TRUE(charstack2.full());
EXPECT_EQ(alphalist[4], charstack2.top_of());
int i = 4;
while (!charstack2.empty()) {
EXPECT_EQ(alphalist[i], charstack2[i]);
EXPECT_EQ(alphalist[i], charstack2.pop());
i--;
}
EXPECT_TRUE(charstack2.empty());
TemplateStack<char> charstack3(arr, 5);
charstack3.reset();
EXPECT_TRUE(charstack3.empty());
}
TEST(CharStackTest, MoveCtorTest) {
char arr[5]{'a', 'b', 'c', 'd', 'e'};
TemplateStack<char> charstack2(arr, 5);
TemplateStack<char> charstack3(move(charstack2));
int i = 4;
while (!charstack3.empty()) {
EXPECT_EQ(alphalist[i], charstack3.pop());
i--;
}
}
TEST(StringStackTest, ReverseTest) {
char *strarr[5];
array<string, 5> strarr1 = {"Heute", "ist", "ein", "staatlicher", "Feiertag"};
for (int i = 0; i < 5; i++) {
strarr[i] = const_cast<char *>(strarr1[i].c_str());
}
reverse(strarr, 5);
for (int i = 0; i < 5; i++) {
EXPECT_EQ(strarr[i], strarr1[4 - i]);
}
reverse(strarr, 5);
for (int i = 0; i < 5; i++) {
EXPECT_EQ(strarr[i], strarr1[i]);
}
}
// double-free():
// (gdb) p charstack2
// $1 = {max_len_ = 0x5, top_ = 0x4, data_ = 0x5555555db600 "abcdeU"}
// (gdb) p charstack4
// $2 = {max_len_ = 0x5, top_ = 0x4, data_ = 0x5555555db600 "abcdeU"}
TEST(CharStackTest, MoveAssignment) {
char arr[5]{'a', 'b', 'c', 'd', 'e'};
TemplateStack<char> charstack2(arr, 5);
TemplateStack<char> charstack3(arr, 5);
// This initialization is a work-around for the Most Vexing Parse, which
// TemplateStack<char> charstack4();
// triggers. There appears to be no way to call the default constructor.
char arr2[5]{'f', 'f', 'f', 'f', 'f'};
TemplateStack<char> charstack4(arr2, 5);
EXPECT_NE(charstack4.top_of(), charstack3.top_of());
EXPECT_NE(charstack4[0], charstack3[0]);
charstack4 = move(charstack2);
EXPECT_FALSE(charstack4.empty());
int i = 4;
while (!charstack4.empty()) {
EXPECT_EQ(charstack4.top_of(), charstack3[i]);
EXPECT_EQ(charstack4[i], charstack3[i]);
EXPECT_EQ(charstack4.pop(), charstack3[i]);
i--;
}
}
/* clang-format off
==14045==ERROR: AddressSanitizer: attempting free on address which was not
malloc()-ed
Otherwise the test passes.
TEST(CharStackTest, ArrayCtor) {
char newarray[5];
for (int i = 0;
i < 5; i++) { newarray[i] = alphalist[i];
}
TemplateStack<const char> newdata = newarray;
EXPECT_EQ(5, newdata.size());
EXPECT_EQ(alphalist[4], newdata.top_of());
cout << "newdata: " << endl;
cout << newdata;
cout << endl << "***" << endl;
for (int i = 0; i < 5; i++) {
EXPECT_EQ(newarray[i], newdata[i]);
}
}
clang-format on
*/
const vector<Complex> compvec{{
{0.0, 0.0},
{1.0, 1.0},
{2.0, 2.0},
{3.0, 3.0},
{4.0, 4.0},
}};
TEST(ComplexStackTest, DefaultCtorTest) {
TemplateStack<Complex> tsc;
EXPECT_TRUE(tsc.empty());
for (auto it = compvec.cbegin(); it != compvec.cend(); it++) {
tsc.push(*it);
}
EXPECT_FALSE(tsc.empty());
cout << tsc << endl;
for (auto it = compvec.crbegin(); it != compvec.crend(); it++) {
EXPECT_EQ(*it, tsc.pop());
}
}
TEST(ComplexStackTest, ReverseTest) {
// Won't work due to most vexing parse.
// Complex *carr[5](), *carr1[5]();
Complex carr[] = {Complex(0, 0), Complex(1, 1), Complex(2, 2), Complex(3, 3),
Complex(4, 4)};
Complex carr1[] = {Complex(4, 4), Complex(3, 3), Complex(2, 2), Complex(1, 1),
Complex(0, 0)};
for (int i = 0; i < 5; i++) {
EXPECT_EQ(carr[i], carr1[4 - i]);
}
reverse(carr, 5);
for (int i = 0; i < 5; i++) {
EXPECT_EQ(carr[i], carr1[i]);
}
reverse(carr, 5);
for (int i = 0; i < 5; i++) {
EXPECT_EQ(carr[i], carr1[4 - i]);
}
}
TEST(ComplexStackTest, MoveCtor) {
Complex carr[] = {Complex(0, 0), Complex(1, 1), Complex(2, 2), Complex(3, 3),
Complex(4, 4)};
TemplateStack<Complex> ts1(carr, 5);
TemplateStack<Complex> ts2(carr, 5);
TemplateStack<Complex> ts3(move(ts1));
int i = 0;
for (auto it = compvec.cbegin(); it != compvec.cend(); it++, i++) {
EXPECT_EQ((*it), ts3[i]);
}
}
TEST(ComplexStackTest, MoveAssignment) {
Complex carr[] = {Complex(0, 0), Complex(1, 1), Complex(2, 2), Complex(3, 3),
Complex(4, 4)};
TemplateStack<Complex> ts1(carr, 5);
TemplateStack<Complex> ts2(carr, 5);
TemplateStack<Complex> tsc = move(ts1);
int i = 0;
for (auto it = compvec.cbegin(); it != compvec.cend(); it++, i++) {
EXPECT_EQ((*it), tsc[i]);
}
}
} // namespace testing
} // namespace template_stack