-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable_test.go
116 lines (95 loc) · 2.61 KB
/
hashtable_test.go
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
package hashtable_test
import (
"fmt"
"testing"
"github.com/HotPotatoC/hashtable"
)
func populate(n int) hashtable.HashTable {
ht := hashtable.New()
for i := 0; i < n; i++ {
ht.Set(fmt.Sprintf("k%d", i+1), fmt.Sprintf("v%d", i+1))
}
return ht
}
func TestSet(t *testing.T) {
ht := populate(4)
if ht.Len() != 4 {
t.Errorf("Failed TestSet -> Expected Size: %d | Got: %d", 4, ht.Len())
}
ht.Set("my-key", "value")
if ht.Len() != 5 {
t.Errorf("Failed TestSet -> Expected Size: %d | Got: %d", 5, ht.Len())
}
}
func TestRemove(t *testing.T) {
ht := populate(4)
ht.Remove("k1")
if ht.Len() != 3 {
t.Errorf("Failed TestRemove -> Expected Size: %d | Got: %d", 3, ht.Len())
}
}
func TestGet(t *testing.T) {
ht := populate(5)
value, ok := ht.Get("k2")
if !ok {
t.Errorf("Failed TestGet -> Expected value: %v | Got: %v", true, ok)
}
expected := "v2"
if value != expected {
t.Errorf("Failed TestGet -> Expected value: %s | Got: %s", expected, value)
}
}
func TestIter(t *testing.T) {
ht := populate(5)
kv := make([]*hashtable.Entry, 0)
for entry := range ht.Iter() {
kv = append(kv, entry)
}
if len(kv) != 5 {
t.Errorf("Failed TestIter -> Expected size: %d | Got: %d", 5, len(kv))
}
}
func TestPopulate_100(t *testing.T) {
ht := populate(100)
if ht.Len() != 100 {
t.Errorf("Failed TestPopulate100 -> Expected Size: %d | Got: %d", 100, ht.Len())
}
for i := 0; i < 100; i++ {
if !ht.Exist(fmt.Sprintf("k%d", i+1)) {
t.Errorf("Failed TestPopulate100 -> Expected: %v | Got: %v", true, ht.Exist(fmt.Sprintf("k%d", i)))
}
}
}
func TestPopulate_1000(t *testing.T) {
ht := populate(1000)
if ht.Len() != 1000 {
t.Errorf("Failed TestPopulate100 -> Expected Size: %d | Got: %d", 1000, ht.Len())
}
for i := 0; i < 1000; i++ {
if !ht.Exist(fmt.Sprintf("k%d", i+1)) {
t.Errorf("Failed TestPopulate100 -> Expected: %v | Got: %v", true, ht.Exist(fmt.Sprintf("k%d", i)))
}
}
}
func TestPopulate_10000(t *testing.T) {
ht := populate(10000)
if ht.Len() != 10000 {
t.Errorf("Failed TestPopulate10000 -> Expected Size: %d | Got: %d", 10000, ht.Len())
}
for i := 0; i < 10000; i++ {
if !ht.Exist(fmt.Sprintf("k%d", i+1)) {
t.Errorf("Failed TestPopulate100 -> Expected: %v | Got: %v", true, ht.Exist(fmt.Sprintf("k%d", i)))
}
}
}
func TestPopulate_100000(t *testing.T) {
ht := populate(100000)
if ht.Len() != 100000 {
t.Errorf("Failed TestPopulate100000 -> Expected Size: %d | Got: %d", 100000, ht.Len())
}
for i := 0; i < 100000; i++ {
if !ht.Exist(fmt.Sprintf("k%d", i+1)) {
t.Errorf("Failed TestPopulate100 -> Expected: %v | Got: %v", true, ht.Exist(fmt.Sprintf("k%d", i)))
}
}
}