-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
318 lines (284 loc) · 6.08 KB
/
main.cpp
File metadata and controls
318 lines (284 loc) · 6.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*******************
Refer to string.h for instructions
Your string class should make it through this
guantlet without an assertion failing.
If it succeeds, then everything is awesome!
If you are stumped on a particular test,
or there is an unusual looking test you can't seem to get past,
you may comment it out, temporarily, to check further tests.
e-mail terry and I if you have any further questions or trouble.
esmes@aie.edu.au, Esmeralda Salamone
terryn@aie.edu.au, Terry Nguyen
********************/
#define _CRTDBG_MAP_ALLOC
#include <cassert>
#include <crtdbg.h>
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif// dbgnew
#ifndef GLB_DEBUG
#define GLB_DEBUG
_CrtMemState s1, s2, s3;
#define mem_test_start() do{ \
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); \
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT); \
_CrtMemCheckpoint(&s1);\
}while (0)
#define mem_test_check() do{_CrtMemCheckpoint(&s2);\
if(_CrtMemDifference(&s3, &s1, &s2))\
{\
_CrtMemDumpStatistics(&s3); \
assert(_CrtDumpMemoryLeaks() == 0); \
}\
_CrtMemCheckpoint(&s1);\
}while(0)
#endif //glb
#else //if release mode, disable tests
#define mem_test_start()
#define mem_test_check()
#endif //debug
#include <sstream>
#include <iostream>
#include <climits>
#include "string.h"
//if you change the name of the namespace or class, adjust the typedef appropriately.
typedef sfw::string string;
using namespace sfw::literals;
void TEST_ALL_STRING_STUFF();
int main()
{
TEST_ALL_STRING_STUFF();
return 0;
}
void TEST_ALL_STRING_STUFF()
{
#ifndef _DEBUG
return;
#endif
//spit memory leaks to the console
mem_test_start();
// Constructors and length
{
//Default
{
assert(string().length() == 0);
string a;
string c = string();
delete new string();
}
mem_test_check();
//Size
{
assert(string(10).size() == 10);
string b(10);
string c = string(10);
delete new string(10);
}
mem_test_check();
//array
{
assert(string("Dinosaur").length() == 8);
string b("Dinosaur");
string c = string("Dinosaur");
delete new string("Dinosaur");
char *n = nullptr;
delete new string(n);
}
mem_test_check();
//array and size
{
assert(string("Dinosaur", 4).length() == 3);
delete new string("Dinosaur", 2000);
}
mem_test_check();
// move
{
assert(string(string()).length() == 0);
assert(string(string(10)).size() == 10);
assert(string("dinosaur").length() == 8);
assert(string("dinosaur", 4).length() == 4);
string &&r = string();
string &q = string(r);
string d("Dinosaur");
string m(std::move(d));
assert(d.length() == 0);
assert(m.length() == 8);
}
mem_test_check();
// copy
{
string a = string("dinosaur", 4);
string b(a);
string *c = new string(b);
delete c;
assert(b.length() == 3);
}
mem_test_check();
// literal
{
string() = "what?"_sfw;
string("yep"_sfw);
}
mem_test_check();
}
//Assignment Operators
{
//string copy/move
{
string b;
b = b;
string a = string(a);
b = std::move(a);
assert(a.length() == 0);
a = "Dinosaur";
b = std::move(b);
a = b;
b = a;
assert(a.length() == b.length());
}
mem_test_check();
//char array
{
string a;
a = "Dinosaur";
a = "Whoopie";
char t[] = "Wumpus";
a = t;
char *trouble = new char;
a = trouble;
}
mem_test_check();
}
//relational operators
{
//equivalence
{
assert(""_sfw == "");
assert("" == ""_sfw);
assert(""_sfw == ""_sfw);
assert("a"_sfw == "a");
assert("b" == "b"_sfw);
assert("c"_sfw == "c"_sfw);
assert(!("a"_sfw == "A"));
assert(!("b" == "B"_sfw));
assert(!("c"_sfw == "C"_sfw));
}
mem_test_check();
//nequals
{
assert(""_sfw != " ");
assert("" != " "_sfw);
assert(""_sfw != " "_sfw);
assert(!("a"_sfw != "a"));
assert(!("b" != "b"_sfw));
assert(!("c"_sfw != "c"_sfw));
}
mem_test_check();
//lesser
{
assert("a"_sfw <= "b");
assert("a" <= "a"_sfw);
assert(!("b"_sfw <= "a"_sfw));
assert("a"_sfw < "b");
assert(!("a" < "a"_sfw));
assert(!("b"_sfw < "a"_sfw));
}
mem_test_check();
//greater
{
assert("b"_sfw >= "a");
assert("b" >= "b"_sfw);
assert(!("a"_sfw >= "b"_sfw));
assert("b"_sfw > "a");
assert(!("b" > "b"_sfw));
assert(!("a"_sfw > "b"_sfw));
}
mem_test_check();
}
//concat and assignconcat
{
//assign
{
assert((string() += "Dinosaur") == "Dinosaur");
assert((string() += string()) == string());
string b("woo");
assert((b += "psies"_sfw) == "woopsies"_sfw);
}
mem_test_check();
{
assert(""_sfw+""=="");
assert("" + ""_sfw == "");
assert(""_sfw + ""_sfw == "");
assert("dino"_sfw + "saur" == "dinosaur");
assert("dino" + "saur"_sfw == "dinosaur");
assert("dino"_sfw + "saur"_sfw == "dinosaur");
}
mem_test_check();
}
//miscellaneous operations
{
//subcript
{
assert("blah"_sfw[2] == 'a');
string b("hmmh");
b[2] = 'p';
assert(b == "hmph");
}
mem_test_check();
//stream operator
{
std::stringstream s;
s << "blah"_sfw;
string b;
s >> b;
assert(b == "blah"_sfw);
}
mem_test_check();
//cstring
{
assert(strlen("blah"_sfw.cstring()) == 4);
string d("ddd");
assert(d == "dfd");
}
mem_test_check();
//empty
{
assert(string().length() == 0);
assert(string().empty());
assert(string(23).empty());
assert(!string("Dinosaur").empty());
assert(!string(string("Dinosaur")).empty());
}
mem_test_check();
//resize
{
string().resize(0);
string().resize(ULLONG_MAX); // TROLLOLOL
string(string()).resize(0);
string(string(10)).resize(0);
string d;
d.resize(10);
assert(d.size() == 10);
}
mem_test_check();
//empty
{
assert(string().length() == 0);
assert(string().empty());
assert(string(23).empty());
assert(!string("Dinosaur").empty());
assert(!string(string("Dinosaur")).empty());
}
mem_test_check();
//clear
{
string d("RAURGH");
d.clear();
assert(d == "");
assert(d.length() == 0);
}
mem_test_check();
}
}