forked from stevenarditti/part2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-map.cpp
122 lines (113 loc) · 2.8 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
#include "map.h"
#include "object.h"
#include "string.h"
void FAIL() { exit(1); }
void OK(const char *m) { printf("test %s passed\n", m); }
void t_true(bool p) {
if (!p)
FAIL();
}
void t_false(bool p) {
if (p)
FAIL();
}
String *cast_string(Object *object) { return dynamic_cast<String *>(object); }
bool list_contains(Object** list, Object *object, size_t size) {
for (size_t i = 0; i < size; i++) {
if (list[i]) {
if (list[i]->equals(object)) {
return true;
}
}
}
return false;
}
String *a = new String("key_1");
String *b = new String("key_2");
String *c = new String("key_3");
String *d = new String("key_4");
String *av = new String("val_1");
String *bv = new String("val_2");
String *cv = new String("val_3");
String *dv = new String("val_4");
Object *o = new Object();
Object *ov = new Object();
void string_test() {
String *s = new String("Hello");
String *t = new String("World");
String *u = s->concat(t);
t_true(s->equals(s));
t_false(s->equals(t));
t_false(s->equals(u));
OK("string_test");
}
void object_test() {
Object *o1 = new Object();
Object *o2 = new Object();
t_false(o1->equals(o2));
t_true(o1->equals(o1));
t_true(o2->equals(o2));
OK("Object_test");
}
void string_advance_test() {
String *s = new String("Hello");
String *t = new String("World");
String *u = s->concat(t);
t_true(s->hash() != u->hash());
t_false(s->hash() == t->hash());
OK("string_advance_test");
}
void test_constructor() {
Hashmap *map = new Hashmap();
t_true(map->size() == 0);
t_true(map->hash() != 0);
Hashmap *map2 = new Hashmap();
t_true(map->equals(map2));
t_true(map->hash() == map2->hash());
map->put(a, av);
map->put(b, bv);
t_false(map->hash() == map2->hash());
OK("map_constructor");
}
void test_basic_method() {
Hashmap *map = new Hashmap();
map->put(a, av);
map->put(b, bv);
map->put(c, cv);
map->put(d, dv);
t_true(map->size() == 4);
t_true(map->capacity_ >= 4);
map->remove(a);
t_true(map->size() == 3);
size_t size_before_expand = map->capacity_;
map->expand();
t_true(map->capacity_ == 2 * size_before_expand);
OK("map_basic_method");
}
void test_advance_method() {
Hashmap *map = new Hashmap();
map->put(a, av);
map->put(b, bv);
map->put(c, cv);
map->put(d, dv);
map->put(o, ov);
t_true(map->size() == 5);
map->put(a, ov);
t_true(map->size() == 5);
Object** keys = map->key_array();
t_true(list_contains(keys, a, map->size_));
t_true(list_contains(keys, b, map->size_));
t_true(list_contains(keys, c, map->size_));
t_true(list_contains(keys, d, map->size_));
t_true(list_contains(keys, o, map->size_));
OK("map_advance_method");
}
int main(void) {
object_test();
string_test();
string_advance_test();
test_constructor();
test_basic_method();
test_advance_method();
return 0;
}