コード例 #1
0
    def test_iter_points(self):
        start = Point2D(1, 2)
        end = Point2D(3, -2)
        grid = Grid(4, 4)
        points = list(grid.iter_points(start, end))

        P = Point2D
        self.assertEqual(points, [
            P(1, 2),
            P(1.5, 2),
            P(2, 2),
            P(2.5, 2),
            P(3, 2),
            P(1, 1),
            P(1.5, 1),
            P(2, 1),
            P(2.5, 1),
            P(3, 1),
            P(1, 0),
            P(1.5, 0),
            P(2, 0),
            P(2.5, 0),
            P(3, 0),
            P(1, -1),
            P(1.5, -1),
            P(2, -1),
            P(2.5, -1),
            P(3, -1),
            P(1, -2),
            P(1.5, -2),
            P(2, -2),
            P(2.5, -2),
            P(3, -2),
        ])
コード例 #2
0
    def test_ceil(self):
        p1 = Point2D(2.3, 3.7)
        p2 = Point2D(-2.3, -3.7)
        res1 = math.ceil(p1)
        res2 = math.ceil(p2)

        self.assertEqual(res1, (3, 4))
        self.assertEqual(res2, (-2, -3))
コード例 #3
0
    def test_floor(self):
        p1 = Point2D(2.3, 3.7)
        p2 = Point2D(-2.3, -3.7)
        res1 = math.floor(p1)
        res2 = math.floor(p2)

        self.assertEqual(res1, (2, 3))
        self.assertEqual(res2, (-3, -4))
コード例 #4
0
    def test_trunc(self):
        p1 = Point2D(2.3, 3.7)
        p2 = Point2D(-2.3, -3.7)
        res1 = math.trunc(p1)
        res2 = math.trunc(p2)

        self.assertEqual(res1, (2, 3))
        self.assertEqual(res2, (-2, -3))
コード例 #5
0
    def test_round(self):
        p1 = Point2D(2.3, 3.7)
        p2 = Point2D(-2.3, -3.7)
        res1 = round(p1)
        res2 = round(p2)

        self.assertEqual(res1, (2, 4))
        self.assertEqual(res2, (-2, -4))
コード例 #6
0
class AreaTests(unittest.TestCase):

    MIN = Point2D(-1, -1)
    MAX = Point2D(1, 1)

    def test_from_radius(self):
        Area.from_radius()

    def test_from_sides(self):
        # Note that MAX.x was passed as x1.
        area = Area.from_sides(self.MAX.x, self.MIN.x, self.MIN.y, self.MAX.y)
        pmin, pmax = area.min, area.max

        self.assertEqual(pmin, self.MIN)
        self.assertEqual(pmax, self.MAX)

    def test_reorder(self):
        area1 = Area(self.MIN, self.MAX)
        area2 = Area(self.MAX, self.MIN)

        self.assertEqual(area1, area2)

    def test_coerce_tuples(self):
        area = Area(tuple(self.MIN), tuple(self.MAX))
        pmin, pmax = area

        self.assertEqual(pmin, self.MIN)
        self.assertEqual(pmax, self.MAX)

    def test_coerce_strings(self):
        area = Area(str(self.MIN), str(self.MAX))
        pmin, pmax = area

        self.assertEqual(pmin, self.MIN)
        self.assertEqual(pmax, self.MAX)

    def test_delta(self):
        area = Area(self.MIN, self.MAX)
        delta = area.delta

        self.assertEqual(delta, (2, 2))

    def test_namedtuple(self):
        area = Area(self.MIN, self.MAX)
        triple = tuple(v for v in area)
        pmin, pmax = area.min, area.max
        fields = area._fields
        a0, a1 = area[0], area[1]

        self.assertEqual(triple, (self.MIN, self.MAX))
        self.assertEqual(pmin, self.MIN)
        self.assertEqual(pmax, self.MAX)
        self.assertEqual(fields, ('min', 'max'))
        self.assertEqual(a0, self.MIN)
        self.assertEqual(a1, self.MAX)
コード例 #7
0
 def test_from_raw_unsupported(self):
     tests = [
         '(1,1',
         object(),
         [],
         (1, ),
         (1, 1, 1),
     ]
     for raw in tests:
         with self.subTest(repr(raw)):
             with self.assertRaises(ValueError):
                 Point2D.from_raw(raw)
コード例 #8
0
    def test_negate(self):
        pa = Point2D(1, 1)
        pb = Point2D(1, -1)
        pc = Point2D(-1, -1)
        pd = Point2D(-1, 1)
        nega = -pa
        negb = -pb
        negc = -pc
        negd = -pd

        self.assertEqual(nega, pc)
        self.assertEqual(negb, pd)
        self.assertEqual(negc, pa)
        self.assertEqual(negd, pb)
コード例 #9
0
    def test_floordiv(self):
        point = Point2D(2, 3)
        tests = [
            (Point2D(1, 1), (2, 3)),
            ((1, 1), (2, 3)),
            ((0.4, -6.0), (4.0, -1.0)),
            (1, (2, 3)),
            (1.0, (2, 3)),
            (ns(x=1, y=1), (2, 3)),
        ]
        for other, expected in tests:
            with self.subTest(other):
                result = point // other

                self.assertEqual(result, expected)
コード例 #10
0
    def test_multiply(self):
        point = Point2D(2, 3)
        tests = [
            (Point2D(1, 1), (2, 3)),
            ((1, 1), (2, 3)),
            ((0.5, 4.5), (1, 13.5)),
            (1, (2, 3)),
            (1.0, (2, 3)),
            (ns(x=1, y=1), (2, 3)),
        ]
        for other, expected in tests:
            with self.subTest(other):
                result = point * other

                self.assertEqual(result, expected)
コード例 #11
0
    def test_subtract(self):
        point = Point2D(2, 3)
        tests = [
            (Point2D(1, 1), (1, 2)),
            ((1, 1), (1, 2)),
            ((0.5, 4.5), (1.5, -1.5)),
            (1, (1, 2)),
            (1.0, (1, 2)),
            (ns(x=1, y=1), (1, 2)),
        ]
        for other, expected in tests:
            with self.subTest(other):
                result = point - other

                self.assertEqual(result, expected)
コード例 #12
0
    def test_divide(self):
        point = Point2D(2, 3)
        tests = [
            (Point2D(1, 1), (2, 3)),
            ((1, 1), (2, 3)),
            ((0.5, 6.0), (4, 0.5)),
            (1, (2, 3)),
            (1.0, (2, 3)),
            (ns(x=1, y=1), (2, 3)),
        ]
        for other, expected in tests:
            with self.subTest(other):
                result = point / other

                self.assertEqual(result, expected)
コード例 #13
0
    def test_complex(self):
        for a in [-1, -0.5, 0, 0.5, 1]:
            for b in [-1, -0.5, 0, 0.5, 1]:
                with self.subTest((a, b)):
                    p = Point2D(a, b)
                    c = complex(p)

                    self.assertEqual(c, a + b * 1j)
コード例 #14
0
 def test_parse_unsupported(self):
     tests = [
         # malformed pairs
         '(1,1',
         '1,1)',
         '[1,1',
         '1,1]',
         # unsupported pairs
         '1 1',
         '(1 1)',
         '[1 1]',
         '(,)',
         '(1,)',
         '(,1)',
         '(spam,spam)',
         # extra numbers
         '1,1,1',
         # not pairs
         '1',
         '1.0',
         'spam',
         # malformed complex
         '1+1jj',
         'x+1j',
         # non-strings
         object(),
         None,
         True,
         False,
         0,
         1,
         1.0,
         [1, 1],
         (1, 1),
         {
             1: 1
         },
     ]
     for raw in tests:
         with self.subTest(repr(raw)):
             with self.assertRaises(ValueError):
                 Point2D.parse(raw)
コード例 #15
0
    def test_namedtuple(self):
        p = Point2D(1.0, 1.0)
        pair = tuple(v for v in p)
        x, y = p.x, p.y
        fields = p._fields
        p0, p1 = p[0], p[1]

        self.assertEqual(pair, (1.0, 1.0))
        self.assertEqual(x, 1.0)
        self.assertEqual(y, 1.0)
        self.assertEqual(fields, ('x', 'y'))
        self.assertEqual(p0, 1.0)
        self.assertEqual(p1, 1.0)
コード例 #16
0
    def test_coerce_fields_supported(self):
        values = [
            1,
            1.0,
            True,
            '1',
            '1.0',
        ]
        for val in values:
            with self.subTest(repr(val)):
                x, y = Point2D(val, val)

                self.assertEqual(x, 1.0)
                self.assertEqual(y, 1.0)
コード例 #17
0
 def test_coerce_fields_unsupported(self):
     values = [
         '',
         1 + 1j,
         '1+1j',
         'spam',
         (),
         [],
         {},
     ]
     for val in values:
         with self.subTest(repr(val)):
             exc = ValueError if isinstance(val, str) else TypeError
             with self.assertRaises(exc):
                 Point2D(val, val)
コード例 #18
0
    def test_abs(self):  # also distance from origin
        tests = {
            (0, 0): 0,
            (1, 0): 1,
            (0, 1): 1,
            (-1, 0): 1,
            (1, 1): 2**0.5,
            (3, 4): 5,
        }
        for p, expected in tests.items():
            with self.subTest(p):
                p = Point2D(*p)
                result = abs(p)

                self.assertEqual(result, expected)
コード例 #19
0
    def test_quadrance(self):
        tests = {
            (0, 0): 0,
            (1, 0): 1,
            (0, 1): 1,
            (-1, 0): 1,
            (1, 1): 2,
            (3, 4): 25,
        }
        for p, expected in tests.items():
            with self.subTest(p):
                p = Point2D(*p)
                result = p.quadrance

                self.assertEqual(result, expected)
コード例 #20
0
    def test_from_raw_supported(self):
        tests = {
            None:
            None,
            Point2D():
            None,
            '1,1':
            Point2D(1, 1),
            1:
            Point2D(1, 0),
            # 1.0 collids with 1, so we check separately below.
            #1.0: Point2D(1.0, 0),
            1 + 1j:
            Point2D(1, 1),
            (1, 1):
            Point2D(1, 1),
        }
        for raw, expected in tests.items():
            with self.subTest(repr(raw)):
                p = Point2D.from_raw(raw)
                self.assertEqual(p, expected or raw)

        p = Point2D.from_raw(1.0)
        self.assertEqual(p, Point2D(1.0, 0))
コード例 #21
0
    def test_defaults(self):
        x, y = Point2D()

        self.assertEqual(x, 0.0)
        self.assertEqual(y, 0.0)
コード例 #22
0
 def test_validation(self):
     with self.assertRaises(TypeError):
         Point2D(None, 1)
     with self.assertRaises(TypeError):
         Point2D(1, None)
コード例 #23
0
    def test_str(self):
        ptstr = str(Point2D(1.0, 1.0))

        self.assertEqual(ptstr, '(1.0, 1.0)')
コード例 #24
0
 def test_parse_supported(self):
     tests = {
         '1, 1': Point2D(1, 1),
         '1,1': Point2D(1, 1),
         '(1, 1)': Point2D(1, 1),
         '(1,1)': Point2D(1, 1),
         '[1, 1]': Point2D(1, 1),
         '[1,1]': Point2D(1, 1),
         '1.0,1.0': Point2D(1.0, 1.0),
         '1+1i': Point2D(1, 1),
         '1+1j': Point2D(1, 1),
         '-1,-1': Point2D(-1, -1),
     }
     for raw, expected in tests.items():
         with self.subTest(repr(raw)):
             p = Point2D.parse(raw)
             self.assertEqual(p, expected)
コード例 #25
0
    def test_parse_empty(self):
        p = Point2D.parse('')

        self.assertEqual(p, Point2D())
コード例 #26
0
    def test_invert(self):
        point = Point2D(2, 3)
        inv = ~point

        self.assertEqual(inv, (3, 2))