Skip to content

Commit

Permalink
Sync Knapsack (exercism#787)
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
fejan-malek authored Aug 12, 2024
1 parent 2f5a386 commit 455e73f
Showing 1 changed file with 95 additions and 45 deletions.
140 changes: 95 additions & 45 deletions exercises/practice/knapsack/KnapsackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,105 +20,155 @@ protected function setUp(): void

/**
* uuid: 3993a824-c20e-493d-b3c9-ee8a7753ee59
* @testdox No Items
*/
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);
}

/**
* uuid: 1d39e98c-6249-4a8b-912f-87cb12e506b0
* @testdox One item, too heavy
*/
public function testOneItemTooHeavy(): void
{
$items = [[ 'weight' => 100, 'value' => 1 ]];
$this->assertEquals(0, $this->knapsack->getMaximumValue(10, $items));
$expected = 0;
$maximumWeight = 10;
$items = [['weight' => 100, 'value' => 1]];

$knapsack = new Knapsack();
$actual = $knapsack->getMaximumValue($maximumWeight, $items);

$this->assertEquals($expected, $actual);
}

/**
* uuid: 833ea310-6323-44f2-9d27-a278740ffbd8
* @testdox Five items (cannot be greedy by weight)
*/
public function testFiveItemsCannotBeGreedyByWeight(): void
{
$expected = 21;
$maximumWeight = 10;
$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));

$knapsack = new Knapsack();
$actual = $knapsack->getMaximumValue($maximumWeight, $items);

$this->assertEquals($expected, $actual);
}

/**
* uuid: 277cdc52-f835-4c7d-872b-bff17bab2456
* @testdox Five items (cannot be greedy by value)
*/
public function testFiveItemsCannotBeGreedyByValue(): void
{
$expected = 80;
$maximumWeight = 10;
$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));

$knapsack = new Knapsack();
$actual = $knapsack->getMaximumValue($maximumWeight, $items);

$this->assertEquals($expected, $actual);
}

/**
* uuid: 81d8e679-442b-4f7a-8a59-7278083916c9
* @testdox Example knapsack
*/
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 ],
['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);
}

/**
* uuid: f23a2449-d67c-4c26-bf3e-cde020f27ecc
* @testdox 8 items
*/
public function testEightItems(): void
{
$expected = 900;
$maximumWeight = 104;
$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));

$knapsack = new Knapsack();
$actual = $knapsack->getMaximumValue($maximumWeight, $items);

$this->assertEquals($expected, $actual);
}

/**
* uuid: 7c682ae9-c385-4241-a197-d2fa02c81a11
* @testdox 15 items
*/
public function testFifteenItems(): void
{
$expected = 1458;
$maximumWeight = 750;
$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));

$knapsack = new Knapsack();
$actual = $knapsack->getMaximumValue($maximumWeight, $items);

$this->assertEquals($expected, $actual);
}
}

0 comments on commit 455e73f

Please sign in to comment.