github.com/mitranim/gg@v0.1.17/map_test.go (about) 1 package gg_test 2 3 import ( 4 "testing" 5 6 "github.com/mitranim/gg" 7 "github.com/mitranim/gg/gtest" 8 ) 9 10 func TestMapInit(t *testing.T) { 11 defer gtest.Catch(t) 12 13 var tar IntMap 14 gtest.Equal(gg.MapInit(&tar), tar) 15 gtest.NotZero(tar) 16 17 tar[10] = 20 18 gtest.Equal(gg.MapInit(&tar), tar) 19 gtest.Equal(tar, IntMap{10: 20}) 20 } 21 22 func TestMapClone(t *testing.T) { 23 defer gtest.Catch(t) 24 25 gtest.Equal(gg.MapClone(IntMap(nil)), IntMap(nil)) 26 27 src := IntMap{10: 20, 30: 40} 28 out := gg.MapClone(src) 29 gtest.Equal(out, src) 30 31 src[10] = 50 32 gtest.Equal(src, IntMap{10: 50, 30: 40}) 33 gtest.Equal(out, IntMap{10: 20, 30: 40}) 34 } 35 36 func TestMapKeys(t *testing.T) { 37 defer gtest.Catch(t) 38 39 test := func(src IntMap, exp []int) { 40 gtest.Equal(gg.SortedPrim(gg.MapKeys(src)), exp) 41 } 42 43 test(IntMap(nil), []int(nil)) 44 test(IntMap{}, []int{}) 45 test(IntMap{10: 20}, []int{10}) 46 test(IntMap{10: 20, 30: 40}, []int{10, 30}) 47 } 48 49 func TestMapVals(t *testing.T) { 50 defer gtest.Catch(t) 51 52 test := func(src IntMap, exp []int) { 53 gtest.Equal(gg.SortedPrim(gg.MapVals(src)), exp) 54 } 55 56 test(IntMap(nil), []int(nil)) 57 test(IntMap{}, []int{}) 58 test(IntMap{10: 20}, []int{20}) 59 test(IntMap{10: 20, 30: 40}, []int{20, 40}) 60 } 61 62 func TestMapHas(t *testing.T) { 63 defer gtest.Catch(t) 64 65 gtest.False(gg.MapHas(IntMap(nil), 10)) 66 gtest.False(gg.MapHas(IntMap{10: 20}, 20)) 67 gtest.True(gg.MapHas(IntMap{10: 20}, 10)) 68 } 69 70 func TestMapGot(t *testing.T) { 71 defer gtest.Catch(t) 72 73 { 74 val, ok := gg.MapGot(IntMap(nil), 10) 75 gtest.Zero(val) 76 gtest.False(ok) 77 } 78 79 { 80 val, ok := gg.MapGot(IntMap{10: 20}, 20) 81 gtest.Zero(val) 82 gtest.False(ok) 83 } 84 85 { 86 val, ok := gg.MapGot(IntMap{10: 20}, 10) 87 gtest.Eq(val, 20) 88 gtest.True(ok) 89 } 90 } 91 92 func TestMapGet(t *testing.T) { 93 defer gtest.Catch(t) 94 95 gtest.Zero(gg.MapGet(IntMap(nil), 10)) 96 gtest.Zero(gg.MapGet(IntMap{10: 20}, 20)) 97 gtest.Eq(gg.MapGet(IntMap{10: 20}, 10), 20) 98 } 99 100 func TestMapSet(t *testing.T) { 101 defer gtest.Catch(t) 102 103 tar := IntMap{} 104 105 gg.MapSet(tar, 10, 20) 106 gtest.Equal(tar, IntMap{10: 20}) 107 108 gg.MapSet(tar, 10, 30) 109 gtest.Equal(tar, IntMap{10: 30}) 110 } 111 112 func TestMapSetOpt(t *testing.T) { 113 defer gtest.Catch(t) 114 115 tar := IntMap{} 116 117 gg.MapSetOpt(tar, 0, 20) 118 gtest.Equal(tar, IntMap{}) 119 120 gg.MapSetOpt(tar, 10, 0) 121 gtest.Equal(tar, IntMap{}) 122 123 gg.MapSetOpt(tar, 10, 20) 124 gtest.Equal(tar, IntMap{10: 20}) 125 126 gg.MapSetOpt(tar, 10, 30) 127 gtest.Equal(tar, IntMap{10: 30}) 128 } 129 130 func TestMapClear(t *testing.T) { 131 defer gtest.Catch(t) 132 133 var tar IntMap 134 gg.MapClear(tar) 135 gtest.Equal(tar, IntMap(nil)) 136 137 tar = IntMap{10: 20, 30: 40} 138 gg.MapClear(tar) 139 gtest.Equal(tar, IntMap{}) 140 } 141 142 /* 143 func Benchmark_map_iteration(b *testing.B) { 144 tar := make(map[string]float64) 145 for ind := range gg.Iter(1024) { 146 tar[gg.String(ind)] = float64((ind % 2) * ind) 147 } 148 149 b.ResetTimer() 150 151 for ind := 0; ind < b.N; ind++ { 152 for key, val := range tar { 153 gg.Nop2(key, val) 154 } 155 } 156 } 157 */