AllYourBaseTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. declare(strict_types=1);
  3. class AllYourBaseTest extends PHPUnit\Framework\TestCase
  4. {
  5. public static function setUpBeforeClass(): void
  6. {
  7. require_once 'AllYourBase.php';
  8. }
  9. /**
  10. * uuid 5ce422f9-7a4b-4f44-ad29-49c67cb32d2c
  11. * @testdox Single bit one to decimal
  12. */
  13. public function testSingleBitOneToDecimal(): void
  14. {
  15. $this->assertEquals([1], rebase(2, [1], 10));
  16. }
  17. /**
  18. * uuid 0cc3fea8-bb79-46ac-a2ab-5a2c93051033
  19. * @testdox Binary to single decimal
  20. */
  21. public function testBinaryToSingleDecimal(): void
  22. {
  23. $this->assertEquals([5], rebase(2, [1, 0, 1], 10));
  24. }
  25. /**
  26. * uuid f12db0f9-0d3d-42c2-b3ba-e38cb375a2b8
  27. * @testdox Single decimal to binary
  28. */
  29. public function testSingleDecimalToBinary(): void
  30. {
  31. $this->assertEquals([1, 0, 1], rebase(10, [5], 2));
  32. }
  33. /**
  34. * uuid 2c45cf54-6da3-4748-9733-5a3c765d925b
  35. * @testdox Binary to multiple decimal
  36. */
  37. public function testBinaryToMultipleDecimal(): void
  38. {
  39. $this->assertEquals([4, 2], rebase(2, [1, 0, 1, 0, 1, 0], 10));
  40. }
  41. /**
  42. * uuid 65ddb8b4-8899-4fcc-8618-181b2cf0002d
  43. * @testdox Decimal to binary
  44. */
  45. public function testDecimalToBinary(): void
  46. {
  47. $this->assertEquals([1, 0, 1, 0, 1, 0], rebase(10, [4, 2], 2));
  48. }
  49. /**
  50. * uuid 8d418419-02a7-4824-8b7a-352d33c6987e
  51. * @testdox Trinary to hexadecimal
  52. */
  53. public function testTrinaryToHexadecimal(): void
  54. {
  55. $this->assertEquals([2, 10], rebase(3, [1, 1, 2, 0], 16));
  56. }
  57. /**
  58. * uuid d3901c80-8190-41b9-bd86-38d988efa956
  59. * @testdox Hexadecimal to trinary
  60. */
  61. public function testHexadecimalToTrinary(): void
  62. {
  63. $this->assertEquals([1, 1, 2, 0], rebase(16, [2, 10], 3));
  64. }
  65. /**
  66. * uuid 5d42f85e-21ad-41bd-b9be-a3e8e4258bbf
  67. * @testdox 15-bit integer
  68. */
  69. public function test15BitIntegers(): void
  70. {
  71. $this->assertEquals([6, 10, 45], rebase(97, [3, 46, 60], 73));
  72. }
  73. /**
  74. * uuid d68788f7-66dd-43f8-a543-f15b6d233f83
  75. * @testdox Empty list
  76. */
  77. public function testEmptyList(): void
  78. {
  79. $this->assertEquals([0], rebase(2, [], 10));
  80. }
  81. /**
  82. * uuid 5e27e8da-5862-4c5f-b2a9-26c0382b6be7
  83. * @testdox Single zero
  84. */
  85. public function testSingleZero(): void
  86. {
  87. $this->assertEquals([0], rebase(10, [0], 2));
  88. }
  89. /**
  90. * uuid 2e1c2573-77e4-4b9c-8517-6c56c5bcfdf2
  91. * @testdox Multiple zeros
  92. */
  93. public function testMultipleZeros(): void
  94. {
  95. $this->assertEquals([0], rebase(10, [0, 0, 0], 2));
  96. }
  97. /**
  98. * uuid 3530cd9f-8d6d-43f5-bc6e-b30b1db9629b
  99. * @testdox Leading zeros
  100. */
  101. public function testLeadingZeros(): void
  102. {
  103. $this->assertEquals([4, 2], rebase(7, [0, 6, 0], 10));
  104. }
  105. /**
  106. * uuid a6b476a1-1901-4f2a-92c4-4d91917ae023
  107. * @testdox Input base is one
  108. */
  109. public function testFirstBaseIsOne(): void
  110. {
  111. $this->expectException(InvalidArgumentException::class);
  112. $this->expectExceptionMessage('input base must be >= 2');
  113. rebase(1, [0], 10);
  114. }
  115. /**
  116. * uuid e21a693a-7a69-450b-b393-27415c26a016
  117. * @testdox Input base is zero
  118. */
  119. public function testFirstBaseIsZero(): void
  120. {
  121. $this->expectException(InvalidArgumentException::class);
  122. $this->expectExceptionMessage('input base must be >= 2');
  123. rebase(0, [], 10);
  124. }
  125. /**
  126. * uuid 54a23be5-d99e-41cc-88e0-a650ffe5fcc2
  127. * @testdox Input base is negative
  128. */
  129. public function testFirstBaseIsNegative(): void
  130. {
  131. $this->expectException(InvalidArgumentException::class);
  132. $this->expectExceptionMessage('input base must be >= 2');
  133. rebase(-2, [1], 10);
  134. }
  135. /**
  136. * uuid 9eccf60c-dcc9-407b-95d8-c37b8be56bb6
  137. * @testdox Negative digit
  138. */
  139. public function testNegativeDigit(): void
  140. {
  141. $this->expectException(InvalidArgumentException::class);
  142. $this->expectExceptionMessage('all digits must satisfy 0 <= d < input base');
  143. rebase(2, [1, -1, 1, 0, 1, 0], 10);
  144. }
  145. /**
  146. * uuid 232fa4a5-e761-4939-ba0c-ed046cd0676a
  147. * @testdox Invalid positive digit
  148. */
  149. public function testInvalidPositiveDigit(): void
  150. {
  151. $this->expectException(InvalidArgumentException::class);
  152. $this->expectExceptionMessage('all digits must satisfy 0 <= d < input base');
  153. rebase(2, [1, 2, 1, 0, 1, 0], 10);
  154. }
  155. /**
  156. * uuid 14238f95-45da-41dc-95ce-18f860b30ad3
  157. * @testdox Output base is one
  158. */
  159. public function testSecondBaseIsOne(): void
  160. {
  161. $this->expectException(InvalidArgumentException::class);
  162. $this->expectExceptionMessage('output base must be >= 2');
  163. rebase(2, [1, 0, 1, 0, 1, 0], 1);
  164. }
  165. /**
  166. * uuid 73dac367-da5c-4a37-95fe-c87fad0a4047
  167. * @testdox Output base is zero
  168. */
  169. public function testSecondBaseIsZero(): void
  170. {
  171. $this->expectException(InvalidArgumentException::class);
  172. $this->expectExceptionMessage('output base must be >= 2');
  173. rebase(10, [7], 0);
  174. }
  175. /**
  176. * uuid 13f81f42-ff53-4e24-89d9-37603a48ebd9
  177. * @testdox Output base is negative
  178. */
  179. public function testSecondBaseIsNegative(): void
  180. {
  181. $this->expectException(InvalidArgumentException::class);
  182. $this->expectExceptionMessage('output base must be >= 2');
  183. rebase(2, [1], -7);
  184. }
  185. /**
  186. * uuid 0e6c895d-8a5d-4868-a345-309d094cfe8d
  187. * @testdox Both bases are negative
  188. */
  189. public function testBothBasesIsNegative(): void
  190. {
  191. $this->expectException(InvalidArgumentException::class);
  192. rebase(-2, [1], -7);
  193. }
  194. }