diff options
Diffstat (limited to 'tests/StructureTest.php')
| -rw-r--r-- | tests/StructureTest.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/StructureTest.php b/tests/StructureTest.php new file mode 100644 index 0000000..80d94fe --- /dev/null +++ b/tests/StructureTest.php @@ -0,0 +1,70 @@ +<?php + +declare(strict_types=1); + +namespace Zhineng\Snowflake\Tests; + +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; +use Zhineng\Snowflake\Constant; +use Zhineng\Snowflake\Sequence; +use Zhineng\Snowflake\Structure; + +#[CoversClass(Structure::class)] +final class StructureTest extends TestCase +{ + public function testAddFieldAndRetrieveComponents(): void + { + $struct = new Structure; + $struct->add(Constant::make('machine_id', 10)); + $this->assertCount(1, $struct->components()); + } + + public function testAddMultipleFields(): void + { + $struct = new Structure; + $struct->add(Constant::make('machine_id', 5)); + $struct->add(Constant::make('datacenter_id', 5)); + $this->assertCount(2, $struct->components()); + } + + public function testOffsetsAreCalculatedCorrectly(): void + { + $struct = new Structure; + $struct->add($machineId = Constant::make('machine_id', 5)); + $struct->add($dataCenterId = Constant::make('datacenter_id', 5)); + $this->assertSame(0, $machineId->offset()); + $this->assertSame(5, $dataCenterId->offset()); + } + + public function testSizeResolution(): void + { + $struct = new Structure; + $struct->add(Constant::make('machine_id', 5)); + $struct->add(Constant::make('datacenter_id', 5)); + $this->assertSame(10, $struct->size()); + } + + public function testAtMostOneSequenceIsAllowed(): void + { + $struct = new Structure; + $struct->add(Sequence::make('seq1', 12)); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Only one sequence field is allowed in a structure.'); + + $struct->add(Sequence::make('seq2', 12)); + } + + public function testTotalSizeCannotExceed63Bits(): void + { + $struct = new Structure; + $struct->add(Constant::make('field1', 32)); + $struct->add(Constant::make('field2', 31)); + + $this->expectException(\OverflowException::class); + $this->expectExceptionMessage('Total structure size cannot exceed 63 bits.'); + + $struct->add(Constant::make('field3', 1)); + } +} |
