Skip to content

Commit 89d0e33

Browse files
committed
update unsigned integer
update abstracts add unsigned tests
1 parent 350fd1f commit 89d0e33

File tree

10 files changed

+436
-25
lines changed

10 files changed

+436
-25
lines changed

Diff for: index.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
use Nejcc\PhpDatatypes\Integers\Signed\Int32;
66
use Nejcc\PhpDatatypes\Integers\Signed\Int64;
77
use Nejcc\PhpDatatypes\Integers\Signed\Int8;
8+
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
89

910
require_once __DIR__ . '/vendor/autoload.php';
1011

1112
class test
1213
{
1314
public Int8 $min;
15+
public UInt8 $minU;
1416
public Int16 $max;
1517
public Int32 $max2;
1618
// public Int64 $max3;
1719
// public Int128 $max4;
1820

1921
public function __construct(int $int, int $max, int $max2, int $max3, int $max4)
2022
{
21-
$this->min = int8($int);
22-
$this->max = int16($max);
23-
$this->max2 = int32($max2);
23+
$this->minU = uint8(1);
24+
// $this->min = int8($int);
25+
// $this->max = int16($max);
26+
// $this->max2 = int32($max2);
2427
// $this->max3 = int64($max3);
2528
// $this->max4 = int128($max4);
2629
}

Diff for: src/Abstract/AbstractNativeInteger.php

-2
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@ public function getValue(): int
4646
return $this->value;
4747
}
4848

49-
// Implement comparison method
5049
public function compare(NativeIntegerInterface $other): int
5150
{
5251
return $this->value <=> $other->getValue();
5352
}
5453

55-
// Implement operation methods required by the trait
5654
protected function performOperation(
5755
NativeIntegerInterface $other,
5856
callable $operation,

Diff for: src/Integers/Unsigned/UInt16.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Integers\Unsigned;
6+
7+
use Nejcc\PhpDatatypes\Abstract\AbstractNativeInteger;
8+
9+
/**
10+
* Represents a 16-bit unsigned integer.
11+
*
12+
* @package Nejcc\PhpDatatypes\Integers\Unsigned
13+
*/
14+
final class UInt16 extends AbstractNativeInteger
15+
{
16+
public const MIN_VALUE = 0;
17+
public const MAX_VALUE = 65535;
18+
}

Diff for: src/Integers/Unsigned/UInt32.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Integers\Unsigned;
6+
7+
use Nejcc\PhpDatatypes\Abstract\AbstractNativeInteger;
8+
9+
/**
10+
* Represents a 32-bit unsigned integer.
11+
*
12+
* @package Nejcc\PhpDatatypes\Integers\Unsigned
13+
*/
14+
final class UInt32 extends AbstractNativeInteger
15+
{
16+
public const MIN_VALUE = '0';
17+
public const MAX_VALUE = '4294967295';
18+
}

Diff for: src/Integers/Unsigned/UInt8.php

+10-18
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,16 @@
44

55
namespace Nejcc\PhpDatatypes\Integers\Unsigned;
66

7-
use Nejcc\PhpDatatypes\Abstract\AbstractInteger;
8-
use Nejcc\PhpDatatypes\Interfaces\IntegerInterface;
7+
use Nejcc\PhpDatatypes\Abstract\AbstractNativeInteger;
98

10-
final class UInt8 extends AbstractInteger
11-
{
12-
protected function getMinValue(): int|string
13-
{
14-
return 0;
15-
}
16-
17-
protected function getMaxValue(): int|string
18-
{
19-
return 255;
20-
}
219

22-
public function add(IntegerInterface $other): static
23-
{
24-
$result = $this->value + $other->getValue();
25-
return new static($result);
26-
}
10+
/**
11+
* Represents an 8-bit unsigned integer.
12+
*
13+
* @package Nejcc\PhpDatatypes\Integers\Unsigned
14+
*/
15+
final class UInt8 extends AbstractNativeInteger
16+
{
17+
public const MIN_VALUE = 0;
18+
public const MAX_VALUE = 255;
2719
}

Diff for: src/Traits/ArithmeticOperationsTrait.php

-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,4 @@ public function mod(NativeIntegerInterface $other): static
3838
{
3939
return $this->performOperation($other, [$this, 'modValues'], 'mod');
4040
}
41-
42-
// The methods addValues, subtractValues, etc., will be implemented in the abstract classes
4341
}

Diff for: src/helpers.php

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Nejcc\PhpDatatypes\Integers\Signed\Int32;
66
use Nejcc\PhpDatatypes\Integers\Signed\Int64;
77
use Nejcc\PhpDatatypes\Integers\Signed\Int8;
8+
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
89

910
function int8(int $value): Int8
1011
{
@@ -30,3 +31,29 @@ function int128(int $value): Int128
3031
{
3132
return new Int128($value);
3233
}
34+
35+
function uint8(int $value): UInt8
36+
{
37+
return new UInt8($value);
38+
}
39+
40+
function uint16(int $value): UInt16
41+
{
42+
return new UInt16($value);
43+
}
44+
45+
function uint32(int $value): UInt32
46+
{
47+
return new UInt32($value);
48+
}
49+
50+
//
51+
//function uint64(int $value): Int64
52+
//{
53+
// return new UInt64($value);
54+
//}
55+
//
56+
//function uint128(int $value): Int128
57+
//{
58+
// return new UInt128($value);
59+
//}

Diff for: tests/Integers/Unsigned/UInt16Test.php

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)