-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSafeOrderedMap_test.go
More file actions
113 lines (94 loc) · 1.99 KB
/
ThreadSafeOrderedMap_test.go
File metadata and controls
113 lines (94 loc) · 1.99 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
package omap
import (
"cmp"
"iter"
"testing"
)
func seqToSeq2(s iter.Seq[int]) func() iter.Seq2[int, int] {
return func() iter.Seq2[int, int] {
return func(yield func(int, int) bool) {
count := -1
for k := range s {
count++
if !yield(count, k) {
return
}
}
}
}
}
func TestLockIters(t *testing.T) {
s := New[int, int](cmp.Compare)
for i := range 3 {
s.Put(i, i)
}
for range s.keys() {
break
}
for _, f := range []*KvSet[string, func() iter.Seq2[int, int]]{
{"All", s.ToTs().All},
{"Keys", seqToSeq2(s.ToTs().Keys())},
{"Values", seqToSeq2(s.ToTs().Values())},
} {
count := 0
t.Logf("Testing loop iterator from: %s", f.Key)
for range f.Value() {
count++
}
if count != 3 {
t.Fatalf("Failed to get 3 elements from %s, got: %d", f.Key, count)
}
count = 0
for range f.Value() {
count++
break
}
if count != 1 {
t.Fatalf("Failed to break at element: 0 elements from %s, got: %d", f.Key, count)
}
}
}
func TestMerge(t *testing.T) {
dst := NewTs[int, int](cmp.Compare)
src := NewTs[int, int](cmp.Compare)
for i := range 3 {
src.Put(i, i+3)
}
if check := dst.Merge(src); check != src.Size() {
t.Fatalf("Expected size to be: %d, got %d", src.Size(), check)
}
if check := src.Merge(src); check != 0 {
t.Fatalf("Should not be able to merge onto an ourself")
}
if src.Size() != dst.Size() {
t.Fatalf("Source and dst do not match")
}
next, stop := iter.Pull2(dst.All())
defer stop()
for k, v := range dst.All() {
x, y, ok := next()
if x != k || y != v || !ok {
t.Fail()
}
}
stop()
dst = NewTs[int, int](cmp.Compare)
m := map[int]int{1: 2}
if count := Merge(dst, m); count != 1 {
t.Fail()
}
if v, ok := dst.Get(1); !ok || v != 2 {
t.Fail()
}
dst = New[int, int](cmp.Compare)
dst.Merge(dst)
}
func TestBug(t *testing.T) {
s := NewSliceTree[int64, any](100, cmp.Compare)
s.Put(1769664118175, nil)
for range 1 {
iter := s.RemoveBetweenKV(-1, 1769664118175, FIRST_KEY)
for range iter {
}
}
}