build123d/tests/test_pack.py
Ami Fischman dd649308c1 pack(): update to support Shape, not just Part.
Also fix SlotOverall so that passing it equal height and width doesn't
error out and instead reduces to a Circle. (bug discovered while
writing the new test case).
2023-11-10 09:07:54 -08:00

58 lines
2.3 KiB
Python

"""
build123d Pack tests
name: build_pack.py
by: fischman
date: November 9th 2023
desc: Unit tests for the build123d pack module
"""
import operator
import random
import unittest
from functools import reduce
from build123d import *
class TestPack(unittest.TestCase):
"""Tests for the pack helper."""
def test_simple(self):
"""Test pack with hand-picked data against expected output."""
packed = pack([Box(10, 2, 1), Box(1, 5, 1), Box(1, 5, 1)], padding=1)
self.assertEqual(
# Nothing magically interesting here, and other packings
# would also be fine, but this shows that padding is
# working, as is the preference towards square(ish)
# output.
"[bbox: 0.0 <= x <= 10.0, 0.0 <= y <= 2.0, -0.5 <= z <= 0.5,"
" bbox: 0.0 <= x <= 1.0, 3.0 <= y <= 8.0, -0.5 <= z <= 0.5,"
" bbox: 2.0 <= x <= 3.0, 3.0 <= y <= 8.0, -0.5 <= z <= 0.5]",
str([p.bounding_box() for p in packed]))
def test_random_boxes(self):
"""Test pack with larger (and randomized) inputs."""
random.seed(123456)
# 50 is an arbitrary number that is large enough to exercise
# different aspects of the packer while still completing quickly.
test_boxes = [Box(random.randint(1, 20), random.randint(1, 20), 1)
for _ in range(50)]
# Not raising in this call shows successfull non-overlap.
packed = pack(test_boxes, 1)
self.assertEqual(
"bbox: 0.0 <= x <= 94.0, 0.0 <= y <= 86.0, -0.5 <= z <= 0.5",
str(reduce(operator.add, packed, Part()).bounding_box()))
def test_random_slots(self):
"""Test pack for 2D objects."""
random.seed(123456)
# 50 is an arbitrary number that is large enough to exercise
# different aspects of the packer while still completing quickly.
inputs = [SlotOverall(random.randint(1,20), random.randint(1,20)) for _ in range(50)]
# Not raising in this call shows successfull non-overlap.
packed = pack(inputs, 1)
self.assertEqual(
"bbox: 0.0 <= x <= 124.0, 0.0 <= y <= 105.0, 0.0 <= z <= 0.0",
str(reduce(operator.add, packed, Sketch()).bounding_box()))
if __name__ == "__main__":
unittest.main()