BigintTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. namespace BigintTest;
  4. use OverflowException;
  5. use PHPUnit\Framework\TestCase;
  6. use ZipStream\Bigint;
  7. class BigintTest extends TestCase
  8. {
  9. public function testConstruct(): void
  10. {
  11. $bigint = new Bigint(0x12345678);
  12. $this->assertSame('0x0000000012345678', $bigint->getHex64());
  13. $this->assertSame(0x12345678, $bigint->getLow32());
  14. $this->assertSame(0, $bigint->getHigh32());
  15. }
  16. public function testConstructLarge(): void
  17. {
  18. $bigint = new Bigint(0x87654321);
  19. $this->assertSame('0x0000000087654321', $bigint->getHex64());
  20. $this->assertSame('87654321', bin2hex(pack('N', $bigint->getLow32())));
  21. $this->assertSame(0, $bigint->getHigh32());
  22. }
  23. public function testAddSmallValue(): void
  24. {
  25. $bigint = new Bigint(1);
  26. $bigint = $bigint->add(Bigint::init(2));
  27. $this->assertSame(3, $bigint->getLow32());
  28. $this->assertFalse($bigint->isOver32());
  29. $this->assertTrue($bigint->isOver32(true));
  30. $this->assertSame($bigint->getLowFF(), (float)$bigint->getLow32());
  31. $this->assertSame($bigint->getLowFF(true), (float)0xFFFFFFFF);
  32. }
  33. public function testAddWithOverflowAtLowestByte(): void
  34. {
  35. $bigint = new Bigint(0xFF);
  36. $bigint = $bigint->add(Bigint::init(0x01));
  37. $this->assertSame(0x100, $bigint->getLow32());
  38. }
  39. public function testAddWithOverflowAtInteger32(): void
  40. {
  41. $bigint = new Bigint(0xFFFFFFFE);
  42. $this->assertFalse($bigint->isOver32());
  43. $bigint = $bigint->add(Bigint::init(0x01));
  44. $this->assertTrue($bigint->isOver32());
  45. $bigint = $bigint->add(Bigint::init(0x01));
  46. $this->assertSame('0x0000000100000000', $bigint->getHex64());
  47. $this->assertTrue($bigint->isOver32());
  48. $this->assertSame((float)0xFFFFFFFF, $bigint->getLowFF());
  49. }
  50. public function testAddWithOverflowAtInteger64(): void
  51. {
  52. $bigint = Bigint::fromLowHigh(0xFFFFFFFF, 0xFFFFFFFF);
  53. $this->assertSame('0xFFFFFFFFFFFFFFFF', $bigint->getHex64());
  54. $this->expectException(OverflowException::class);
  55. $bigint->add(Bigint::init(1));
  56. }
  57. }