-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest-map.cpp
152 lines (127 loc) · 3.2 KB
/
test-map.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
#include "object.h"
#include "string.h"
#include "map.h"
#include "helper.h"
void testMapConstructorAndDestructor() {
Map* map = new Map();
printf("Successfully constructed map\n");
map->~Map();
printf("Successfully deconstructed map\n");
delete map;
}
void testMapSet() {
Map* map = new Map();
printf("Map length is: %zu\n", map->length());
Object* o1 = new Object();
Object* o2 = new Object();
Object* o3 = new Object();
Object* o4 = new Object();
map->set(o1, o2);
printf("Map length is: %zu\n", map->length());
// Exits the program if map length is incorrect
if (map->length() != 1) exit(1);
delete map;
delete o1;
delete o2;
delete o3;
delete o4;
}
void testMapRemove() {
Map* map = new Map();
Object* o1 = new Object();
Object* o2 = new Object();
Object* o3 = new Object();
Object* o4 = new Object();
map->set(o1, o2);
map->set(o3, o4);
printf("Before remove, map length is: %zu\n", map->length());
map->remove(o1);
printf("After remove, map length is: %zu\n", map->length());
// Exits the program if map length is incorrect
if (map->length() != 1) exit(1);
map->remove(o3);
printf("After remove, map length is: %zu\n", map->length());
// Exits the program if map length is incorrect
if (map->length() != 0) exit(1);
delete map;
delete o1;
delete o2;
delete o3;
delete o4;
}
void testMapClear() {
Map* map = new Map();
Object* o1 = new Object();
Object* o2 = new Object();
Object* o3 = new Object();
Object* o4 = new Object();
map->set(o1, o2);
map->set(o3, o4);
printf("Before clear, map length is: %zu\n", map->length());
map->clear();
printf("After clear, map length is: %zu\n", map->length());
// Exits the program if map length is incorrect
if (map->length() != 0) exit(1);
delete map;
delete o1;
delete o2;
delete o3;
delete o4;
}
void testMapGet() {
Map* map = new Map();
Object* o1 = new Object();
Object* o2 = new Object();
Object* o3 = new Object();
Object* o4 = new Object();
map->set(o1, o2);
map->set(o3, o4);
if (!map->get(o1)->equals(o2) || !map->get(o3)->equals(o4)) {
exit(1);
}
delete map;
delete o1;
delete o2;
delete o3;
delete o4;
}
void testStringMap() {
StringMap* stringMap = new StringMap();
printf("Successfully constructed string map\n");
String* s1 = new String("hi");
String* s2 = new String("bye");
String* s3 = new String("hello");
String* s4 = new String("goodbye");
stringMap->set(s1, s2);
stringMap->set(s3, s4);
if (!stringMap->get(s1)->equals(s2) || !stringMap->get(s3)->equals(s4)) {
exit(1);
}
printf("Before remove, map length is: %zu\n", stringMap->length());
stringMap->remove(s1);
printf("After remove, map length is: %zu\n", stringMap->length());
// Exits the program if map length is incorrect
if (stringMap->length() != 1) exit(1);
stringMap->clear();
printf("After clear, map length is: %zu\n", stringMap->length());
// Exits the program if map length is incorrect
if (stringMap->length() != 0) {
exit(1);
}
delete stringMap;
delete s1;
delete s2;
delete s3;
delete s4;
}
int main() {
// Test suite
testMapConstructorAndDestructor();
testMapSet();
testMapRemove();
testMapClear();
testMapGet();
testStringMap();
// Returns successfully if all tests are successfully completed
return 0;
}