github.com/cilium/statedb@v0.3.2/index/seq_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package index_test
     5  
     6  import (
     7  	"maps"
     8  	"slices"
     9  	"testing"
    10  
    11  	"github.com/cilium/statedb/index"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestSeq(t *testing.T) {
    16  	tests := [][]uint64{
    17  		{},
    18  		{1},
    19  		{1, 2, 3},
    20  	}
    21  	for _, keys := range tests {
    22  		expected := []index.Key{}
    23  		for _, x := range keys {
    24  			expected = append(expected, index.Uint64(x))
    25  		}
    26  		keySet := index.Seq(index.Uint64, slices.Values(keys))
    27  		actual := []index.Key{}
    28  		keySet.Foreach(func(k index.Key) {
    29  			actual = append(actual, k)
    30  		})
    31  		assert.ElementsMatch(t, expected, actual)
    32  	}
    33  }
    34  
    35  func TestSeq2(t *testing.T) {
    36  	tests := []map[uint64]int{
    37  		nil,
    38  		map[uint64]int{},
    39  		map[uint64]int{1: 1},
    40  		map[uint64]int{1: 1, 2: 2, 3: 3},
    41  	}
    42  	for _, m := range tests {
    43  		expected := []index.Key{}
    44  		for x := range m {
    45  			expected = append(expected, index.Uint64(x))
    46  		}
    47  		keySet := index.Seq2(index.Uint64, maps.All(m))
    48  		actual := []index.Key{}
    49  		keySet.Foreach(func(k index.Key) {
    50  			actual = append(actual, k)
    51  		})
    52  		assert.ElementsMatch(t, expected, actual)
    53  	}
    54  }
    55  
    56  type testObj struct {
    57  	x string
    58  }
    59  
    60  func (t testObj) String() string {
    61  	return t.x
    62  }
    63  
    64  func TestStringerSeq(t *testing.T) {
    65  	tests := [][]testObj{
    66  		{},
    67  		{testObj{"foo"}},
    68  		{testObj{"foo"}, testObj{"bar"}},
    69  	}
    70  	for _, objs := range tests {
    71  		expected := []index.Key{}
    72  		for _, o := range objs {
    73  			expected = append(expected, index.String(o.x))
    74  		}
    75  		keySet := index.StringerSeq(slices.Values(objs))
    76  		actual := []index.Key{}
    77  		keySet.Foreach(func(k index.Key) {
    78  			actual = append(actual, k)
    79  		})
    80  		assert.ElementsMatch(t, expected, actual)
    81  	}
    82  }
    83  
    84  func TestStringerSeq2(t *testing.T) {
    85  	tests := []map[testObj]int{
    86  		{},
    87  		{testObj{"foo"}: 1},
    88  		{testObj{"foo"}: 1, testObj{"bar"}: 2},
    89  	}
    90  	for _, m := range tests {
    91  		expected := []index.Key{}
    92  		for o := range m {
    93  			expected = append(expected, index.String(o.x))
    94  		}
    95  		keySet := index.StringerSeq2(maps.All(m))
    96  		actual := []index.Key{}
    97  		keySet.Foreach(func(k index.Key) {
    98  			actual = append(actual, k)
    99  		})
   100  		assert.ElementsMatch(t, expected, actual)
   101  	}
   102  }