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
198 changes: 198 additions & 0 deletions Tests/ByteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

declare(strict_types=1);

namespace Nejcc\PhpDatatypes\Tests;

use Nejcc\PhpDatatypes\Scalar\Byte;
use PHPUnit\Framework\TestCase;

class ByteTest extends TestCase
{
/**
* Test that the constructor throws an exception when the value is out of range.
*/
public function testConstructorThrowsExceptionOnInvalidValue()
{
$this->expectException(\InvalidArgumentException::class);
new Byte(300); // Out of range value
}

/**
* Test that the constructor correctly assigns the value.
*/
public function testConstructorAssignsValidValue()
{
$byte = new Byte(100);
$this->assertEquals(100, $byte->getValue());
}

/**
* Test bitwise AND operation.
*/
public function testAndOperation()
{
$byte1 = new Byte(170); // 10101010
$byte2 = new Byte(85); // 01010101
$result = $byte1->and($byte2);

$this->assertEquals(0, $result->getValue()); // 00000000
}

/**
* Test bitwise OR operation.
*/
public function testOrOperation()
{
$byte1 = new Byte(170); // 10101010
$byte2 = new Byte(85); // 01010101
$result = $byte1->or($byte2);

$this->assertEquals(255, $result->getValue()); // 11111111
}

/**
* Test bitwise XOR operation.
*/
public function testXorOperation()
{
$byte1 = new Byte(170); // 10101010
$byte2 = new Byte(85); // 01010101
$result = $byte1->xor($byte2);

$this->assertEquals(255, $result->getValue()); // 11111111
}

/**
* Test bitwise NOT operation.
*/
public function testNotOperation()
{
$byte = new Byte(170); // 10101010
$result = $byte->not();

$this->assertEquals(85, $result->getValue()); // 01010101
}

/**
* Test left bit shifting.
*/
public function testShiftLeftOperation()
{
$byte = new Byte(15); // 00001111
$result = $byte->shiftLeft(2);

$this->assertEquals(60, $result->getValue()); // 00111100
}

/**
* Test right bit shifting.
*/
public function testShiftRightOperation()
{
$byte = new Byte(240); // 11110000
$result = $byte->shiftRight(2);

$this->assertEquals(60, $result->getValue()); // 00111100
}

/**
* Test equality comparison between two bytes.
*/
public function testEquals()
{
$byte1 = new Byte(100);
$byte2 = new Byte(100);

$this->assertTrue($byte1->equals($byte2));
}

/**
* Test greater than comparison.
*/
public function testIsGreaterThan()
{
$byte1 = new Byte(200);
$byte2 = new Byte(100);

$this->assertTrue($byte1->isGreaterThan($byte2));
}

/**
* Test less than comparison.
*/
public function testIsLessThan()
{
$byte1 = new Byte(50);
$byte2 = new Byte(100);

$this->assertTrue($byte1->isLessThan($byte2));
}

/**
* Test converting byte to binary string.
*/
public function testToBinary()
{
$byte = new Byte(170); // 10101010
$this->assertEquals('10101010', $byte->toBinary());
}

/**
* Test converting byte to hexadecimal string.
*/
public function testToHex()
{
$byte = new Byte(170); // 0xAA
$this->assertEquals('AA', $byte->toHex());
}

/**
* Test creating a byte from binary string.
*/
public function testFromBinary()
{
$byte = Byte::fromBinary('10101010');
$this->assertEquals(170, $byte->getValue());
}

/**
* Test creating a byte from hexadecimal string.
*/
public function testFromHex()
{
$byte = Byte::fromHex('AA');
$this->assertEquals(170, $byte->getValue());
}

/**
* Test adding a value to the byte and wrapping around at 255.
*/
public function testAddWithOverflow()
{
$byte = new Byte(250);
$result = $byte->add(10);

$this->assertEquals(4, $result->getValue()); // 250 + 10 = 260, wrapped to 4
}

/**
* Test subtracting a value from the byte and wrapping around at 0.
*/
public function testSubtractWithUnderflow()
{
$byte = new Byte(5);
$result = $byte->subtract(10);

$this->assertEquals(251, $result->getValue()); // 5 - 10 = -5, wrapped to 251
}

/**
* Test string representation of the byte.
*/
public function testToString()
{
$byte = new Byte(100);
$this->assertEquals('100', (string) $byte);
}
}
148 changes: 148 additions & 0 deletions Tests/CharTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

namespace Nejcc\PhpDatatypes\Tests;

use Nejcc\PhpDatatypes\Scalar\Char;
use PHPUnit\Framework\TestCase;

class CharTest extends TestCase
{
/**
* Test that the constructor throws an exception for a string longer than 1 character.
*/
public function testConstructorThrowsExceptionOnInvalidValue()
{
$this->expectException(\InvalidArgumentException::class);
new Char('AB'); // Invalid, more than 1 character
}

/**
* Test that the constructor correctly assigns a single character.
*/
public function testConstructorAssignsValidValue()
{
$char = new Char('A');
$this->assertEquals('A', $char->getValue());
}

/**
* Test converting a character to uppercase.
*/
public function testToUpperCase()
{
$char = new Char('a');
$upperChar = $char->toUpperCase();

$this->assertEquals('A', $upperChar->getValue());
}

/**
* Test converting a character to lowercase.
*/
public function testToLowerCase()
{
$char = new Char('A');
$lowerChar = $char->toLowerCase();

$this->assertEquals('a', $lowerChar->getValue());
}

/**
* Test if a character is a letter.
*/
public function testIsLetter()
{
$char = new Char('A');
$this->assertTrue($char->isLetter());

$char = new Char('1');
$this->assertFalse($char->isLetter());
}

/**
* Test if a character is a digit.
*/
public function testIsDigit()
{
$char = new Char('1');
$this->assertTrue($char->isDigit());

$char = new Char('A');
$this->assertFalse($char->isDigit());
}

/**
* Test if a character is uppercase.
*/
public function testIsUpperCase()
{
$char = new Char('A');
$this->assertTrue($char->isUpperCase());

$char = new Char('a');
$this->assertFalse($char->isUpperCase());
}

/**
* Test if a character is lowercase.
*/
public function testIsLowerCase()
{
$char = new Char('a');
$this->assertTrue($char->isLowerCase());

$char = new Char('A');
$this->assertFalse($char->isLowerCase());
}

/**
* Test the equality of two Char objects.
*/
public function testEquals()
{
$char1 = new Char('A');
$char2 = new Char('A');
$char3 = new Char('B');

$this->assertTrue($char1->equals($char2));
$this->assertFalse($char1->equals($char3));
}

/**
* Test converting a character to its ASCII code.
*/
public function testToAscii()
{
$char = new Char('A');
$this->assertEquals(65, $char->toAscii());
}

/**
* Test creating a Char from an ASCII code.
*/
public function testFromAscii()
{
$char = Char::fromAscii(65); // ASCII for 'A'
$this->assertEquals('A', $char->getValue());
}

/**
* Test that an exception is thrown for an invalid ASCII code.
*/
public function testFromAsciiThrowsExceptionOnInvalidValue()
{
$this->expectException(\InvalidArgumentException::class);
Char::fromAscii(300); // Invalid, out of ASCII range
}

/**
* Test converting a Char object to a string.
*/
public function testToString()
{
$char = new Char('A');
$this->assertEquals('A', (string) $char);
}
}
5 changes: 5 additions & 0 deletions Tests/DictionaryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
declare(strict_types=1);

namespace Nejcc\PhpDatatypes\Tests;

use InvalidArgumentException;
use Nejcc\PhpDatatypes\Composite\Dictionary;
use OutOfBoundsException;
use PHPUnit\Framework\TestCase;

class DictionaryTest extends TestCase
Expand Down
1 change: 1 addition & 0 deletions Tests/Floats/Float64Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Nejcc\PhpDatatypes\Tests;


use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
use PHPUnit\Framework\TestCase;

Expand Down
4 changes: 4 additions & 0 deletions Tests/ListDataTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

declare(strict_types=1);

namespace Nejcc\PhpDatatypes\Tests;

use Nejcc\PhpDatatypes\Composite\ListData;
use OutOfBoundsException;
use PHPUnit\Framework\TestCase;
class ListDataTest extends TestCase
{
Expand Down
5 changes: 5 additions & 0 deletions Tests/StructTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

declare(strict_types=1);

namespace Nejcc\PhpDatatypes\Tests;

use InvalidArgumentException;
use Nejcc\PhpDatatypes\Composite\Struct\Struct;
use PHPUnit\Framework\TestCase;

Expand Down
Loading
Loading