From f5abd5b0bdc0b9f524456dc9216bd0f3732e82a0 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Tue, 28 Oct 2014 22:11:43 -0400 Subject: Add arc rendering and tests --- gerber/tests/test_primitives.py | 85 +++++++++++++++++++++++++++++++++++++++++ gerber/tests/tests.py | 5 ++- 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 gerber/tests/test_primitives.py (limited to 'gerber/tests') diff --git a/gerber/tests/test_primitives.py b/gerber/tests/test_primitives.py new file mode 100644 index 0000000..29036b4 --- /dev/null +++ b/gerber/tests/test_primitives.py @@ -0,0 +1,85 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# Author: Hamilton Kibbe +from ..primitives import * +from tests import * + + + +def test_line_angle(): + """ Test Line primitive angle calculation + """ + cases = [((0, 0), (1, 0), math.radians(0)), + ((0, 0), (1, 1), math.radians(45)), + ((0, 0), (0, 1), math.radians(90)), + ((0, 0), (-1, 1), math.radians(135)), + ((0, 0), (-1, 0), math.radians(180)), + ((0, 0), (-1, -1), math.radians(225)), + ((0, 0), (0, -1), math.radians(270)), + ((0, 0), (1, -1), math.radians(315)),] + for start, end, expected in cases: + l = Line(start, end, 0) + line_angle = (l.angle + 2 * math.pi) % (2 * math.pi) + assert_almost_equal(line_angle, expected) + +def test_line_bounds(): + """ Test Line primitive bounding box calculation + """ + cases = [((0, 0), (1, 1), ((0, 1), (0, 1))), + ((-1, -1), (1, 1), ((-1, 1), (-1, 1))), + ((1, 1), (-1, -1), ((-1, 1), (-1, 1))), + ((-1, 1), (1, -1), ((-1, 1), (-1, 1))),] + for start, end, expected in cases: + l = Line(start, end, 0) + assert_equal(l.bounding_box, expected) + +def test_arc_radius(): + """ Test Arc primitive radius calculation + """ + cases = [((-3, 4), (5, 0), (0, 0), 5), + ((0, 1), (1, 0), (0, 0), 1),] + + for start, end, center, radius in cases: + a = Arc(start, end, center, 'clockwise', 0) + assert_equal(a.radius, radius) + + +def test_arc_sweep_angle(): + """ Test Arc primitive sweep angle calculation + """ + cases = [((1, 0), (0, 1), (0, 0), 'counterclockwise', math.radians(90)), + ((1, 0), (0, 1), (0, 0), 'clockwise', math.radians(270)), + ((1, 0), (-1, 0), (0, 0), 'clockwise', math.radians(180)), + ((1, 0), (-1, 0), (0, 0), 'counterclockwise', math.radians(180)),] + + for start, end, center, direction, sweep in cases: + a = Arc(start, end, center, direction, 0) + assert_equal(a.sweep_angle, sweep) + + +def test_arc_bounds(): + """ Test Arc primitive bounding box calculation + """ + cases = [((1, 0), (0, 1), (0, 0), 'clockwise', ((-1, 1), (-1, 1))), + ((1, 0), (0, 1), (0, 0), 'counterclockwise', ((0, 1), (0, 1))), + #TODO: ADD MORE TEST CASES HERE + ] + + for start, end, center, direction, bounds in cases: + a = Arc(start, end, center, direction, 0) + assert_equal(a.bounding_box, bounds) + +def test_circle_radius(): + """ Test Circle primitive radius calculation + """ + c = Circle((1, 1), 2) + assert_equal(c.radius, 1) + +def test_circle_bounds(): + """ Test Circle bounding box calculation + """ + c = Circle((1, 1), 2) + assert_equal(c.bounding_box, ((0, 2), (0, 2))) + + diff --git a/gerber/tests/tests.py b/gerber/tests/tests.py index 29b7899..222eea3 100644 --- a/gerber/tests/tests.py +++ b/gerber/tests/tests.py @@ -7,6 +7,7 @@ from nose.tools import assert_in from nose.tools import assert_not_in from nose.tools import assert_equal from nose.tools import assert_not_equal +from nose.tools import assert_almost_equal from nose.tools import assert_true from nose.tools import assert_false from nose.tools import assert_raises @@ -14,5 +15,5 @@ from nose.tools import raises from nose import with_setup __all__ = ['assert_in', 'assert_not_in', 'assert_equal', 'assert_not_equal', - 'assert_true', 'assert_false', 'assert_raises', 'raises', - 'with_setup' ] + 'assert_almost_equal', 'assert_true', 'assert_false', + 'assert_raises', 'raises', 'with_setup' ] -- cgit