-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_adt.cc
More file actions
120 lines (111 loc) · 3.9 KB
/
test_adt.cc
File metadata and controls
120 lines (111 loc) · 3.9 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
#include "gtest/gtest.h"
#include "adt.h"
#include "csString.h"
#include "hashTable.h"
#include <string.h>
TEST(AdtTest, testHash){
csAdtRef thing1 = newCsAdt();
csAdtRef thing2 = newCsAdt();
uint32_t hash1 = SEND(thing1,hash);
uint32_t hash2 = SEND(thing2,hash);
EXPECT_EQ(hash1, SEND(thing1,hash));
EXPECT_EQ(hash2, SEND(thing2,hash));
EXPECT_NE(hash1,hash2);
}
TEST(AdtTest, testEquals){
csAdtRef thing1 = newCsAdt();
csAdtRef thing2 = newCsAdt();
EXPECT_TRUE(SEND1(thing1,equals,thing1));
EXPECT_TRUE(SEND1(thing2,equals,thing2));
EXPECT_FALSE(SEND1(thing1,equals,thing2));
}
TEST(csStringTest, testHash){
csStringRef thing1 = newCSString((char *)"testString1");
csStringRef thing2 = newCSString((char *)"testString2");
uint32_t hash1 = SEND(thing1,hash);
uint32_t hash2 = SEND(thing2,hash);
EXPECT_EQ(hash1, SEND(thing1,hash));
EXPECT_EQ(hash2, SEND(thing2,hash));
EXPECT_NE(hash1,hash2);
}
TEST(csStringTest, testEquals){
csStringRef string1 = newCSString((char *)"testString");
csStringRef string2 = newCSString((char *)"testString");
csStringRef string3 = newCSString((char *)"testString");
csStringRef string4 = newCSString((char *)"notLikeTheOthers");
EXPECT_TRUE(SEND1(string1, equals, (csAdtRef)string2));
EXPECT_TRUE(SEND1(string2, equals, (csAdtRef)string3));
EXPECT_TRUE(SEND1(string1, equals, (csAdtRef)string3));
EXPECT_FALSE(SEND1(string1, equals, (csAdtRef)string4));
}
TEST(HashTableTest, testConstructor){
csHashTableRef hashTableRef = newCsHashTable(6);
EXPECT_EQ(hashTableRef->size, 13);
EXPECT_EQ(hashTableRef->count, 0);
}
TEST(HashTableTest, testAddAndIncludesNoGrow){
csHashTableRef hashTable = newCsHashTable(6);
csAdtRef item1 = newCsAdt();
csStringRef item2 = newCSString((char *)"testString1");
EXPECT_FALSE(SEND1(hashTable, includes, item1));
EXPECT_FALSE(SEND1(hashTable, includes, (csAdtRef)item2));
EXPECT_EQ(SEND(hashTable, size), 0);
SEND1(hashTable, add, item1);
EXPECT_TRUE(SEND1(hashTable, includes, item1));
EXPECT_FALSE(SEND1(hashTable, includes, (csAdtRef)item2));
EXPECT_EQ(SEND(hashTable, size), 1);
SEND1(hashTable, add, (csAdtRef)item2);
EXPECT_TRUE(SEND1(hashTable, includes, item1));
EXPECT_TRUE(SEND1(hashTable, includes, (csAdtRef)item2));
EXPECT_EQ(SEND(hashTable, size), 2);
SEND1(hashTable, add, (csAdtRef)item2);
EXPECT_TRUE(SEND1(hashTable, includes, item1));
EXPECT_TRUE(SEND1(hashTable, includes, (csAdtRef)item2));
EXPECT_EQ(SEND(hashTable, size), 2);
}
TEST(HashTableTest, testGrow){
csHashTableRef hashTable = newCsHashTable(1);
csAdtRef item1 = newCsAdt();
csAdtRef item2 = newCsAdt();
csAdtRef item3 = newCsAdt();
csAdtRef item4 = newCsAdt();
csAdtRef item5 = newCsAdt();
csAdtRef item6 = newCsAdt();
csAdtRef item7 = newCsAdt();
EXPECT_EQ(hashTable->size, 7);
EXPECT_EQ(SEND(hashTable, size), 0);
SEND1(hashTable, add, item1);
SEND1(hashTable, add, item2);
SEND1(hashTable, add, item3);
SEND1(hashTable, add, item4);
EXPECT_EQ(hashTable->size, 7);
EXPECT_EQ(SEND(hashTable,size), 4);
SEND1(hashTable, add, item5);
EXPECT_EQ(hashTable->size, 13);
EXPECT_EQ(SEND(hashTable,size), 5);
SEND1(hashTable, add, item6);
SEND1(hashTable, add, item7);
EXPECT_EQ(hashTable->size, 13);
EXPECT_EQ(SEND(hashTable,size), 7);
}
TEST(HashTableTest, testRemove){
csHashTableRef hashTable = newCsHashTable(1);
csAdtRef item1 = newCsAdt();
csAdtRef item2 = newCsAdt();
csAdtRef item3 = newCsAdt();
csAdtRef item4 = newCsAdt();
//Modify default hashes so we get hash collisions.
item2->hash = item1->hash;
item3->hash = item1->hash;
item4->hash = item1->hash;
SEND1(hashTable, add, item1);
SEND1(hashTable, add, item2);
SEND1(hashTable, add, item3);
SEND1(hashTable, add, item4);
EXPECT_EQ(hashTable->size, 7);
EXPECT_EQ(SEND(hashTable, size), 4);
EXPECT_EQ(SEND1(hashTable, remove, item1), (csAdtRef)item1);
EXPECT_EQ(SEND(hashTable, size), 3);
EXPECT_EQ(SEND1(hashTable, remove, item2), (csAdtRef)item2);
EXPECT_EQ(SEND(hashTable, size), 2);
}