Skip to content

Commit a52ccdc

Browse files
committed
🧪 add test cases for binpack
1 parent b91f1f9 commit a52ccdc

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

src/binpack.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Size, Item, firstFitDecreasing } from "./binpack";
2+
3+
describe("firstFitDecreasing", () => {
4+
it("should pack items into bins correctly", () => {
5+
const capacity = new Size([10]);
6+
const items: Item<string>[] = [
7+
{ obj: "item1", size: new Size([4]) },
8+
{ obj: "item2", size: new Size([3]) },
9+
{ obj: "item3", size: new Size([2]) },
10+
{ obj: "item4", size: new Size([1]) },
11+
];
12+
13+
const bins = firstFitDecreasing(capacity, items);
14+
15+
expect(bins.length).toBe(1);
16+
expect(bins[0].items.length).toBe(4);
17+
});
18+
19+
it("should create new bin if item does not fit in existing bins", () => {
20+
const capacity = new Size([5]);
21+
const items: Item<string>[] = [
22+
{ obj: "item1", size: new Size([4]) },
23+
{ obj: "item2", size: new Size([3]) },
24+
{ obj: "item3", size: new Size([2]) },
25+
{ obj: "item4", size: new Size([1]) },
26+
];
27+
28+
const bins = firstFitDecreasing(capacity, items);
29+
30+
expect(bins.length).toBe(2);
31+
expect(bins[0].items.length).toBe(2);
32+
expect(bins[1].items.length).toBe(2);
33+
});
34+
35+
it("should handle empty items list", () => {
36+
const capacity = new Size([10]);
37+
const items: Item<string>[] = [];
38+
39+
const bins = firstFitDecreasing(capacity, items);
40+
41+
expect(bins.length).toBe(0);
42+
});
43+
44+
it("should handle items larger than capacity", () => {
45+
const capacity = new Size([5]);
46+
const items: Item<string>[] = [{ obj: "item1", size: new Size([6]) }];
47+
48+
const bins = firstFitDecreasing(capacity, items);
49+
50+
expect(bins.length).toBe(0);
51+
});
52+
53+
it("should pack items with multiple dimensions correctly", () => {
54+
const capacity = new Size([10, 10]);
55+
const items: Item<string>[] = [
56+
{ obj: "item1", size: new Size([4, 4]) },
57+
{ obj: "item2", size: new Size([3, 3]) },
58+
{ obj: "item3", size: new Size([2, 2]) },
59+
{ obj: "item4", size: new Size([1, 1]) },
60+
];
61+
62+
const bins = firstFitDecreasing(capacity, items);
63+
64+
expect(bins.length).toBe(1);
65+
expect(bins[0].items.length).toBe(4);
66+
});
67+
});

0 commit comments

Comments
 (0)