-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkeyset_test.go
55 lines (42 loc) · 1.14 KB
/
keyset_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
package collections
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestKeySet(t *testing.T) {
sk, ctx, _ := deps()
keyset := NewKeySet[string](sk, 0, StringKeyEncoder)
// test insert and get
key := "hi"
keyset.Insert(ctx, key)
require.True(t, keyset.Has(ctx, key))
// test delete and get error
keyset.Delete(ctx, key)
require.False(t, keyset.Has(ctx, key))
}
func TestKeySet_Iterate(t *testing.T) {
sk, ctx, _ := deps()
keyset := NewKeySet[string](sk, 0, StringKeyEncoder)
keyset.Insert(ctx, "a")
keyset.Insert(ctx, "aa")
keyset.Insert(ctx, "b")
keyset.Insert(ctx, "bb")
expectedKeys := []string{"a", "aa", "b", "bb"}
iter := keyset.Iterate(ctx, Range[string]{})
defer iter.Close()
for i, o := range iter.Keys() {
require.Equal(t, expectedKeys[i], o)
}
}
func TestKeysetIterator(t *testing.T) {
sk, ctx, _ := deps()
keyset := NewKeySet[string](sk, 0, StringKeyEncoder)
keyset.Insert(ctx, "a")
iter := keyset.Iterate(ctx, Range[string]{})
defer iter.Close()
assert.True(t, iter.Valid())
assert.EqualValues(t, "a", iter.Key())
iter.Next()
assert.False(t, iter.Valid())
}