-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmaxrects-bin.spec.js
350 lines (304 loc) · 11.9 KB
/
maxrects-bin.spec.js
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
/* eslint-disable no-constant-condition */
"use strict";
let MaxRectsBin = require("../dist/maxrects-packer").MaxRectsBin;
let Rectangle = require("../dist/maxrects-packer").Rectangle;
const EDGE_MAX_VALUE = 4096;
const EDGE_MIN_VALUE = 128;
const opt = {
smart: true,
pot: true,
square: false,
allowRotation: false,
tag: true,
};
let bin;
describe("no padding", () => {
beforeEach(() => {
bin = new MaxRectsBin(1024, 1024, 0, opt);
});
test("is initially empty", () => {
expect(bin.width).toBe(0);
expect(bin.height).toBe(0);
});
test("adds rects correctly", () => {
let position = bin.add(200, 100, {});
expect(position.x).toBe(0);
expect(position.y).toBe(0);
});
test("report/set bin dirty status", () => {
bin.add(200, 100, {});
expect(bin.dirty).toBe(true); // add element to bin will render bin dirty
bin.setDirty(false);
expect(bin.dirty).toBe(false); // clean bin dirty
bin.add(200, 100, {});
expect(bin.dirty).toBe(true); // add new element is dirty
bin.setDirty(false);
bin.setDirty();
expect(bin.dirty).toBe(true); // setDirty is dirty
bin.reset();
expect(bin.dirty).toBe(false); // reset clean dirty
let rect = bin.add(new Rectangle(200, 100));
bin.setDirty(false);
rect.width = 256;
expect(bin.dirty).toBe(true); // modify rects is dirty
});
test("updates size correctly", () => {
bin.add(200, 100, {});
expect(bin.width).toBe(256);
expect(bin.height).toBe(128);
});
test("stores data correctly", () => {
bin.add(200, 100, {foo: "bar"});
expect(bin.rects[0].data.foo).toBe("bar");
});
test("set rotation correctly", () => {
bin = new MaxRectsBin(1024, 1024, 0, {...opt, allowRotation: true});
bin.add({width: 512, height: 1024});
bin.add({width: 1024, height: 512});
expect(bin.rects.length).toBe(2);
expect(bin.rects[1].rot).toBe(true);
bin.reset(true);
bin.add({width: 512, height: 1024});
bin.add({width: 1024, height: 512, rot: true});
expect(bin.rects.length).toBe(2);
expect(bin.rects[1].rot).toBe(false);
});
test("stores custom rect correctly", () => {
bin.add({width: 200, height: 100, foo: "bar"});
expect(bin.rects[0].foo).toBe("bar");
});
test("none tag bin reject all tagged rects on exclusive tag mode", () => {
bin.add({width: 200, height: 100});
bin.add({width: 200, height: 100, tag: "foo"});
bin.add({width: 200, height: 100, tag: "bar"});
expect(bin.rects.length).toBe(1);
});
test("tagged bin reject different tagged rects on exclusive tag mode", () => {
bin.tag = "foo";
let one = bin.add({width: 200, height: 100, tag: "foo"});
let two = bin.add({width: 200, height: 100, tag: "bar"});
expect(bin.rects.length).toBe(1);
expect(bin.rects[0].tag).toBe("foo");
expect(two).toBeUndefined();
});
test("tagged bin accept different tagged rects on non-exclusive tag mode", () => {
bin.tag = "foo";
bin.options.exclusiveTag = false;
let one = bin.add({width: 200, height: 100, tag: "foo"});
let two = bin.add({width: 200, height: 100, tag: "bar"});
expect(bin.rects.length).toBe(2);
expect(bin.rects[0].tag).toBe("foo");
expect(two).toBeDefined();
});
test("fits squares correctly", () => {
let i = 0;
while(bin.add(100, 100, {num: i})) {
// circuit breaker
if (i++ === 1000) {
break;
}
}
expect(i).toBe(100);
expect(bin.rects.length).toBe(100);
expect(bin.width).toBe(1024);
expect(bin.height).toBe(1024);
bin.rects.forEach((rect, i) => {
expect(rect.data.num).toBe(i);
});
});
test("reset & deep reset", () => {
bin.add({width: 200, height: 100});
bin.add({width: 200, height: 100});
bin.add({width: 200, height: 100});
expect(bin.rects.length).toBe(3);
expect(bin.width).toBe(512);
bin.reset();
expect(bin.width).toBe(0);
expect(bin.freeRects.length).toBe(1);
let unpacked = bin.repack();
expect(unpacked).toBeUndefined();
expect(bin.width).toBe(512);
bin.reset(true);
expect(bin.width).toBe(0);
expect(bin.rects.length).toBe(0);
expect(bin.options.tag).toBe(true);
bin.reset(true, true);
expect(bin.options.tag).toBe(false);
});
test("repack", () => {
let rect1 = bin.add({width: 512, height: 512, id: "one"});
let rect2 = bin.add({width: 512, height: 512, id: "two"});
let rect3 = bin.add({width: 512, height: 512, id: "three"});
rect2.width = 1024;
rect2.height = 513;
let unpacked = bin.repack();
expect(unpacked.length).toBe(2);
expect(unpacked[0].id).toBe("one");
expect(unpacked[1].id).toBe("three");
expect(bin.rects.length).toBe(1);
});
test("monkey testing", () => {
let rects = [];
while (true) {
let width = Math.floor(Math.random() * 200);
let height = Math.floor(Math.random() * 200);
let rect = new Rectangle(width, height);
let position = bin.add(rect);
if (position) {
expect(position.width).toBe(width);
expect(position.height).toBe(height);
rects.push(position);
} else {
break;
}
}
expect(bin.width).toBeLessThanOrEqual(1024);
expect(bin.height).toBeLessThanOrEqual(1024);
rects.forEach(rect1 => {
// Make sure rects are not overlapping
rects.forEach(rect2 => {
if (rect1 !== rect2) {
expect(rect1.collide(rect2)).toBe(false, "intersection detected: " + JSON.stringify(rect1) + " " + JSON.stringify(rect2));
}
});
// Make sure no rect is outside bounds
expect(rect1.x + rect1.width).toBeLessThanOrEqual(bin.width);
expect(rect1.y + rect1.height).toBeLessThanOrEqual(bin.height);
});
});
});
let padding = 4;
describe("padding", () => {
beforeEach(() => {
bin = new MaxRectsBin(1024, 1024, padding, opt);
});
test("is initially empty", () => {
expect(bin.width).toBe(0);
expect(bin.height).toBe(0);
});
test("handles padding correctly", () => {
bin.add(512, 512, {});
bin.add(512 - padding, 512, {});
bin.add(512, 512 - padding, {});
expect(bin.width).toBe(1024);
expect(bin.height).toBe(1024);
expect(bin.rects.length).toBe(3);
});
test("adds rects with sizes close to the max", () => {
expect(bin.add(1024, 1024)).toBeDefined();
expect(bin.rects.length).toBe(1);
});
test("monkey testing", () => {
// bin = new MaxRectsBin(1024, 1024, 40);
let rects = [];
while (true) {
let width = Math.floor(Math.random() * 200);
let height = Math.floor(Math.random() * 200);
let rect = new Rectangle(width, height);
let position = bin.add(rect);
if (position) {
expect(position.width).toBe(width);
expect(position.height).toBe(height);
rects.push(position);
} else {
break;
}
}
expect(bin.width).toBeLessThanOrEqual(1024);
expect(bin.height).toBeLessThanOrEqual(1024);
rects.forEach(rect1 => {
// Make sure rects are not overlapping
rects.forEach(rect2 => {
if (rect1 !== rect2) {
try {
expect(rect1.collide(rect2)).toBe(false);
} catch (e) {
throw new Error("intersection detected: " + JSON.stringify(rect1) + " " + JSON.stringify(rect2));
}
}
});
// Make sure no rect is outside bounds
expect(rect1.x).toBeGreaterThanOrEqual(0);
expect(rect1.y).toBeGreaterThanOrEqual(0);
expect(rect1.x + rect1.width).toBeLessThanOrEqual(bin.width);
expect(rect1.y + rect1.height).toBeLessThanOrEqual(bin.height);
});
});
});
padding = 4;
let border = 5;
describe("border", () => {
beforeEach(() => {
const borderOpt = {...opt, ...{border: border, square: false}};
bin = new MaxRectsBin(1024, 1024, padding, borderOpt);
});
test("is initially empty", () => {
expect(bin.width).toBe(0);
expect(bin.height).toBe(0);
});
test("handles border & padding correctly", () => {
let size = 512 - border * 2; //
let pos1 = bin.add(size + 1, size, {});
expect(pos1.x).toBe(5);
expect(pos1.y).toBe(5);
expect(bin.width).toBe(1024);
expect(bin.height).toBe(512);
let pos2 = bin.add(size, size, {});
expect(pos2.x - pos1.x - pos1.width).toBe(padding); // handle space correctly
expect(pos2.y).toBe(border);
expect(bin.width).toBe(1024);
expect(bin.height).toBe(512);
bin.add(size, size, {});
bin.add(512, 508, {});
expect(bin.width).toBe(1024);
expect(bin.height).toBe(1024);
expect(bin.rects.length).toBe(3);
});
test("adds rects with sizes close to the max", () => {
expect(bin.add(1024, 1024)).toBeUndefined();
expect(bin.rects.length).toBe(0);
});
let repeat = 5;
test(`super monkey testing (${repeat} loop)`, () => {
while (repeat > 0) {
padding = Math.floor(Math.random() * 10);
border = Math.floor(Math.random() * 20);
const borderOpt = {...opt, ...{border: border, square: false}};
bin = new MaxRectsBin(1024, 1024, padding, borderOpt);
let rects = [];
while (true) {
let width = Math.floor(Math.random() * 200);
let height = Math.floor(Math.random() * 200);
let rect = new Rectangle(width, height);
let position = bin.add(rect);
if (position) {
expect(position.width).toBe(width);
expect(position.height).toBe(height);
rects.push(position);
} else {
break;
}
}
expect(bin.width).toBeLessThanOrEqual(1024);
expect(bin.height).toBeLessThanOrEqual(1024);
rects.forEach(rect1 => {
// Make sure rects are not overlapping
rects.forEach(rect2 => {
if (rect1 !== rect2) {
try {
expect(rect1.collide(rect2)).toBe(false);
} catch (e) {
throw new Error("intersection detected: " + JSON.stringify(rect1) + " " + JSON.stringify(rect2));
}
}
});
// Make sure no rect is outside bounds
expect(rect1.x).toBeGreaterThanOrEqual(bin.options.border);
expect(rect1.y).toBeGreaterThanOrEqual(bin.options.border);
expect(rect1.x + rect1.width).toBeLessThanOrEqual(bin.width - bin.options.border);
expect(rect1.y + rect1.height).toBeLessThanOrEqual(bin.height - bin.options.border);
});
repeat --;
}
});
});