github.com/m4gshm/gollections@v0.0.10/collection/immutable/ordered/map_/test/map_test.go (about) 1 package test 2 3 import ( 4 "testing" 5 6 "github.com/m4gshm/gollections/collection/immutable/ordered" 7 "github.com/m4gshm/gollections/collection/immutable/ordered/map_" 8 "github.com/m4gshm/gollections/k" 9 "github.com/m4gshm/gollections/slice" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func Test_Map_Iterate(t *testing.T) { 14 ordered := map_.Of(k.V(1, "1"), k.V(1, "1"), k.V(2, "2"), k.V(4, "4"), k.V(3, "3"), k.V(1, "1")) 15 assert.Equal(t, 4, len(ordered.Map())) 16 17 expectedK := slice.Of(1, 2, 4, 3) 18 expectedV := slice.Of("1", "2", "4", "3") 19 20 keys := []int{} 21 values := []string{} 22 for it, key, val, ok := ordered.First(); ok; key, val, ok = it.Next() { 23 keys = append(keys, key) 24 values = append(values, val) 25 } 26 assert.Equal(t, expectedK, keys) 27 assert.Equal(t, expectedV, values) 28 29 assert.Equal(t, slice.Of(1, 2, 4, 3), ordered.Keys().Slice()) 30 assert.Equal(t, slice.Of("1", "2", "4", "3"), ordered.Values().Slice()) 31 } 32 33 func Test_Map_Iterate_Keys(t *testing.T) { 34 ordered := map_.Of(k.V(1, "1"), k.V(1, "1"), k.V(2, "2"), k.V(4, "4"), k.V(3, "3"), k.V(1, "1")) 35 assert.Equal(t, 4, len(ordered.Map())) 36 37 expectedK := slice.Of(1, 2, 4, 3) 38 39 keys := []int{} 40 for it, key, ok := ordered.Keys().First(); ok; key, ok = it.Next() { 41 keys = append(keys, key) 42 } 43 assert.Equal(t, expectedK, keys) 44 45 } 46 47 func Test_Map_Iterate_Values(t *testing.T) { 48 ordered := map_.Of(k.V(1, "1"), k.V(1, "1"), k.V(2, "2"), k.V(4, "4"), k.V(3, "3"), k.V(1, "1")) 49 assert.Equal(t, 4, len(ordered.Map())) 50 51 expectedV := slice.Of("1", "2", "4", "3") 52 53 values := []string{} 54 for it, val, ok := ordered.Values().First(); ok; val, ok = it.Next() { 55 values = append(values, val) 56 } 57 58 assert.Equal(t, expectedV, values) 59 } 60 61 func Test_Map_Zero(t *testing.T) { 62 var m ordered.Map[string, string] 63 64 m.Contains("") 65 66 out := m.Map() 67 assert.Equal(t, 0, len(out)) 68 69 e := m.IsEmpty() 70 assert.True(t, e) 71 72 head, _, _, ok := m.First() 73 assert.False(t, ok) 74 _, _, ok = head.Next() 75 assert.False(t, ok) 76 77 head = m.Head() 78 _, _, ok = head.Next() 79 assert.False(t, ok) 80 81 _, ok = m.Get("") 82 assert.False(t, ok) 83 84 m.For(nil) 85 m.ForEach(nil) 86 m.Track(nil) 87 m.TrackEach(nil) 88 89 m.Filter(nil) 90 m.FilterKey(nil) 91 m.FilterValue(nil) 92 93 m.Values().For(nil) 94 m.Values().ForEach(nil) 95 m.ConvertValue(nil).Track(nil) 96 m.ConvertValue(nil).TrackEach(nil) 97 m.ConvertValue(nil).Filter(nil).FilterKey(nil) 98 m.ConvertValue(nil).Filter(nil).FilterValue(nil) 99 100 m.Keys().For(nil) 101 m.Keys().ForEach(nil) 102 m.ConvertKey(nil).Track(nil) 103 m.ConvertKey(nil).TrackEach(nil) 104 m.ConvertKey(nil).Filter(nil).FilterKey(nil) 105 m.ConvertKey(nil).Filter(nil).FilterValue(nil) 106 m.Convert(nil) 107 108 m.Sort(nil).For(nil) 109 m.Sort(nil).ForEach(nil) 110 111 m.StableSort(nil).For(nil) 112 m.StableSort(nil).ForEach(nil) 113 }