BrickMathCalculatorTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Math;
  4. use Ramsey\Uuid\Exception\InvalidArgumentException;
  5. use Ramsey\Uuid\Math\BrickMathCalculator;
  6. use Ramsey\Uuid\Math\RoundingMode;
  7. use Ramsey\Uuid\Test\TestCase;
  8. use Ramsey\Uuid\Type\Hexadecimal;
  9. use Ramsey\Uuid\Type\Integer as IntegerObject;
  10. class BrickMathCalculatorTest extends TestCase
  11. {
  12. public function testAdd(): void
  13. {
  14. $int1 = new IntegerObject(5);
  15. $int2 = new IntegerObject(6);
  16. $int3 = new IntegerObject(7);
  17. $calculator = new BrickMathCalculator();
  18. $result = $calculator->add($int1, $int2, $int3);
  19. $this->assertSame('18', $result->toString());
  20. }
  21. public function testSubtract(): void
  22. {
  23. $int1 = new IntegerObject(5);
  24. $int2 = new IntegerObject(6);
  25. $int3 = new IntegerObject(7);
  26. $calculator = new BrickMathCalculator();
  27. $result = $calculator->subtract($int1, $int2, $int3);
  28. $this->assertSame('-8', $result->toString());
  29. }
  30. public function testMultiply(): void
  31. {
  32. $int1 = new IntegerObject(5);
  33. $int2 = new IntegerObject(6);
  34. $int3 = new IntegerObject(7);
  35. $calculator = new BrickMathCalculator();
  36. $result = $calculator->multiply($int1, $int2, $int3);
  37. $this->assertSame('210', $result->toString());
  38. }
  39. public function testDivide(): void
  40. {
  41. $int1 = new IntegerObject(1023);
  42. $int2 = new IntegerObject(6);
  43. $int3 = new IntegerObject(7);
  44. $calculator = new BrickMathCalculator();
  45. $result = $calculator->divide(RoundingMode::HALF_UP, 0, $int1, $int2, $int3);
  46. $this->assertSame('24', $result->toString());
  47. }
  48. public function testFromBase(): void
  49. {
  50. $calculator = new BrickMathCalculator();
  51. $result = $calculator->fromBase('ffffffffffffffffffff', 16);
  52. $this->assertInstanceOf(IntegerObject::class, $result);
  53. $this->assertSame('1208925819614629174706175', $result->toString());
  54. }
  55. public function testToBase(): void
  56. {
  57. $intValue = new IntegerObject('1208925819614629174706175');
  58. $calculator = new BrickMathCalculator();
  59. $this->assertSame('ffffffffffffffffffff', $calculator->toBase($intValue, 16));
  60. }
  61. public function testToHexadecimal(): void
  62. {
  63. $intValue = new IntegerObject('1208925819614629174706175');
  64. $calculator = new BrickMathCalculator();
  65. $result = $calculator->toHexadecimal($intValue);
  66. $this->assertInstanceOf(Hexadecimal::class, $result);
  67. $this->assertSame('ffffffffffffffffffff', $result->toString());
  68. }
  69. public function testFromBaseThrowsException(): void
  70. {
  71. $calculator = new BrickMathCalculator();
  72. $this->expectException(InvalidArgumentException::class);
  73. $this->expectExceptionMessage('"o" is not a valid character in base 16');
  74. $calculator->fromBase('foobar', 16);
  75. }
  76. public function testToBaseThrowsException(): void
  77. {
  78. $calculator = new BrickMathCalculator();
  79. $this->expectException(InvalidArgumentException::class);
  80. $this->expectExceptionMessage('Base 1024 is out of range [2, 36]');
  81. $calculator->toBase(new IntegerObject(42), 1024);
  82. }
  83. }