github.com/tawesoft/golib/v2@v2.10.0/legacy/operator/int_test.go (about) 1 package operator 2 3 import ( 4 "math" 5 "testing" 6 ) 7 8 func TestIntMSB(t *testing.T) { 9 type test struct { 10 n int 11 msb int 12 } 13 14 var tests = []test { 15 // 8421 16 { 0, 0}, // 1100 17 { 1, 1}, // 1100 18 { 8, 4}, // 1000 19 {12, 4}, // 1100 20 {int(maxInt32), 31}, 21 } 22 23 for idx, i := range tests { 24 var result = intMostSignificantBit(i.n) 25 if result != i.msb { 26 t.Errorf("test %d: got %d, but expected %d", idx, result, i.msb) 27 } 28 } 29 } 30 31 func TestIntBinary(t *testing.T) { 32 type test struct { 33 a int 34 b int 35 f func(int, int) int 36 expected int 37 } 38 39 var tests = []test { 40 {5, 7, Int.Binary.Add, 12}, 41 {7, 5, Int.Binary.Sub, 2}, 42 {5, 7, Int.Binary.Sub, -2}, 43 {5, 7, Int.Binary.Mul, 35}, 44 {6, 2, Int.Binary.Div, 3}, 45 {1+2+4, 2+8, Int.Binary.And, 2}, 46 {1+2+4, 2+8, Int.Binary.Or, 15}, 47 {1+2+4, 2+8, Int.Binary.Xor, 13}, 48 {1+2+4, 2+8, Int.Binary.AndNot, 5}, // 0111 &^ 1010 = 0111 & 0101 = 0101 = 5 49 } 50 51 type testShift struct { 52 a int 53 b uint 54 f func(int, uint) int 55 expected int 56 } 57 58 var testsShift = []testShift { 59 {3, 2, Int.Binary.Shl, 12}, // 0011 << 2 = 1100 = 12 60 {12, 2, Int.Binary.Shr, 3}, // 1100 >> 2 = 0011 = 3 61 } 62 63 for idx, i := range tests { 64 var result = i.f(i.a, i.b) 65 if result != i.expected { 66 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expected) 67 } 68 } 69 70 for idx, i := range testsShift { 71 var result = i.f(i.a, i.b) 72 if result != i.expected { 73 t.Errorf("shift test %d: got %d, but expected %d", idx, result, i.expected) 74 } 75 } 76 } 77 78 79 func TestIntCheckedShl(t *testing.T) { 80 type test struct { 81 a int32 82 b uint 83 expectedValue int32 84 expectedError error 85 } 86 87 var tests = []test { 88 {1, 1, 2, nil}, 89 {3, 1, 6, nil}, 90 {-1, 1, 0, ErrorUndefined}, 91 {maxInt32, 1, 0, ErrorOverflow}, 92 {int32(math.Exp2(31) - 1), 1, 0, ErrorOverflow}, 93 {int32(math.Exp2(30)), 2, 0, ErrorOverflow}, 94 {int32(math.Exp2(29)), 1, int32(math.Exp(30)), nil}, 95 {int32(math.Exp2(30)), 1, 0, ErrorOverflow}, 96 } 97 98 for idx, i := range tests { 99 var result, err = int32BinaryCheckedShl(i.a, i.b) 100 if err == i.expectedError { 101 // pass 102 } else if err != nil { 103 t.Errorf("test %d: unexpected error %v (expected %v)", idx, err, i.expectedError) 104 } else if result != i.expectedValue { 105 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expectedValue) 106 } 107 } 108 } 109 110 func TestUintCheckedShl(t *testing.T) { 111 type test struct { 112 a uint32 113 b uint 114 expectedValue uint32 115 expectedError error 116 } 117 118 var tests = []test { 119 {1, 1, 2, nil}, 120 {3, 1, 6, nil}, 121 {maxInt32, 1, 0, ErrorOverflow}, 122 {uint32(math.Exp2(31)), 1, 0, ErrorOverflow}, 123 {uint32(math.Exp2(32) - 1), 1, 0, ErrorOverflow}, 124 {uint32(math.Exp2(30)), 1, uint32(math.Exp(31)), nil}, 125 {uint32(math.Exp2(31)), 1, uint32(math.Exp(32)), nil}, 126 {uint32(math.Exp2(30)), 2, uint32(math.Exp(32)), nil}, 127 } 128 129 for idx, i := range tests { 130 var result, err = uint32BinaryCheckedShl(i.a, i.b) 131 if err == i.expectedError { 132 // pass 133 } else if err != nil { 134 t.Errorf("test %d: unexpected error %v (expected %v)", idx, err, i.expectedError) 135 } else if result != i.expectedValue { 136 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expectedValue) 137 } 138 } 139 } 140 141 func TestIntCheckedAdd(t *testing.T) { 142 type test struct { 143 a int8 144 b int8 145 expectedValue int8 146 expectedError error 147 } 148 149 var tests = []test { 150 { -1, -1, -2, nil}, 151 { 1, -1, 0, nil}, 152 { 0, 0, 0, nil}, 153 { 8, 4, 12, nil}, 154 { 64, 63, 127, nil}, 155 { 64, 63, 127, nil}, 156 { -64, -64, -128, nil}, 157 { 64, 64, 0, ErrorOverflow}, 158 { -64, -65, 0, ErrorOverflow}, 159 } 160 161 for idx, i := range tests { 162 var result, err = int8BinaryCheckedAdd(i.a, i.b) 163 if err == i.expectedError { 164 // pass 165 } else if err != nil { 166 t.Errorf("test %d: unexpected error %v (expected %v)", idx, err, i.expectedError) 167 } else if result != i.expectedValue { 168 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expectedValue) 169 } 170 } 171 } 172 173 func TestUintCheckedAdd(t *testing.T) { 174 type test struct { 175 a uint8 176 b uint8 177 expectedValue uint8 178 expectedError error 179 } 180 181 var tests = []test { 182 { 0, 0, 0, nil}, 183 { 8, 4, 12, nil}, 184 { 128, 127, 255, nil}, 185 { 128, 128, 0, ErrorOverflow}, 186 } 187 188 for idx, i := range tests { 189 var result, err = uint8BinaryCheckedAdd(i.a, i.b) 190 if err == i.expectedError { 191 // pass 192 } else if err != nil { 193 t.Errorf("test %d: unexpected error %v (expected %v)", idx, err, i.expectedError) 194 } else if result != i.expectedValue { 195 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expectedValue) 196 } 197 } 198 } 199 200 func TestIntNary(t *testing.T) { 201 type test struct { 202 xs []int 203 f func(...int) int 204 expected int 205 } 206 207 var tests = []test { 208 {[]int{1, 2, 3, 4, 5, 6}, Int.Nary.Add, 21}, 209 {[]int{1, 2, 3, 4, 5, 6}, Int.Nary.Mul, 720}, 210 } 211 212 for idx, i := range tests { 213 var result = i.f(i.xs...) 214 if result != i.expected { 215 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expected) 216 } 217 } 218 } 219 220 func TestIntNaryChecked(t *testing.T) { 221 type test struct { 222 xs []int8 223 f func(...int8) (int8, error) 224 expectedValue int8 225 expectedError error 226 } 227 228 var tests = []test { 229 {[]int8{1, 2, 3, 4, 5, 6}, Int8Checked.Nary.Add, 21, nil}, 230 {[]int8{1, 2, 3, 4}, Int8Checked.Nary.Mul, 24, nil}, 231 {[]int8{120, -120, -120, -8, -1}, Int8Checked.Nary.Add, 0, ErrorOverflow}, 232 {[]int8{120, 4, 4}, Int8Checked.Nary.Add, 0, ErrorOverflow}, 233 {[]int8{32, 2, 2}, Int8Checked.Nary.Mul, 0, ErrorOverflow}, 234 } 235 236 for idx, i := range tests { 237 var result, err = i.f(i.xs...) 238 if err == i.expectedError { 239 // pass 240 } else if err != nil { 241 t.Errorf("test %d: unexpected error %v (expected %v)", idx, err, i.expectedError) 242 } else if result != i.expectedValue { 243 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expectedValue) 244 } 245 } 246 } 247 248 func TestIntReduce(t *testing.T) { 249 type test struct { 250 operatorIdentity int 251 xs []int 252 f func(int, int) int 253 expected int 254 } 255 256 var tests = []test { 257 {0, []int{1, 2, 3, 4, 5, 6}, Int.Binary.Add, 21}, 258 {1, []int{1, 2, 3, 4, 5, 6}, Int.Binary.Mul, 720}, 259 } 260 261 for idx, i := range tests { 262 var result = Int.Reduce(i.operatorIdentity, i.f, i.xs...) 263 if result != i.expected { 264 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expected) 265 } 266 } 267 } 268 269 func TestIntCheckedReduce(t *testing.T) { 270 type test struct { 271 operatorIdentity int8 272 xs []int8 273 f func(int8, int8) (int8, error) 274 expectedValue int8 275 expectedError error 276 } 277 278 var tests = []test { 279 {0, []int8{1, 2, 3, 4, 5, 6}, Int8Checked.Binary.Add, 21, nil}, 280 {1, []int8{1, 2, 3, 4}, Int8Checked.Binary.Mul, 24, nil}, 281 {0, []int8{120, -120, -120, -8, -1}, Int8Checked.Binary.Add, 0, ErrorOverflow}, 282 {0, []int8{120, 4, 4}, Int8Checked.Binary.Add, 0, ErrorOverflow}, 283 {1, []int8{32, 2, 2}, Int8Checked.Binary.Mul, 0, ErrorOverflow}, 284 } 285 286 for idx, i := range tests { 287 var result, err = Int8Checked.Reduce(i.operatorIdentity, i.f, i.xs...) 288 if err == i.expectedError { 289 // pass 290 } else if err != nil { 291 t.Errorf("test %d: unexpected error %v (expected %v)", idx, err, i.expectedError) 292 } else if result != i.expectedValue { 293 t.Errorf("test %d: got %d, but expected %d", idx, result, i.expectedValue) 294 } 295 } 296 }