From a49763dd739c3c68c4a8322896d594e926ac8e6b Mon Sep 17 00:00:00 2001 From: Zhineng Li Date: Mon, 5 Jan 2026 16:26:11 +0800 Subject: first commit --- tests/ConstantTest.php | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/ConstantTest.php (limited to 'tests/ConstantTest.php') diff --git a/tests/ConstantTest.php b/tests/ConstantTest.php new file mode 100644 index 0000000..78d1be3 --- /dev/null +++ b/tests/ConstantTest.php @@ -0,0 +1,70 @@ +assertSame('machine_id', $field->name); + $this->assertSame(10, $field->bits); + $this->assertSame(0, $field->value); + } + + public function testMaxValueShouldBeCalculatedCorrectly(): void + { + $field = new Constant('machine_id', 10); + $maxValue = (1 << 10) - 1; + $this->assertSame($maxValue, $field->maxValue()); + } + + public function testInitialValueCanBeSet(): void + { + $field = new Constant('machine_id', 10, 2); + $this->assertSame(2, $field->value); + } + + public function testInitialValueMustBePositive(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Field value must be non-negative.'); + new Constant('machine_id', 10, -1); + } + + public function testInitialValueMustNotExceedMaxValue(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Field value 1024 exceeds maximum 1023 for 10 bits.'); + new Constant('machine_id', 10, 1024); + } + + public function testConstantHasMakeFactoryMethod(): void + { + $field = Constant::make('machine_id', 10, 5); + $this->assertSame('machine_id', $field->name); + $this->assertSame(10, $field->bits); + $this->assertSame(5, $field->value); + } + + public function testBitsMustBeAtLeastOne(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Bits must be between 1 and 63.'); + new Constant('machine_id', 0); + } + + public function testBitsMustNotExceed63(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Bits must be between 1 and 63.'); + new Constant('machine_id', 64); + } +} -- cgit v1.2.3