-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpacker_property_test.go
More file actions
350 lines (305 loc) · 9.95 KB
/
Copy pathpacker_property_test.go
File metadata and controls
350 lines (305 loc) · 9.95 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package boxpacker3_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/bavix/boxpacker3"
)
// strategyName returns a string representation of the packing strategy.
func strategyName(strategy boxpacker3.PackingStrategy) string {
switch strategy {
case boxpacker3.StrategyMinimizeBoxes:
return "MinimizeBoxes"
case boxpacker3.StrategyGreedy:
return "Greedy"
case boxpacker3.StrategyBestFit:
return "BestFit"
case boxpacker3.StrategyBestFitDecreasing:
return "BestFitDecreasing"
case boxpacker3.StrategyNextFit:
return "NextFit"
case boxpacker3.StrategyWorstFit:
return "WorstFit"
case boxpacker3.StrategyAlmostWorstFit:
return "AlmostWorstFit"
default:
return fmt.Sprintf("Unknown(%d)", int(strategy))
}
}
// TestPacker_Property_AllItemsAccountedFor is a property-based test that verifies
// all items are either packed or marked as unfit.
//
//nolint:funlen
func TestPacker_Property_AllItemsAccountedFor(t *testing.T) {
t.Parallel()
// Test with different strategies
strategies := []boxpacker3.PackingStrategy{
boxpacker3.StrategyMinimizeBoxes,
boxpacker3.StrategyGreedy,
boxpacker3.StrategyBestFit,
boxpacker3.StrategyBestFitDecreasing,
boxpacker3.StrategyNextFit,
boxpacker3.StrategyWorstFit,
boxpacker3.StrategyAlmostWorstFit,
}
// Test with different scenarios
testCases := []struct {
name string
boxes []*boxpacker3.Box
items []*boxpacker3.Item
}{
{
name: "SingleBox",
boxes: []*boxpacker3.Box{
boxpacker3.NewBox("box-1", 100, 100, 100, 2000),
},
items: []*boxpacker3.Item{
boxpacker3.NewItem("item-1", 30, 30, 30, 100),
boxpacker3.NewItem("item-2", 30, 30, 30, 100),
boxpacker3.NewItem("item-3", 30, 30, 30, 100),
},
},
{
name: "MultipleBoxes",
boxes: []*boxpacker3.Box{
boxpacker3.NewBox("box-1", 100, 100, 100, 2000),
boxpacker3.NewBox("box-2", 100, 100, 100, 2000),
boxpacker3.NewBox("box-3", 100, 100, 100, 2000),
},
items: []*boxpacker3.Item{
boxpacker3.NewItem("item-1", 40, 40, 40, 200),
boxpacker3.NewItem("item-2", 40, 40, 40, 200),
boxpacker3.NewItem("item-3", 40, 40, 40, 200),
boxpacker3.NewItem("item-4", 40, 40, 40, 200),
boxpacker3.NewItem("item-5", 40, 40, 40, 200),
},
},
{
name: "MixedSizes",
boxes: []*boxpacker3.Box{
boxpacker3.NewBox("small", 50, 50, 50, 1000),
boxpacker3.NewBox("medium", 100, 100, 100, 2000),
boxpacker3.NewBox("large", 200, 200, 200, 5000),
},
items: []*boxpacker3.Item{
boxpacker3.NewItem("tiny", 10, 10, 10, 50),
boxpacker3.NewItem("small", 30, 30, 30, 200),
boxpacker3.NewItem("medium", 60, 60, 60, 500),
boxpacker3.NewItem("large", 80, 80, 80, 800),
},
},
{
name: "UnfitItems",
boxes: []*boxpacker3.Box{
boxpacker3.NewBox("small", 50, 50, 50, 1000),
},
items: []*boxpacker3.Item{
boxpacker3.NewItem("fit", 30, 30, 30, 200),
boxpacker3.NewItem("unfit", 200, 200, 200, 5000), // Too large
},
},
}
for _, strategy := range strategies {
for _, tc := range testCases {
strategyName := strategyName(strategy)
t.Run(strategyName+"_"+tc.name, func(t *testing.T) {
t.Parallel()
packer := boxpacker3.NewPacker(boxpacker3.WithStrategy(strategy))
result, err := packer.PackCtx(context.Background(), tc.boxes, tc.items)
require.NoError(t, err)
require.NotNil(t, result)
// Property: All items must be accounted for
totalPacked := 0
for _, box := range result.Boxes {
totalPacked += len(box.GetItems())
}
require.Equal(t, len(tc.items), totalPacked+len(result.UnfitItems),
"All items must be either packed or in UnfitItems")
})
}
}
}
// TestPacker_Property_NoDuplicates is a property-based test that verifies
// no item is packed multiple times.
func TestPacker_Property_NoDuplicates(t *testing.T) {
t.Parallel()
strategies := []boxpacker3.PackingStrategy{
boxpacker3.StrategyMinimizeBoxes,
boxpacker3.StrategyGreedy,
boxpacker3.StrategyBestFit,
boxpacker3.StrategyBestFitDecreasing,
boxpacker3.StrategyNextFit,
boxpacker3.StrategyWorstFit,
boxpacker3.StrategyAlmostWorstFit,
}
boxes := []*boxpacker3.Box{
boxpacker3.NewBox("box-1", 100, 100, 100, 2000),
boxpacker3.NewBox("box-2", 100, 100, 100, 2000),
}
items := []*boxpacker3.Item{
boxpacker3.NewItem("item-1", 30, 30, 30, 100),
boxpacker3.NewItem("item-2", 30, 30, 30, 100),
boxpacker3.NewItem("item-3", 30, 30, 30, 100),
boxpacker3.NewItem("item-4", 30, 30, 30, 100),
boxpacker3.NewItem("item-5", 30, 30, 30, 100),
}
for _, strategy := range strategies {
strategyName := strategyName(strategy)
t.Run(strategyName, func(t *testing.T) {
t.Parallel()
packer := boxpacker3.NewPacker(boxpacker3.WithStrategy(strategy))
result, err := packer.PackCtx(context.Background(), boxes, items)
require.NoError(t, err)
require.NotNil(t, result)
// Property: No item should appear multiple times
itemCounts := make(map[string]int)
// Count items in boxes
for _, box := range result.Boxes {
for _, item := range box.GetItems() {
itemCounts[item.GetID()]++
}
}
// Count items in UnfitItems
for _, item := range result.UnfitItems {
itemCounts[item.GetID()]++
}
// Each item should appear exactly once
for _, item := range items {
require.Equal(t, 1, itemCounts[item.GetID()],
"Item %s should appear exactly once", item.GetID())
}
})
}
}
// TestPacker_Property_NoIntersections is a property-based test that verifies
// no items intersect within the same box.
func TestPacker_Property_NoIntersections(t *testing.T) {
t.Parallel()
strategies := []boxpacker3.PackingStrategy{
boxpacker3.StrategyMinimizeBoxes,
boxpacker3.StrategyGreedy,
boxpacker3.StrategyBestFit,
boxpacker3.StrategyBestFitDecreasing,
boxpacker3.StrategyNextFit,
boxpacker3.StrategyWorstFit,
boxpacker3.StrategyAlmostWorstFit,
}
boxes := []*boxpacker3.Box{
boxpacker3.NewBox("box-1", 100, 100, 100, 2000),
boxpacker3.NewBox("box-2", 100, 100, 100, 2000),
}
items := []*boxpacker3.Item{
boxpacker3.NewItem("item-1", 30, 30, 30, 100),
boxpacker3.NewItem("item-2", 30, 30, 30, 100),
boxpacker3.NewItem("item-3", 30, 30, 30, 100),
boxpacker3.NewItem("item-4", 30, 30, 30, 100),
}
for _, strategy := range strategies {
strategyName := strategyName(strategy)
t.Run(strategyName, func(t *testing.T) {
t.Parallel()
packer := boxpacker3.NewPacker(boxpacker3.WithStrategy(strategy))
result, err := packer.PackCtx(context.Background(), boxes, items)
require.NoError(t, err)
require.NotNil(t, result)
// Property: No items should intersect within the same box
for _, box := range result.Boxes {
boxItems := box.GetItems()
for i := range boxItems {
for j := i + 1; j < len(boxItems); j++ {
require.False(t, boxItems[i].Intersect(boxItems[j]),
"Items %s and %s in box %s should not intersect",
boxItems[i].GetID(), boxItems[j].GetID(), box.GetID())
}
}
}
})
}
}
// TestPacker_Property_WeightConstraints is a property-based test that verifies
// weight constraints are respected.
func TestPacker_Property_WeightConstraints(t *testing.T) {
t.Parallel()
strategies := []boxpacker3.PackingStrategy{
boxpacker3.StrategyMinimizeBoxes,
boxpacker3.StrategyGreedy,
boxpacker3.StrategyBestFit,
boxpacker3.StrategyBestFitDecreasing,
boxpacker3.StrategyNextFit,
boxpacker3.StrategyWorstFit,
boxpacker3.StrategyAlmostWorstFit,
}
boxes := []*boxpacker3.Box{
boxpacker3.NewBox("light", 100, 100, 100, 500),
boxpacker3.NewBox("heavy", 100, 100, 100, 2000),
}
items := []*boxpacker3.Item{
boxpacker3.NewItem("item-1", 30, 30, 30, 200),
boxpacker3.NewItem("item-2", 30, 30, 30, 200),
boxpacker3.NewItem("item-3", 30, 30, 30, 200),
}
for _, strategy := range strategies {
strategyName := strategyName(strategy)
t.Run(strategyName, func(t *testing.T) {
t.Parallel()
packer := boxpacker3.NewPacker(boxpacker3.WithStrategy(strategy))
result, err := packer.PackCtx(context.Background(), boxes, items)
require.NoError(t, err)
require.NotNil(t, result)
// Property: Weight constraints must be respected
for _, box := range result.Boxes {
totalWeight := 0.0
for _, item := range box.GetItems() {
totalWeight += item.GetWeight()
}
require.LessOrEqual(t, totalWeight, box.GetMaxWeight(),
"Box %s should respect weight constraint", box.GetID())
}
})
}
}
// validatePackingInvariants verifies that packing invariants are maintained:
// - No items intersect within the same box
// - Weight constraints are respected
// - Volume constraints are respected
// - Items are within box boundaries.
func validatePackingInvariants(t *testing.T, result *boxpacker3.Result) {
t.Helper()
for _, box := range result.Boxes {
boxItems := box.GetItems()
// Check for intersections
for i := range boxItems {
for j := i + 1; j < len(boxItems); j++ {
require.False(t, boxItems[i].Intersect(boxItems[j]),
"Items %s and %s in box %s should not intersect",
boxItems[i].GetID(), boxItems[j].GetID(), box.GetID())
}
}
// Check weight constraint
totalWeight := 0.0
for _, item := range boxItems {
totalWeight += item.GetWeight()
}
require.LessOrEqual(t, totalWeight, box.GetMaxWeight(),
"Box %s should respect weight constraint", box.GetID())
// Check volume constraint
totalVolume := 0.0
for _, item := range boxItems {
totalVolume += item.GetVolume()
}
require.LessOrEqual(t, totalVolume, box.GetVolume(),
"Box %s should respect volume constraint", box.GetID())
// Check geometric constraints (items within box boundaries)
for _, item := range boxItems {
dim := item.GetDimension()
pos := item.GetPosition()
require.LessOrEqual(t, pos[0]+dim[0], box.GetWidth(),
"Item %s width should be <= box width", item.GetID())
require.LessOrEqual(t, pos[1]+dim[1], box.GetHeight(),
"Item %s height should be <= box height", item.GetID())
require.LessOrEqual(t, pos[2]+dim[2], box.GetDepth(),
"Item %s depth should be <= box depth", item.GetID())
}
}
}