github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bmap/bmap_test.go (about) 1 package bmap 2 3 import ( 4 "github.com/songzhibin97/go-baseutils/base/bslice" 5 "math" 6 "sort" 7 "strconv" 8 "testing" 9 ) 10 11 var m1 = map[int]int{1: 2, 2: 4, 4: 8, 8: 16} 12 var m2 = map[int]string{1: "2", 2: "4", 4: "8", 8: "16"} 13 14 func TestKeys(t *testing.T) { 15 want := []int{1, 2, 4, 8} 16 17 got1 := Keys(m1) 18 sort.Ints(got1) 19 if !bslice.Equal(got1, want) { 20 t.Errorf("Keys(%v) = %v, want %v", m1, got1, want) 21 } 22 23 got2 := Keys(m2) 24 sort.Ints(got2) 25 if !bslice.Equal(got2, want) { 26 t.Errorf("Keys(%v) = %v, want %v", m2, got2, want) 27 } 28 } 29 30 func TestValues(t *testing.T) { 31 got1 := Values(m1) 32 want1 := []int{2, 4, 8, 16} 33 sort.Ints(got1) 34 if !bslice.Equal(got1, want1) { 35 t.Errorf("Values(%v) = %v, want %v", m1, got1, want1) 36 } 37 38 got2 := Values(m2) 39 want2 := []string{"16", "2", "4", "8"} 40 sort.Strings(got2) 41 if !bslice.Equal(got2, want2) { 42 t.Errorf("Values(%v) = %v, want %v", m2, got2, want2) 43 } 44 } 45 46 func TestEqual(t *testing.T) { 47 if !Equal(m1, m1) { 48 t.Errorf("Equal(%v, %v) = false, want true", m1, m1) 49 } 50 if Equal(m1, (map[int]int)(nil)) { 51 t.Errorf("Equal(%v, nil) = true, want false", m1) 52 } 53 if Equal((map[int]int)(nil), m1) { 54 t.Errorf("Equal(nil, %v) = true, want false", m1) 55 } 56 if !Equal[map[int]int, map[int]int](nil, nil) { 57 t.Error("Equal(nil, nil) = false, want true") 58 } 59 if ms := map[int]int{1: 2}; Equal(m1, ms) { 60 t.Errorf("Equal(%v, %v) = true, want false", m1, ms) 61 } 62 63 // Comparing NaN for equality is expected to fail. 64 mf := map[int]float64{1: 0, 2: math.NaN()} 65 if Equal(mf, mf) { 66 t.Errorf("Equal(%v, %v) = true, want false", mf, mf) 67 } 68 } 69 70 // equal is simply ==. 71 func equal[T comparable](v1, v2 T) bool { 72 return v1 == v2 73 } 74 75 // equalNaN is like == except that all NaNs are equal. 76 func equalNaN[T comparable](v1, v2 T) bool { 77 isNaN := func(f T) bool { return f != f } 78 return v1 == v2 || (isNaN(v1) && isNaN(v2)) 79 } 80 81 // equalStr compares ints and strings. 82 func equalIntStr(v1 int, v2 string) bool { 83 return strconv.Itoa(v1) == v2 84 } 85 86 func TestEqualFunc(t *testing.T) { 87 if !EqualFunc(m1, m1, equal[int]) { 88 t.Errorf("EqualFunc(%v, %v, equal) = false, want true", m1, m1) 89 } 90 if EqualFunc(m1, (map[int]int)(nil), equal[int]) { 91 t.Errorf("EqualFunc(%v, nil, equal) = true, want false", m1) 92 } 93 if EqualFunc((map[int]int)(nil), m1, equal[int]) { 94 t.Errorf("EqualFunc(nil, %v, equal) = true, want false", m1) 95 } 96 if !EqualFunc[map[int]int, map[int]int](nil, nil, equal[int]) { 97 t.Error("EqualFunc(nil, nil, equal) = false, want true") 98 } 99 if ms := map[int]int{1: 2}; EqualFunc(m1, ms, equal[int]) { 100 t.Errorf("EqualFunc(%v, %v, equal) = true, want false", m1, ms) 101 } 102 103 // Comparing NaN for equality is expected to fail. 104 mf := map[int]float64{1: 0, 2: math.NaN()} 105 if EqualFunc(mf, mf, equal[float64]) { 106 t.Errorf("EqualFunc(%v, %v, equal) = true, want false", mf, mf) 107 } 108 // But it should succeed using equalNaN. 109 if !EqualFunc(mf, mf, equalNaN[float64]) { 110 t.Errorf("EqualFunc(%v, %v, equalNaN) = false, want true", mf, mf) 111 } 112 113 if !EqualFunc(m1, m2, equalIntStr) { 114 t.Errorf("EqualFunc(%v, %v, equalIntStr) = false, want true", m1, m2) 115 } 116 } 117 118 func TestClear(t *testing.T) { 119 ml := map[int]int{1: 1, 2: 2, 3: 3} 120 Clear(ml) 121 if got := len(ml); got != 0 { 122 t.Errorf("len(%v) = %d after Clear, want 0", ml, got) 123 } 124 if !Equal(ml, (map[int]int)(nil)) { 125 t.Errorf("Equal(%v, nil) = false, want true", ml) 126 } 127 } 128 129 func TestClone(t *testing.T) { 130 mc := Clone(m1) 131 if !Equal(mc, m1) { 132 t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1) 133 } 134 mc[16] = 32 135 if Equal(mc, m1) { 136 t.Errorf("Equal(%v, %v) = true, want false", mc, m1) 137 } 138 } 139 140 func TestCloneNil(t *testing.T) { 141 var m1 map[string]int 142 mc := Clone(m1) 143 if mc != nil { 144 t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1) 145 } 146 } 147 148 func TestCopy(t *testing.T) { 149 mc := Clone(m1) 150 Copy(mc, mc) 151 if !Equal(mc, m1) { 152 t.Errorf("Copy(%v, %v) = %v, want %v", m1, m1, mc, m1) 153 } 154 Copy(mc, map[int]int{16: 32}) 155 want := map[int]int{1: 2, 2: 4, 4: 8, 8: 16, 16: 32} 156 if !Equal(mc, want) { 157 t.Errorf("Copy result = %v, want %v", mc, want) 158 } 159 160 type M1 map[int]bool 161 type M2 map[int]bool 162 Copy(make(M1), make(M2)) 163 } 164 165 func TestDeleteFunc(t *testing.T) { 166 mc := Clone(m1) 167 DeleteFunc(mc, func(int, int) bool { return false }) 168 if !Equal(mc, m1) { 169 t.Errorf("DeleteFunc(%v, true) = %v, want %v", m1, mc, m1) 170 } 171 DeleteFunc(mc, func(k, v int) bool { return k > 3 }) 172 want := map[int]int{1: 2, 2: 4} 173 if !Equal(mc, want) { 174 t.Errorf("DeleteFunc result = %v, want %v", mc, want) 175 } 176 }