Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions Tests/ByteSliceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);

namespace Nejcc\PhpDatatypes\Tests;

use Nejcc\PhpDatatypes\Composite\Arrays\ByteSlice;
use Nejcc\PhpDatatypes\Exceptions\InvalidByteException;
use PHPUnit\Framework\TestCase;

class ByteSliceTest extends TestCase
{
/**
* Test creating a valid ByteSlice instance.
*/
public function testCreateValidByteSlice(): void
{
$byteSlice = new ByteSlice([10, 20, 255]);
$this->assertSame([10, 20, 255], $byteSlice->getValue());
}

/**
* Test creating a ByteSlice with invalid byte values.
*/
public function testCreateInvalidByteSlice(): void
{
$this->expectException(InvalidByteException::class);
$this->expectExceptionMessage("All elements must be valid bytes (0-255). Invalid value: -1");

new ByteSlice([10, -1, 255]); // -1 is out of valid byte range
}

/**
* Test converting ByteSlice to hexadecimal representation.
*/
public function testConvertToHexadecimal(): void
{
$byteSlice = new ByteSlice([10, 20, 255]);
$this->assertSame("0A14FF", $byteSlice->toHex());
}

/**
* Test slicing a portion of ByteSlice.
*/
public function testSliceByteSlice(): void
{
$byteSlice = new ByteSlice([10, 20, 255, 30, 40]);
$sliced = $byteSlice->slice(1, 3);
$this->assertSame([20, 255, 30], $sliced->getValue());
}

/**
* Test merging two ByteSlices.
*/
public function testMergeByteSlices(): void
{
$byteSlice1 = new ByteSlice([10, 20, 255]);
$byteSlice2 = new ByteSlice([1, 2, 3]);
$merged = $byteSlice1->merge($byteSlice2);

$this->assertSame([10, 20, 255, 1, 2, 3], $merged->getValue());
}

/**
* Test getting the count of bytes in a ByteSlice.
*/
public function testGetByteCount(): void
{
$byteSlice = new ByteSlice([10, 20, 255]);
$this->assertCount(3, $byteSlice);
}

/**
* Test ArrayAccess implementation for accessing a byte at a specific index.
*/
public function testArrayAccessGet(): void
{
$byteSlice = new ByteSlice([10, 20, 255]);
$this->assertSame(20, $byteSlice[1]);
$this->assertNull($byteSlice[999]); // Accessing an invalid index returns null
}

/**
* Test ArrayAccess prevents modification of ByteSlice.
*/
public function testArrayAccessSetThrowsException(): void
{
$this->expectException(InvalidByteException::class);
$this->expectExceptionMessage("Cannot modify an immutable ByteSlice.");

$byteSlice = new ByteSlice([10, 20, 255]);
$byteSlice[1] = 100; // Should throw an exception
}

/**
* Test ArrayAccess prevents unsetting a byte in ByteSlice.
*/
public function testArrayAccessUnsetThrowsException(): void
{
$this->expectException(InvalidByteException::class);
$this->expectExceptionMessage("Cannot unset a value in an immutable ByteSlice.");

$byteSlice = new ByteSlice([10, 20, 255]);
unset($byteSlice[1]); // Should throw an exception
}

/**
* Test iterating over ByteSlice with IteratorAggregate.
*/
public function testIterationOverByteSlice(): void
{
$byteSlice = new ByteSlice([10, 20, 255]);
$result = [];

foreach ($byteSlice as $byte) {
$result[] = $byte;
}

$this->assertSame([10, 20, 255], $result);
}

/**
* Test an empty ByteSlice.
*/
public function testEmptyByteSlice(): void
{
$byteSlice = new ByteSlice([]);
$this->assertSame([], $byteSlice->getValue());
$this->assertCount(0, $byteSlice);
}
}

160 changes: 160 additions & 0 deletions Tests/FloatArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
declare(strict_types=1);

namespace Nejcc\PhpDatatypes\Tests;

use Nejcc\PhpDatatypes\Composite\Arrays\FloatArray;
use Nejcc\PhpDatatypes\Exceptions\InvalidFloatException;
use PHPUnit\Framework\TestCase;

class FloatArrayTest extends TestCase
{
/**
* Test creating a valid FloatArray instance.
* @throws InvalidFloatException
*/
public function testCreateValidFloatArray(): void
{
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
$this->assertSame([10.5, 20.1, 30.7], $floatArray->getValue());
}

/**
* Test creating a FloatArray with invalid float values.
*/
public function testCreateInvalidFloatArray(): void
{
$this->expectException(InvalidFloatException::class);
$this->expectExceptionMessage("All elements must be floats. Invalid value: 1");

new FloatArray([10.5, 1, 'not a float']); // Invalid non-float values
}

/**
* Test adding floats to a FloatArray.
* @throws InvalidFloatException
*/
public function testAddFloats(): void
{
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
$newFloatArray = $floatArray->add(40.2, 50.3);

$this->assertSame([10.5, 20.1, 30.7, 40.2, 50.3], $newFloatArray->getValue());
}

/**
* Test removing floats from a FloatArray.
* @throws InvalidFloatException
*/
public function testRemoveFloats(): void
{
$floatArray = new FloatArray([10.5, 20.1, 30.7, 40.2]);
$modifiedFloatArray = $floatArray->remove(20.1, 30.7);

$this->assertSame([10.5, 40.2], $modifiedFloatArray->getValue());
}

/**
* Test calculating the sum of floats.
* @throws InvalidFloatException
*/
public function testSumOfFloats(): void
{
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
$this->assertSame(61.3, $floatArray->sum());
}

/**
* Test calculating the average of floats.
* @throws InvalidFloatException
*/
public function testAverageOfFloats(): void
{
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
$this->assertSame(20.433333333333334, $floatArray->average());
}

/**
* Test calculating the average of an empty FloatArray.
*/
public function testAverageOfEmptyFloatArray(): void
{
$this->expectException(InvalidFloatException::class);
$this->expectExceptionMessage("Cannot calculate average of an empty array.");

$floatArray = new FloatArray([]);
$floatArray->average(); // Should throw exception
}

/**
* Test getting the count of floats in a FloatArray.
* @throws InvalidFloatException
*/
public function testGetFloatCount(): void
{
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
$this->assertCount(3, $floatArray);
}

/**
* Test ArrayAccess implementation for accessing a float at a specific index.
* @throws InvalidFloatException
*/
public function testArrayAccessGet(): void
{
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
$this->assertSame(20.1, $floatArray[1]);
$this->assertNull($floatArray[999]); // Accessing an invalid index returns null
}

/**
* Test ArrayAccess prevents modification of FloatArray.
*/
public function testArrayAccessSetThrowsException(): void
{
$this->expectException(InvalidFloatException::class);
$this->expectExceptionMessage("Cannot modify an immutable FloatArray.");

$floatArray = new FloatArray([10.5, 20.1, 30.7]);
$floatArray[1] = 50.3; // Should throw exception
}

/**
* Test ArrayAccess prevents unsetting a float in FloatArray.
*/
public function testArrayAccessUnsetThrowsException(): void
{
$this->expectException(InvalidFloatException::class);
$this->expectExceptionMessage("Cannot unset a value in an immutable FloatArray.");

$floatArray = new FloatArray([10.5, 20.1, 30.7]);
unset($floatArray[1]); // Should throw exception
}

/**
* Test iterating over FloatArray with IteratorAggregate.
* @throws InvalidFloatException
*/
public function testIterationOverFloatArray(): void
{
$floatArray = new FloatArray([10.5, 20.1, 30.7]);
$result = [];

foreach ($floatArray as $float) {
$result[] = $float;
}

$this->assertSame([10.5, 20.1, 30.7], $result);
}

/**
* Test creating an empty FloatArray.
* @throws InvalidFloatException
*/
public function testEmptyFloatArray(): void
{
$floatArray = new FloatArray([]);
$this->assertSame([], $floatArray->getValue());
$this->assertCount(0, $floatArray);
}
}
Loading
Loading