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