Skip to content

Commit

Permalink
Fix issue #30
Browse files Browse the repository at this point in the history
Thanks to @IBBoard for the tests and the report
  • Loading branch information
dvdoug committed Oct 10, 2015
1 parent 12c727c commit c263b69
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
10 changes: 9 additions & 1 deletion BoxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ class BoxList extends \SplMinHeap {
* @see \SplMinHeap::compare()
*/
public function compare($aBoxA, $aBoxB) {
return $aBoxB->getInnerVolume() - $aBoxA->getInnerVolume();
if ($aBoxB->getInnerVolume() > $aBoxA->getInnerVolume()) {
return 1;
}
else if ($aBoxB->getInnerVolume() < $aBoxA->getInnerVolume()) {
return -1;
}
else {
return 0;
}
}

}
10 changes: 9 additions & 1 deletion ItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ class ItemList extends \SplMaxHeap {
* @see \SplMaxHeap::compare()
*/
public function compare($aItemA, $aItemB) {
return $aItemA->getVolume() - $aItemB->getVolume();
if ($aItemA->getVolume() > $aItemB->getVolume()) {
return 1;
}
else if ($aItemA->getVolume() < $aItemB->getVolume()) {
return -1;
}
else {
return 0;
}
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/BoxListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,43 @@ function testCompare() {
}
self::assertEquals(array($box1,$box3,$box2), $sorted);
}

function testIssue14A() {
$box1 = new TestBox('Small', 21, 21, 3, 1, 20, 20, 2, 100);
$box2 = new TestBox('Large', 1301, 1301, 1301, 1, 1300, 1300, 1300, 1000);
$box3 = new TestBox('Medium', 101, 101, 11, 5, 100, 100, 10, 500);
$list = new BoxList;
$list->insert($box1);
$list->insert($box2);
$list->insert($box3);
$sorted = [];
while (!$list->isEmpty()) {
$sorted[] = $list->extract();
}
self::assertEquals(array($box1,$box3,$box2), $sorted);
}

function testIssue14B() {
$box1 = new TestBox('Small', 21, 21, 3, 1, 20, 20, 2, 100);
$box2 = new TestBox('Large', 1301, 1301, 1301, 1, 1300, 1300, 1300, 1000);
$box3 = new TestBox('Medium', 101, 101, 11, 5, 100, 100, 10, 500);
$list = new BoxList;
$list->insert($box3);
$list->insert($box2);
$list->insert($box1);
$sorted = [];
while (!$list->isEmpty()) {
$sorted[] = $list->extract();
}
self::assertEquals(array($box1,$box3,$box2), $sorted);
$list = new BoxList;
$list->insert($box2);
$list->insert($box1);
$list->insert($box3);
$sorted = [];
while (!$list->isEmpty()) {
$sorted[] = $list->extract();
}
self::assertEquals(array($box1,$box3,$box2), $sorted);
}
}

0 comments on commit c263b69

Please sign in to comment.