github.com/jxskiss/gopkg@v0.17.3/gemap/utils_test.go (about) 1 package gemap 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 type simple struct { 9 A string 10 } 11 12 var mapKeyValueTests = []map[string]interface{}{ 13 { 14 "map": map[int]int{1: 11, 2: 12, 3: 13}, 15 "keys": []int{1, 2, 3}, 16 "values": []int{11, 12, 13}, 17 }, 18 { 19 "map": map[string]string{"1": "11", "2": "12", "3": "13"}, 20 "keys": []string{"1", "2", "3"}, 21 "values": []string{"11", "12", "13"}, 22 }, 23 { 24 "map": map[simple]simple{ 25 {"1"}: {"11"}, 26 {"2"}: {"12"}, 27 {"3"}: {"13"}, 28 }, 29 "keys": []simple{{"1"}, {"2"}, {"3"}}, 30 "values": []simple{{"11"}, {"12"}, {"13"}}, 31 }, 32 { 33 "map": map[int32]string{1: "11", 2: "12", 3: "13"}, 34 "keys": []int32{1, 2, 3}, 35 "values": []string{"11", "12", "13"}, 36 }, 37 { 38 "map": map[int64]int64{1: 11, 2: 12, 3: 13}, 39 "keys": []int64{1, 2, 3}, 40 "values": []int64{11, 12, 13}, 41 }, 42 } 43 44 func TestMapKeysValues(t *testing.T) { 45 for _, test := range mapKeyValueTests { 46 keys := MapKeys(test["map"]) 47 values := MapValues(test["map"]) 48 assert.ElementsMatch(t, test["keys"], keys) 49 assert.ElementsMatch(t, test["values"], values) 50 } 51 } 52 53 func TestKeysValues_Int_String(t *testing.T) { 54 intMap := map[uint16]string{1: "a", 2: "b", 3: "c"} 55 stringMap := map[string]uint8{"a": 1, "b": 2, "c": 3} 56 assert.Panics(t, func() { _ = IntValues(intMap) }) 57 assert.Panics(t, func() { _ = StringKeys(intMap) }) 58 assert.Panics(t, func() { _ = IntKeys(stringMap) }) 59 assert.Panics(t, func() { _ = StringValues(stringMap) }) 60 61 intWant := []int64{1, 2, 3} 62 strWant := []string{"a", "b", "c"} 63 assert.ElementsMatch(t, intWant, IntKeys(intMap)) 64 assert.ElementsMatch(t, intWant, IntValues(stringMap)) 65 assert.ElementsMatch(t, strWant, StringValues(intMap)) 66 assert.ElementsMatch(t, strWant, StringKeys(stringMap)) 67 } 68 69 func TestMapKeysValues_panic(t *testing.T) { 70 notMapTests := []interface{}{ 71 123, 72 []int{1, 2, 3}, 73 simple{"a"}, 74 &simple{"b"}, 75 []*simple{{"a"}, {"b"}, {"c"}}, 76 []string{}, 77 } 78 for _, test := range notMapTests { 79 assert.Panics(t, func() { _ = MapKeys(test) }) 80 assert.Panics(t, func() { _ = MapValues(test) }) 81 assert.Panics(t, func() { _ = IntKeys(test) }) 82 assert.Panics(t, func() { _ = IntValues(test) }) 83 assert.Panics(t, func() { _ = StringKeys(test) }) 84 assert.Panics(t, func() { _ = StringValues(test) }) 85 } 86 } 87 88 var intKeysTests = []map[string]interface{}{ 89 { 90 "map": map[int]int{1: 11, 2: 12, 3: 13}, 91 "keys": []int64{1, 2, 3}, 92 }, 93 { 94 "map": map[int32]int{1: 11, 2: 12, 3: 13}, 95 "keys": []int64{1, 2, 3}, 96 }, 97 { 98 "map": map[int64]int{1: 11, 2: 13, 3: 13}, 99 "keys": []int64{1, 2, 3}, 100 }, 101 { 102 "map": map[uint]string{1: "11", 2: "12", 3: "13"}, 103 "keys": []int64{1, 2, 3}, 104 }, 105 { 106 "map": map[uint64]simple{ 107 1: {"11"}, 2: {"12"}, 3: {"13"}, 108 }, 109 "keys": []int64{1, 2, 3}, 110 }, 111 } 112 113 func TestIntKeys(t *testing.T) { 114 for _, test := range intKeysTests { 115 got := IntKeys(test["map"]) 116 assert.ElementsMatch(t, test["keys"], got) 117 } 118 } 119 120 var intValuesTests = []map[string]interface{}{ 121 { 122 "map": map[int]int{1: 11, 2: 12, 3: 13}, 123 "values": []int64{11, 12, 13}, 124 }, 125 { 126 "map": map[int32]uint16{1: 11, 2: 12, 3: 13}, 127 "values": []int64{11, 12, 13}, 128 }, 129 { 130 "map": map[int64]int64{1: 11, 2: 12, 3: 13}, 131 "values": []int64{11, 12, 13}, 132 }, 133 { 134 "map": map[string]uint{"1": 11, "2": 12, "3": 13}, 135 "values": []int64{11, 12, 13}, 136 }, 137 { 138 "map": map[simple]int32{ 139 {"1"}: 11, {"2"}: 12, {"3"}: 13, 140 }, 141 "values": []int64{11, 12, 13}, 142 }, 143 } 144 145 func TestIntValues(t *testing.T) { 146 for _, test := range intValuesTests { 147 got := IntValues(test["map"]) 148 assert.ElementsMatch(t, test["values"], got) 149 } 150 } 151 152 func TestStringKeysValues(t *testing.T) { 153 m := map[string]string{"1": "11", "2": "12", "3": "13"} 154 assert.ElementsMatch(t, []string{"1", "2", "3"}, StringKeys(m)) 155 assert.ElementsMatch(t, []string{"11", "12", "13"}, StringValues(m)) 156 } 157 158 func TestMergeMaps(t *testing.T) { 159 m1 := map[int64]int64{1: 2, 3: 4, 5: 6} 160 m2 := map[int64]int64{7: 8, 9: 10} 161 got := MergeMaps(m1, m2).(map[int64]int64) 162 assert.Equal(t, 3, len(m1)) 163 assert.Equal(t, 2, len(m2)) 164 assert.Equal(t, 5, len(got)) 165 assert.Equal(t, int64(4), got[3]) 166 assert.Equal(t, int64(8), got[7]) 167 } 168 169 func TestMergeMapsTo(t *testing.T) { 170 m1 := map[int64]int64{1: 2, 3: 4, 5: 6} 171 m2 := map[int64]int64{7: 8, 9: 10} 172 _ = MergeMapsTo(m1, m2).(map[int64]int64) 173 assert.Equal(t, 5, len(m1)) 174 assert.Equal(t, 2, len(m2)) 175 assert.Equal(t, int64(4), m1[3]) 176 assert.Equal(t, int64(8), m1[7]) 177 assert.Equal(t, int64(10), m1[9]) 178 179 // merge to a nil map 180 var m3 map[int64]int64 181 m4 := MergeMapsTo(m3, m1).(map[int64]int64) 182 assert.Nil(t, m3) 183 assert.Equal(t, 5, len(m4)) 184 assert.Equal(t, int64(4), m4[3]) 185 assert.Equal(t, int64(10), m4[9]) 186 } 187 188 var benchmarkMapData = map[int]*simple{ 189 1: {"abc"}, 190 2: {"bcd"}, 191 3: {"cde"}, 192 4: {"def"}, 193 5: {"efg"}, 194 6: {"fgh"}, 195 7: {"ghi"}, 196 8: {"hij"}, 197 9: {"ijk"}, 198 10: {"jkl"}, 199 } 200 201 func BenchmarkMapKeys_static(b *testing.B) { 202 203 MapKeys_static := func(m map[int]*simple) []int { 204 keys := make([]int, 0, len(benchmarkMapData)) 205 for k := range benchmarkMapData { 206 keys = append(keys, k) 207 } 208 return keys 209 } 210 211 b.ReportAllocs() 212 for i := 0; i < b.N; i++ { 213 _ = MapKeys_static(benchmarkMapData) 214 } 215 } 216 217 func BenchmarkMapKeys_reflect(b *testing.B) { 218 b.ReportAllocs() 219 for i := 0; i < b.N; i++ { 220 _ = MapKeys(benchmarkMapData) 221 } 222 } 223 224 func BenchmarkMapKeys_int64(b *testing.B) { 225 b.ReportAllocs() 226 for i := 0; i < b.N; i++ { 227 _ = IntKeys(benchmarkMapData) 228 } 229 } 230 231 func BenchmarkMapValues_static(b *testing.B) { 232 233 MapValues_static := func(m map[int]*simple) []*simple { 234 values := make([]*simple, 0, len(benchmarkMapData)) 235 for _, v := range benchmarkMapData { 236 values = append(values, v) 237 } 238 return values 239 } 240 241 b.ReportAllocs() 242 for i := 0; i < b.N; i++ { 243 _ = MapValues_static(benchmarkMapData) 244 } 245 } 246 247 func BenchmarkMapValues_reflect(b *testing.B) { 248 b.ReportAllocs() 249 for i := 0; i < b.N; i++ { 250 _ = MapValues(benchmarkMapData) 251 } 252 }