-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_multimap.cpp
179 lines (142 loc) · 3.57 KB
/
test_multimap.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
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
#include "test_multimap.h"
class MultimapTest : public testing::Test
{
public:
SinglyLinkedList<int> *sll;
Multimap<SinglyLinkedList<int> > *map;
void SetUp() override
{
sll = new SinglyLinkedList<int>();
map = new Multimap<SinglyLinkedList<int> >();
}
void TearDown() override
{
free(sll);
free(map);
}
};
TEST_F(MultimapTest, test_addNode)
{
int* a = (int*)malloc(sizeof(int));
int* key = (int*)malloc(sizeof(int));
*a = 123;
*key = 42;
//Testing add for SLL
sll->addNode(a);
ASSERT_TRUE(sll->getLength() == 1);
//Testing add for map
map->addNode(key, sll);
ASSERT_TRUE(map->getLength() == 1);
//Testing for no duplicate keys
try
{
map->addNode(key, sll);
ASSERT_TRUE (1 == 2);
}
catch(...)
{
ASSERT_TRUE(map->getLength() == 1);
}
free(a);
free(key);
}
TEST_F(MultimapTest, test_removeNode)
{
int* a = (int*)malloc(sizeof(int));
int* key = (int*)malloc(sizeof(int));
*a = 123;
*key = 42;
sll->removeNode(a);
sll->addNode(a);
*a = 124;
sll->addNode(a);
*a = 125;
sll->addNode(a);
*a = 124;
sll->removeNode(a);
*a = 125;
sll->removeNode(a);
*a = 123;
sll->removeNode(a);
//If only one node is available, it will be set to NULL, but will still exist.
cout << sll->getLength() << endl<<endl;
ASSERT_TRUE(sll->getLength() == 0);
sll->addNode(a);
map->removeNode(key);
map->addNode(key, sll);
*key = 43;
map->addNode(key, sll);
*key = 44;
map->addNode(key, sll);
//Testing middle delete, last delete and first delete.
*key = 43;
map->removeNode(key);
ASSERT_TRUE(map->getLength() == 2);
*key = 44;
map->removeNode(key);
ASSERT_TRUE(map->getLength() == 1);
*key = 42;
map->removeNode(key);
ASSERT_TRUE(map->getLength() == 0);
free(a);
free(key);
}
TEST_F(MultimapTest, test_searchNode)
{
int* a = (int*)malloc(sizeof(int));
int* key = (int*)malloc(sizeof(int));
*a = 123;
*key = 42;
//Testing add for SLL
sll->addNode(a);
ASSERT_TRUE(sll->searchNode(a) == true);
map->addNode(key, sll);
ASSERT_TRUE(map->searchNode(key) == true);
*a = 124;
*key = 43;
ASSERT_TRUE(sll->searchNode(a) != true);
ASSERT_TRUE(map->searchNode(key) != true);
free(a);
free(key);
}
TEST_F(MultimapTest, test_getValues)
{
int* a = (int*)malloc(sizeof(int));
int* key = (int*)malloc(sizeof(int));
*a = 123;
*key = 42;
sll->printSLL();
sll->addNode(a);
sll->printSLL();
int **values = (int**)malloc(sll->getLength() * sizeof(int));
values = sll->getValues();
ASSERT_TRUE(*values[0] == *a);
map->printMultimap();
map->addNode(key, sll);
map->printMultimap();
values = map->getKeys();
ASSERT_TRUE(*values[0] == *key);
free(a);
free(key);
}
TEST_F(MultimapTest, test_overloader)
{
int* a = (int*)malloc(sizeof(int));
int* key = (int*)malloc(sizeof(int));
*a = 123;
*key = 42;
SinglyLinkedList<int> slltest = SinglyLinkedList<int>();
slltest.addNode(a);
SinglyLinkedList<int> slltest2 = slltest;
SinglyLinkedList<int> slltest3;
slltest3 = slltest;
slltest3 = slltest3;
Multimap<SinglyLinkedList<int> > maptest = Multimap<SinglyLinkedList<int> >();
maptest.addNode(key,&slltest);
Multimap<SinglyLinkedList<int> > maptest2 = maptest;
Multimap<SinglyLinkedList<int> > maptest3;
maptest3 = maptest;
maptest3 = maptest3;
free(a);
free(key);
}