-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
96 lines (83 loc) · 1.88 KB
/
Copy pathmain.cpp
File metadata and controls
96 lines (83 loc) · 1.88 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
#include <iostream>
#include <vector>
#include <algorithm>
#include"hashmap.h"
#include "hashset.h"
using namespace std;
void test_hashmap() {
int n = 20000,i;
hashmap<int, int> m;
for (i = 0; i < n; i++) {
m.put(i, i);
}
cout << "the size of the hashmap is " << m.getSize() << endl;
hashmap<int, int>::iterator it = m.begin();
pair<int, int> p;
i = 0;
cout << "---------- test the iterator --------------" << endl;
while (it != m.end()) {
i += 1;
if (i % 200 == 0) {
p = *it;
cout << p.first << "," << p.second << endl;
}
++it;
}
cout << "-------------- test method contain ------------" << endl;
for (i = 0; i < n+5; i++) {
if (!m.contain(i)) {
cout << "the hashmap don't contain " << i << endl;
}
}
cout << "-------------- test method remove ------------" << endl;
int x, j = min(100, n);
cout << "try to remove " << j << " number" << endl;
for (i = 0; i < j; i++) {
x = rand() % n;
m.remove(x);
if (m.contain(x)) {
cout << "remove number " << x << " failed" << endl;
}
}
cout << "current size" << m.getSize() << endl;
}
void test_hashset() {
cout << "----------------------------test hashset--------------" << endl;
int i, j, n = 10000;
hashset<int> s;
for (i = 0; i < n; i++) {
j = rand()%100;
s.add(j);
}
cout << "size=" << s.getSize() << endl;
int *d = new int[s.getSize()];
hashset<int>::iterator it = s.begin();
i = 0;
while (it != s.end()) {
d[i++] = *it;
++it;
}
sort(d, d + i);
for (i = 0; i < s.getSize(); i++) {
if (!s.contain(d[i])) {
cout << "error,don't contain " << d[i] << endl;
}
}
j = min(4, s.getSize());
for (i = 0; i < j; i++) {
cout << "remove " << d[i] << endl;
s.remove(d[i]);
}
for (i = 0; i < j; i++) {
if (s.contain(d[i])) {
cout << "error,contain " << d[i] << endl;
}
}
cout << "size " << s.getSize() << endl;
}
int main() {
int i;
test_hashmap();
test_hashset();
cin >> i;
}