github.com/cilium/statedb@v0.3.2/index/keyset_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package index_test 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 11 "github.com/cilium/statedb/index" 12 ) 13 14 func TestKeySet_Single(t *testing.T) { 15 ks := index.NewKeySet([]byte("baz")) 16 require.EqualValues(t, "baz", ks.First()) 17 require.True(t, ks.Exists([]byte("baz"))) 18 require.False(t, ks.Exists([]byte("foo"))) 19 vs := []index.Key{} 20 ks.Foreach(func(bs index.Key) { 21 vs = append(vs, bs) 22 }) 23 require.ElementsMatch(t, vs, []index.Key{index.Key("baz")}) 24 } 25 26 func TestKeySet_Multi(t *testing.T) { 27 ks := index.NewKeySet([]byte("baz"), []byte("quux")) 28 require.EqualValues(t, "baz", ks.First()) 29 require.True(t, ks.Exists([]byte("baz"))) 30 require.True(t, ks.Exists([]byte("quux"))) 31 require.False(t, ks.Exists([]byte("foo"))) 32 vs := [][]byte{} 33 ks.Foreach(func(bs index.Key) { 34 vs = append(vs, bs) 35 }) 36 require.ElementsMatch(t, vs, [][]byte{[]byte("baz"), []byte("quux")}) 37 }