-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_test.go
65 lines (56 loc) · 1.24 KB
/
iterator_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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package statedb
import (
"testing"
"github.com/cilium/statedb/index"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCollectFilterMapToSeq(t *testing.T) {
type testObject struct {
ID int
}
db := New()
idIndex := Index[*testObject, int]{
Name: "id",
FromObject: func(t *testObject) index.KeySet {
return index.NewKeySet(index.Int(t.ID))
},
FromKey: index.Int,
Unique: true,
}
table, _ := NewTable("test", idIndex)
require.NoError(t, db.RegisterTable(table))
db.Start()
defer db.Stop()
txn := db.WriteTxn(table)
table.Insert(txn, &testObject{ID: 1})
table.Insert(txn, &testObject{ID: 2})
table.Insert(txn, &testObject{ID: 3})
table.Insert(txn, &testObject{ID: 4})
table.Insert(txn, &testObject{ID: 5})
txn.Commit()
iter := table.All(db.ReadTxn())
filtered := Collect(
Map(
Filter(
iter,
func(obj *testObject) bool {
return obj.ID%2 == 0
},
),
func(obj *testObject) int {
return obj.ID
},
),
)
assert.Len(t, filtered, 2)
assert.Equal(t, []int{2, 4}, filtered)
count := 0
for obj := range ToSeq(iter) {
assert.Greater(t, obj.ID, 0)
count++
}
assert.Equal(t, 5, count)
}