Skip to content

Commit 918b122

Browse files
authored
Merge pull request #10 from Nejcc/dev
update stringArray
2 parents 329b308 + 9a86f1c commit 918b122

11 files changed

+1502
-18
lines changed

Diff for: Tests/ByteSliceTest.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Nejcc\PhpDatatypes\Tests;
5+
6+
use Nejcc\PhpDatatypes\Composite\Arrays\ByteSlice;
7+
use Nejcc\PhpDatatypes\Exceptions\InvalidByteException;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ByteSliceTest extends TestCase
11+
{
12+
/**
13+
* Test creating a valid ByteSlice instance.
14+
*/
15+
public function testCreateValidByteSlice(): void
16+
{
17+
$byteSlice = new ByteSlice([10, 20, 255]);
18+
$this->assertSame([10, 20, 255], $byteSlice->getValue());
19+
}
20+
21+
/**
22+
* Test creating a ByteSlice with invalid byte values.
23+
*/
24+
public function testCreateInvalidByteSlice(): void
25+
{
26+
$this->expectException(InvalidByteException::class);
27+
$this->expectExceptionMessage("All elements must be valid bytes (0-255). Invalid value: -1");
28+
29+
new ByteSlice([10, -1, 255]); // -1 is out of valid byte range
30+
}
31+
32+
/**
33+
* Test converting ByteSlice to hexadecimal representation.
34+
*/
35+
public function testConvertToHexadecimal(): void
36+
{
37+
$byteSlice = new ByteSlice([10, 20, 255]);
38+
$this->assertSame("0A14FF", $byteSlice->toHex());
39+
}
40+
41+
/**
42+
* Test slicing a portion of ByteSlice.
43+
*/
44+
public function testSliceByteSlice(): void
45+
{
46+
$byteSlice = new ByteSlice([10, 20, 255, 30, 40]);
47+
$sliced = $byteSlice->slice(1, 3);
48+
$this->assertSame([20, 255, 30], $sliced->getValue());
49+
}
50+
51+
/**
52+
* Test merging two ByteSlices.
53+
*/
54+
public function testMergeByteSlices(): void
55+
{
56+
$byteSlice1 = new ByteSlice([10, 20, 255]);
57+
$byteSlice2 = new ByteSlice([1, 2, 3]);
58+
$merged = $byteSlice1->merge($byteSlice2);
59+
60+
$this->assertSame([10, 20, 255, 1, 2, 3], $merged->getValue());
61+
}
62+
63+
/**
64+
* Test getting the count of bytes in a ByteSlice.
65+
*/
66+
public function testGetByteCount(): void
67+
{
68+
$byteSlice = new ByteSlice([10, 20, 255]);
69+
$this->assertCount(3, $byteSlice);
70+
}
71+
72+
/**
73+
* Test ArrayAccess implementation for accessing a byte at a specific index.
74+
*/
75+
public function testArrayAccessGet(): void
76+
{
77+
$byteSlice = new ByteSlice([10, 20, 255]);
78+
$this->assertSame(20, $byteSlice[1]);
79+
$this->assertNull($byteSlice[999]); // Accessing an invalid index returns null
80+
}
81+
82+
/**
83+
* Test ArrayAccess prevents modification of ByteSlice.
84+
*/
85+
public function testArrayAccessSetThrowsException(): void
86+
{
87+
$this->expectException(InvalidByteException::class);
88+
$this->expectExceptionMessage("Cannot modify an immutable ByteSlice.");
89+
90+
$byteSlice = new ByteSlice([10, 20, 255]);
91+
$byteSlice[1] = 100; // Should throw an exception
92+
}
93+
94+
/**
95+
* Test ArrayAccess prevents unsetting a byte in ByteSlice.
96+
*/
97+
public function testArrayAccessUnsetThrowsException(): void
98+
{
99+
$this->expectException(InvalidByteException::class);
100+
$this->expectExceptionMessage("Cannot unset a value in an immutable ByteSlice.");
101+
102+
$byteSlice = new ByteSlice([10, 20, 255]);
103+
unset($byteSlice[1]); // Should throw an exception
104+
}
105+
106+
/**
107+
* Test iterating over ByteSlice with IteratorAggregate.
108+
*/
109+
public function testIterationOverByteSlice(): void
110+
{
111+
$byteSlice = new ByteSlice([10, 20, 255]);
112+
$result = [];
113+
114+
foreach ($byteSlice as $byte) {
115+
$result[] = $byte;
116+
}
117+
118+
$this->assertSame([10, 20, 255], $result);
119+
}
120+
121+
/**
122+
* Test an empty ByteSlice.
123+
*/
124+
public function testEmptyByteSlice(): void
125+
{
126+
$byteSlice = new ByteSlice([]);
127+
$this->assertSame([], $byteSlice->getValue());
128+
$this->assertCount(0, $byteSlice);
129+
}
130+
}
131+

Diff for: Tests/FloatArrayTest.php

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Nejcc\PhpDatatypes\Tests;
5+
6+
use Nejcc\PhpDatatypes\Composite\Arrays\FloatArray;
7+
use Nejcc\PhpDatatypes\Exceptions\InvalidFloatException;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class FloatArrayTest extends TestCase
11+
{
12+
/**
13+
* Test creating a valid FloatArray instance.
14+
* @throws InvalidFloatException
15+
*/
16+
public function testCreateValidFloatArray(): void
17+
{
18+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
19+
$this->assertSame([10.5, 20.1, 30.7], $floatArray->getValue());
20+
}
21+
22+
/**
23+
* Test creating a FloatArray with invalid float values.
24+
*/
25+
public function testCreateInvalidFloatArray(): void
26+
{
27+
$this->expectException(InvalidFloatException::class);
28+
$this->expectExceptionMessage("All elements must be floats. Invalid value: 1");
29+
30+
new FloatArray([10.5, 1, 'not a float']); // Invalid non-float values
31+
}
32+
33+
/**
34+
* Test adding floats to a FloatArray.
35+
* @throws InvalidFloatException
36+
*/
37+
public function testAddFloats(): void
38+
{
39+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
40+
$newFloatArray = $floatArray->add(40.2, 50.3);
41+
42+
$this->assertSame([10.5, 20.1, 30.7, 40.2, 50.3], $newFloatArray->getValue());
43+
}
44+
45+
/**
46+
* Test removing floats from a FloatArray.
47+
* @throws InvalidFloatException
48+
*/
49+
public function testRemoveFloats(): void
50+
{
51+
$floatArray = new FloatArray([10.5, 20.1, 30.7, 40.2]);
52+
$modifiedFloatArray = $floatArray->remove(20.1, 30.7);
53+
54+
$this->assertSame([10.5, 40.2], $modifiedFloatArray->getValue());
55+
}
56+
57+
/**
58+
* Test calculating the sum of floats.
59+
* @throws InvalidFloatException
60+
*/
61+
public function testSumOfFloats(): void
62+
{
63+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
64+
$this->assertSame(61.3, $floatArray->sum());
65+
}
66+
67+
/**
68+
* Test calculating the average of floats.
69+
* @throws InvalidFloatException
70+
*/
71+
public function testAverageOfFloats(): void
72+
{
73+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
74+
$this->assertSame(20.433333333333334, $floatArray->average());
75+
}
76+
77+
/**
78+
* Test calculating the average of an empty FloatArray.
79+
*/
80+
public function testAverageOfEmptyFloatArray(): void
81+
{
82+
$this->expectException(InvalidFloatException::class);
83+
$this->expectExceptionMessage("Cannot calculate average of an empty array.");
84+
85+
$floatArray = new FloatArray([]);
86+
$floatArray->average(); // Should throw exception
87+
}
88+
89+
/**
90+
* Test getting the count of floats in a FloatArray.
91+
* @throws InvalidFloatException
92+
*/
93+
public function testGetFloatCount(): void
94+
{
95+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
96+
$this->assertCount(3, $floatArray);
97+
}
98+
99+
/**
100+
* Test ArrayAccess implementation for accessing a float at a specific index.
101+
* @throws InvalidFloatException
102+
*/
103+
public function testArrayAccessGet(): void
104+
{
105+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
106+
$this->assertSame(20.1, $floatArray[1]);
107+
$this->assertNull($floatArray[999]); // Accessing an invalid index returns null
108+
}
109+
110+
/**
111+
* Test ArrayAccess prevents modification of FloatArray.
112+
*/
113+
public function testArrayAccessSetThrowsException(): void
114+
{
115+
$this->expectException(InvalidFloatException::class);
116+
$this->expectExceptionMessage("Cannot modify an immutable FloatArray.");
117+
118+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
119+
$floatArray[1] = 50.3; // Should throw exception
120+
}
121+
122+
/**
123+
* Test ArrayAccess prevents unsetting a float in FloatArray.
124+
*/
125+
public function testArrayAccessUnsetThrowsException(): void
126+
{
127+
$this->expectException(InvalidFloatException::class);
128+
$this->expectExceptionMessage("Cannot unset a value in an immutable FloatArray.");
129+
130+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
131+
unset($floatArray[1]); // Should throw exception
132+
}
133+
134+
/**
135+
* Test iterating over FloatArray with IteratorAggregate.
136+
* @throws InvalidFloatException
137+
*/
138+
public function testIterationOverFloatArray(): void
139+
{
140+
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
141+
$result = [];
142+
143+
foreach ($floatArray as $float) {
144+
$result[] = $float;
145+
}
146+
147+
$this->assertSame([10.5, 20.1, 30.7], $result);
148+
}
149+
150+
/**
151+
* Test creating an empty FloatArray.
152+
* @throws InvalidFloatException
153+
*/
154+
public function testEmptyFloatArray(): void
155+
{
156+
$floatArray = new FloatArray([]);
157+
$this->assertSame([], $floatArray->getValue());
158+
$this->assertCount(0, $floatArray);
159+
}
160+
}

0 commit comments

Comments
 (0)