-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
200 lines (186 loc) · 8.47 KB
/
test.py
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
import unittest
from guillotineopt import layout2d
class TestCase(unittest.TestCase):
def test_simple(self):
self.maxDiff = None
rect = {"size": (10, 10)}
res = layout2d([rect], (10, 10))
self.assertIn(res["along"], (0, 1))
self.assertEqual(0, rect["amount"])
if res["along"] == 0:
self.assertDictEqual(
{"along": 0, "elements": [{"type": 2, "rect": rect, "size": 10}]},
res)
else:
self.assertDictEqual(
{"along": 1, "elements": [{"type": 2, "rect": rect, "size": 10}]},
res)
# two identical rectangles along x axis
rect = {"size": (5, 10), "amount": 2}
self.assertDictEqual(
{"along": 0, "elements": [{"type": 2, "rect": rect, "size": 5},
{"type": 1, "size": 0},
{"type": 2, "rect": rect, "size": 5}]},
layout2d([rect], (10, 10)))
def test_rectangle_with_remain(self):
self.maxDiff = None
rect = {"size": (5, 10), "amount": 2}
self.assertDictEqual(
{"along": 0, "elements": [{"type": 2, "rect": rect, "size": 5},
{"type": 1, "size": 0},
{"type": 2, "rect": rect, "size": 5},
{"type": 1, "size": 0},
{"type": 0, "size": 2},
]},
layout2d([rect], (12, 10)))
self.assertEqual(0, rect["amount"])
def test_rectangle_with_remains(self):
self.maxDiff = None
rect = {"size": (5, 10), "amount": 2}
det_layout = {"along": 0, "elements": [{"type": 2, "rect": rect, "size": 5},
{"type": 1, "size": 0},
{"type": 2, "rect": rect, "size": 5},
]}
left_layout = {"along": 1, "elements": [
{"type": 3, "size": 10, "layout": det_layout},
{"type": 1, "size": 0},
{"type": 0, "size": 1}]}
self.assertDictEqual(
{"along": 0, "elements": [
{"type": 3, "size": 10, "layout": left_layout},
{"type": 1, "size": 0},
{"type": 0, "size": 2}
]
},
layout2d([rect], (12, 11)))
self.assertEqual(0, rect["amount"])
def test_skinny_detail(self):
# +-+---------+
# + + 2 +
# + + +
# +1+---------+
# + + 2 +
# + + +
# + +---------+
# +-+---------+
rect1 = {"size": (1, 10)}
rect2 = {"size": (9, 4), "amount": 2}
res = layout2d([rect1, rect2], (10, 10))
self.maxDiff = None
self.assertDictEqual(
{"along": 0, "elements": [
{"type": 2, "rect": rect1, "size": 1},
{"type": 1, "size": 0},
{"type": 3, "layout": {
"along": 1, "elements": [
{"type": 2, "rect": rect2, "size": 4},
{"type": 1, "size": 0},
{"type": 2, "rect": rect2, "size": 4},
{"type": 1, "size": 0},
{"type": 0, "size": 2},
]
}, "size": 9}]},
res)
self.assertEqual(0, rect1["amount"])
self.assertEqual(0, rect2["amount"])
def test_consumption_bug(self):
rect1 = {"size": (1, 10)}
rect2 = {"size": (9, 4)}
res = layout2d([rect1, rect2], (10, 10))
self.maxDiff = None
self.assertDictEqual(
{"along": 0, "elements": [
{"type": 2, "rect": rect1, "size": 1},
{"type": 1, "size": 0},
{"type": 3, "layout": {
"along": 1, "elements": [
{"type": 2, "rect": rect2, "size": 4},
{"type": 1, "size": 0},
{"type": 0, "size": 6},
]
}, "size": 9}]},
res)
self.assertEqual(0, rect1["amount"])
self.assertEqual(0, rect2["amount"])
def test_rects_with_one_same_size(self):
rect1 = {"size": (2, 4)}
rect2 = {"size": (3, 4)}
res = layout2d([rect1, rect2], (5, 4))
self.assertEqual(0, rect1["amount"])
self.assertEqual(0, rect2["amount"])
def test_duplicate_details(self):
rect1 = {"size": (4, 10), "name": "rect1"}
rect2 = {"size": (4, 10), "name": "rect2"}
self.assertDictEqual(
{"along": 0, "elements": [{"type": 2, "rect": rect1, "size": 4},
{"type": 1, "size": 0},
{"type": 2, "rect": rect2, "size": 4}]},
layout2d([rect1, rect2], (8, 10)))
self.assertEqual(0, rect1["amount"])
self.assertEqual(0, rect2["amount"])
def test_large_parts(self):
rect1 = {"size": (30, 30), "amount": 1}
rect2 = {"size": (10, 5), "amount": 3}
res = layout2d([rect1, rect2], (11, 5))
self.assertDictEqual({"along": 0, "elements": [{"type": 2, "rect": rect2, "size": 10},
{"type": 1, "size": 0},
{"type": 0, "size": 1}]},
res)
self.assertEqual(1, rect1["amount"])
self.assertEqual(2, rect2["amount"])
def test_zero_parts(self):
rect1 = {"size": (10, 5), "amount": 0}
rect2 = {"size": (5, 5), "amount": 3}
res = layout2d([rect1, rect2], (10, 5))
self.assertDictEqual({"along": 0, "elements": [{"type": 2, "rect": rect2, "size": 5},
{"type": 1, "size": 0},
{"type": 2, "rect": rect2, "size": 5}]},
res)
self.assertEqual(0, rect1["amount"])
self.assertEqual(1, rect2["amount"])
def test_transposed_sublayout(self):
rect1 = {"size": (3, 9)}
rect2 = {"size": (7, 5), "amount": 2}
rect3 = {"size": (2, 2)}
res = layout2d([rect1, rect2, rect3], (10, 11))
self.assertEqual(0, rect1["amount"])
self.assertEqual(0, rect2["amount"])
self.assertEqual(0, rect3["amount"])
self.maxDiff = None
left_bottom_layout = {
"along": 0, "elements": [{"type": 2, "rect": rect3, "size": 2},
{"type": 1, "size": 0},
{"type": 0, "size": 1}]}
left_layout = {"along": 1, "elements": [{"type": 2, "rect": rect1, "size": 9},
{"type": 1, "size": 0},
{"type": 3, "layout": left_bottom_layout, "size": 2}]}
right_layout = {
"along": 1, "elements": [{"type": 2, "rect": rect2, "size": 5},
{"type": 1, "size": 0},
{"type": 2, "rect": rect2, "size": 5},
{"type": 1, "size": 0},
{"type": 0, "size": 1}]}
self.assertDictEqual(
{"along": 0, "elements": [{"type": 3, "layout": left_layout, "size": 3},
{"type": 1, "size": 0},
{"type": 3, "layout": right_layout, "size": 7}]},
res)
def test_cut_size(self):
rect = {"size": (2, 4), "amount": 2}
res = layout2d([rect], (7, 4), cut_size=2)
self.assertDictEqual(
{"along": 0, "elements": [{"type": 2, "rect": rect, "size": 2},
{"type": 1, "size": 2},
{"type": 2, "rect": rect, "size": 2},
{"type": 1, "size": 1}]},
res)
def test_detail_rotate(self):
rect1 = {"size": (4, 2), "can_rotate": True}
rect2 = {"size": (2, 4)}
res = layout2d([rect1, rect2], (2, 8))
self.assertDictEqual(
{"along": 1, "elements": [{"type": 2, "rect": rect2, "size": 4},
{"type": 1, "size": 0},
{"type": 2, "rect": rect1, "size": 4},
]},
res)