From 626ccdcaad80c7751bd9a493058c32ff9b7f337d Mon Sep 17 00:00:00 2001 From: Haj8110 Date: Tue, 18 Mar 2025 13:47:00 +0530 Subject: [PATCH 1/2] 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 6cd6280f58f44903cadfb177f42307207ea881ae Mon Sep 17 00:00:00 2001 From: Haj8110 Date: Fri, 11 Jul 2025 15:22:24 +0530 Subject: [PATCH 2/2] Add length and weight conversion classes with input validation --- Conversions/Lengthconversions.php | 68 +++++++++++++++++++++++++++ Conversions/Weightconversions.php | 52 ++++++++++++++++++++ Searches/TwoPointers.php | 4 +- composer.json | 3 +- tests/Conversions/ConversionsTest.php | 32 ++++++------- 5 files changed, 138 insertions(+), 21 deletions(-) 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..64f78f5 --- /dev/null +++ b/Conversions/Lengthconversions.php @@ -0,0 +1,68 @@ + $target) { + } elseif ($list[$left] + $list[$right] > $target) { // the sum is greater than the target, so we need to decrease the sum // to decrease the sum we will move our $right pointer to the left $right--; - } else if ($list[$left] + $list[$right] == $target) { + } elseif ($list[$left] + $list[$right] == $target) { // if it's true, we have found a pair $ans++; // now we will move one of our pointers, otherwise it'll run forever diff --git a/composer.json b/composer.json index 89db671..f049d31 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,9 @@ }, "require-dev": { "phpunit/phpunit": "^9", - "squizlabs/php_codesniffer": "^3.7" + "squizlabs/php_codesniffer": "^3.13" }, "scripts": { "test": "vendor/bin/phpunit tests" } } - diff --git a/tests/Conversions/ConversionsTest.php b/tests/Conversions/ConversionsTest.php index 771f88a..667756f 100644 --- a/tests/Conversions/ConversionsTest.php +++ b/tests/Conversions/ConversionsTest.php @@ -159,7 +159,7 @@ protected function assertInvalidInputConversion($method, $value, $expectedMessag $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) { @@ -170,85 +170,83 @@ protected function assertInvalidInputConversion($method, $value, $expectedMessag public function testKgToLbs() { $this->assertEquals(220.462, WeightConversions::kgToLbs(100), 0.001); - $this->assertInvalidInputConversion('WeightConversions::kgToLbs', "invalid string", 'Invalid input for kgToLbs'); + $this->assertInvalidInputConversion('WeightConversions::kgToLbs', "invalid string", "Invalid input for kgToLbs: expected numeric, got string ('invalid string')"); } public function testLbsToKg() { $this->assertEquals(45.3593, WeightConversions::lbsToKg(100), 0.001); - $this->assertInvalidInputConversion('WeightConversions::lbsToKg', "invalid string", 'Invalid input for lbsToKg'); + $this->assertInvalidInputConversion('WeightConversions::lbsToKg', "invalid string", "Invalid input for lbsToKg: expected numeric, got string ('invalid string')"); } public function testGToKg() { $this->assertEquals(0.5, WeightConversions::gToKg(500), 0.001); - $this->assertInvalidInputConversion('WeightConversions::gToKg', "invalid string", 'Invalid input for gToKg'); + $this->assertInvalidInputConversion('WeightConversions::gToKg', "invalid string", "Invalid input for gToKg: expected numeric, got string ('invalid string')"); } public function testKgToG() { $this->assertEquals(1000, WeightConversions::kgToG(1), 0.001); - $this->assertInvalidInputConversion('WeightConversions::kgToG', "invalid string", 'Invalid input for kgToG'); + $this->assertInvalidInputConversion('WeightConversions::kgToG', "invalid string", "Invalid input for kgToG: expected numeric, got string ('invalid string')"); } public function testOzToLbs() { $this->assertEquals(3.5, WeightConversions::ozToLbs(56), 0.001); - $this->assertInvalidInputConversion('WeightConversions::ozToLbs', "invalid string", 'Invalid input for ozToLbs'); + $this->assertInvalidInputConversion('WeightConversions::ozToLbs', "invalid string", "Invalid input for ozToLbs: expected numeric, got string ('invalid string')"); } public function testLbsToOz() { $this->assertEquals(64, WeightConversions::lbsToOz(4), 0.001); - $this->assertInvalidInputConversion('WeightConversions::lbsToOz', "invalid string", 'Invalid input for lbsToOz'); + $this->assertInvalidInputConversion('WeightConversions::lbsToOz', "invalid string", "Invalid input for lbsToOz: expected numeric, got string ('invalid string')"); } public function testMToKm() { $this->assertEquals(1, LengthConversions::mToKm(1000), 0.001); - $this->assertInvalidInputConversion('LengthConversions::mToKm', "invalid string", 'Invalid input for mToKm'); + $this->assertInvalidInputConversion('LengthConversions::mToKm', "invalid string", "Invalid input for mToKm: expected numeric, got string ('invalid string')"); } public function testKmToM() { $this->assertEquals(5000, LengthConversions::kmToM(5), 0.001); - $this->assertInvalidInputConversion('LengthConversions::kmToM', "invalid string", 'Invalid input for kmToM'); + $this->assertInvalidInputConversion('LengthConversions::kmToM', "invalid string", "Invalid input for kmToM: expected numeric, got string ('invalid string')"); } public function testMToMiles() { $this->assertEquals(0.621373, LengthConversions::mToMiles(1000), 0.001); - $this->assertInvalidInputConversion('LengthConversions::mToMiles', "invalid string", 'Invalid input for mToMiles'); + $this->assertInvalidInputConversion('LengthConversions::mToMiles', "invalid string", "Invalid input for mToMiles: expected numeric, got string ('invalid string')"); } public function testMilesToM() { $this->assertEquals(1609.34, LengthConversions::milesToM(1), 0.001); - $this->assertInvalidInputConversion('LengthConversions::milesToM', "invalid string", 'Invalid input for milesToM'); + $this->assertInvalidInputConversion('LengthConversions::milesToM', "invalid string", "Invalid input for milesToM: expected numeric, got string ('invalid string')"); } public function testInToCm() { $this->assertEquals(25.4, LengthConversions::inToCm(10), 0.001); - $this->assertInvalidInputConversion('LengthConversions::inToCm', "invalid string", 'Invalid input for inToCm'); + $this->assertInvalidInputConversion('LengthConversions::inToCm', "invalid string", "Invalid input for inToCm: expected numeric, got string ('invalid string')"); } public function testCmToIn() { $this->assertEquals(39.37, LengthConversions::cmToIn(100), 0.001); - $this->assertInvalidInputConversion('LengthConversions::cmToIn', "invalid string", 'Invalid input for cmToIn'); + $this->assertInvalidInputConversion('LengthConversions::cmToIn', "invalid string", "Invalid input for cmToIn: expected numeric, got string ('invalid string')"); } public function testKmToMiles() { $this->assertEquals(6.21504, LengthConversions::kmToMiles(10), 0.001); - $this->assertInvalidInputConversion('LengthConversions::kmToMiles', "invalid string", 'Invalid input for kmToMiles'); + $this->assertInvalidInputConversion('LengthConversions::kmToMiles', "invalid string", "Invalid input for kmToMiles: expected numeric, got string ('invalid string')"); } public function testMilesToKm() { $this->assertEquals(16.09, LengthConversions::milesToKm(10), 0.001); - $this->assertInvalidInputConversion('LengthConversions::milesToKm', "invalid string", 'Invalid input for milesToKm'); + $this->assertInvalidInputConversion('LengthConversions::milesToKm', "invalid string", "Invalid input for milesToKm: expected numeric, got string ('invalid string')"); } - - }