From 2aa8b158e88b901407f8e80659337c14962b9e2b Mon Sep 17 00:00:00 2001 From: Fejan Malek Date: Mon, 12 Aug 2024 07:21:44 +0530 Subject: [PATCH 1/2] Testdox updated for knapsack --- exercises/practice/knapsack/KnapsackTest.php | 83 +++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/exercises/practice/knapsack/KnapsackTest.php b/exercises/practice/knapsack/KnapsackTest.php index 1abe41afd..66797aeed 100644 --- a/exercises/practice/knapsack/KnapsackTest.php +++ b/exercises/practice/knapsack/KnapsackTest.php @@ -20,6 +20,7 @@ protected function setUp(): void /** * uuid: 3993a824-c20e-493d-b3c9-ee8a7753ee59 + * @testdox No Items */ public function testNoItems(): void { @@ -28,96 +29,102 @@ public function testNoItems(): void /** * uuid: 1d39e98c-6249-4a8b-912f-87cb12e506b0 + * @testdox One item, too heavy */ public function testOneItemTooHeavy(): void { - $items = [[ 'weight' => 100, 'value' => 1 ]]; + $items = [['weight' => 100, 'value' => 1]]; $this->assertEquals(0, $this->knapsack->getMaximumValue(10, $items)); } /** * uuid: 833ea310-6323-44f2-9d27-a278740ffbd8 + * @testdox Five items (cannot be greedy by weight) */ public function testFiveItemsCannotBeGreedyByWeight(): void { $items = [ - [ 'weight' => 2, 'value' => 5 ], - [ 'weight' => 2, 'value' => 5 ], - [ 'weight' => 2, 'value' => 5 ], - [ 'weight' => 2, 'value' => 5 ], - [ 'weight' => 10, 'value' => 21 ], + ['weight' => 2, 'value' => 5], + ['weight' => 2, 'value' => 5], + ['weight' => 2, 'value' => 5], + ['weight' => 2, 'value' => 5], + ['weight' => 10, 'value' => 21], ]; $this->assertEquals(21, $this->knapsack->getMaximumValue(10, $items)); } /** * uuid: 277cdc52-f835-4c7d-872b-bff17bab2456 + * @testdox Five items (cannot be greedy by value) */ public function testFiveItemsCannotBeGreedyByValue(): void { $items = [ - [ 'weight' => 2, 'value' => 20 ], - [ 'weight' => 2, 'value' => 20 ], - [ 'weight' => 2, 'value' => 20 ], - [ 'weight' => 2, 'value' => 20 ], - [ 'weight' => 10, 'value' => 50 ], + ['weight' => 2, 'value' => 20], + ['weight' => 2, 'value' => 20], + ['weight' => 2, 'value' => 20], + ['weight' => 2, 'value' => 20], + ['weight' => 10, 'value' => 50], ]; $this->assertEquals(80, $this->knapsack->getMaximumValue(10, $items)); } /** * uuid: 81d8e679-442b-4f7a-8a59-7278083916c9 + * @testdox Example knapsack */ public function testExampleKnapsack(): void { $items = [ - [ 'weight' => 5, 'value' => 10 ], - [ 'weight' => 4, 'value' => 40 ], - [ 'weight' => 6, 'value' => 30 ], - [ 'weight' => 4, 'value' => 50 ], + ['weight' => 5, 'value' => 10], + ['weight' => 4, 'value' => 40], + ['weight' => 6, 'value' => 30], + ['weight' => 4, 'value' => 50], ]; $this->assertEquals(90, $this->knapsack->getMaximumValue(10, $items)); } /** * uuid: f23a2449-d67c-4c26-bf3e-cde020f27ecc + * @testdox 8 items */ public function testEightItems(): void { $items = [ - [ 'weight' => 25, 'value' => 350 ], - [ 'weight' => 35, 'value' => 400 ], - [ 'weight' => 45, 'value' => 450 ], - [ 'weight' => 5, 'value' => 20 ], - [ 'weight' => 25, 'value' => 70 ], - [ 'weight' => 3, 'value' => 8 ], - [ 'weight' => 2, 'value' => 5 ], - [ 'weight' => 2, 'value' => 5 ], + ['weight' => 25, 'value' => 350], + ['weight' => 35, 'value' => 400], + ['weight' => 45, 'value' => 450], + ['weight' => 5, 'value' => 20], + ['weight' => 25, 'value' => 70], + ['weight' => 3, 'value' => 8], + ['weight' => 2, 'value' => 5], + ['weight' => 2, 'value' => 5], ]; $this->assertEquals(900, $this->knapsack->getMaximumValue(104, $items)); } /** * uuid: 7c682ae9-c385-4241-a197-d2fa02c81a11 + * @testdox 15 items */ public function testFifteenItems(): void { $items = [ - [ 'weight' => 70, 'value' => 135 ], - [ 'weight' => 73, 'value' => 139 ], - [ 'weight' => 77, 'value' => 149 ], - [ 'weight' => 80, 'value' => 150 ], - [ 'weight' => 82, 'value' => 156 ], - [ 'weight' => 87, 'value' => 163 ], - [ 'weight' => 90, 'value' => 173 ], - [ 'weight' => 94, 'value' => 184 ], - [ 'weight' => 98, 'value' => 192 ], - [ 'weight' => 106, 'value' => 201 ], - [ 'weight' => 110, 'value' => 210 ], - [ 'weight' => 113, 'value' => 214 ], - [ 'weight' => 115, 'value' => 221 ], - [ 'weight' => 118, 'value' => 229 ], - [ 'weight' => 120, 'value' => 240 ], + ['weight' => 70, 'value' => 135], + ['weight' => 73, 'value' => 139], + ['weight' => 77, 'value' => 149], + ['weight' => 80, 'value' => 150], + ['weight' => 82, 'value' => 156], + ['weight' => 87, 'value' => 163], + ['weight' => 90, 'value' => 173], + ['weight' => 94, 'value' => 184], + ['weight' => 98, 'value' => 192], + ['weight' => 106, 'value' => 201], + ['weight' => 110, 'value' => 210], + ['weight' => 113, 'value' => 214], + ['weight' => 115, 'value' => 221], + ['weight' => 118, 'value' => 229], + ['weight' => 120, 'value' => 240], ]; $this->assertEquals(1458, $this->knapsack->getMaximumValue(750, $items)); } From e9b3532b2614353ecc24cd9b8d6da3ce4395517f Mon Sep 17 00:00:00 2001 From: Fejan Malek Date: Mon, 12 Aug 2024 18:32:37 +0530 Subject: [PATCH 2/2] Testcases updated --- exercises/practice/knapsack/KnapsackTest.php | 57 +++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/exercises/practice/knapsack/KnapsackTest.php b/exercises/practice/knapsack/KnapsackTest.php index 66797aeed..f48872730 100644 --- a/exercises/practice/knapsack/KnapsackTest.php +++ b/exercises/practice/knapsack/KnapsackTest.php @@ -24,7 +24,14 @@ protected function setUp(): void */ public function testNoItems(): void { - $this->assertEquals(0, $this->knapsack->getMaximumValue(100, [])); + $expected = 0; + $maximumWeight = 100; + $items = []; + + $knapsack = new Knapsack(); + $actual = $knapsack->getMaximumValue($maximumWeight, $items); + + $this->assertEquals($expected, $actual); } /** @@ -33,8 +40,14 @@ public function testNoItems(): void */ public function testOneItemTooHeavy(): void { + $expected = 0; + $maximumWeight = 10; $items = [['weight' => 100, 'value' => 1]]; - $this->assertEquals(0, $this->knapsack->getMaximumValue(10, $items)); + + $knapsack = new Knapsack(); + $actual = $knapsack->getMaximumValue($maximumWeight, $items); + + $this->assertEquals($expected, $actual); } /** @@ -43,6 +56,8 @@ public function testOneItemTooHeavy(): void */ public function testFiveItemsCannotBeGreedyByWeight(): void { + $expected = 21; + $maximumWeight = 10; $items = [ ['weight' => 2, 'value' => 5], ['weight' => 2, 'value' => 5], @@ -50,7 +65,11 @@ public function testFiveItemsCannotBeGreedyByWeight(): void ['weight' => 2, 'value' => 5], ['weight' => 10, 'value' => 21], ]; - $this->assertEquals(21, $this->knapsack->getMaximumValue(10, $items)); + + $knapsack = new Knapsack(); + $actual = $knapsack->getMaximumValue($maximumWeight, $items); + + $this->assertEquals($expected, $actual); } /** @@ -59,6 +78,8 @@ public function testFiveItemsCannotBeGreedyByWeight(): void */ public function testFiveItemsCannotBeGreedyByValue(): void { + $expected = 80; + $maximumWeight = 10; $items = [ ['weight' => 2, 'value' => 20], ['weight' => 2, 'value' => 20], @@ -66,7 +87,11 @@ public function testFiveItemsCannotBeGreedyByValue(): void ['weight' => 2, 'value' => 20], ['weight' => 10, 'value' => 50], ]; - $this->assertEquals(80, $this->knapsack->getMaximumValue(10, $items)); + + $knapsack = new Knapsack(); + $actual = $knapsack->getMaximumValue($maximumWeight, $items); + + $this->assertEquals($expected, $actual); } /** @@ -75,13 +100,19 @@ public function testFiveItemsCannotBeGreedyByValue(): void */ public function testExampleKnapsack(): void { + $expected = 90; + $maximumWeight = 10; $items = [ ['weight' => 5, 'value' => 10], ['weight' => 4, 'value' => 40], ['weight' => 6, 'value' => 30], ['weight' => 4, 'value' => 50], ]; - $this->assertEquals(90, $this->knapsack->getMaximumValue(10, $items)); + + $knapsack = new Knapsack(); + $actual = $knapsack->getMaximumValue($maximumWeight, $items); + + $this->assertEquals($expected, $actual); } /** @@ -90,6 +121,8 @@ public function testExampleKnapsack(): void */ public function testEightItems(): void { + $expected = 900; + $maximumWeight = 104; $items = [ ['weight' => 25, 'value' => 350], ['weight' => 35, 'value' => 400], @@ -100,7 +133,11 @@ public function testEightItems(): void ['weight' => 2, 'value' => 5], ['weight' => 2, 'value' => 5], ]; - $this->assertEquals(900, $this->knapsack->getMaximumValue(104, $items)); + + $knapsack = new Knapsack(); + $actual = $knapsack->getMaximumValue($maximumWeight, $items); + + $this->assertEquals($expected, $actual); } /** @@ -109,6 +146,8 @@ public function testEightItems(): void */ public function testFifteenItems(): void { + $expected = 1458; + $maximumWeight = 750; $items = [ ['weight' => 70, 'value' => 135], ['weight' => 73, 'value' => 139], @@ -126,6 +165,10 @@ public function testFifteenItems(): void ['weight' => 118, 'value' => 229], ['weight' => 120, 'value' => 240], ]; - $this->assertEquals(1458, $this->knapsack->getMaximumValue(750, $items)); + + $knapsack = new Knapsack(); + $actual = $knapsack->getMaximumValue($maximumWeight, $items); + + $this->assertEquals($expected, $actual); } }