rectpackbin 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ const { Rectangle } = require('../../src/rectpack/geometry');
2
+ const { GuillotineBssfSas } = require('../../src/rectpack/guillotine');
3
+
4
+ describe('GuillotineBssfSas', () => {
5
+ let packer;
6
+
7
+ beforeEach(() => {
8
+ packer = new GuillotineBssfSas(100, 100);
9
+ });
10
+
11
+ describe('constructor', () => {
12
+ it('should create packer with default values', () => {
13
+ expect(packer.width).toBe(100);
14
+ expect(packer.height).toBe(100);
15
+ expect(packer.rot).toBe(true);
16
+ expect(packer._merge).toBe(true);
17
+ expect(packer._sections).toHaveLength(1);
18
+ expect(packer._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
19
+ });
20
+
21
+ it('should create packer with custom values', () => {
22
+ const custom = new GuillotineBssfSas(200, 150, false, false, 'test-id');
23
+ expect(custom.width).toBe(200);
24
+ expect(custom.height).toBe(150);
25
+ expect(custom.rot).toBe(false);
26
+ expect(custom._merge).toBe(false);
27
+ expect(custom.bid).toBe('test-id');
28
+ });
29
+ });
30
+
31
+ describe('_section_fitness (BSSF)', () => {
32
+ it('should return null for sections too small', () => {
33
+ const section = new Rectangle(0, 0, 30, 30);
34
+ expect(packer._section_fitness(section, 40, 20)).toBeNull();
35
+ });
36
+
37
+ it('should return minimum of remaining sides', () => {
38
+ const section = new Rectangle(0, 0, 100, 100);
39
+ // For a 60x40 rectangle in 100x100 section:
40
+ // Remaining sides are: 100-60=40 and 100-40=60
41
+ // BSSF should return the minimum = 40
42
+ expect(packer._section_fitness(section, 60, 40)).toBe(40);
43
+ });
44
+ });
45
+
46
+ describe('_split (SAS)', () => {
47
+ it('should split horizontally when width < height', () => {
48
+ const section = new Rectangle(0, 0, 50, 100);
49
+ packer._sections = [];
50
+ packer._split(section, 30, 40);
51
+
52
+ // Should split horizontally because width (50) < height (100)
53
+ expect(packer._sections).toHaveLength(2);
54
+ expect(packer._sections).toContainEqual(new Rectangle(0, 40, 50, 60,0)); // Top section
55
+ expect(packer._sections).toContainEqual(new Rectangle(30, 0, 20, 40,0)); // Right section
56
+ });
57
+
58
+ it('should split vertically when width >= height', () => {
59
+ const section = new Rectangle(0, 0, 100, 50);
60
+ packer._sections = [];
61
+ packer._split(section, 30, 40);
62
+
63
+ // Should split vertically because width (100) >= height (50)
64
+ expect(packer._sections).toHaveLength(2);
65
+ expect(packer._sections).toContainEqual(new Rectangle(0, 40, 30, 10,0)); // Top section
66
+ expect(packer._sections).toContainEqual(new Rectangle(30, 0, 70, 50,0)); // Right section
67
+ });
68
+ });
69
+
70
+ // describe('addRect', () => {
71
+ // it('should place rectangle using BSSF and SAS', () => {
72
+ // const rect = packer.addRect(60, 40, 'test');
73
+ // expect(rect).toBeTruthy();
74
+ // expect(rect.width).toBe(60);
75
+ // expect(rect.height).toBe(40);
76
+ // expect(rect.x).toBe(0);
77
+ // expect(rect.y).toBe(0);
78
+
79
+ // // Verify sections were split according to SAS rule
80
+ // expect(packer._sections).toHaveLength(2);
81
+ // // Since width (100) >= height (100), it should split vertically
82
+ // expect(packer._sections[0]).toContainEqual(new Rectangle(0, 40, 60, 60,0)); // Top section
83
+ // expect(packer._sections[1]).toContainEqual(new Rectangle(60, 0, 40, 100,0)); // Right section
84
+ // });
85
+
86
+ // it('should handle multiple rectangles', () => {
87
+ // const rect1 = packer.addRect(60, 40, 'rect1');
88
+ // const rect2 = packer.addRect(30, 30, 'rect2');
89
+
90
+ // expect(rect1).toBeTruthy();
91
+ // expect(rect2).toBeTruthy();
92
+ // expect(packer.rectangles).toHaveLength(2);
93
+ // });
94
+
95
+ // it('should utilize rotation when beneficial', () => {
96
+ // packer._sections = [new Rectangle(0, 0, 100, 130,0)];
97
+ // const rect = packer.addRect(120, 30);
98
+ // expect(rect).toBeTruthy();
99
+ // expect(rect.width).toBe(30);
100
+ // expect(rect.height).toBe(120);
101
+ // });
102
+
103
+ // it('should return null when rectangle cannot be placed', () => {
104
+ // packer.addRect(90, 90); // Fill most of the space
105
+ // const rect = packer.addRect(50, 50); // Try to place another large rectangle
106
+ // expect(rect).toBeNull();
107
+ // });
108
+ // });
109
+
110
+ describe('fitness', () => {
111
+ it('should calculate correct fitness for placeable rectangles', () => {
112
+ const fitness = packer.fitness(60, 40);
113
+ expect(fitness).toBe(40); // min(100-60, 100-40) = 40
114
+ });
115
+
116
+ it('should handle rotation in fitness calculation', () => {
117
+ packer._sections = [new Rectangle(0, 0, 100, 130,0)];
118
+ const fitness = packer.fitness(120, 30);
119
+ expect(fitness).toBe(10); // min(100-30, 100-120) after rotation
120
+ });
121
+
122
+ it('should return null for unplaceable rectangles', () => {
123
+ const fitness = packer.fitness(150, 150);
124
+ expect(fitness).toBeNull();
125
+ });
126
+ });
127
+
128
+ describe('reset', () => {
129
+ it('should restore initial state', () => {
130
+ packer.addRect(60, 40);
131
+ packer.addRect(30, 30);
132
+ packer.reset();
133
+
134
+ expect(packer.rectangles).toHaveLength(0);
135
+ expect(packer._sections).toHaveLength(1);
136
+ expect(packer._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
137
+ });
138
+ });
139
+ });
@@ -0,0 +1,141 @@
1
+ const { GuillotineBssfSas, Rectangle } = require('../../src/rectpack/guillotine');
2
+
3
+ describe('GuillotineBssfSas', () => {
4
+ let packer;
5
+
6
+ beforeEach(() => {
7
+ // Initialize a new packer before each test with a 100x200 container
8
+ packer = new GuillotineBssfSas(100, 200);
9
+ });
10
+
11
+ describe('initialization', () => {
12
+ it('should initialize with correct dimensions', () => {
13
+ expect(packer.width).toBe(100);
14
+ expect(packer.height).toBe(200);
15
+ });
16
+
17
+ it('should start with an empty rectangles list', () => {
18
+ expect(packer.rectangles).toHaveLength(0);
19
+ });
20
+ });
21
+
22
+ describe('addRect', () => {
23
+ it('should add a rectangle successfully', () => {
24
+ const rect = packer.addRect(50, 30);
25
+ expect(rect).toBeTruthy();
26
+ expect(rect.width).toBe(50);
27
+ expect(rect.height).toBe(30);
28
+ expect(rect.x).toBe(0);
29
+ expect(rect.y).toBe(0);
30
+ });
31
+
32
+ it('should return null when rectangle does not fit', () => {
33
+ const rect = packer.addRect(150, 150);
34
+ expect(rect).toBeNull();
35
+ });
36
+
37
+ it('should handle multiple rectangles', () => {
38
+ const rect1 = packer.addRect(50, 50);
39
+ const rect2 = packer.addRect(50, 50);
40
+
41
+ expect(rect1).toBeTruthy();
42
+ expect(rect2).toBeTruthy();
43
+ expect(rect1.intersects(rect2)).toBeFalsy();
44
+ });
45
+
46
+ it('should throw error for invalid dimensions', () => {
47
+ expect(() => packer.addRect(0, 50)).toThrow();
48
+ expect(() => packer.addRect(50, 0)).toThrow();
49
+ expect(() => packer.addRect(-10, 50)).toThrow();
50
+ });
51
+ });
52
+
53
+ describe('BSSF (Best Short Side Fit) strategy', () => {
54
+ it('should choose section with minimum short side difference', () => {
55
+ // Add a rectangle that will create two sections
56
+ const rect1 = packer.addRect(60, 40);
57
+ expect(rect1).toBeTruthy();
58
+
59
+ // The next rectangle should be placed in the section with the best short side fit
60
+ const rect2 = packer.addRect(30, 30);
61
+ expect(rect2).toBeTruthy();
62
+
63
+ // Based on BSSF, it should prefer the section with smaller waste on the shorter side
64
+ expect(rect2.x).toBe(60);
65
+ expect(rect2.y).toBe(0);
66
+ });
67
+ });
68
+
69
+ describe('SAS (Shorter Axis Split) strategy', () => {
70
+ it('should split along shorter axis', () => {
71
+ // Add a rectangle that will trigger a split
72
+ const rect = packer.addRect(60, 40);
73
+ expect(rect).toBeTruthy();
74
+
75
+ // The split should create two new sections:
76
+ // 1. A section to the right (width: 40, height: 40)
77
+ // 2. A section below (width: 100, height: 160)
78
+
79
+ // Test that we can place rectangles in both sections
80
+ const rectRight = packer.addRect(35, 35);
81
+ expect(rectRight).toBeTruthy();
82
+ expect(rectRight.x).toBe(60);
83
+
84
+ const rectBottom = packer.addRect(90, 150);
85
+ expect(rectBottom).toBeTruthy();
86
+ expect(rectBottom.y).toBe(40);
87
+ });
88
+ });
89
+
90
+ describe('reset', () => {
91
+ it('should clear all placed rectangles', () => {
92
+ packer.addRect(50, 50);
93
+ packer.addRect(30, 30);
94
+ expect(packer.rectangles).toHaveLength(2);
95
+
96
+ packer.reset();
97
+ expect(packer.rectangles).toHaveLength(0);
98
+ });
99
+
100
+ it('should restore initial container state', () => {
101
+ packer.addRect(50, 50);
102
+ packer.reset();
103
+
104
+ // Should be able to add a rectangle using full container dimensions
105
+ const rect = packer.addRect(100, 200);
106
+ expect(rect).toBeTruthy();
107
+ });
108
+ });
109
+
110
+ describe('edge cases', () => {
111
+ it('should handle exact fit rectangles', () => {
112
+ const rect = packer.addRect(100, 200);
113
+ expect(rect).toBeTruthy();
114
+ expect(rect.width).toBe(100);
115
+ expect(rect.height).toBe(200);
116
+ });
117
+
118
+ it('should handle rectangles with one maximum dimension', () => {
119
+ const rect = packer.addRect(100, 100);
120
+ expect(rect).toBeTruthy();
121
+ expect(rect.width).toBe(100);
122
+ expect(rect.height).toBe(100);
123
+ });
124
+
125
+ it('should handle multiple small rectangles efficiently', () => {
126
+ const smallRects = [];
127
+ for (let i = 0; i < 10; i++) {
128
+ const rect = packer.addRect(20, 20);
129
+ expect(rect).toBeTruthy();
130
+ smallRects.push(rect);
131
+ }
132
+
133
+ // Verify no overlaps
134
+ for (let i = 0; i < smallRects.length; i++) {
135
+ for (let j = i + 1; j < smallRects.length; j++) {
136
+ expect(smallRects[i].intersects(smallRects[j])).toBeFalsy();
137
+ }
138
+ }
139
+ });
140
+ });
141
+ });
@@ -0,0 +1,248 @@
1
+ const { Rectangle } = require('../../src/rectpack/geometry');
2
+ const { Guillotine } = require('../../src/rectpack/guillotine');
3
+
4
+ describe('Guillotine', () => {
5
+ let guillotine;
6
+
7
+ beforeEach(() => {
8
+ guillotine = new Guillotine(100, 100);
9
+ });
10
+
11
+ describe('constructor', () => {
12
+ it('should create guillotine with default values', () => {
13
+ expect(guillotine.width).toBe(100);
14
+ expect(guillotine.height).toBe(100);
15
+ expect(guillotine.rot).toBe(true);
16
+ expect(guillotine._merge).toBe(true);
17
+ expect(guillotine._sections).toHaveLength(1);
18
+ expect(guillotine._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
19
+ });
20
+
21
+ it('should create guillotine with custom values', () => {
22
+ const custom = new Guillotine(200, 150, false, false, 'test-id');
23
+ expect(custom.width).toBe(200);
24
+ expect(custom.height).toBe(150);
25
+ expect(custom.rot).toBe(false);
26
+ expect(custom._merge).toBe(false);
27
+ expect(custom.bid).toBe('test-id');
28
+ });
29
+ });
30
+
31
+ describe('_add_section', () => {
32
+ it('should add section when merge is disabled', () => {
33
+ guillotine._merge = false;
34
+ const section = new Rectangle(10, 10, 50, 50);
35
+ guillotine._add_section(section);
36
+ expect(guillotine._sections).toContainEqual(section);
37
+ expect(section.rid).toBe(0);
38
+ });
39
+
40
+ it('should merge sections when possible', () => {
41
+ guillotine._sections = [];
42
+ const section1 = new Rectangle(0, 0, 50, 100);
43
+ const section2 = new Rectangle(50, 0, 50, 100);
44
+ guillotine._add_section(section1);
45
+ guillotine._add_section(section2);
46
+ expect(guillotine._sections).toHaveLength(1);
47
+ expect(guillotine._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
48
+ });
49
+ });
50
+
51
+ describe('_split_horizontal', () => {
52
+ it('should create correct sections after horizontal split', () => {
53
+ const section = new Rectangle(0, 0, 100, 100);
54
+ guillotine._sections = [];
55
+ guillotine._split_horizontal(section, 60, 40);
56
+
57
+ expect(guillotine._sections).toHaveLength(2);
58
+ expect(guillotine._sections).toContainEqual(new Rectangle(0, 40, 100, 60,0)); // Top section
59
+ expect(guillotine._sections).toContainEqual(new Rectangle(60, 0, 40, 40,0)); // Right section
60
+ });
61
+
62
+ it('should handle split when width equals section width', () => {
63
+ const section = new Rectangle(0, 0, 100, 100);
64
+ guillotine._sections = [];
65
+ guillotine._split_horizontal(section, 100, 40);
66
+
67
+ expect(guillotine._sections).toHaveLength(1);
68
+ expect(guillotine._sections[0]).toEqual(new Rectangle(0, 40, 100, 60,0));
69
+ });
70
+ });
71
+
72
+ describe('_split_vertical', () => {
73
+ it('should create correct sections after vertical split', () => {
74
+ const section = new Rectangle(0, 0, 100, 100);
75
+ guillotine._sections = [];
76
+ guillotine._split_vertical(section, 60, 40);
77
+
78
+ expect(guillotine._sections).toHaveLength(2);
79
+ expect(guillotine._sections).toContainEqual(new Rectangle(0, 40, 60, 60,0)); // Top section
80
+ expect(guillotine._sections).toContainEqual(new Rectangle(60, 0, 40, 100,0)); // Right section
81
+ });
82
+
83
+ it('should handle split when height equals section height', () => {
84
+ const section = new Rectangle(0, 0, 100, 100);
85
+ guillotine._sections = [];
86
+ guillotine._split_vertical(section, 60, 100);
87
+
88
+ expect(guillotine._sections).toHaveLength(1);
89
+ expect(guillotine._sections[0]).toEqual(new Rectangle(60, 0, 40, 100,0));
90
+ });
91
+ });
92
+
93
+ describe('_select_fittest_section', () => {
94
+ class TestGuillotine extends Guillotine {
95
+ _section_fitness(section, width, height) {
96
+ if (width > section.width || height > section.height) {
97
+ return null;
98
+ }
99
+ return Math.min(section.width - width, section.height - height);
100
+ }
101
+
102
+ _split(section, width, height) {
103
+ if (section.width < section.height) {
104
+ return this._split_horizontal(section, width, height);
105
+ } else {
106
+ return this._split_vertical(section, width, height);
107
+ }
108
+ }
109
+ }
110
+
111
+ let testGuillotine;
112
+
113
+ beforeEach(() => {
114
+ testGuillotine = new TestGuillotine(100, 100);
115
+ });
116
+
117
+ it('should select best fitting section', () => {
118
+ const [section, rotated] = testGuillotine._select_fittest_section(50, 50);
119
+ expect(section).toBeTruthy();
120
+ expect(rotated).toBe(false);
121
+ });
122
+
123
+ it('should handle rotation when enabled', () => {
124
+ testGuillotine._add_section(new Rectangle(0, 0, 100, 120,0));
125
+ const [section, rotated] = testGuillotine._select_fittest_section(120, 50);
126
+ expect(section).toBeTruthy();
127
+ expect(rotated).toBe(true);
128
+ });
129
+
130
+ it('should return null when no fit is possible', () => {
131
+ const [section, rotated] = testGuillotine._select_fittest_section(150, 150);
132
+ expect(section).toBeNull();
133
+ expect(rotated).toBeNull();
134
+ });
135
+ });
136
+
137
+ describe('addRect', () => {
138
+ class TestGuillotine extends Guillotine {
139
+ _section_fitness(section, width, height) {
140
+ if (width > section.width || height > section.height) {
141
+ return null;
142
+ }
143
+ return Math.min(section.width - width, section.height - height);
144
+ }
145
+
146
+ _split(section, width, height) {
147
+ if (section.width < section.height) {
148
+ return this._split_horizontal(section, width, height);
149
+ } else {
150
+ return this._split_vertical(section, width, height);
151
+ }
152
+ }
153
+ }
154
+
155
+ let testGuillotine;
156
+
157
+ beforeEach(() => {
158
+ testGuillotine = new TestGuillotine(100, 100);
159
+ });
160
+
161
+ it('should add rectangle successfully', () => {
162
+ const rect = testGuillotine.addRect(50, 50, 'test');
163
+ expect(rect).toBeTruthy();
164
+ expect(rect.width).toBe(50);
165
+ expect(rect.height).toBe(50);
166
+ expect(rect.rid).toBe('test');
167
+ });
168
+
169
+ it('should handle rotation when needed', () => {
170
+ testGuillotine._add_section(new Rectangle(0, 0, 100, 120,0));
171
+ const rect = testGuillotine.addRect(120, 50);
172
+ expect(rect).toBeTruthy();
173
+ expect(rect.width).toBe(50);
174
+ expect(rect.height).toBe(120);
175
+ });
176
+
177
+ it('should return null when rectangle cannot be placed', () => {
178
+ const rect = testGuillotine.addRect(150, 150);
179
+ expect(rect).toBeNull();
180
+ });
181
+
182
+ it('should throw error for invalid dimensions', () => {
183
+ expect(() => testGuillotine.addRect(0, 50)).toThrow('Width and height must be positive');
184
+ expect(() => testGuillotine.addRect(50, 0)).toThrow('Width and height must be positive');
185
+ expect(() => testGuillotine.addRect(-50, 50)).toThrow('Width and height must be positive');
186
+ });
187
+ });
188
+
189
+ describe('fitness', () => {
190
+ class TestGuillotine extends Guillotine {
191
+ _section_fitness(section, width, height) {
192
+ if (width > section.width || height > section.height) {
193
+ return null;
194
+ }
195
+ return Math.min(section.width - width, section.height - height);
196
+ }
197
+
198
+ _split(section, width, height) {
199
+ if (section.width < section.height) {
200
+ return this._split_horizontal(section, width, height);
201
+ } else {
202
+ return this._split_vertical(section, width, height);
203
+ }
204
+ }
205
+ }
206
+
207
+ let testGuillotine;
208
+
209
+ beforeEach(() => {
210
+ testGuillotine = new TestGuillotine(100, 130);
211
+ });
212
+
213
+ it('should calculate fitness correctly', () => {
214
+
215
+ const fitness = testGuillotine.fitness(50, 50);
216
+ expect(fitness).toBe(50);
217
+ });
218
+
219
+ it('should handle rotation in fitness calculation', () => {
220
+ const fitness = testGuillotine.fitness(120, 50);
221
+ expect(fitness).toBe(10);
222
+ });
223
+
224
+ it('should return null when no fit is possible', () => {
225
+ const fitness = testGuillotine.fitness(150, 150);
226
+ expect(fitness).toBeNull();
227
+ });
228
+
229
+ it('should throw error for invalid dimensions', () => {
230
+ expect(() => testGuillotine.fitness(0, 50)).toThrow('Width and height must be positive');
231
+ expect(() => testGuillotine.fitness(50, 0)).toThrow('Width and height must be positive');
232
+ expect(() => testGuillotine.fitness(-50, 50)).toThrow('Width and height must be positive');
233
+ });
234
+ });
235
+
236
+ describe('reset', () => {
237
+ it('should reset to initial state', () => {
238
+ const testGuillotine = new Guillotine(100, 100);
239
+ testGuillotine.rectangles.push(new Rectangle(0, 0, 50, 50,0));
240
+ // testGuillotine.addRect(50, 50);
241
+ testGuillotine.reset();
242
+
243
+ expect(testGuillotine.rectangles).toHaveLength(0);
244
+ expect(testGuillotine._sections).toHaveLength(1);
245
+ expect(testGuillotine._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
246
+ });
247
+ });
248
+ });
@@ -0,0 +1,164 @@
1
+ const { Rectangle } = require('../../src/rectpack/geometry');
2
+ const PackingAlgorithm = require('../../src/rectpack/packAlgo');
3
+
4
+ describe('PackingAlgorithm', () => {
5
+ let packer;
6
+
7
+ beforeEach(() => {
8
+ packer = new PackingAlgorithm(100, 100);
9
+ });
10
+
11
+ describe('constructor', () => {
12
+ it('should create packing algorithm with default values', () => {
13
+ expect(packer.width).toBe(100);
14
+ expect(packer.height).toBe(100);
15
+ expect(packer.rot).toBe(true);
16
+ expect(packer.bid).toBeNull();
17
+ expect(packer.rectangles).toHaveLength(0);
18
+ });
19
+
20
+ it('should create packing algorithm with custom values', () => {
21
+ const customPacker = new PackingAlgorithm(200, 150, false, 'test-id');
22
+ expect(customPacker.width).toBe(200);
23
+ expect(customPacker.height).toBe(150);
24
+ expect(customPacker.rot).toBe(false);
25
+ expect(customPacker.bid).toBe('test-id');
26
+ });
27
+ });
28
+
29
+ describe('length', () => {
30
+ it('should return correct number of rectangles', () => {
31
+ expect(packer.length).toBe(0);
32
+ packer.rectangles.push(new Rectangle(0, 0, 10, 10));
33
+ expect(packer.length).toBe(1);
34
+ });
35
+ });
36
+
37
+ describe('iterator', () => {
38
+ it('should be iterable', () => {
39
+ const rect1 = new Rectangle(0, 0, 10, 10);
40
+ const rect2 = new Rectangle(20, 20, 10, 10);
41
+ packer.rectangles.push(rect1, rect2);
42
+
43
+ const items = [...packer];
44
+ expect(items).toHaveLength(2);
45
+ expect(items).toContain(rect1);
46
+ expect(items).toContain(rect2);
47
+ });
48
+ });
49
+
50
+ describe('_fitsSurface', () => {
51
+ it('should throw error for non-positive dimensions', () => {
52
+ expect(() => packer._fitsSurface(0, 10)).toThrow('Width and height must be positive');
53
+ expect(() => packer._fitsSurface(10, 0)).toThrow('Width and height must be positive');
54
+ expect(() => packer._fitsSurface(-10, 10)).toThrow('Width and height must be positive');
55
+ });
56
+
57
+ it('should handle rotation when enabled', () => {
58
+ expect(packer._fitsSurface(120, 50)).toBe(true); // Will rotate to fit
59
+ expect(packer._fitsSurface(120, 120)).toBe(false); // Too large even with rotation
60
+ });
61
+
62
+ it('should not rotate when disabled', () => {
63
+ packer.rot = false;
64
+ expect(packer._fitsSurface(120, 50)).toBe(false); // Won't rotate
65
+ expect(packer._fitsSurface(50, 50)).toBe(true); // Fits without rotation
66
+ });
67
+ });
68
+
69
+ describe('getItem', () => {
70
+ it('should return rectangle at specified index', () => {
71
+ const rect = new Rectangle(0, 0, 10, 10);
72
+ packer.rectangles.push(rect);
73
+ expect(packer.getItem(0)).toBe(rect);
74
+ });
75
+
76
+ it('should return undefined for invalid index', () => {
77
+ expect(packer.getItem(0)).toBeUndefined();
78
+ });
79
+ });
80
+
81
+ describe('usedArea', () => {
82
+ it('should calculate total area of placed rectangles', () => {
83
+ packer.rectangles.push(
84
+ new Rectangle(0, 0, 10, 10), // area 100
85
+ new Rectangle(20, 20, 5, 5) // area 25
86
+ );
87
+ expect(packer.usedArea()).toBe(125);
88
+ });
89
+
90
+ it('should return 0 when no rectangles placed', () => {
91
+ expect(packer.usedArea()).toBe(0);
92
+ });
93
+ });
94
+
95
+ describe('abstract methods', () => {
96
+ it('should throw error for unimplemented fitness method', () => {
97
+ expect(() => packer.fitness(10, 10)).toThrow('Not implemented');
98
+ });
99
+
100
+ it('should throw error for unimplemented addRect method', () => {
101
+ expect(() => packer.addRect(10, 10)).toThrow('Not implemented');
102
+ });
103
+ });
104
+
105
+ describe('rectList', () => {
106
+ it('should return correct list format', () => {
107
+ packer.rectangles.push(
108
+ new Rectangle(0, 0, 10, 10, 'rect1'),
109
+ new Rectangle(20, 20, 5, 5, 'rect2')
110
+ );
111
+ const list = packer.rectList();
112
+ expect(list).toEqual([
113
+ [0, 0, 10, 10, 'rect1'],
114
+ [20, 20, 5, 5, 'rect2']
115
+ ]);
116
+ });
117
+ });
118
+
119
+ describe('validatePacking', () => {
120
+ it('should not throw for valid packing', () => {
121
+ packer.rectangles.push(
122
+ new Rectangle(0, 0, 10, 10),
123
+ new Rectangle(20, 20, 10, 10)
124
+ );
125
+ expect(() => packer.validatePacking()).not.toThrow();
126
+ });
127
+
128
+ it('should throw for rectangles outside surface', () => {
129
+ packer.rectangles.push(new Rectangle(95, 95, 10, 10));
130
+ expect(() => packer.validatePacking()).toThrow('Rectangle placed outside surface');
131
+ });
132
+
133
+ it('should throw for colliding rectangles', () => {
134
+ packer.rectangles.push(
135
+ new Rectangle(0, 0, 20, 20),
136
+ new Rectangle(10, 10, 20, 20)
137
+ );
138
+ expect(() => packer.validatePacking()).toThrow('Rectangle collision detected');
139
+ });
140
+ });
141
+
142
+ describe('isEmpty', () => {
143
+ it('should return true when no rectangles placed', () => {
144
+ expect(packer.isEmpty()).toBe(true);
145
+ });
146
+
147
+ it('should return false when rectangles are placed', () => {
148
+ packer.rectangles.push(new Rectangle(0, 0, 10, 10));
149
+ expect(packer.isEmpty()).toBe(false);
150
+ });
151
+ });
152
+
153
+ describe('reset', () => {
154
+ it('should clear all placed rectangles', () => {
155
+ packer.rectangles.push(
156
+ new Rectangle(0, 0, 10, 10),
157
+ new Rectangle(20, 20, 10, 10)
158
+ );
159
+ packer.reset();
160
+ expect(packer.rectangles).toHaveLength(0);
161
+ expect(packer.isEmpty()).toBe(true);
162
+ });
163
+ });
164
+ });