BaseEncoderDecoderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace MathPHP\Tests\Functions;
  3. use MathPHP\Exception;
  4. use MathPHP\Functions\BaseEncoderDecoder;
  5. use MathPHP\Number\ArbitraryInteger;
  6. class BaseEncoderDecoderTest extends \PHPUnit\Framework\TestCase
  7. {
  8. /**
  9. * @test toBase
  10. * @dataProvider dataProviderForTestToBase
  11. * @param string $int
  12. * @param int $base
  13. * @param string $expected
  14. * @throws \Exception
  15. */
  16. public function testToBase(string $int, int $base, string $expected)
  17. {
  18. // Given
  19. $int = new ArbitraryInteger($int);
  20. // When
  21. $toBase = BaseEncoderDecoder::toBase($int, $base);
  22. // Theb
  23. $this->assertEquals($expected, $toBase);
  24. }
  25. public function dataProviderForTestToBase(): array
  26. {
  27. return [
  28. ['0xf', 16, 'f'],
  29. ['100', 256, \chr(100)],
  30. ];
  31. }
  32. /**
  33. * @test creation with string representation
  34. * @dataProvider dataProviderForTestCreateArbitrary
  35. */
  36. public function testCreateArbitrary(string $int, int $base, string $expected)
  37. {
  38. // Given
  39. $int = BaseEncoderDecoder::createArbitraryInteger($int, $base);
  40. // When
  41. $stringRepresentation = (string) $int;
  42. // Then
  43. $this->assertEquals($expected, $stringRepresentation);
  44. }
  45. public function dataProviderForTestCreateArbitrary(): array
  46. {
  47. return [
  48. ['123', 10, '123'],
  49. ['7b', 16, '123'],
  50. [\chr(123), 256, '123'],
  51. ];
  52. }
  53. /**
  54. * @test toBase throws an exception when base>256
  55. * @throws \Exception
  56. */
  57. public function testInvalidToBaseException()
  58. {
  59. // Given
  60. $base = 300;
  61. $int = new ArbitraryInteger('123456');
  62. // Then
  63. $this->expectException(Exception\BadParameterException::class);
  64. // When
  65. $string = BaseEncoderDecoder::toBase($int, $base);
  66. }
  67. /**
  68. * @test Function throws an exception when given an empty string
  69. * @throws \Exception
  70. */
  71. public function testEmptyStringException()
  72. {
  73. // Given
  74. $number = "";
  75. // Then
  76. $this->expectException(Exception\BadParameterException::class);
  77. // When
  78. $int = BaseEncoderDecoder::createArbitraryInteger($number, 10);
  79. }
  80. /**
  81. * @test Function throws an exception when base>256
  82. * @throws \Exception
  83. */
  84. public function testInvalidBaseException()
  85. {
  86. // Given
  87. $base = 300;
  88. // Then
  89. $this->expectException(Exception\BadParameterException::class);
  90. // When
  91. $int = BaseEncoderDecoder::createArbitraryInteger('123456', $base);
  92. }
  93. /**
  94. * @test Function throws an exception when base>256
  95. * @dataProvider dataProviderForTestInvalidCharInStringException
  96. * @param string $value
  97. * @param int $base
  98. * @throws \Exception
  99. */
  100. public function testInvalidCharInStringException(string $value, int $base)
  101. {
  102. // Then
  103. $this->expectException(Exception\BadParameterException::class);
  104. // When
  105. $int = BaseEncoderDecoder::createArbitraryInteger($value, $base);
  106. }
  107. public function dataProviderForTestInvalidCharInStringException(): array
  108. {
  109. return [
  110. ['12a', 10],
  111. ['0x12afg', 16],
  112. ];
  113. }
  114. }