From 626ccdcaad80c7751bd9a493058c32ff9b7f337d Mon Sep 17 00:00:00 2001 From: Haj8110 Date: Tue, 18 Mar 2025 13:47:00 +0530 Subject: [PATCH 1/3] Add weight and length conversion functions --- DIRECTORY.md | 2 + tests/Conversions/ConversionsTest.php | 109 ++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 18c6742..3889f1f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -16,6 +16,8 @@ * [Octaltodecimal](./Conversions/OctalToDecimal.php) * [Speedconversion](./Conversions/SpeedConversion.php) * [Temperatureconversions](./Conversions/TemperatureConversions.php) + * [Weightconversions](./Conversions/Weightconversions.php) + * [Lengthconversions](./Conversions/Lengthconversions.php) ## Datastructures * Avltree diff --git a/tests/Conversions/ConversionsTest.php b/tests/Conversions/ConversionsTest.php index 93893b4..771f88a 100644 --- a/tests/Conversions/ConversionsTest.php +++ b/tests/Conversions/ConversionsTest.php @@ -10,6 +10,8 @@ require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php'; require_once __DIR__ . '/../../Conversions/SpeedConversion.php'; require_once __DIR__ . '/../../Conversions/TemperatureConversions.php'; +require_once __DIR__ . '/../../Conversions/Weightconversions.php'; +require_once __DIR__ . '/../../Conversions/Lengthconversions.php'; class ConversionsTest extends TestCase { @@ -142,4 +144,111 @@ public function testFahrenheitToKelvin() $this->expectExceptionMessage('Temperature (Fahrenheit) must be a number'); FahrenheitToKelvin("non-numeric"); } + /** + * Helper method to test invalid input handling in conversion methods + * + * @param string $method The conversion method to test (e.g. 'WeightConversions::kgToLbs') + * @param mixed $value The invalid input value + * @param string $expectedMessage The expected exception message + */ + protected function assertInvalidInputConversion($method, $value, $expectedMessage) + { + try { + // Call the method with the invalid input + $parts = explode('::', $method); + $className = $parts[0]; + $methodName = $parts[1]; + $className::$methodName($value); + + // If we get here, no exception was thrown + $this->fail("Expected exception was not thrown for $method with invalid input"); + } catch (InvalidArgumentException $e) { + // Check if the exception message matches the expected message + $this->assertEquals($expectedMessage, $e->getMessage()); + } + } + public function testKgToLbs() + { + $this->assertEquals(220.462, WeightConversions::kgToLbs(100), 0.001); + $this->assertInvalidInputConversion('WeightConversions::kgToLbs', "invalid string", 'Invalid input for kgToLbs'); + } + + public function testLbsToKg() + { + $this->assertEquals(45.3593, WeightConversions::lbsToKg(100), 0.001); + $this->assertInvalidInputConversion('WeightConversions::lbsToKg', "invalid string", 'Invalid input for lbsToKg'); + } + + public function testGToKg() + { + $this->assertEquals(0.5, WeightConversions::gToKg(500), 0.001); + $this->assertInvalidInputConversion('WeightConversions::gToKg', "invalid string", 'Invalid input for gToKg'); + } + + public function testKgToG() + { + $this->assertEquals(1000, WeightConversions::kgToG(1), 0.001); + $this->assertInvalidInputConversion('WeightConversions::kgToG', "invalid string", 'Invalid input for kgToG'); + } + + public function testOzToLbs() + { + $this->assertEquals(3.5, WeightConversions::ozToLbs(56), 0.001); + $this->assertInvalidInputConversion('WeightConversions::ozToLbs', "invalid string", 'Invalid input for ozToLbs'); + } + + public function testLbsToOz() + { + $this->assertEquals(64, WeightConversions::lbsToOz(4), 0.001); + $this->assertInvalidInputConversion('WeightConversions::lbsToOz', "invalid string", 'Invalid input for lbsToOz'); + } + public function testMToKm() + { + $this->assertEquals(1, LengthConversions::mToKm(1000), 0.001); + $this->assertInvalidInputConversion('LengthConversions::mToKm', "invalid string", 'Invalid input for mToKm'); + } + + public function testKmToM() + { + $this->assertEquals(5000, LengthConversions::kmToM(5), 0.001); + $this->assertInvalidInputConversion('LengthConversions::kmToM', "invalid string", 'Invalid input for kmToM'); + } + + public function testMToMiles() + { + $this->assertEquals(0.621373, LengthConversions::mToMiles(1000), 0.001); + $this->assertInvalidInputConversion('LengthConversions::mToMiles', "invalid string", 'Invalid input for mToMiles'); + } + + public function testMilesToM() + { + $this->assertEquals(1609.34, LengthConversions::milesToM(1), 0.001); + $this->assertInvalidInputConversion('LengthConversions::milesToM', "invalid string", 'Invalid input for milesToM'); + } + + public function testInToCm() + { + $this->assertEquals(25.4, LengthConversions::inToCm(10), 0.001); + $this->assertInvalidInputConversion('LengthConversions::inToCm', "invalid string", 'Invalid input for inToCm'); + } + + public function testCmToIn() + { + $this->assertEquals(39.37, LengthConversions::cmToIn(100), 0.001); + $this->assertInvalidInputConversion('LengthConversions::cmToIn', "invalid string", 'Invalid input for cmToIn'); + } + + public function testKmToMiles() + { + $this->assertEquals(6.21504, LengthConversions::kmToMiles(10), 0.001); + $this->assertInvalidInputConversion('LengthConversions::kmToMiles', "invalid string", 'Invalid input for kmToMiles'); + } + + public function testMilesToKm() + { + $this->assertEquals(16.09, LengthConversions::milesToKm(10), 0.001); + $this->assertInvalidInputConversion('LengthConversions::milesToKm', "invalid string", 'Invalid input for milesToKm'); + } + + } From 9aa383fb807088a2b6dd7d99355acea18efab39b Mon Sep 17 00:00:00 2001 From: Haj8110 Date: Tue, 18 Mar 2025 13:50:29 +0530 Subject: [PATCH 2/3] Add weight and length conversion functions --- Conversions/Lengthconversions.php | 107 ++++++++++++++++++++++++++++++ Conversions/Weightconversions.php | 87 ++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 Conversions/Lengthconversions.php create mode 100644 Conversions/Weightconversions.php diff --git a/Conversions/Lengthconversions.php b/Conversions/Lengthconversions.php new file mode 100644 index 0000000..7fe4200 --- /dev/null +++ b/Conversions/Lengthconversions.php @@ -0,0 +1,107 @@ + Date: Tue, 15 Apr 2025 13:34:15 +0530 Subject: [PATCH 3/3] Update Conversions/Lengthconversions.php Co-authored-by: Brandon Johnson --- Conversions/Lengthconversions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/Lengthconversions.php b/Conversions/Lengthconversions.php index 7fe4200..c62d510 100644 --- a/Conversions/Lengthconversions.php +++ b/Conversions/Lengthconversions.php @@ -13,7 +13,7 @@ class LengthConversions { private static function validateInput($value, $method) { // Make sure we have a numeric value if (!is_numeric($value)) { - throw new InvalidArgumentException("Invalid input for $method"); + throw new InvalidArgumentException("Invalid input for $method: expected numeric, got " . gettype($value) . " (" . var_export($value, true) . ")"); } }