Repository navigation
Expand file tree
/
Copy pathgoals_order_internal_test.go
More file actions
103 lines (85 loc) · 2.49 KB
/
Copy pathgoals_order_internal_test.go
File metadata and controls
103 lines (85 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package boxpacker3
import (
"testing"
)
const evenLoad = 10.0
func resultWith(boxes int, boxVolume, fillShare float64, unfit int) *Result {
packed := make([]*Container, 0, boxes)
for i := range boxes {
box := openBox(string(rune('a'+i)), boxVolume, 1, 1, 1e9)
box.itemsVolume = boxVolume * fillShare
box.itemsWeight = evenLoad
box.items = []*piece{testPiece("x", 1, 1, 1, evenLoad)}
packed = append(packed, box)
}
left := make([]*piece, unfit)
for i := range left {
left[i] = testPiece("left", 1, 1, 1, 0)
}
return resultOf(packed, left...)
}
func TestGoals_DecideInTheDocumentedOrder(t *testing.T) {
t.Parallel()
cases := []struct {
name string
goal Goal
better *Result
worse *Result
because string
}{
{
"MinimizeBoxes prefers fewer left behind", FewestBoxes,
resultWith(4, 100, 0.5, 0), resultWith(1, 100, 0.5, 3),
"unfit count comes first",
},
{
"MinimizeBoxes then prefers fewer boxes", FewestBoxes,
resultWith(2, 100, 0.5, 0), resultWith(3, 10, 0.5, 0),
"box count comes before volume",
},
{
"MinimizeBoxes then prefers less volume", FewestBoxes,
resultWith(2, 50, 0.5, 0), resultWith(2, 100, 0.5, 0),
"volume breaks a tie on box count",
},
{
"MaximizeItems looks only at what is left behind", MostItems,
resultWith(9, 900, 0.1, 0), resultWith(1, 10, 0.9, 1),
"nothing but the unfit count",
},
{
"TightestPacking prefers less volume before fewer boxes", LeastVolume,
resultWith(3, 30, 0.5, 0), resultWith(2, 100, 0.5, 0),
"volume comes before box count",
},
{
"MaxAverageFillRate prefers the fuller boxes", HighestFill,
resultWith(4, 100, 0.9, 0), resultWith(1, 100, 0.2, 0),
"fill rate, not box count",
},
}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if test.goal.Compare(test.better, test.worse) >= 0 {
t.Errorf("the goal did not prefer the better result: %s", test.because)
}
if test.goal.Compare(test.worse, test.better) < 0 {
t.Errorf("the goal preferred the worse result too: %s", test.because)
}
})
}
}
func TestGoals_BalancedPackingRanksOnTheSpread(t *testing.T) {
t.Parallel()
even := resultWith(2, 100, 0.5, 0)
uneven := resultWith(2, 100, 0.5, 0)
uneven.Boxes[0].Stats.ItemsWeight = 1
uneven.Boxes[1].Stats.ItemsWeight = 100
if BalancedWeight.Compare(even, uneven) >= 0 {
t.Error("the even load was not preferred")
}
if BalancedWeight.Compare(uneven, even) < 0 {
t.Error("the uneven load was preferred as well")
}
}