rectpackbin 0.0.2 → 0.0.3

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,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
+ });
@@ -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