-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist_test.go
103 lines (89 loc) · 2.08 KB
/
linkedlist_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
package armap
import (
"slices"
"testing"
)
func TestLinkedList(t *testing.T) {
t.Run("string,string", func(tt *testing.T) {
a := NewArena(1000, 10)
defer a.Release()
l := NewLinkedList[string, string](a)
l.Push("hello", "world")
l.Push("foo", "bar")
l.Push("test", "testvalue")
if v, ok := l.Get("hello"); ok != true {
tt.Errorf("already push hello")
} else {
if v != "world" {
tt.Errorf("actual: %s", v)
}
}
if v, ok := l.Get("foo"); ok != true {
tt.Errorf("already push hello")
} else {
if v != "bar" {
tt.Errorf("actual: %s", v)
}
}
if v, ok := l.Delete("hello"); ok != true {
tt.Errorf("delete hello")
} else {
if v != "world" {
tt.Errorf("actual: %s", v)
}
}
if _, ok := l.Get("hello"); ok {
tt.Errorf("already deleted hello")
}
if v, ok := l.Get("test"); ok != true {
tt.Errorf("already push test")
} else {
if v != "testvalue" {
tt.Errorf("actual: %s", v)
}
}
})
t.Run("push/delete/scan", func(tt *testing.T) {
a := NewArena(1000, 10)
defer a.Release()
l := NewLinkedList[string, string](a)
l.Push("test1", "t1")
l.Push("test2", "t2")
l.Push("test3", "t3")
tt.Logf("dump keys %v", l.keys())
l.Delete("test1")
tt.Logf("dump keys %v", l.keys())
keys := make([]string, 0)
l.Scan(func(key string, value string) bool {
keys = append(keys, key)
return true
})
if len(keys) != 2 {
tt.Errorf("exists keys=%v", keys)
}
if slices.Contains(keys, "test2") != true {
tt.Errorf("exists keys=%v", keys)
}
if slices.Contains(keys, "test3") != true {
tt.Errorf("exists keys=%v", keys)
}
l.Push("test4", "t4")
keys = keys[len(keys):] // reset
l.Scan(func(key string, value string) bool {
keys = append(keys, key)
return true
})
if len(keys) != 3 {
tt.Errorf("exists keys=%v", keys)
}
if slices.Contains(keys, "test2") != true {
tt.Errorf("exists keys=%v", keys)
}
if slices.Contains(keys, "test3") != true {
tt.Errorf("exists keys=%v", keys)
}
if slices.Contains(keys, "test4") != true {
tt.Errorf("exists keys=%v", keys)
}
})
}