|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Box packing (3D bin packing, knapsack problem). |
|
4
|
|
|
* |
|
5
|
|
|
* @author Doug Wright |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace DVDoug\BoxPacker; |
|
9
|
|
|
|
|
10
|
|
|
use DVDoug\BoxPacker\Test\TestItem; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @covers \DVDoug\BoxPacker\ItemList |
|
15
|
|
|
*/ |
|
16
|
|
|
class ItemListTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Test that sorting of items with different dimensions works as expected i.e. |
|
20
|
|
|
* - Largest (by volume) first |
|
21
|
|
|
* - If identical volume, sort by weight. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testDimensionalSorting() |
|
24
|
|
|
{ |
|
25
|
|
|
$item1 = new TestItem('Small', 20, 20, 2, 100); |
|
26
|
|
|
$item2 = new TestItem('Large', 200, 200, 20, 1000); |
|
27
|
|
|
$item3 = new TestItem('Medium', 100, 100, 10, 500); |
|
28
|
|
|
$item4 = new TestItem('Medium Heavy', 100, 100, 10, 501); |
|
29
|
|
|
|
|
30
|
|
|
$list = new ItemList(); |
|
31
|
|
|
$list->insert($item1); |
|
32
|
|
|
$list->insert($item2); |
|
33
|
|
|
$list->insert($item3); |
|
34
|
|
|
$list->insert($item4); |
|
35
|
|
|
|
|
36
|
|
|
$sorted = iterator_to_array($list, false); |
|
37
|
|
|
self::assertEquals([$item2, $item4, $item3, $item1], $sorted); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Test that sorting of items with identical dimensions works as expected i.e. |
|
42
|
|
|
* - Items with the same name (i.e. same type) are kept together. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testKeepingItemsOfSameTypeTogether() |
|
45
|
|
|
{ |
|
46
|
|
|
$item1 = new TestItem('Item A', 20, 20, 2, 100); |
|
47
|
|
|
$item2 = new TestItem('Item B', 20, 20, 2, 100); |
|
48
|
|
|
$item3 = new TestItem('Item A', 20, 20, 2, 100); |
|
49
|
|
|
$item4 = new TestItem('Item B', 20, 20, 2, 100); |
|
50
|
|
|
|
|
51
|
|
|
$list = new ItemList(); |
|
52
|
|
|
$list->insert($item1); |
|
53
|
|
|
$list->insert($item2); |
|
54
|
|
|
$list->insert($item3); |
|
55
|
|
|
$list->insert($item4); |
|
56
|
|
|
|
|
57
|
|
|
$sorted = iterator_to_array($list, false); |
|
58
|
|
|
self::assertEquals([$item1, $item3, $item2, $item4], $sorted); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Test that we can retrieve an accurate count of items in the list. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testCount() |
|
65
|
|
|
{ |
|
66
|
|
|
$itemList = new ItemList(); |
|
67
|
|
|
self::assertEquals(0, count($itemList)); |
|
68
|
|
|
|
|
69
|
|
|
$item1 = new TestItem('Item A', 20, 20, 2, 100); |
|
70
|
|
|
$itemList->insert($item1); |
|
71
|
|
|
self::assertEquals(1, count($itemList)); |
|
72
|
|
|
|
|
73
|
|
|
$item2 = new TestItem('Item B', 20, 20, 2, 100); |
|
74
|
|
|
$itemList->insert($item2); |
|
75
|
|
|
self::assertEquals(2, count($itemList)); |
|
76
|
|
|
|
|
77
|
|
|
$item3 = new TestItem('Item C', 20, 20, 2, 100); |
|
78
|
|
|
$itemList->insert($item3); |
|
79
|
|
|
self::assertEquals(3, count($itemList)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Test we can peek at the "top" (next) item in the list. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testTop() |
|
86
|
|
|
{ |
|
87
|
|
|
$itemList = new ItemList(); |
|
88
|
|
|
$item1 = new TestItem('Item A', 20, 20, 2, 100); |
|
89
|
|
|
$itemList->insert($item1); |
|
90
|
|
|
|
|
91
|
|
|
self::assertEquals($item1, $itemList->top()); |
|
92
|
|
|
self::assertEquals(1, count($itemList)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Test we can retrieve the "top" (next) item in the list. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testExtract() |
|
99
|
|
|
{ |
|
100
|
|
|
$itemList = new ItemList(); |
|
101
|
|
|
$item1 = new TestItem('Item A', 20, 20, 2, 100); |
|
102
|
|
|
$itemList->insert($item1); |
|
103
|
|
|
|
|
104
|
|
|
self::assertEquals($item1, $itemList->extract()); |
|
105
|
|
|
self::assertEquals(0, count($itemList)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|