github.com/enetx/g@v1.0.80/tests/int_test.go (about) 1 package g_test 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/enetx/g" 8 "github.com/enetx/g/cmp" 9 ) 10 11 func TestIntAbs(t *testing.T) { 12 // Test positive integer 13 posInt := g.Int(5) 14 posAbs := posInt.Abs() 15 if posAbs != posInt { 16 t.Errorf("Abs function incorrect for positive integer. Expected: %d, Got: %d", posInt, posAbs) 17 } 18 19 // Test negative integer 20 negInt := g.Int(-5) 21 negAbs := negInt.Abs() 22 if negAbs != posInt { 23 t.Errorf("Abs function incorrect for negative integer. Expected: %d, Got: %d", posInt, negAbs) 24 } 25 26 // Test zero 27 zero := g.Int(0) 28 zeroAbs := zero.Abs() 29 if zeroAbs != zero { 30 t.Errorf("Abs function incorrect for zero. Expected: %d, Got: %d", zero, zeroAbs) 31 } 32 } 33 34 func TestIntToInt(t *testing.T) { 35 // Test ToBigInt 36 intVal := g.Int(123) 37 bigInt := intVal.ToBigInt() 38 expectedBigInt := big.NewInt(123) 39 if bigInt.Cmp(expectedBigInt) != 0 { 40 t.Errorf("ToBigInt function incorrect. Expected: %s, Got: %s", expectedBigInt, bigInt) 41 } 42 43 // Test Div 44 dividend := g.Int(10) 45 divisor := g.Int(2) 46 quotient := dividend.Div(divisor) 47 expectedQuotient := g.Int(5) 48 if quotient != expectedQuotient { 49 t.Errorf("Div function incorrect. Expected: %d, Got: %d", expectedQuotient, quotient) 50 } 51 } 52 53 func TestIntToString(t *testing.T) { 54 // Test positive integer 55 posInt := g.Int(123) 56 posStr := posInt.ToString() 57 expectedPosStr := g.NewString("123") 58 if posStr != expectedPosStr { 59 t.Errorf("ToString function incorrect for positive integer. Expected: %s, Got: %s", expectedPosStr, posStr) 60 } 61 62 // Test negative integer 63 negInt := g.Int(-123) 64 negStr := negInt.ToString() 65 expectedNegStr := g.NewString("-123") 66 if negStr != expectedNegStr { 67 t.Errorf("ToString function incorrect for negative integer. Expected: %s, Got: %s", expectedNegStr, negStr) 68 } 69 70 // Test zero 71 zero := g.Int(0) 72 zeroStr := zero.ToString() 73 expectedZeroStr := g.NewString("0") 74 if zeroStr != expectedZeroStr { 75 t.Errorf("ToString function incorrect for zero. Expected: %s, Got: %s", expectedZeroStr, zeroStr) 76 } 77 } 78 79 func TestIntAsInt16(t *testing.T) { 80 // Test positive integer within int16 range 81 posInt := g.Int(123) 82 posInt16 := posInt.AsInt16() 83 expectedPosInt16 := int16(123) 84 if posInt16 != expectedPosInt16 { 85 t.Errorf("AsInt16 function incorrect for positive integer. Expected: %d, Got: %d", expectedPosInt16, posInt16) 86 } 87 } 88 89 func TestIntAsInt32(t *testing.T) { 90 // Test positive integer within int32 range 91 posInt := g.Int(123) 92 posInt32 := posInt.AsInt32() 93 expectedPosInt32 := int32(123) 94 if posInt32 != expectedPosInt32 { 95 t.Errorf("AsInt32 function incorrect for positive integer. Expected: %d, Got: %d", expectedPosInt32, posInt32) 96 } 97 98 // Test negative integer within int32 range 99 negInt := g.Int(-123) 100 negInt32 := negInt.AsInt32() 101 expectedNegInt32 := int32(-123) 102 if negInt32 != expectedNegInt32 { 103 t.Errorf("AsInt32 function incorrect for negative integer. Expected: %d, Got: %d", expectedNegInt32, negInt32) 104 } 105 } 106 107 func TestIntAsInt8(t *testing.T) { 108 // Test positive integer within int8 range 109 posInt := g.Int(123) 110 posInt8 := posInt.AsInt8() 111 expectedPosInt8 := int8(123) 112 if posInt8 != expectedPosInt8 { 113 t.Errorf("AsInt8 function incorrect for positive integer. Expected: %d, Got: %d", expectedPosInt8, posInt8) 114 } 115 116 // Test negative integer within int8 range 117 negInt := g.Int(-123) 118 negInt8 := negInt.AsInt8() 119 expectedNegInt8 := int8(-123) 120 if negInt8 != expectedNegInt8 { 121 t.Errorf("AsInt8 function incorrect for negative integer. Expected: %d, Got: %d", expectedNegInt8, negInt8) 122 } 123 124 // Test integer outside int8 range 125 bigInt := g.Int(2000) // larger than int8 max value 126 bigInt8 := bigInt.AsInt8() 127 expectedBigInt8 := int8(-48) // expected value after overflow 128 if bigInt8 != expectedBigInt8 { 129 t.Errorf( 130 "AsInt8 function incorrect for integer outside int8 range. Expected: %d, Got: %d", 131 expectedBigInt8, 132 bigInt8, 133 ) 134 } 135 } 136 137 func TestIntIsZero(t *testing.T) { 138 // Test zero value 139 zeroInt := g.Int(0) 140 isZero := zeroInt.IsZero() 141 if !isZero { 142 t.Errorf("IsZero function incorrect for zero value. Expected: true, Got: %t", isZero) 143 } 144 145 // Test non-zero value 146 nonZeroInt := g.Int(123) 147 isZero = nonZeroInt.IsZero() 148 if isZero { 149 t.Errorf("IsZero function incorrect for non-zero value. Expected: false, Got: %t", isZero) 150 } 151 } 152 153 func TestIntIsPositive(t *testing.T) { 154 tests := []struct { 155 name string 156 i g.Int 157 want bool 158 }{ 159 {"positive", 1, true}, 160 {"negative", -1, false}, 161 {"zero", 0, true}, 162 } 163 for _, tt := range tests { 164 t.Run(tt.name, func(t *testing.T) { 165 if got := tt.i.IsPositive(); got != tt.want { 166 t.Errorf("Int.IsPositive() = %v, want %v", got, tt.want) 167 } 168 }) 169 } 170 } 171 172 func TestIntNegative(t *testing.T) { 173 tests := []struct { 174 name string 175 i g.Int 176 want bool 177 }{ 178 {"positive", 1, false}, 179 {"negative", -1, true}, 180 {"zero", 0, false}, 181 } 182 for _, tt := range tests { 183 t.Run(tt.name, func(t *testing.T) { 184 if got := tt.i.IsNegative(); got != tt.want { 185 t.Errorf("Int.IsNegative() = %v, want %v", got, tt.want) 186 } 187 }) 188 } 189 } 190 191 func TestIntRandomRange(t *testing.T) { 192 for range 100 { 193 min := g.NewInt(100).Random() 194 max := g.NewInt(100).Random().Add(min) 195 196 r := min.RandomRange(max) 197 if r.Lt(min) || r.Gt(max) { 198 t.Errorf("RandomRange(%d, %d) = %d, want in range [%d, %d]", min, max, r, min, max) 199 } 200 } 201 } 202 203 func TestIntMax(t *testing.T) { 204 if max := g.NewInt(1).Max(1, 2, 3, 4, 5); max != 5 { 205 t.Errorf("Max() = %d, want: %d.", max, 5) 206 } 207 } 208 209 func TestIntMin(t *testing.T) { 210 if min := g.NewInt(1).Min(2, 3, 4, 5); min != 1 { 211 t.Errorf("Min() = %d; want: %d", min, 1) 212 } 213 } 214 215 func TestIntLte(t *testing.T) { 216 // Test for less than 217 ltInt1 := g.Int(5) 218 ltInt2 := g.Int(10) 219 isLte := ltInt1.Lte(ltInt2) 220 if !isLte { 221 t.Errorf("Lte function incorrect for less than. Expected: true, Got: %t", isLte) 222 } 223 224 // Test for equal 225 eqInt1 := g.Int(10) 226 eqInt2 := g.Int(10) 227 isLte = eqInt1.Lte(eqInt2) 228 if !isLte { 229 t.Errorf("Lte function incorrect for equal values. Expected: true, Got: %t", isLte) 230 } 231 232 // Test for greater than 233 gtInt1 := g.Int(15) 234 gtInt2 := g.Int(10) 235 isLte = gtInt1.Lte(gtInt2) 236 if isLte { 237 t.Errorf("Lte function incorrect for greater than. Expected: false, Got: %t", isLte) 238 } 239 } 240 241 func TestIntMul(t *testing.T) { 242 // Test for positive multiplication 243 posInt1 := g.Int(5) 244 posInt2 := g.Int(10) 245 result := posInt1.Mul(posInt2) 246 expected := g.Int(50) 247 if result != expected { 248 t.Errorf("Mul function incorrect for positive multiplication. Expected: %d, Got: %d", expected, result) 249 } 250 251 // Test for negative multiplication 252 negInt1 := g.Int(-5) 253 negInt2 := g.Int(10) 254 result = negInt1.Mul(negInt2) 255 expected = g.Int(-50) 256 if result != expected { 257 t.Errorf("Mul function incorrect for negative multiplication. Expected: %d, Got: %d", expected, result) 258 } 259 } 260 261 func TestIntNe(t *testing.T) { 262 // Test for inequality 263 ineqInt1 := g.Int(5) 264 ineqInt2 := g.Int(10) 265 isNe := ineqInt1.Ne(ineqInt2) 266 if !isNe { 267 t.Errorf("Ne function incorrect for inequality. Expected: true, Got: %t", isNe) 268 } 269 270 // Test for equality 271 eqInt1 := g.Int(10) 272 eqInt2 := g.Int(10) 273 isNe = eqInt1.Ne(eqInt2) 274 if isNe { 275 t.Errorf("Ne function incorrect for equality. Expected: false, Got: %t", isNe) 276 } 277 } 278 279 func TestIntToBinary(t *testing.T) { 280 // Test for positive integer 281 posInt := g.Int(10) 282 binary := posInt.ToBinary() 283 expected := g.String("00001010") 284 if binary != expected { 285 t.Errorf("ToBinary function incorrect for positive integer. Expected: %s, Got: %s", expected, binary) 286 } 287 288 // Test for negative integer 289 negInt := g.Int(-10) 290 binary = negInt.ToBinary() 291 expected = g.String("-0001010") // Two's complement representation 292 if binary != expected { 293 t.Errorf("ToBinary function incorrect for negative integer. Expected: %s, Got: %s", expected, binary) 294 } 295 296 // Test for zero 297 zeroInt := g.Int(0) 298 binary = zeroInt.ToBinary() 299 expected = g.String("00000000") 300 if binary != expected { 301 t.Errorf("ToBinary function incorrect for zero. Expected: %s, Got: %s", expected, binary) 302 } 303 } 304 305 func TestIntAsUInt16(t *testing.T) { 306 // Test for positive integer 307 posInt := g.Int(100) 308 uint16Val := posInt.AsUInt16() 309 expected := uint16(100) 310 if uint16Val != expected { 311 t.Errorf("AsUInt16 function incorrect for positive integer. Expected: %d, Got: %d", expected, uint16Val) 312 } 313 314 // Test for negative integer 315 negInt := g.Int(-100) 316 uint16Val = negInt.AsUInt16() 317 expected = 65436 // Conversion to uint16 of negative number results in 0 318 if uint16Val != expected { 319 t.Errorf("AsUInt16 function incorrect for negative integer. Expected: %d, Got: %d", expected, uint16Val) 320 } 321 } 322 323 func TestIntAsUInt32(t *testing.T) { 324 // Test for positive integer 325 posInt := g.Int(100) 326 uint32Val := posInt.AsUInt32() 327 expected := uint32(100) 328 if uint32Val != expected { 329 t.Errorf("AsUInt32 function incorrect for positive integer. Expected: %d, Got: %d", expected, uint32Val) 330 } 331 332 // Test for negative integer 333 negInt := g.Int(-100) 334 uint32Val = negInt.AsUInt32() 335 expected = 4294967196 // Conversion to uint32 of negative number results in 0 336 if uint32Val != expected { 337 t.Errorf("AsUInt32 function incorrect for negative integer. Expected: %d, Got: %d", expected, uint32Val) 338 } 339 } 340 341 func TestIntAsUInt8(t *testing.T) { 342 // Test for positive integer within range 343 posInt := g.Int(100) 344 uint8Val := posInt.AsUInt8() 345 expected := uint8(100) 346 if uint8Val != expected { 347 t.Errorf( 348 "AsUInt8 function incorrect for positive integer within range. Expected: %d, Got: %d", 349 expected, 350 uint8Val, 351 ) 352 } 353 354 // Test for positive integer outside range 355 posInt = g.Int(300) 356 uint8Val = posInt.AsUInt8() 357 expected = 44 // Overflow results in 44 358 if uint8Val != expected { 359 t.Errorf( 360 "AsUInt8 function incorrect for positive integer outside range. Expected: %d, Got: %d", 361 expected, 362 uint8Val, 363 ) 364 } 365 366 // Test for negative integer 367 negInt := g.Int(-100) 368 uint8Val = negInt.AsUInt8() 369 expected = 156 // Conversion to uint8 of negative number results in 156 370 if uint8Val != expected { 371 t.Errorf("AsUInt8 function incorrect for negative integer. Expected: %d, Got: %d", expected, uint8Val) 372 } 373 } 374 375 func TestIntHashingFunctions(t *testing.T) { 376 // Test case for SHA1 hashing 377 input := g.Int(42) 378 expectedSHA1 := "df58248c414f342c81e056b40bee12d17a08bf61" 379 sha1Hash := input.Hash().SHA1().Std() 380 if sha1Hash != expectedSHA1 { 381 t.Errorf("SHA1 hashing failed. Expected: %s, Got: %s", expectedSHA1, sha1Hash) 382 } 383 384 // Test case for SHA256 hashing 385 expectedSHA256 := "684888c0ebb17f374298b65ee2807526c066094c701bcc7ebbe1c1095f494fc1" 386 sha256Hash := input.Hash().SHA256().Std() 387 if sha256Hash != expectedSHA256 { 388 t.Errorf("SHA256 hashing failed. Expected: %s, Got: %s", expectedSHA256, sha256Hash) 389 } 390 391 // Test case for SHA512 hashing 392 expectedSHA512 := "7846cdd4c2b9052768b8901640122e5282e0b833a6a58312a7763472d448ee23781c7f08d90793fdfe71ffe74238cf6e4aa778cc9bb8cec03ea7268d4893a502" 393 sha512Hash := input.Hash().SHA512().Std() 394 if sha512Hash != expectedSHA512 { 395 t.Errorf("SHA512 hashing failed. Expected: %s, Got: %s", expectedSHA512, sha512Hash) 396 } 397 398 // Test case for MD5 hashing 399 expectedMD5 := "3389dae361af79b04c9c8e7057f60cc6" 400 md5Hash := input.Hash().MD5().Std() 401 if md5Hash != expectedMD5 { 402 t.Errorf("MD5 hashing failed. Expected: %s, Got: %s", expectedMD5, md5Hash) 403 } 404 } 405 406 func TestIntRem(t *testing.T) { 407 // Test cases 408 testCases := []struct { 409 dividend int 410 divisor int 411 expected int 412 }{ 413 {10, 3, 1}, // 10 % 3 = 1 414 {15, 7, 1}, // 15 % 7 = 1 415 {20, 5, 0}, // 20 % 5 = 0 416 {100, 17, 15}, // 100 % 17 = 15 417 {35, 11, 2}, // 35 % 11 = 2 418 {7, 3, 1}, // 7 % 3 = 1 419 {8, 4, 0}, // 8 % 4 = 0 420 } 421 422 // Test each case 423 for _, tc := range testCases { 424 // Wrap the input integers 425 i := g.Int(tc.dividend) 426 b := g.Int(tc.divisor) 427 428 // Call the Rem method 429 result := i.Rem(b) 430 431 if result.Std() != tc.expected { 432 t.Errorf( 433 "Rem function incorrect for %d %% %d. Expected: %d, Got: %d", 434 tc.dividend, 435 tc.divisor, 436 tc.expected, 437 result, 438 ) 439 } 440 } 441 } 442 443 func TestIntSub(t *testing.T) { 444 // Testing subtraction with positive integers 445 result := g.Int(5).Sub(3) 446 expected := g.Int(2) 447 if result != expected { 448 t.Errorf("Subtraction failed: expected %v, got %v", expected, result) 449 } 450 451 // Testing subtraction with negative integers 452 result = g.Int(-5).Sub(-3) 453 expected = g.Int(-2) 454 if result != expected { 455 t.Errorf("Subtraction failed: expected %v, got %v", expected, result) 456 } 457 458 // Testing subtraction with positive and negative integers 459 result = g.Int(5).Sub(-3) 460 expected = g.Int(8) 461 if result != expected { 462 t.Errorf("Subtraction failed: expected %v, got %v", expected, result) 463 } 464 465 // Testing subtraction with negative and positive integers 466 result = g.Int(-5).Sub(3) 467 expected = g.Int(-8) 468 if result != expected { 469 t.Errorf("Subtraction failed: expected %v, got %v", expected, result) 470 } 471 472 // Testing subtraction with zero 473 result = g.Int(0).Sub(0) 474 expected = g.Int(0) 475 if result != expected { 476 t.Errorf("Subtraction failed: expected %v, got %v", expected, result) 477 } 478 } 479 480 func TestIntCmp(t *testing.T) { 481 tests := []struct { 482 name string 483 i, other g.Int 484 expected cmp.Ordering 485 }{ 486 {"LessThan", g.Int(5), g.Int(10), cmp.Less}, 487 {"GreaterThan", g.Int(15), g.Int(10), cmp.Greater}, 488 {"EqualTo", g.Int(10), g.Int(10), cmp.Equal}, 489 } 490 491 for _, test := range tests { 492 t.Run(test.name, func(t *testing.T) { 493 result := test.i.Cmp(test.other) 494 if result != test.expected { 495 t.Errorf("%s: Expected %v, Got %v", test.name, test.expected, result) 496 } 497 }) 498 } 499 }