-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest1.cpp
115 lines (86 loc) · 2.59 KB
/
test1.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
#include <iostream>
#include <cassert>
#include "hashmap.h"
constexpr unsigned MAX_TABLE_SIZE = 100;
static constexpr unsigned hashConst = 17; // A prime number
class UnsignedHash
{
public:
unsigned operator()(unsigned key)
{
return (key * key + hashConst) % MAX_TABLE_SIZE;
}
};
HashMap<unsigned, std::string, UnsignedHash> umap(MAX_TABLE_SIZE);
int main()
{
std::string msg;
umap.insert(25, "hello");
umap.insert(34, "world");
umap.insert(43, "one");
umap.insert(143, "two");
umap.insert(754, "three");
assert(umap.lookup(25) == "hello");
assert(umap.lookup(34) == "world");
assert(umap.lookup(43) == "one");
assert(umap.lookup(143) == "two");
assert(umap.lookup(754) == "three");
assert(umap.exists(25) == true);
assert(umap.exists(34) == true);
assert(umap.exists(43) == true);
assert(umap.exists(143) == true);
assert(umap.exists(754) == true);
umap.remove(25);
umap.remove(143);
assert(umap.exists(25) == false);
assert(umap.exists(143) == false);
umap.insert(43, "new value");
assert(umap.lookup(43) == "new value");
umap.insert(143, "143");
assert(umap.lookup(143) == "143");
try
{
// Try to lookup non-existing key
umap.lookup(30);
}
catch (std::out_of_range &e)
{
msg = e.what();
}
assert(msg == "HashMap: key doesn't exists");
try
{
// Try to remove non-existing key
umap.remove(60);
}
catch (std::out_of_range &e)
{
msg = e.what();
}
assert(msg == "HashMap: key doesn't exists");
// Test move constructor
size_t tmpSize = umap.getSize();
HashMap<unsigned, std::string, UnsignedHash> umap2 = std::move(umap);
assert(umap.getSize() == 0);
assert(umap2.getSize() == tmpSize);
// Test move assignment operator
tmpSize = umap2.getSize();
HashMap<unsigned, std::string, UnsignedHash> umap3;
umap3 = std::move(umap2);
assert(umap2.getSize() == 0);
assert(umap3.getSize() == tmpSize);
// Test copy constructor
tmpSize = umap3.getSize();
HashMap<unsigned, std::string, UnsignedHash> umap4 = umap3;
assert(umap3.getSize() == tmpSize);
assert(umap4.getSize() == tmpSize);
assert(umap3.lookup(43) == umap4.lookup(43));
// Test copy assignment operator
tmpSize = umap4.getSize();
HashMap<unsigned, std::string, UnsignedHash> umap5;
umap5 = umap4;
assert(umap4.getSize() == tmpSize);
assert(umap5.getSize() == tmpSize);
assert(umap4.lookup(754) == umap5.lookup(754));
std::cout << "Success!" << std::endl;
}