Skip to content

Commit 329b308

Browse files
authored
Merge pull request #9 from Nejcc/dev
Dev
2 parents 0b73739 + ad2bb76 commit 329b308

14 files changed

+930
-49
lines changed

Tests/ByteTest.php

+198
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+
}

Tests/CharTest.php

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Tests;
6+
7+
use Nejcc\PhpDatatypes\Scalar\Char;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class CharTest extends TestCase
11+
{
12+
/**
13+
* Test that the constructor throws an exception for a string longer than 1 character.
14+
*/
15+
public function testConstructorThrowsExceptionOnInvalidValue()
16+
{
17+
$this->expectException(\InvalidArgumentException::class);
18+
new Char('AB'); // Invalid, more than 1 character
19+
}
20+
21+
/**
22+
* Test that the constructor correctly assigns a single character.
23+
*/
24+
public function testConstructorAssignsValidValue()
25+
{
26+
$char = new Char('A');
27+
$this->assertEquals('A', $char->getValue());
28+
}
29+
30+
/**
31+
* Test converting a character to uppercase.
32+
*/
33+
public function testToUpperCase()
34+
{
35+
$char = new Char('a');
36+
$upperChar = $char->toUpperCase();
37+
38+
$this->assertEquals('A', $upperChar->getValue());
39+
}
40+
41+
/**
42+
* Test converting a character to lowercase.
43+
*/
44+
public function testToLowerCase()
45+
{
46+
$char = new Char('A');
47+
$lowerChar = $char->toLowerCase();
48+
49+
$this->assertEquals('a', $lowerChar->getValue());
50+
}
51+
52+
/**
53+
* Test if a character is a letter.
54+
*/
55+
public function testIsLetter()
56+
{
57+
$char = new Char('A');
58+
$this->assertTrue($char->isLetter());
59+
60+
$char = new Char('1');
61+
$this->assertFalse($char->isLetter());
62+
}
63+
64+
/**
65+
* Test if a character is a digit.
66+
*/
67+
public function testIsDigit()
68+
{
69+
$char = new Char('1');
70+
$this->assertTrue($char->isDigit());
71+
72+
$char = new Char('A');
73+
$this->assertFalse($char->isDigit());
74+
}
75+
76+
/**
77+
* Test if a character is uppercase.
78+
*/
79+
public function testIsUpperCase()
80+
{
81+
$char = new Char('A');
82+
$this->assertTrue($char->isUpperCase());
83+
84+
$char = new Char('a');
85+
$this->assertFalse($char->isUpperCase());
86+
}
87+
88+
/**
89+
* Test if a character is lowercase.
90+
*/
91+
public function testIsLowerCase()
92+
{
93+
$char = new Char('a');
94+
$this->assertTrue($char->isLowerCase());
95+
96+
$char = new Char('A');
97+
$this->assertFalse($char->isLowerCase());
98+
}
99+
100+
/**
101+
* Test the equality of two Char objects.
102+
*/
103+
public function testEquals()
104+
{
105+
$char1 = new Char('A');
106+
$char2 = new Char('A');
107+
$char3 = new Char('B');
108+
109+
$this->assertTrue($char1->equals($char2));
110+
$this->assertFalse($char1->equals($char3));
111+
}
112+
113+
/**
114+
* Test converting a character to its ASCII code.
115+
*/
116+
public function testToAscii()
117+
{
118+
$char = new Char('A');
119+
$this->assertEquals(65, $char->toAscii());
120+
}
121+
122+
/**
123+
* Test creating a Char from an ASCII code.
124+
*/
125+
public function testFromAscii()
126+
{
127+
$char = Char::fromAscii(65); // ASCII for 'A'
128+
$this->assertEquals('A', $char->getValue());
129+
}
130+
131+
/**
132+
* Test that an exception is thrown for an invalid ASCII code.
133+
*/
134+
public function testFromAsciiThrowsExceptionOnInvalidValue()
135+
{
136+
$this->expectException(\InvalidArgumentException::class);
137+
Char::fromAscii(300); // Invalid, out of ASCII range
138+
}
139+
140+
/**
141+
* Test converting a Char object to a string.
142+
*/
143+
public function testToString()
144+
{
145+
$char = new Char('A');
146+
$this->assertEquals('A', (string) $char);
147+
}
148+
}

Tests/DictionaryTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
2+
declare(strict_types=1);
23

4+
namespace Nejcc\PhpDatatypes\Tests;
5+
6+
use InvalidArgumentException;
37
use Nejcc\PhpDatatypes\Composite\Dictionary;
8+
use OutOfBoundsException;
49
use PHPUnit\Framework\TestCase;
510

611
class DictionaryTest extends TestCase

Tests/Floats/Float64Test.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Nejcc\PhpDatatypes\Tests;
66

7+
78
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
89
use PHPUnit\Framework\TestCase;
910

Tests/ListDataTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Tests;
36

47
use Nejcc\PhpDatatypes\Composite\ListData;
8+
use OutOfBoundsException;
59
use PHPUnit\Framework\TestCase;
610
class ListDataTest extends TestCase
711
{

Tests/StructTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Tests;
6+
7+
use InvalidArgumentException;
38
use Nejcc\PhpDatatypes\Composite\Struct\Struct;
49
use PHPUnit\Framework\TestCase;
510

0 commit comments

Comments
 (0)