github.com/m4gshm/gollections@v0.0.10/kv/loop/test/api_test.go (about)

     1  package test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	breakkvloop "github.com/m4gshm/gollections/break/kv/loop"
    10  	"github.com/m4gshm/gollections/c"
    11  	"github.com/m4gshm/gollections/k"
    12  	kvloop "github.com/m4gshm/gollections/kv/loop"
    13  	"github.com/m4gshm/gollections/loop"
    14  	"github.com/m4gshm/gollections/op"
    15  	"github.com/m4gshm/gollections/slice"
    16  )
    17  
    18  func Test_HasAny(t *testing.T) {
    19  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "one"), k.V(2, "two"), k.V(3, "three"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    20  
    21  	result := kvloop.HasAny(kvl, func(key int, val string) bool { return key == 2 })
    22  
    23  	assert.True(t, result)
    24  }
    25  
    26  func Test_Firstt(t *testing.T) {
    27  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "one"), k.V(2, "two"), k.V(3, "three"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    28  
    29  	k, v, ok, _ := kvloop.Firstt(kvl, func(key int, val string) (bool, error) { return key == 2 || val == "three", nil })
    30  
    31  	assert.True(t, ok)
    32  	assert.Equal(t, 2, k)
    33  	assert.Equal(t, "two", v)
    34  }
    35  
    36  func Test_Reduce(t *testing.T) {
    37  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "one"), k.V(2, "two"), k.V(3, "three"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    38  
    39  	k, v := kvloop.Reduce(kvl, func(kl, kr int, vl, vr string) (int, string) { return kl + kr, vl + vr })
    40  
    41  	assert.Equal(t, 1+2+3, k)
    42  	assert.Equal(t, "one"+"two"+"three", v)
    43  }
    44  
    45  func Test_Reducee(t *testing.T) {
    46  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "one"), k.V(2, "two"), k.V(3, "three"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    47  
    48  	k, v, _ := kvloop.Reducee(kvl, func(kl, kr int, vl, vr string) (int, string, error) { return kl + kr, vl + vr, nil })
    49  
    50  	assert.Equal(t, 1+2+3, k)
    51  	assert.Equal(t, "one"+"two"+"three", v)
    52  }
    53  
    54  func Test_Convert(t *testing.T) {
    55  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "1"), k.V(2, "2"), k.V(3, "3"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    56  
    57  	out := kvloop.ToSlice(kvloop.Convert(kvl, func(k int, v string) (int, string) { return k * k, v + v }).Next, k.V[int, string])
    58  
    59  	assert.Equal(t, slice.Of(k.V(1, "11"), k.V(4, "22"), k.V(9, "33")), out)
    60  }
    61  
    62  func Test_Conv(t *testing.T) {
    63  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "1"), k.V(2, "2"), k.V(3, "3"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    64  
    65  	out, _ := breakkvloop.ToSlice(kvloop.Conv(kvl, func(k int, v string) (int, string, error) { return k * k, v + v, nil }).Next, k.V[int, string])
    66  
    67  	assert.Equal(t, slice.Of(k.V(1, "11"), k.V(4, "22"), k.V(9, "33")), out)
    68  }
    69  
    70  func Test_Filter(t *testing.T) {
    71  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "1"), k.V(2, "2"), k.V(3, "3"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    72  
    73  	out := kvloop.ToSlice(kvloop.Filter(kvl, func(key int, val string) bool { return key != 2 }).Next, k.V[int, string])
    74  
    75  	assert.Equal(t, slice.Of(k.V(1, "1"), k.V(3, "3")), out)
    76  }
    77  
    78  func Test_Filt(t *testing.T) {
    79  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "1"), k.V(2, "2"), k.V(3, "3"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    80  
    81  	out, _ := breakkvloop.ToSlice(kvloop.Filt(kvl, func(key int, val string) (bool, error) { return key != 2, nil }).Next, k.V[int, string])
    82  
    83  	assert.Equal(t, slice.Of(k.V(1, "1"), k.V(3, "3")), out)
    84  }
    85  
    86  func Test_Filt2(t *testing.T) {
    87  	kvl := loop.KeyValue(slice.NewIter(slice.Of(k.V(1, "1"), k.V(2, "2"), k.V(3, "3"))).Next, c.KV[int, string].Key, c.KV[int, string].Value).Next
    88  
    89  	out, err := breakkvloop.ToSlice(kvloop.Filt(kvl, func(key int, val string) (bool, error) {
    90  		ok := key <= 2
    91  		return ok, op.IfElse(key == 2, errors.New("abort"), nil)
    92  	}).Next, k.V[int, string])
    93  
    94  	assert.Error(t, err)
    95  	assert.Equal(t, slice.Of(k.V(1, "1"), k.V(2, "2")), out)
    96  }
    97  
    98  func Test_NewIter(t *testing.T) {
    99  	s := slice.Of(k.V(1, "1"), k.V(2, "2"), k.V(3, "3"))
   100  	i := 0
   101  	it := kvloop.NewIter(s, func(s []c.KV[int, string]) bool { return i < len(s) }, func(s []c.KV[int, string]) (int, string, error) {
   102  		n := s[i]
   103  		i++
   104  		return n.K, n.V, nil
   105  	})
   106  
   107  	out := kvloop.ToSlice(it.Next, k.V[int, string])
   108  	assert.NoError(t, it.Error())
   109  	assert.Equal(t, slice.Of(k.V(1, "1"), k.V(2, "2"), k.V(3, "3")), out)
   110  }