github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/math/checked/checked_test.go (about) 1 package checked 2 3 import ( 4 "math" 5 "reflect" 6 "runtime" 7 "strings" 8 "testing" 9 ) 10 11 func TestInt64(t *testing.T) { 12 cases := []struct { 13 f func(a, b int64) (int64, bool) 14 a, b, want int64 15 wantOk bool 16 }{ 17 {AddInt64, 2, 3, 5, true}, 18 {AddInt64, 2, -3, -1, true}, 19 {AddInt64, -2, -3, -5, true}, 20 {AddInt64, math.MaxInt64, 1, 0, false}, 21 {AddInt64, math.MinInt64, math.MinInt64, 0, false}, 22 {AddInt64, math.MinInt64, -1, 0, false}, 23 {SubInt64, 3, 2, 1, true}, 24 {SubInt64, 2, 3, -1, true}, 25 {SubInt64, -2, -3, 1, true}, 26 {SubInt64, math.MinInt64, 1, 0, false}, 27 {SubInt64, -2, math.MaxInt64, 0, false}, 28 {MulInt64, 2, 3, 6, true}, 29 {MulInt64, -2, -3, 6, true}, 30 {MulInt64, -2, 3, -6, true}, 31 {MulInt64, math.MaxInt64, -1, math.MinInt64 + 1, true}, 32 {MulInt64, math.MinInt64, 2, 0, false}, 33 {MulInt64, math.MaxInt64, 2, 0, false}, 34 {MulInt64, 2, math.MinInt64, 0, false}, 35 {MulInt64, -2, math.MinInt64, 0, false}, 36 {DivInt64, 2, 2, 1, true}, 37 {DivInt64, -2, -2, 1, true}, 38 {DivInt64, -2, 2, -1, true}, 39 {DivInt64, 1, 0, 0, false}, 40 {DivInt64, math.MinInt64, -1, 0, false}, 41 {ModInt64, 3, 2, 1, true}, 42 {ModInt64, -3, -2, -1, true}, 43 {ModInt64, -3, 2, -1, true}, 44 {ModInt64, 1, 0, 0, false}, 45 {ModInt64, math.MinInt64, -1, 0, false}, 46 {LshiftInt64, 1, 2, 4, true}, 47 {LshiftInt64, -1, 2, -4, true}, 48 {LshiftInt64, 1, 64, 0, false}, 49 {LshiftInt64, 2, 63, 0, false}, 50 } 51 52 for _, c := range cases { 53 got, gotOk := c.f(c.a, c.b) 54 55 if got != c.want { 56 t.Errorf("%s(%d, %d) = %d want %d", fname(c.f), c.a, c.b, got, c.want) 57 } 58 59 if gotOk != c.wantOk { 60 t.Errorf("%s(%d, %d) ok = %v want %v", fname(c.f), c.a, c.b, gotOk, c.wantOk) 61 } 62 } 63 64 negateCases := []struct { 65 a, want int64 66 wantOk bool 67 }{ 68 {1, -1, true}, 69 {-1, 1, true}, 70 {0, 0, true}, 71 {math.MinInt64, 0, false}, 72 } 73 for _, c := range negateCases { 74 got, gotOk := NegateInt64(c.a) 75 76 if got != c.want { 77 t.Errorf("NegateInt64(%d) = %d want %d", c.a, got, c.want) 78 } 79 80 if gotOk != c.wantOk { 81 t.Errorf("NegateInt64(%d) ok = %v want %v", c.a, gotOk, c.wantOk) 82 } 83 } 84 } 85 86 func TestUint64(t *testing.T) { 87 cases := []struct { 88 f func(a, b uint64) (uint64, bool) 89 a, b, want uint64 90 wantOk bool 91 }{ 92 {AddUint64, 2, 3, 5, true}, 93 {AddUint64, math.MaxUint64, 1, 0, false}, 94 {SubUint64, 3, 2, 1, true}, 95 {SubUint64, 2, 3, 0, false}, 96 {MulUint64, 2, 3, 6, true}, 97 {MulUint64, math.MaxUint64, 2, 0, false}, 98 {DivUint64, 2, 2, 1, true}, 99 {DivUint64, 1, 0, 0, false}, 100 {ModUint64, 3, 2, 1, true}, 101 {ModUint64, 1, 0, 0, false}, 102 {LshiftUint64, 1, 2, 4, true}, 103 {LshiftUint64, 1, 64, 0, false}, 104 {LshiftUint64, 2, 63, 0, false}, 105 } 106 107 for _, c := range cases { 108 got, gotOk := c.f(c.a, c.b) 109 110 if got != c.want { 111 t.Errorf("%s(%d, %d) = %d want %d", fname(c.f), c.a, c.b, got, c.want) 112 } 113 114 if gotOk != c.wantOk { 115 t.Errorf("%s(%d, %d) ok = %v want %v", fname(c.f), c.a, c.b, gotOk, c.wantOk) 116 } 117 } 118 } 119 120 func TestInt32(t *testing.T) { 121 cases := []struct { 122 f func(a, b int32) (int32, bool) 123 a, b, want int32 124 wantOk bool 125 }{ 126 {AddInt32, 2, 3, 5, true}, 127 {AddInt32, 2, -3, -1, true}, 128 {AddInt32, -2, -3, -5, true}, 129 {AddInt32, math.MaxInt32, 1, 0, false}, 130 {AddInt32, math.MinInt32, math.MinInt32, 0, false}, 131 {AddInt32, math.MinInt32, -1, 0, false}, 132 {SubInt32, 3, 2, 1, true}, 133 {SubInt32, 2, 3, -1, true}, 134 {SubInt32, -2, -3, 1, true}, 135 {SubInt32, math.MinInt32, 1, 0, false}, 136 {SubInt32, -2, math.MaxInt32, 0, false}, 137 {MulInt32, 2, 3, 6, true}, 138 {MulInt32, -2, -3, 6, true}, 139 {MulInt32, -2, 3, -6, true}, 140 {MulInt32, math.MaxInt32, -1, math.MinInt32 + 1, true}, 141 {MulInt32, math.MinInt32, 2, 0, false}, 142 {MulInt32, math.MaxInt32, 2, 0, false}, 143 {MulInt32, 2, math.MinInt32, 0, false}, 144 {MulInt32, -2, math.MinInt32, 0, false}, 145 {DivInt32, 2, 2, 1, true}, 146 {DivInt32, -2, -2, 1, true}, 147 {DivInt32, -2, 2, -1, true}, 148 {DivInt32, 1, 0, 0, false}, 149 {DivInt32, math.MinInt32, -1, 0, false}, 150 {ModInt32, 3, 2, 1, true}, 151 {ModInt32, -3, -2, -1, true}, 152 {ModInt32, -3, 2, -1, true}, 153 {ModInt32, 1, 0, 0, false}, 154 {ModInt32, math.MinInt32, -1, 0, false}, 155 {LshiftInt32, 1, 2, 4, true}, 156 {LshiftInt32, -1, 2, -4, true}, 157 {LshiftInt32, 1, 32, 0, false}, 158 {LshiftInt32, 2, 31, 0, false}, 159 } 160 161 for _, c := range cases { 162 got, gotOk := c.f(c.a, c.b) 163 164 if got != c.want { 165 t.Errorf("%s(%d, %d) = %d want %d", fname(c.f), c.a, c.b, got, c.want) 166 } 167 168 if gotOk != c.wantOk { 169 t.Errorf("%s(%d, %d) ok = %v want %v", fname(c.f), c.a, c.b, gotOk, c.wantOk) 170 } 171 } 172 173 negateCases := []struct { 174 a, want int32 175 wantOk bool 176 }{ 177 {1, -1, true}, 178 {-1, 1, true}, 179 {0, 0, true}, 180 {math.MinInt32, 0, false}, 181 } 182 for _, c := range negateCases { 183 got, gotOk := NegateInt32(c.a) 184 185 if got != c.want { 186 t.Errorf("NegateInt32(%d) = %d want %d", c.a, got, c.want) 187 } 188 189 if gotOk != c.wantOk { 190 t.Errorf("NegateInt32(%d) ok = %v want %v", c.a, gotOk, c.wantOk) 191 } 192 } 193 } 194 195 func TestUint32(t *testing.T) { 196 cases := []struct { 197 f func(a, b uint32) (uint32, bool) 198 a, b, want uint32 199 wantOk bool 200 }{ 201 {AddUint32, 2, 3, 5, true}, 202 {AddUint32, math.MaxUint32, 1, 0, false}, 203 {SubUint32, 3, 2, 1, true}, 204 {SubUint32, 2, 3, 0, false}, 205 {MulUint32, 2, 3, 6, true}, 206 {MulUint32, math.MaxUint32, 2, 0, false}, 207 {DivUint32, 2, 2, 1, true}, 208 {DivUint32, 1, 0, 0, false}, 209 {ModUint32, 3, 2, 1, true}, 210 {ModUint32, 1, 0, 0, false}, 211 {LshiftUint32, 1, 2, 4, true}, 212 {LshiftUint32, 1, 32, 0, false}, 213 {LshiftUint32, 2, 31, 0, false}, 214 } 215 216 for _, c := range cases { 217 got, gotOk := c.f(c.a, c.b) 218 219 if got != c.want { 220 t.Errorf("%s(%d, %d) = %d want %d", fname(c.f), c.a, c.b, got, c.want) 221 } 222 223 if gotOk != c.wantOk { 224 t.Errorf("%s(%d, %d) ok = %v want %v", fname(c.f), c.a, c.b, gotOk, c.wantOk) 225 } 226 } 227 } 228 229 func fname(f interface{}) string { 230 name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() 231 return name[strings.IndexRune(name, '.')+1:] 232 }