-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple test for Filter iterator
Add a simple unit test to validate the Filter iterator semantic. Signed-off-by: Fabio Falzoi <[email protected]>
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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 TestFilter(t *testing.T) { | ||
type testObject struct { | ||
ID int | ||
} | ||
|
||
db, _ := NewDB(nil, NewExpVarMetrics(false)) | ||
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)) | ||
|
||
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 := CollectSet( | ||
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.True(t, filtered.Has(2)) | ||
assert.True(t, filtered.Has(4)) | ||
} |