|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Nejcc\PhpDatatypes\Tests\Integers\Unsigned; |
| 6 | + |
| 7 | +use Nejcc\PhpDatatypes\Integers\Unsigned\UInt16; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class UInt16Test extends TestCase |
| 11 | +{ |
| 12 | + public function testValidInitialization() |
| 13 | + { |
| 14 | + $uint16 = new UInt16(65535); |
| 15 | + $this->assertSame(65535, $uint16->getValue()); |
| 16 | + } |
| 17 | + |
| 18 | + public function testInvalidInitialization() |
| 19 | + { |
| 20 | + $this->expectException(\OutOfRangeException::class); |
| 21 | + new UInt16(65536); |
| 22 | + } |
| 23 | + |
| 24 | + public function testAdditionWithinBounds() |
| 25 | + { |
| 26 | + $uint16a = new UInt16(50000); |
| 27 | + $uint16b = new UInt16(15535); |
| 28 | + $uint16c = $uint16a->add($uint16b); |
| 29 | + $this->assertSame(65535, $uint16c->getValue()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testAdditionOverflow() |
| 33 | + { |
| 34 | + $this->expectException(\OverflowException::class); |
| 35 | + $uint16a = new UInt16(50000); |
| 36 | + $uint16b = new UInt16(20000); |
| 37 | + $uint16a->add($uint16b); |
| 38 | + } |
| 39 | + |
| 40 | + public function testSubtractionWithinBounds() |
| 41 | + { |
| 42 | + $uint16a = new UInt16(50000); |
| 43 | + $uint16b = new UInt16(10000); |
| 44 | + $uint16c = $uint16a->subtract($uint16b); |
| 45 | + $this->assertSame(40000, $uint16c->getValue()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testSubtractionUnderflow() |
| 49 | + { |
| 50 | + $this->expectException(\UnderflowException::class); |
| 51 | + $uint16a = new UInt16(10000); |
| 52 | + $uint16b = new UInt16(20000); |
| 53 | + $uint16a->subtract($uint16b); |
| 54 | + } |
| 55 | + |
| 56 | + public function testMultiplicationWithinBounds() |
| 57 | + { |
| 58 | + $uint16a = new UInt16(3000); |
| 59 | + $uint16b = new UInt16(20); |
| 60 | + $uint16c = $uint16a->multiply($uint16b); |
| 61 | + $this->assertSame(60000, $uint16c->getValue()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testMultiplicationOverflow() |
| 65 | + { |
| 66 | + $this->expectException(\OverflowException::class); |
| 67 | + $uint16a = new UInt16(4000); |
| 68 | + $uint16b = new UInt16(20); |
| 69 | + $uint16a->multiply($uint16b); |
| 70 | + } |
| 71 | + |
| 72 | + public function testDivisionWithinBounds() |
| 73 | + { |
| 74 | + $uint16a = new UInt16(50000); |
| 75 | + $uint16b = new UInt16(10); |
| 76 | + $uint16c = $uint16a->divide($uint16b); |
| 77 | + $this->assertSame(5000, $uint16c->getValue()); |
| 78 | + } |
| 79 | + |
| 80 | + public function testDivisionByZero() |
| 81 | + { |
| 82 | + $this->expectException(\DivisionByZeroError::class); |
| 83 | + $uint16a = new UInt16(50000); |
| 84 | + $uint16b = new UInt16(0); |
| 85 | + $uint16a->divide($uint16b); |
| 86 | + } |
| 87 | + |
| 88 | + public function testModulusWithinBounds() |
| 89 | + { |
| 90 | + $uint16a = new UInt16(50000); |
| 91 | + $uint16b = new UInt16(12000); |
| 92 | + $uint16c = $uint16a->mod($uint16b); |
| 93 | + $this->assertSame(2000, $uint16c->getValue()); |
| 94 | + } |
| 95 | + |
| 96 | + public function testEquality() |
| 97 | + { |
| 98 | + $uint16a = new UInt16(30000); |
| 99 | + $uint16b = new UInt16(30000); |
| 100 | + $this->assertTrue($uint16a->equals($uint16b)); |
| 101 | + } |
| 102 | + |
| 103 | + public function testInequality() |
| 104 | + { |
| 105 | + $uint16a = new UInt16(30000); |
| 106 | + $uint16b = new UInt16(40000); |
| 107 | + $this->assertFalse($uint16a->equals($uint16b)); |
| 108 | + } |
| 109 | + |
| 110 | + public function testComparison() |
| 111 | + { |
| 112 | + $uint16a = new UInt16(30000); |
| 113 | + $uint16b = new UInt16(40000); |
| 114 | + $this->assertSame(-1, $uint16a->compare($uint16b)); |
| 115 | + $this->assertSame(1, $uint16b->compare($uint16a)); |
| 116 | + $uint16c = new UInt16(30000); |
| 117 | + $this->assertSame(0, $uint16a->compare($uint16c)); |
| 118 | + } |
| 119 | +} |
0 commit comments