Skip to content

Commit 12c392e

Browse files
committed
update byte
1 parent f66a6c3 commit 12c392e

File tree

2 files changed

+410
-4
lines changed

2 files changed

+410
-4
lines changed

Tests/ByteTest.php

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Tests;
6+
7+
use Nejcc\PhpDatatypes\Scalar\Byte;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ByteTest extends TestCase
11+
{
12+
/**
13+
* Test that the constructor throws an exception when the value is out of range.
14+
*/
15+
public function testConstructorThrowsExceptionOnInvalidValue()
16+
{
17+
$this->expectException(\InvalidArgumentException::class);
18+
new Byte(300); // Out of range value
19+
}
20+
21+
/**
22+
* Test that the constructor correctly assigns the value.
23+
*/
24+
public function testConstructorAssignsValidValue()
25+
{
26+
$byte = new Byte(100);
27+
$this->assertEquals(100, $byte->getValue());
28+
}
29+
30+
/**
31+
* Test bitwise AND operation.
32+
*/
33+
public function testAndOperation()
34+
{
35+
$byte1 = new Byte(170); // 10101010
36+
$byte2 = new Byte(85); // 01010101
37+
$result = $byte1->and($byte2);
38+
39+
$this->assertEquals(0, $result->getValue()); // 00000000
40+
}
41+
42+
/**
43+
* Test bitwise OR operation.
44+
*/
45+
public function testOrOperation()
46+
{
47+
$byte1 = new Byte(170); // 10101010
48+
$byte2 = new Byte(85); // 01010101
49+
$result = $byte1->or($byte2);
50+
51+
$this->assertEquals(255, $result->getValue()); // 11111111
52+
}
53+
54+
/**
55+
* Test bitwise XOR operation.
56+
*/
57+
public function testXorOperation()
58+
{
59+
$byte1 = new Byte(170); // 10101010
60+
$byte2 = new Byte(85); // 01010101
61+
$result = $byte1->xor($byte2);
62+
63+
$this->assertEquals(255, $result->getValue()); // 11111111
64+
}
65+
66+
/**
67+
* Test bitwise NOT operation.
68+
*/
69+
public function testNotOperation()
70+
{
71+
$byte = new Byte(170); // 10101010
72+
$result = $byte->not();
73+
74+
$this->assertEquals(85, $result->getValue()); // 01010101
75+
}
76+
77+
/**
78+
* Test left bit shifting.
79+
*/
80+
public function testShiftLeftOperation()
81+
{
82+
$byte = new Byte(15); // 00001111
83+
$result = $byte->shiftLeft(2);
84+
85+
$this->assertEquals(60, $result->getValue()); // 00111100
86+
}
87+
88+
/**
89+
* Test right bit shifting.
90+
*/
91+
public function testShiftRightOperation()
92+
{
93+
$byte = new Byte(240); // 11110000
94+
$result = $byte->shiftRight(2);
95+
96+
$this->assertEquals(60, $result->getValue()); // 00111100
97+
}
98+
99+
/**
100+
* Test equality comparison between two bytes.
101+
*/
102+
public function testEquals()
103+
{
104+
$byte1 = new Byte(100);
105+
$byte2 = new Byte(100);
106+
107+
$this->assertTrue($byte1->equals($byte2));
108+
}
109+
110+
/**
111+
* Test greater than comparison.
112+
*/
113+
public function testIsGreaterThan()
114+
{
115+
$byte1 = new Byte(200);
116+
$byte2 = new Byte(100);
117+
118+
$this->assertTrue($byte1->isGreaterThan($byte2));
119+
}
120+
121+
/**
122+
* Test less than comparison.
123+
*/
124+
public function testIsLessThan()
125+
{
126+
$byte1 = new Byte(50);
127+
$byte2 = new Byte(100);
128+
129+
$this->assertTrue($byte1->isLessThan($byte2));
130+
}
131+
132+
/**
133+
* Test converting byte to binary string.
134+
*/
135+
public function testToBinary()
136+
{
137+
$byte = new Byte(170); // 10101010
138+
$this->assertEquals('10101010', $byte->toBinary());
139+
}
140+
141+
/**
142+
* Test converting byte to hexadecimal string.
143+
*/
144+
public function testToHex()
145+
{
146+
$byte = new Byte(170); // 0xAA
147+
$this->assertEquals('AA', $byte->toHex());
148+
}
149+
150+
/**
151+
* Test creating a byte from binary string.
152+
*/
153+
public function testFromBinary()
154+
{
155+
$byte = Byte::fromBinary('10101010');
156+
$this->assertEquals(170, $byte->getValue());
157+
}
158+
159+
/**
160+
* Test creating a byte from hexadecimal string.
161+
*/
162+
public function testFromHex()
163+
{
164+
$byte = Byte::fromHex('AA');
165+
$this->assertEquals(170, $byte->getValue());
166+
}
167+
168+
/**
169+
* Test adding a value to the byte and wrapping around at 255.
170+
*/
171+
public function testAddWithOverflow()
172+
{
173+
$byte = new Byte(250);
174+
$result = $byte->add(10);
175+
176+
$this->assertEquals(4, $result->getValue()); // 250 + 10 = 260, wrapped to 4
177+
}
178+
179+
/**
180+
* Test subtracting a value from the byte and wrapping around at 0.
181+
*/
182+
public function testSubtractWithUnderflow()
183+
{
184+
$byte = new Byte(5);
185+
$result = $byte->subtract(10);
186+
187+
$this->assertEquals(251, $result->getValue()); // 5 - 10 = -5, wrapped to 251
188+
}
189+
190+
/**
191+
* Test string representation of the byte.
192+
*/
193+
public function testToString()
194+
{
195+
$byte = new Byte(100);
196+
$this->assertEquals('100', (string) $byte);
197+
}
198+
}

0 commit comments

Comments
 (0)