123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- declare(strict_types=1);
- class AllYourBaseTest extends PHPUnit\Framework\TestCase
- {
- public static function setUpBeforeClass(): void
- {
- require_once 'AllYourBase.php';
- }
-
- public function testSingleBitOneToDecimal(): void
- {
- $this->assertEquals([1], rebase(2, [1], 10));
- }
-
- public function testBinaryToSingleDecimal(): void
- {
- $this->assertEquals([5], rebase(2, [1, 0, 1], 10));
- }
-
- public function testSingleDecimalToBinary(): void
- {
- $this->assertEquals([1, 0, 1], rebase(10, [5], 2));
- }
-
- public function testBinaryToMultipleDecimal(): void
- {
- $this->assertEquals([4, 2], rebase(2, [1, 0, 1, 0, 1, 0], 10));
- }
-
- public function testDecimalToBinary(): void
- {
- $this->assertEquals([1, 0, 1, 0, 1, 0], rebase(10, [4, 2], 2));
- }
-
- public function testTrinaryToHexadecimal(): void
- {
- $this->assertEquals([2, 10], rebase(3, [1, 1, 2, 0], 16));
- }
-
- public function testHexadecimalToTrinary(): void
- {
- $this->assertEquals([1, 1, 2, 0], rebase(16, [2, 10], 3));
- }
-
- public function test15BitIntegers(): void
- {
- $this->assertEquals([6, 10, 45], rebase(97, [3, 46, 60], 73));
- }
-
- public function testEmptyList(): void
- {
- $this->assertEquals([0], rebase(2, [], 10));
- }
-
- public function testSingleZero(): void
- {
- $this->assertEquals([0], rebase(10, [0], 2));
- }
-
- public function testMultipleZeros(): void
- {
- $this->assertEquals([0], rebase(10, [0, 0, 0], 2));
- }
-
- public function testLeadingZeros(): void
- {
- $this->assertEquals([4, 2], rebase(7, [0, 6, 0], 10));
- }
-
- public function testFirstBaseIsOne(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('input base must be >= 2');
- rebase(1, [0], 10);
- }
-
- public function testFirstBaseIsZero(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('input base must be >= 2');
- rebase(0, [], 10);
- }
-
- public function testFirstBaseIsNegative(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('input base must be >= 2');
- rebase(-2, [1], 10);
- }
-
- public function testNegativeDigit(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('all digits must satisfy 0 <= d < input base');
- rebase(2, [1, -1, 1, 0, 1, 0], 10);
- }
-
- public function testInvalidPositiveDigit(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('all digits must satisfy 0 <= d < input base');
- rebase(2, [1, 2, 1, 0, 1, 0], 10);
- }
-
- public function testSecondBaseIsOne(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('output base must be >= 2');
- rebase(2, [1, 0, 1, 0, 1, 0], 1);
- }
-
- public function testSecondBaseIsZero(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('output base must be >= 2');
- rebase(10, [7], 0);
- }
-
- public function testSecondBaseIsNegative(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('output base must be >= 2');
- rebase(2, [1], -7);
- }
-
- public function testBothBasesIsNegative(): void
- {
- $this->expectException(InvalidArgumentException::class);
- rebase(-2, [1], -7);
- }
- }
|