From 1375bb356fdb50832f7cf2f4774edbc3fcad84f3 Mon Sep 17 00:00:00 2001 From: Zoltan Czirkos Date: Tue, 12 May 2026 14:13:47 +0200 Subject: [PATCH] Inverting FactorUnitPart calculates the reciprocal of its ratio --- src/FactorUnitPart.php | 8 +++++++ tests/FactorUnitPartTest.php | 12 ++++++++++ tests/UnitTest.php | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/src/FactorUnitPart.php b/src/FactorUnitPart.php index 388dc78..1078750 100644 --- a/src/FactorUnitPart.php +++ b/src/FactorUnitPart.php @@ -14,4 +14,12 @@ public function format(): string { return "{$this->getRatio()}"; } + + #[\Override] + public function invert(): self + { + // A FactorUnitPart always has power = 1. So when inverting it, + // we calculate the reciprocal of the ratio instead of negating the power. + return new self(1 / $this->getRatio()); + } } \ No newline at end of file diff --git a/tests/FactorUnitPartTest.php b/tests/FactorUnitPartTest.php index e8af6c5..6fd3cbf 100644 --- a/tests/FactorUnitPartTest.php +++ b/tests/FactorUnitPartTest.php @@ -18,4 +18,16 @@ public function test_stringifies() { $this->assertEquals('100', (string)new FactorUnitPart(100)); } + + public function test_inverts() + { + $factor = new FactorUnitPart(100); + + $factorInverted = $factor->invert(); + + $this->assertEquals(FactorUnitPart::class, get_class($factorInverted)); + $this->assertNull($factorInverted->getDimension()); + $this->assertEquals(0.01, $factorInverted->getRatio()); + $this->assertEquals(1, $factorInverted->getPower()); + } } diff --git a/tests/UnitTest.php b/tests/UnitTest.php index 84ef554..2f40303 100644 --- a/tests/UnitTest.php +++ b/tests/UnitTest.php @@ -171,4 +171,50 @@ public function test_divide() $this->assertEquals($ms->getDimensions(), $meterDividedBySeconds->getDimensions()); } + + public function test_invert() + { + $hundredSquareMeters = new Unit( + new FactorUnitPart(100), + new UnitPart(1, Dimension::LENGTH, 2), + ); + + $inverted = $hundredSquareMeters->invert(); + + $this->assertEquals(-2, $inverted->getDimensions()[Dimension::LENGTH->name]); + $this->assertEquals(0.01, $inverted->getRatio()); + } + + public function test_invert_twice() + { + $hundredSquareMeters = new Unit( + new FactorUnitPart(100), + new UnitPart(1, Dimension::LENGTH, 2), + ); + + $invertedTwice = $hundredSquareMeters->invert()->invert(); + + $this->assertEquals($invertedTwice->getRatio(), $hundredSquareMeters->getRatio()); + $this->assertEquals($invertedTwice->getDimensions(), $hundredSquareMeters->getDimensions()); + } + + public function test_unit_and_factor_mix() + { + // 100m^2 + $hundredSquareMeters = new Unit( + new FactorUnitPart(100), + new UnitPart(1, Dimension::LENGTH, 2), + ); + + // (10m)^2 + $tenMetersSquared = new Unit( + new UnitPart(10, Dimension::LENGTH, 2), + ); + + $this->assertEquals($hundredSquareMeters->getRatio(), $tenMetersSquared->getRatio()); + $this->assertEquals($hundredSquareMeters->getDimensions(), $tenMetersSquared->getDimensions()); + + $this->assertEquals($hundredSquareMeters->invert()->getRatio(), $tenMetersSquared->invert()->getRatio()); + $this->assertEquals($hundredSquareMeters->invert()->getDimensions(), $tenMetersSquared->invert()->getDimensions()); + } }