def test_point_ordering(points): for p, q in zip(*points): pp, pq = Point(*p), Point(*q) assert (pp < pq) == (p < q) assert (pp <= pq) == (p <= q) assert (pp > pq) == (p > q) assert (pp >= pq) == (p >= q)
def test_rectangle_centered_at(): x, y = 1, 2 radius = 2 rect_centered_at_xy = Rectangle.centered_at(Point(x, y), radius) assert rect_centered_at_xy.area == (2 * radius)**2 assert rect_centered_at_xy.width == 2 * radius assert rect_centered_at_xy.height == 2 * radius assert rect_centered_at_xy == Rectangle.from_list( (x - radius, y - radius, x + radius, y + radius)) with pytest.raises(ValueError): Rectangle.centered_at(Point(1, 2), -1)
def test_rectangle_from_many_representations(): x1, y1 = 0, 1 x2, y2 = 2, 3 r1 = Rectangle.from_list((x1, y1, x2, y2)) r2 = Rectangle(Point(x1, y1), Point(x2, y2)) r3 = Rectangle.from_list([x1, y1, x2, y2]) r4 = Rectangle.from_list([x1, y2, x2, y1]) r5 = Rectangle.from_list([x2, y2, x1, y1]) rects = [r1, r2, r3, r4, r5] for ri, rj in it.product(rects, repeat=2): assert ri == rj r_different = Rectangle.from_list([y1, x1, x2, y2]) for r in rects: assert r != r_different
def test_rectangle_contains(): r_large = Rectangle.from_list([0, 0, 5, 5]) r_small = Rectangle.from_list([1, 1, 2, 2]) r_mid = Rectangle.from_list([-1, -1, 3, 3]) r_touching = Rectangle.from_list([0, 0, 4, 5]) assert r_small in r_small # pylint: disable=comparison-with-itself assert r_small in r_large assert r_mid not in r_large assert r_touching in r_large assert r_small in r_mid assert r_mid not in r_touching assert r_small in r_touching assert Point(0, 0) in r_large assert Point(0, 0) not in r_small assert Point(1, 1) in r_large assert Point(1, 1) in r_small
def test_rectangle_centered_at(): x, y = 1, 2 radius = 2 rect_centered_at_xy = Rectangle.centered_at(Point(x, y), radius) assert rect_centered_at_xy.area == (2 * radius) ** 2 assert rect_centered_at_xy.width == 2 * radius assert rect_centered_at_xy.height == 2 * radius assert rect_centered_at_xy == Rectangle.from_list((x - radius, y - radius, x + radius, y + radius))
def test_rectangle_properties(): x1, y1 = 0, 1 x2, y2 = 2, 3 w = x2 - x1 h = y2 - y1 r = Rectangle.from_list((x1, y1, x2, y2)) assert r.x == x1 assert r.y == y1 assert r.center == Point((x1 + x2) / 2, (y1 + y2) / 2) assert r.width == w assert r.height == h assert r.area == w * h assert r.bounds == (0, 1, 2, 3)
def test_point_unpacking(): x, y = Point(3, 4) assert x == 3 assert y == 4
def test_point_zero(): a = Point.zero() b = Point.zero() assert a + b == Point(0, 0)
def test_point_equality(points): for p, q in zip(*points): pp, pq = Point(*p), Point(*q) assert (pp == pq) == (p == q) assert (pp != pq) == (p != q)
def test_rectangle_unpacking(): x, y, w, h = Rectangle(Point(3, 4), Point(5, 7)) assert x == 3 assert y == 4 assert w == 2 assert h == 3
def test_rectangle_bounding_box(): r1 = Rectangle(Point(0, 1), Point(2, 3)) r2 = Rectangle(Point(0, 0), Point(4, 2)) assert Rectangle.bounding_box([r1, r2]) == Rectangle(Point(0, 0), Point(4, 3))
def test_rectangle_sub(): r_large = Rectangle.from_list([0, 0, 5, 5]) r_moved = r_large - Point(1, 2) assert r_moved.bounds == (-1, -2, 4, 3)
def test_rectangle_add(): r_large = Rectangle.from_list([0, 0, 5, 5]) r_moved = r_large + Point(1, 2) assert r_moved.bounds == (1, 2, 6, 7)