Skip to content

Commit 455e73f

Browse files
authored
Sync Knapsack (exercism#787)
[no important files changed]
1 parent 2f5a386 commit 455e73f

File tree

1 file changed

+95
-45
lines changed

1 file changed

+95
-45
lines changed

exercises/practice/knapsack/KnapsackTest.php

Lines changed: 95 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,105 +20,155 @@ protected function setUp(): void
2020

2121
/**
2222
* uuid: 3993a824-c20e-493d-b3c9-ee8a7753ee59
23+
* @testdox No Items
2324
*/
2425
public function testNoItems(): void
2526
{
26-
$this->assertEquals(0, $this->knapsack->getMaximumValue(100, []));
27+
$expected = 0;
28+
$maximumWeight = 100;
29+
$items = [];
30+
31+
$knapsack = new Knapsack();
32+
$actual = $knapsack->getMaximumValue($maximumWeight, $items);
33+
34+
$this->assertEquals($expected, $actual);
2735
}
2836

2937
/**
3038
* uuid: 1d39e98c-6249-4a8b-912f-87cb12e506b0
39+
* @testdox One item, too heavy
3140
*/
3241
public function testOneItemTooHeavy(): void
3342
{
34-
$items = [[ 'weight' => 100, 'value' => 1 ]];
35-
$this->assertEquals(0, $this->knapsack->getMaximumValue(10, $items));
43+
$expected = 0;
44+
$maximumWeight = 10;
45+
$items = [['weight' => 100, 'value' => 1]];
46+
47+
$knapsack = new Knapsack();
48+
$actual = $knapsack->getMaximumValue($maximumWeight, $items);
49+
50+
$this->assertEquals($expected, $actual);
3651
}
3752

3853
/**
3954
* uuid: 833ea310-6323-44f2-9d27-a278740ffbd8
55+
* @testdox Five items (cannot be greedy by weight)
4056
*/
4157
public function testFiveItemsCannotBeGreedyByWeight(): void
4258
{
59+
$expected = 21;
60+
$maximumWeight = 10;
4361
$items = [
44-
[ 'weight' => 2, 'value' => 5 ],
45-
[ 'weight' => 2, 'value' => 5 ],
46-
[ 'weight' => 2, 'value' => 5 ],
47-
[ 'weight' => 2, 'value' => 5 ],
48-
[ 'weight' => 10, 'value' => 21 ],
62+
['weight' => 2, 'value' => 5],
63+
['weight' => 2, 'value' => 5],
64+
['weight' => 2, 'value' => 5],
65+
['weight' => 2, 'value' => 5],
66+
['weight' => 10, 'value' => 21],
4967
];
50-
$this->assertEquals(21, $this->knapsack->getMaximumValue(10, $items));
68+
69+
$knapsack = new Knapsack();
70+
$actual = $knapsack->getMaximumValue($maximumWeight, $items);
71+
72+
$this->assertEquals($expected, $actual);
5173
}
5274

5375
/**
5476
* uuid: 277cdc52-f835-4c7d-872b-bff17bab2456
77+
* @testdox Five items (cannot be greedy by value)
5578
*/
5679
public function testFiveItemsCannotBeGreedyByValue(): void
5780
{
81+
$expected = 80;
82+
$maximumWeight = 10;
5883
$items = [
59-
[ 'weight' => 2, 'value' => 20 ],
60-
[ 'weight' => 2, 'value' => 20 ],
61-
[ 'weight' => 2, 'value' => 20 ],
62-
[ 'weight' => 2, 'value' => 20 ],
63-
[ 'weight' => 10, 'value' => 50 ],
84+
['weight' => 2, 'value' => 20],
85+
['weight' => 2, 'value' => 20],
86+
['weight' => 2, 'value' => 20],
87+
['weight' => 2, 'value' => 20],
88+
['weight' => 10, 'value' => 50],
6489
];
65-
$this->assertEquals(80, $this->knapsack->getMaximumValue(10, $items));
90+
91+
$knapsack = new Knapsack();
92+
$actual = $knapsack->getMaximumValue($maximumWeight, $items);
93+
94+
$this->assertEquals($expected, $actual);
6695
}
6796

6897
/**
6998
* uuid: 81d8e679-442b-4f7a-8a59-7278083916c9
99+
* @testdox Example knapsack
70100
*/
71101
public function testExampleKnapsack(): void
72102
{
103+
$expected = 90;
104+
$maximumWeight = 10;
73105
$items = [
74-
[ 'weight' => 5, 'value' => 10 ],
75-
[ 'weight' => 4, 'value' => 40 ],
76-
[ 'weight' => 6, 'value' => 30 ],
77-
[ 'weight' => 4, 'value' => 50 ],
106+
['weight' => 5, 'value' => 10],
107+
['weight' => 4, 'value' => 40],
108+
['weight' => 6, 'value' => 30],
109+
['weight' => 4, 'value' => 50],
78110
];
79-
$this->assertEquals(90, $this->knapsack->getMaximumValue(10, $items));
111+
112+
$knapsack = new Knapsack();
113+
$actual = $knapsack->getMaximumValue($maximumWeight, $items);
114+
115+
$this->assertEquals($expected, $actual);
80116
}
81117

82118
/**
83119
* uuid: f23a2449-d67c-4c26-bf3e-cde020f27ecc
120+
* @testdox 8 items
84121
*/
85122
public function testEightItems(): void
86123
{
124+
$expected = 900;
125+
$maximumWeight = 104;
87126
$items = [
88-
[ 'weight' => 25, 'value' => 350 ],
89-
[ 'weight' => 35, 'value' => 400 ],
90-
[ 'weight' => 45, 'value' => 450 ],
91-
[ 'weight' => 5, 'value' => 20 ],
92-
[ 'weight' => 25, 'value' => 70 ],
93-
[ 'weight' => 3, 'value' => 8 ],
94-
[ 'weight' => 2, 'value' => 5 ],
95-
[ 'weight' => 2, 'value' => 5 ],
127+
['weight' => 25, 'value' => 350],
128+
['weight' => 35, 'value' => 400],
129+
['weight' => 45, 'value' => 450],
130+
['weight' => 5, 'value' => 20],
131+
['weight' => 25, 'value' => 70],
132+
['weight' => 3, 'value' => 8],
133+
['weight' => 2, 'value' => 5],
134+
['weight' => 2, 'value' => 5],
96135
];
97-
$this->assertEquals(900, $this->knapsack->getMaximumValue(104, $items));
136+
137+
$knapsack = new Knapsack();
138+
$actual = $knapsack->getMaximumValue($maximumWeight, $items);
139+
140+
$this->assertEquals($expected, $actual);
98141
}
99142

100143
/**
101144
* uuid: 7c682ae9-c385-4241-a197-d2fa02c81a11
145+
* @testdox 15 items
102146
*/
103147
public function testFifteenItems(): void
104148
{
149+
$expected = 1458;
150+
$maximumWeight = 750;
105151
$items = [
106-
[ 'weight' => 70, 'value' => 135 ],
107-
[ 'weight' => 73, 'value' => 139 ],
108-
[ 'weight' => 77, 'value' => 149 ],
109-
[ 'weight' => 80, 'value' => 150 ],
110-
[ 'weight' => 82, 'value' => 156 ],
111-
[ 'weight' => 87, 'value' => 163 ],
112-
[ 'weight' => 90, 'value' => 173 ],
113-
[ 'weight' => 94, 'value' => 184 ],
114-
[ 'weight' => 98, 'value' => 192 ],
115-
[ 'weight' => 106, 'value' => 201 ],
116-
[ 'weight' => 110, 'value' => 210 ],
117-
[ 'weight' => 113, 'value' => 214 ],
118-
[ 'weight' => 115, 'value' => 221 ],
119-
[ 'weight' => 118, 'value' => 229 ],
120-
[ 'weight' => 120, 'value' => 240 ],
152+
['weight' => 70, 'value' => 135],
153+
['weight' => 73, 'value' => 139],
154+
['weight' => 77, 'value' => 149],
155+
['weight' => 80, 'value' => 150],
156+
['weight' => 82, 'value' => 156],
157+
['weight' => 87, 'value' => 163],
158+
['weight' => 90, 'value' => 173],
159+
['weight' => 94, 'value' => 184],
160+
['weight' => 98, 'value' => 192],
161+
['weight' => 106, 'value' => 201],
162+
['weight' => 110, 'value' => 210],
163+
['weight' => 113, 'value' => 214],
164+
['weight' => 115, 'value' => 221],
165+
['weight' => 118, 'value' => 229],
166+
['weight' => 120, 'value' => 240],
121167
];
122-
$this->assertEquals(1458, $this->knapsack->getMaximumValue(750, $items));
168+
169+
$knapsack = new Knapsack();
170+
$actual = $knapsack->getMaximumValue($maximumWeight, $items);
171+
172+
$this->assertEquals($expected, $actual);
123173
}
124174
}

0 commit comments

Comments
 (0)