build123d/tests/test_pack.py
Ami Fischman aef4eb809d Speed up test_pack.py by fuse()ing once per test case.
A classic case of the win described by
https://build123d.readthedocs.io/en/latest/algebra_performance.html

Before, these test cases took 1.26s and 0.28s, making them the slowest
and 8th-slowest test cases in the entire repo according to `py.test
--durations=10`, and a full pytest run takes 12.31s.

After, these test cases both take 0.09s, and a full run takes 10.90s.

Also added `prof/` to `.gitignore` because using pytest's
`--profile{-svg}` options creates this directory but it is unlikely to
be usefully tracked by git.
2023-11-15 11:03:21 -08:00

58 lines
2.2 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((Part() + packed).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((Sketch() + packed).bounding_box()))
if __name__ == "__main__":
unittest.main()