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,466 @@
1
+ const {
2
+ SORT_NONE,
3
+ SORT_AREA,
4
+ SORT_PERI,
5
+ SORT_DIFF,
6
+ SORT_SSIDE,
7
+ SORT_LSIDE,
8
+ SORT_RATIO,
9
+ PackerOnlineBNF,
10
+ PackerOnlineBFF,
11
+ PackerOnlineBBF,
12
+ PackerBNF,
13
+ PackerBFF,
14
+ PackerBBF,
15
+ PackerGlobal,
16
+ PackingMode,
17
+ PackingBin,
18
+ newPacker
19
+ } = require('../../src/rectpack/packer');
20
+
21
+ const { GuillotineBafSas, GuillotineBssfSas, GuillotineBafMaxas, GuillotineBlsfMaxas } = require('../../src/rectpack/guillotine');
22
+ // const { SkylineMwfl, SkylineBl } = require('../../src/rectpack/skyline');
23
+
24
+ describe('Rectangle Sort Tests', () => {
25
+ test('sort none returns list as is', () => {
26
+ const a = [[3, 3], [3, 1], [3, 2], [1, 2], [2, 1]];
27
+ const ordered = SORT_NONE(a);
28
+ expect(ordered).toEqual(a);
29
+
30
+ // Test empty list
31
+ expect(SORT_NONE([])).toEqual([]);
32
+ });
33
+
34
+ test('sort area sorts rectangles by descending area', () => {
35
+ const a = [[5, 5], [7, 7], [3, 4], [100, 1]];
36
+ const ordered = SORT_AREA(a);
37
+ expect(ordered).toEqual([[100, 1], [7, 7], [5, 5], [3, 4]]);
38
+
39
+ // Test empty list
40
+ expect(SORT_AREA([])).toEqual([]);
41
+ });
42
+
43
+ test('sort perimeter sorts rectangles by perimeter', () => {
44
+ const a = [[5, 5], [7, 7], [3, 4], [40, 1]];
45
+ const ordered = SORT_PERI(a);
46
+ expect(ordered).toEqual([[40, 1], [7, 7], [5, 5], [3, 4]]);
47
+
48
+ // Test empty list
49
+ expect(SORT_PERI([])).toEqual([]);
50
+ });
51
+
52
+ test('sort diff sorts rectangles by difference in side lengths', () => {
53
+ const a = [[7, 1], [1, 9], [2, 11], [5, 1]];
54
+ const ordered = SORT_DIFF(a);
55
+ expect(ordered).toEqual([[2, 11], [1, 9], [7, 1], [5, 1]]);
56
+
57
+ // Test empty list
58
+ expect(SORT_DIFF([])).toEqual([]);
59
+ });
60
+
61
+ test('sort short side sorts rectangles by short side descending', () => {
62
+ const a = [[2, 9], [7, 3], [4, 5], [11, 3]];
63
+ const ordered = SORT_SSIDE(a);
64
+ expect(ordered).toEqual([[4, 5], [11, 3], [7, 3], [2, 9]]);
65
+
66
+ // Test empty list
67
+ expect(SORT_SSIDE([])).toEqual([]);
68
+ });
69
+
70
+ test('sort long side sorts rectangles by long side descending', () => {
71
+ const a = [[19, 5], [32, 5], [6, 19], [9, 11]];
72
+ const ordered = SORT_LSIDE(a);
73
+ expect(ordered).toEqual([[32, 5], [6, 19], [19, 5], [9, 11]]);
74
+
75
+ // Test empty list
76
+ expect(SORT_LSIDE([])).toEqual([]);
77
+ });
78
+
79
+ test('sort ratio sorts rectangles by width/height descending', () => {
80
+ const a = [[12, 5], [15, 4], [4, 1], [1, 2]];
81
+ const ordered = SORT_RATIO(a);
82
+ expect(ordered).toEqual([[4, 1], [15, 4], [12, 5], [1, 2]]);
83
+
84
+ // Test empty list
85
+ expect(SORT_RATIO([])).toEqual([]);
86
+ });
87
+ });
88
+
89
+ describe('PackerOnline Tests', () => {
90
+ test('bin iteration only loops over closed and open bins', () => {
91
+ const p = new PackerOnlineBNF(GuillotineBssfSas, false);
92
+ p.addBin(50, 55);
93
+ p.addBin(30, 30);
94
+ p.addBin(5, 5);
95
+ p.addBin(40, 40);
96
+
97
+ // No bins to iterate over
98
+ const bins = Array.from(p);
99
+ expect(bins.length).toBe(0);
100
+
101
+ // One open bin to iterate
102
+ p.addRect(50, 50);
103
+ const binsAfterOneRect = Array.from(p);
104
+ expect(binsAfterOneRect.length).toBe(1);
105
+
106
+ // One closed and one open bin
107
+ p.addRect(29, 29);
108
+ const binsAfterTwoRects = Array.from(p);
109
+ expect(binsAfterTwoRects.length).toBe(2);
110
+ expect(binsAfterTwoRects[0].width).toBe(50); // Test closed bins are first
111
+
112
+ // Two closed bins, one skipped bin, and an open bin
113
+ p.addRect(40, 40);
114
+ const binsAfterThreeRects = Array.from(p);
115
+ expect(binsAfterThreeRects.length).toBe(3);
116
+ });
117
+
118
+ test('bin order - bins are packed in order they were added', () => {
119
+ const p = new PackerOnlineBNF(GuillotineBssfSas, false);
120
+ p.addBin(45, 45);
121
+ p.addBin(30, 30);
122
+ p.addBin(40, 40);
123
+
124
+ p.addRect(20, 20);
125
+ expect(p.length).toBe(1);
126
+ const firstBin = Array.from(p)[0];
127
+ expect(firstBin.width).toBe(45);
128
+ expect(firstBin.height).toBe(45);
129
+
130
+ p.addRect(29, 29);
131
+ expect(p.length).toBe(2);
132
+ const secondBin = Array.from(p)[1];
133
+ expect(secondBin.width).toBe(30);
134
+ expect(secondBin.height).toBe(30);
135
+
136
+ // Check bins are added at the end of the queue and used last
137
+ p.addBin(39, 39);
138
+ p.addRect(39, 39);
139
+ expect(p.length).toBe(3);
140
+ const thirdBin = Array.from(p)[2];
141
+ expect(thirdBin.width).toBe(40);
142
+ expect(thirdBin.height).toBe(40);
143
+ });
144
+ });
145
+
146
+ describe('PackerOnlineBNF Tests', () => {
147
+ test('bin selection - closes bin after failed packing attempt', () => {
148
+ const p = new PackerOnlineBNF(GuillotineBssfSas, false);
149
+ p.addBin(30, 30);
150
+ p.addBin(10, 10);
151
+ p.addBin(40, 40);
152
+
153
+ // This rectangle fits into first bin
154
+ p.addRect(5, 5);
155
+ expect(p.length).toBe(1);
156
+ expect(Array.from(p)[0].width).toBe(30);
157
+
158
+ // This rectangle doesn't fit into the open bin (first), so the bin
159
+ // is closed and rectangle packed into next one where it fits
160
+ p.addRect(29, 29);
161
+ expect(p.length).toBe(2);
162
+ expect(Array.from(p)[1].width).toBe(40);
163
+
164
+ // Try to add a rectangle that would have fit into the first bin
165
+ // but it is packed since first bin is closed
166
+ p.addRect(10, 10);
167
+ expect(p.length).toBe(2);
168
+ const rectList = p.rectList();
169
+ const lastRect = rectList[rectList.length - 1];
170
+ const [binIndex, x, y, w, h, rid] = lastRect;
171
+ expect(binIndex).toBe(1);
172
+ });
173
+
174
+ test('infinite bins are only tested once when rectangle doesnt fit', () => {
175
+ const p = new PackerOnlineBNF({packAlgo: GuillotineBssfSas, rotation: true});
176
+ p.addBin(50, 50, 50);
177
+ p.addBin(100, 100, Infinity);
178
+
179
+ p.addRect(90, 90);
180
+ expect(p.length).toBe(1);
181
+ p.addRect(95, 95);
182
+ expect(p.length).toBe(2);
183
+
184
+ // Check other bins
185
+ p.addRect(40, 40);
186
+ expect(p.length).toBe(3);
187
+ expect(Array.from(p)[2].width).toBe(50);
188
+
189
+ p.addRect(45, 45);
190
+ expect(p.length).toBe(4);
191
+ expect(Array.from(p)[3].width).toBe(50);
192
+ });
193
+
194
+ test('rotation handling', () => {
195
+ const p = new PackerOnlineBNF({packAlgo: GuillotineBssfSas, rotation: false});
196
+ p.addBin(30, 10);
197
+ p.addBin(50, 10);
198
+ p.addRect(10, 30);
199
+
200
+ // Rectangle didn't fit in any bins when rotation was disabled
201
+ const rectList = p.rectList();
202
+ expect(rectList.length).toBe(0);
203
+ // expect(p.length).toBe(0);
204
+
205
+ // With rotation the rectangle is successfully packed
206
+ const p2 = new PackerOnlineBNF({packAlgo: GuillotineBssfSas, rotation: true});
207
+ p2.addBin(30, 10);
208
+ p2.addBin(50, 10);
209
+ p2.addRect(10, 30);
210
+ const rectList2 = p2.rectList();
211
+ expect(rectList2[0]).toEqual([0, 0, 0, 30, 10, null]);
212
+ expect(p2.length).toBe(1);
213
+ });
214
+ });
215
+
216
+ describe('PackerOnlineBFF Tests', () => {
217
+ test('bin selection - packs into first bin where it fits', () => {
218
+ const p = new PackerOnlineBFF({
219
+ packAlgo: GuillotineBafSas,
220
+ rotation: false
221
+ });
222
+
223
+ p.addBin(20, 20, 2);
224
+ p.addBin(100, 100);
225
+
226
+ // Packed into second bin
227
+ p.addRect(90, 90);
228
+ expect(p.length).toBe(1);
229
+ expect(Array.from(p)[0].width).toBe(100);
230
+
231
+ // Rectangles are packed into open bins whenever possible
232
+ p.addRect(10, 10);
233
+ expect(p.length).toBe(1);
234
+ expect(p.rectList().length).toBe(2);
235
+
236
+ p.addRect(5, 5);
237
+ expect(p.length).toBe(1);
238
+ expect(p.rectList().length).toBe(3);
239
+
240
+ // Rectangle doesn't fit, open new bin
241
+ p.addRect(15, 15);
242
+ expect(p.length).toBe(2);
243
+ expect(p.rectList().length).toBe(4);
244
+
245
+ // If there are more than one possible bin select first one
246
+ p.addRect(5, 5);
247
+ expect(p.length).toBe(2);
248
+ expect(p.rectList().length).toBe(5);
249
+ expect(p.rectList()).toContainEqual([0, 10, 90, 5, 5, null]);
250
+ });
251
+ });
252
+
253
+ describe('PackerOnlineBBF Tests', () => {
254
+ test('bin selection - packs into bin with best fitness', () => {
255
+ const p = new PackerOnlineBBF(
256
+ GuillotineBafSas,
257
+ false
258
+ );
259
+
260
+ p.addBin(10, 10);
261
+ p.addBin(15, 15);
262
+ p.addBin(55, 55);
263
+ p.addBin(50, 50);
264
+
265
+ // First rectangle is packed into first bin where it fits
266
+ p.addRect(50, 30);
267
+ expect(p.length).toBe(1);
268
+ expect(Array.from(p)[0].width).toBe(55);
269
+
270
+ // Another bin is opened when it doesn't fit into first one
271
+ p.addRect(50, 30);
272
+ expect(p.length).toBe(2);
273
+ expect(Array.from(p)[1].width).toBe(50);
274
+
275
+ // Rectangle is placed into bin with best fitness, not first where it fits
276
+ p.addRect(20, 20);
277
+ expect(p.length).toBe(2);
278
+ expect(p.rectList()).toContainEqual([1, 0, 30, 20, 20, null]);
279
+ });
280
+ });
281
+
282
+ describe('Packer Tests', () => {
283
+ test('initialization defaults', () => {
284
+ // Test rotation is enabled by default
285
+ const p = new PackerBNF({packAlgo: GuillotineBafSas, rotation: true});
286
+ p.addBin(100, 10);
287
+ p.addRect(10, 89);
288
+ p.pack();
289
+ const rectList = p.rectList();
290
+ expect(rectList[0]).toEqual([0, 0, 0, 89, 10, null]);
291
+
292
+ // Test default packing algo
293
+ const p2 = new PackerBFF();
294
+ p2.addBin(10, 10);
295
+ p2.addRect(1, 1);
296
+ p2.pack();
297
+ for (const b of p2) {
298
+ expect(b).toBeInstanceOf(GuillotineBssfSas);
299
+ }
300
+
301
+ // Test default sorting algo is unsorted
302
+ const p3 = new PackerBFF({
303
+ packAlgo: GuillotineBssfSas,
304
+ rotation: false
305
+ });
306
+ p3.addBin(100, 100, 20);
307
+
308
+ p3.addRect(70, 70);
309
+ p3.addRect(90, 55);
310
+ p3.addRect(90, 90);
311
+ p3.addRect(55, 90);
312
+ p3.addRect(60, 60);
313
+ p3.pack();
314
+
315
+ expect(p3.rectList()).toContainEqual([0, 0, 0, 70, 70, null]);
316
+ expect(p3.rectList()).toContainEqual([1, 0, 0, 90, 55, null]);
317
+ expect(p3.rectList()).toContainEqual([2, 0, 0, 90, 90, null]);
318
+ expect(p3.rectList()).toContainEqual([3, 0, 0, 55, 90, null]);
319
+ expect(p3.rectList()).toContainEqual([4, 0, 0, 60, 60, null]);
320
+ });
321
+
322
+ test('packing functionality', () => {
323
+ // Check packing without bins doesn't raise errors
324
+ const p = new PackerBFF();
325
+ p.addRect(10, 10);
326
+ p.pack();
327
+
328
+ expect(p.rectList()).toEqual([]);
329
+ expect(p.binList()).toEqual([]);
330
+ expect(Array.from(p)).toEqual([]);
331
+
332
+ // No errors when there are no rectangles to pack
333
+ const p2 = new PackerBFF();
334
+ p2.addBin(10, 10);
335
+ p2.pack();
336
+ expect(p2.rectList()).toEqual([]);
337
+ expect(p2.binList()).toEqual([]);
338
+
339
+ // Test rectangles are packed into first available bin
340
+ const p3 = new PackerBFF({
341
+ packAlgo: GuillotineBssfSas,
342
+ rotation: false,
343
+ });
344
+ p3.addBin(20, 10);
345
+ p3.addBin(50, 50);
346
+ p3.addRect(10, 20,1);
347
+ p3.addRect(41, 41,2); // Not enough space for this rectangle
348
+ p3.pack();
349
+ const binList = p3.binList();
350
+ expect(binList.length).toBe(2); // Both bins should be in the list
351
+
352
+ // Verify the bins are correct
353
+ const bins = Array.from(p3);
354
+ expect(bins[0].width).toBe(20); // First bin
355
+ expect(bins[0].height).toBe(10);
356
+ expect(bins[1].width).toBe(50); // Second bin
357
+ expect(bins[1].height).toBe(50);
358
+
359
+ // Verify rectangle placements
360
+ const rectList = p3.rectList();
361
+ expect(rectList.length).toBe(1); // Both rectangles should be packed
362
+
363
+ // First rectangle (10x20) should be in second bin (bin index 1)
364
+ expect(rectList[0]).toEqual([1, 0, 0, 10, 20, 1]);
365
+
366
+ // Second rectangle (41x41) should also be in second bin
367
+ // Note: exact position may vary based on packing algorithm, but must be in bin 1
368
+ expect(rectList[1][0]).toBe(1); // Bin index
369
+ expect(rectList[1][3]).toBe(41); // Width
370
+ expect(rectList[1][4]).toBe(41); // Height
371
+ expect(rectList[1][5]).toBe(2); // Rectangle ID
372
+ });
373
+ });
374
+
375
+ describe('newPacker Tests', () => {
376
+
377
+ test('default_options', () => {
378
+ // Test default options
379
+ const p = newPacker();
380
+
381
+ // Default mode Online BBF
382
+ expect(p).toBeInstanceOf(PackerBBF);
383
+
384
+ // Default rotations true
385
+ expect(p._rotation).toBe(true);
386
+
387
+ // Default packing algo
388
+ p.addRect(100, 10);
389
+ p.addBin(10, 100);
390
+ p.pack();
391
+ for (const b of p) {
392
+ expect(b).toBeInstanceOf(GuillotineBssfSas);
393
+ }
394
+
395
+ // Test default sorting algo is SORT_AREA
396
+ const p2 = newPacker({
397
+ rotation: false
398
+ });
399
+ expect(p2._rotation).toBe(false);
400
+ p2.addBin(100, 100, 10);
401
+
402
+
403
+
404
+
405
+ p2.addRect(70, 70);
406
+ p2.addRect(90, 55);
407
+ p2.addRect(90, 90);
408
+ p2.addRect(55, 90);
409
+ p2.addRect(60, 60);
410
+ p2.pack();
411
+
412
+ const rectList = p2.rectList();
413
+ expect(rectList.length).toBe(5);
414
+ expect(rectList).toContainEqual([0, 0, 0, 90, 90, null]); // Largest area rectangle
415
+ expect(rectList).toContainEqual([1, 0, 0, 90, 55, null]); // Second largest
416
+ expect(rectList).toContainEqual([2, 0, 0, 55, 90, null]); // Third largest
417
+ expect(rectList).toContainEqual([3, 0, 0, 70, 70, null]); // Fourth largest
418
+ expect(rectList).toContainEqual([4, 0, 0, 60, 60, null]); // Fifth largest
419
+ });
420
+
421
+ test('packing modes', () => {
422
+ // Test online mode
423
+ const p1 = newPacker({
424
+ mode: PackingMode.Online,
425
+ binAlgo: PackingBin.BNF
426
+ });
427
+ expect(p1).toBeInstanceOf(PackerOnlineBNF);
428
+
429
+ const p2 = newPacker({
430
+ mode: PackingMode.Online,
431
+ binAlgo: PackingBin.BFF
432
+ });
433
+ expect(p2).toBeInstanceOf(PackerOnlineBFF);
434
+
435
+ const p3 = newPacker({
436
+ mode: PackingMode.Online,
437
+ binAlgo: PackingBin.BBF
438
+ });
439
+ expect(p3).toBeInstanceOf(PackerOnlineBBF);
440
+
441
+ // Test offline mode
442
+ const p4 = newPacker({
443
+ mode: PackingMode.Offline,
444
+ binAlgo: PackingBin.BNF
445
+ });
446
+ expect(p4).toBeInstanceOf(PackerBNF);
447
+
448
+ const p5 = newPacker({
449
+ mode: PackingMode.Offline,
450
+ binAlgo: PackingBin.BFF
451
+ });
452
+ expect(p5).toBeInstanceOf(PackerBFF);
453
+
454
+ const p6 = newPacker({
455
+ mode: PackingMode.Offline,
456
+ binAlgo: PackingBin.BBF
457
+ });
458
+ expect(p6).toBeInstanceOf(PackerBBF);
459
+
460
+ const p7 = newPacker({
461
+ mode: PackingMode.Offline,
462
+ binAlgo: PackingBin.Global
463
+ });
464
+ expect(p7).toBeInstanceOf(PackerGlobal);
465
+ });
466
+ });
@@ -0,0 +1,200 @@
1
+ const {
2
+ float2dec,
3
+ SORT_AREA,
4
+ SORT_PERI,
5
+ SORT_DIFF,
6
+ SORT_SSIDE,
7
+ SORT_LSIDE,
8
+ SORT_RATIO,
9
+ SORT_NONE
10
+ } = require('../../src/rectpack/packer');
11
+
12
+ describe('Utility Functions', () => {
13
+ describe('float2dec', () => {
14
+ it('should round up float numbers to specified decimal places', () => {
15
+ expect(float2dec(3.14159, 2)).toBe(3.15);
16
+ expect(float2dec(2.001, 2)).toBe(2.01);
17
+ expect(float2dec(5.555, 1)).toBe(5.6);
18
+ expect(float2dec(10.123456, 3)).toBe(10.124);
19
+ });
20
+
21
+ it('should handle whole numbers', () => {
22
+ expect(float2dec(5, 2)).toBe(5);
23
+ expect(float2dec(100, 3)).toBe(100);
24
+ });
25
+
26
+ it('should handle zero', () => {
27
+ expect(float2dec(0, 2)).toBe(0);
28
+ expect(float2dec(0.0, 3)).toBe(0);
29
+ });
30
+
31
+ it('should handle negative numbers', () => {
32
+ expect(float2dec(-3.14159, 2)).toBe(-3.14);
33
+ expect(float2dec(-2.001, 1)).toBe(-2);
34
+ });
35
+ });
36
+ });
37
+
38
+ describe('Sorting Functions', () => {
39
+ // Test data
40
+ const rectangles = [
41
+ [2, 3], // area: 6, perimeter: 10, ratio: 0.67
42
+ [5, 2], // area: 10, perimeter: 14, ratio: 2.5
43
+ [4, 4], // area: 16, perimeter: 16, ratio: 1
44
+ [1, 6], // area: 6, perimeter: 14, ratio: 0.17
45
+ [3, 3] // area: 9, perimeter: 12, ratio: 1
46
+ ];
47
+
48
+ describe('SORT_AREA', () => {
49
+ it('should sort rectangles by area in descending order', () => {
50
+ const sorted = SORT_AREA(rectangles);
51
+ expect(sorted).toEqual([
52
+ [4, 4], // area: 16
53
+ [5, 2], // area: 10
54
+ [3, 3], // area: 9
55
+ [2, 3], // area: 6
56
+ [1, 6] // area: 6
57
+ ]);
58
+ });
59
+
60
+ it('should not modify original array', () => {
61
+ const original = [...rectangles];
62
+ SORT_AREA(rectangles);
63
+ expect(rectangles).toEqual(original);
64
+ });
65
+ });
66
+
67
+ describe('SORT_PERI', () => {
68
+ it('should sort rectangles by perimeter in descending order', () => {
69
+ const sorted = SORT_PERI(rectangles);
70
+ expect(sorted).toEqual([
71
+ [4, 4], // perimeter: 16
72
+ [5, 2], // perimeter: 14
73
+ [1, 6], // perimeter: 14
74
+ [3, 3], // perimeter: 12
75
+ [2, 3] // perimeter: 10
76
+ ]);
77
+ });
78
+ });
79
+
80
+ describe('SORT_DIFF', () => {
81
+ it('should sort rectangles by absolute difference of sides in descending order', () => {
82
+ const sorted = SORT_DIFF(rectangles);
83
+ expect(sorted).toEqual([
84
+ [1, 6], // diff: 5
85
+ [5, 2], // diff: 3
86
+ [2, 3], // diff: 1
87
+ [4, 4], // diff: 0
88
+ [3, 3] // diff: 0
89
+ ]);
90
+ });
91
+ });
92
+
93
+ describe('SORT_SSIDE', () => {
94
+ it('should sort rectangles by shorter side in descending order', () => {
95
+ const sorted = SORT_SSIDE(rectangles);
96
+ expect(sorted).toEqual([
97
+ [4, 4], // min side: 4
98
+ [3, 3], // min side: 3
99
+ [5, 2], // min side: 2
100
+ [2, 3], // min side: 2
101
+ [1, 6] // min side: 1
102
+ ]);
103
+ });
104
+
105
+ it('should use longer side as tiebreaker', () => {
106
+ const testData = [[2, 5], [2, 4], [2, 6]];
107
+ const sorted = SORT_SSIDE(testData);
108
+ expect(sorted).toEqual([
109
+ [2, 6], // min: 2, max: 6
110
+ [2, 5], // min: 2, max: 5
111
+ [2, 4] // min: 2, max: 4
112
+ ]);
113
+ });
114
+ });
115
+
116
+ describe('SORT_LSIDE', () => {
117
+ it('should sort rectangles by longer side in descending order', () => {
118
+ const sorted = SORT_LSIDE(rectangles);
119
+ expect(sorted).toEqual([
120
+ [1, 6], // max side: 6
121
+ [5, 2], // max side: 5
122
+ [4, 4], // max side: 4
123
+ [3, 3], // max side: 3
124
+ [2, 3] // max side: 3
125
+ ]);
126
+ });
127
+
128
+ it('should use shorter side as tiebreaker', () => {
129
+ const testData = [[6, 2], [6, 3], [6, 1]];
130
+ const sorted = SORT_LSIDE(testData);
131
+ expect(sorted).toEqual([
132
+ [6, 3],
133
+ [6, 2],
134
+ [6, 1]
135
+ ]);
136
+ });
137
+ });
138
+
139
+ describe('SORT_RATIO', () => {
140
+ it('should sort rectangles by width/height ratio in descending order', () => {
141
+ const sorted = SORT_RATIO(rectangles);
142
+ expect(sorted).toEqual([
143
+ [5, 2], // ratio: 2.5
144
+ [4, 4], // ratio: 1
145
+ [3, 3], // ratio: 1
146
+ [2, 3], // ratio: 0.67
147
+ [1, 6] // ratio: 0.17
148
+ ]);
149
+ });
150
+ });
151
+
152
+ describe('SORT_NONE', () => {
153
+ it('should return a new array with same order', () => {
154
+ const sorted = SORT_NONE(rectangles);
155
+ expect(sorted).toEqual(rectangles);
156
+ expect(sorted).not.toBe(rectangles); // Should be a new array
157
+ });
158
+
159
+ it('should not modify original array', () => {
160
+ const original = [...rectangles];
161
+ SORT_NONE(rectangles);
162
+ expect(rectangles).toEqual(original);
163
+ });
164
+ });
165
+
166
+ describe('Edge Cases', () => {
167
+ it('should handle empty arrays', () => {
168
+ const emptyArray = [];
169
+ expect(SORT_AREA(emptyArray)).toEqual([]);
170
+ expect(SORT_PERI(emptyArray)).toEqual([]);
171
+ expect(SORT_DIFF(emptyArray)).toEqual([]);
172
+ expect(SORT_SSIDE(emptyArray)).toEqual([]);
173
+ expect(SORT_LSIDE(emptyArray)).toEqual([]);
174
+ expect(SORT_RATIO(emptyArray)).toEqual([]);
175
+ expect(SORT_NONE(emptyArray)).toEqual([]);
176
+ });
177
+
178
+ it('should handle single-element arrays', () => {
179
+ const singleRect = [[3, 4]];
180
+ expect(SORT_AREA(singleRect)).toEqual([[3, 4]]);
181
+ expect(SORT_PERI(singleRect)).toEqual([[3, 4]]);
182
+ expect(SORT_DIFF(singleRect)).toEqual([[3, 4]]);
183
+ expect(SORT_SSIDE(singleRect)).toEqual([[3, 4]]);
184
+ expect(SORT_LSIDE(singleRect)).toEqual([[3, 4]]);
185
+ expect(SORT_RATIO(singleRect)).toEqual([[3, 4]]);
186
+ expect(SORT_NONE(singleRect)).toEqual([[3, 4]]);
187
+ });
188
+
189
+ it('should handle arrays with identical rectangles', () => {
190
+ const identicalRects = [[2, 3], [2, 3], [2, 3]];
191
+ expect(SORT_AREA(identicalRects)).toEqual(identicalRects);
192
+ expect(SORT_PERI(identicalRects)).toEqual(identicalRects);
193
+ expect(SORT_DIFF(identicalRects)).toEqual(identicalRects);
194
+ expect(SORT_SSIDE(identicalRects)).toEqual(identicalRects);
195
+ expect(SORT_LSIDE(identicalRects)).toEqual(identicalRects);
196
+ expect(SORT_RATIO(identicalRects)).toEqual(identicalRects);
197
+ expect(SORT_NONE(identicalRects)).toEqual(identicalRects);
198
+ });
199
+ });
200
+ });
File without changes