def test_all_rectangles(self): a = Area(Rectangle(Field(0, 0), Field(127, 127)), 4) original = set() for i in range(0, 100): rect = Rectangle(Field(i, i), Field(i, i)) original.add(rect) a += rect result = a.all_rectangles() self.assertEqual(original, result)
def test_add_multiple_list(self): a = Area(Rectangle(Field(0, 0), Field(15, 15)), 1) rectangles = [ Rectangle(Field(2, 2), Field(3, 3)), Rectangle(Field(2, 5), Field(3, 5)), Rectangle(Field(5, 2), Field(6, 3)), Rectangle(Field(5, 5), Field(6, 6)) ] a += rectangles # Check self.assertEqual(set(rectangles), a.all_rectangles())
def test_all_rectangles_complex(self): a = Area(Rectangle(Field(0, 0), Field(511, 511)), 4) original = set() for i in range(1, 20): for j in range(1, 20): rect = (Rectangle(Field(i * 6, j * 6), Field(i * 6 + 2, j * 6 + 2))) original.add(rect) a += rect result = a.all_rectangles() self.assertEqual(original, result)
def test_rectangles_in_contact(self): a = Area(Rectangle(Field(0, 0), Field(31, 31)), 2) target = Rectangle(Field(5, 5), Field(10, 10)) a += Rectangle(Field(10, 10), Field(30, 30)) a += Rectangle(Field(0, 0), Field(6, 6)) a += Rectangle(Field(7, 0), Field(9, 9)) a += Rectangle(Field(0, 9), Field(5, 9)) self.assertTrue(a.rectangles_in_contact(target).__len__() is 4)
def test_root(self): a = Area(Rectangle(Field(0, 0), Field(1023, 1023)), 8) other = Area(Rectangle(Field(0, 0), Field(1023, 1023)), 8) for child in a.children: while child.is_subindexed(): child = child.bottom_left self.assertEquals(child.get_root(), a) # Abusive self.assertNotEqual(child.get_root(), child) self.assertNotEqual(child.get_root(), child.parent) self.assertNotEqual(child.get_root(), other)
def gen_area(self): a = Area(Rectangle(Field(0, 0), Field(7, 7)), 1) rectangles = [ Rectangle(Field(2, 2), Field(3, 3), type=FieldType.inaccessible), Rectangle(Field(2, 5), Field(3, 6), type=FieldType.inaccessible), Rectangle(Field(5, 2), Field(6, 3), type=FieldType.inaccessible), Rectangle(Field(5, 5), Field(6, 6), type=FieldType.inaccessible) ] a.main.size = MagicMock(return_value=64) a.all_rectangles = MagicMock(return_value=set(rectangles)) a += rectangles # TODO: Mocking should work without this instruction return a
def test_simple(self): dim = 64 a = Area(Rectangle(Field(0, 0), Field(dim - 1, dim - 1)), 1) load_from_file("../maps/4", a, 'x') pass
def test_is_free_corner(self): a = Area(Rectangle(Field(0, 0), Field(7, 7)), 1) r = Rectangle(Field(2, 1), Field(5, 5)) for c in r.corners(): with self.assertRaises(Exception): c.is_free_corner() a += r for c in r.corners(): self.assertTrue(c.is_free_corner()) # We will add a new rectangle which is in contact with the former r2 = Rectangle(Field(1, 6), Field(5, 7)) a += r2 self.assertTrue(r.bottom_left.is_free_corner()) self.assertTrue(r.bottom_right.is_free_corner()) # Not free anymore self.assertFalse(r.top_left.is_free_corner()) self.assertFalse(r.top_right.is_free_corner()) # Now for the second self.assertFalse(r2.bottom_left.is_free_corner()) self.assertFalse(r2.bottom_right.is_free_corner()) self.assertTrue(r2.top_left.is_free_corner()) self.assertTrue(r2.top_right.is_free_corner())
def test_place(self): start = Field(3, 3) a = Area(Rectangle(Field(0, 0), Field(15, 15)), 2) w = Agent(a, start, RandomBehaviour()) self.assertTrue(w.position, start) bad_start = Field(323233223, 3322332) with self.assertRaises(Exception): w.position(bad_start)
def test_remove3(self): a = Area(Rectangle(Field(0, 0), Field(15, 15)), 3) r = Rectangle(Field(8, 10), Field(10, 11)) self.assertFalse(r in a.top_right.bottom_left.rectangles) a += r self.assertTrue(r in a.top_right.bottom_left.rectangles) a -= r self.assertFalse(r in a.top_right.bottom_left.rectangles)
def test_remove2(self): a = Area(Rectangle(Field(0, 0), Field(15, 15)), 2) r = Rectangle(Field(0, 0), Field(6, 6)) self.assertFalse(r in a.bottom_left.rectangles) a += r self.assertTrue(r in a.bottom_left.rectangles) a -= r self.assertFalse(r in a.bottom_left.rectangles)
def test_remove(self): a = Area(Rectangle(Field(0, 0), Field(7, 7)), 1) r = Rectangle(Field(0, 0), Field(4, 4)) self.assertFalse(r in a.rectangles) a += r self.assertTrue(r in a.rectangles) a -= r self.assertFalse(r in a.rectangles)
def test_init_subindexes(self): a = Area(Rectangle(Field(0, 0), Field(7, 7)), 2) self.assertTrue( a.bottom_left.main == Rectangle(Field(0, 0), Field(3, 3))) self.assertTrue( a.bottom_right.main == Rectangle(Field(4, 0), Field(7, 3))) self.assertTrue(a.top_left.main == Rectangle(Field(0, 4), Field(3, 7))) self.assertTrue( a.top_right.main == Rectangle(Field(4, 4), Field(7, 7)))
def area(): a = Area.of_size(64) rectangles = [ Rectangle(Field(50, 0), Field(40, 50), type=FieldType.inaccessible), ] a += rectangles return a
def test_init_area(self): a = Area(Rectangle(Field(0, 0), Field(31, 31)), 2) rect = Rectangle(Field(5, 5), Field(10, 10)) a += rect for c in rect.corners(): self.assertEqual(c.rectangle.area, a)
def area(): a = Area.of_size(16) rectangles = [ Rectangle(Field(3, 3), Field(12, 3), type=FieldType.inaccessible), Rectangle(Field(3, 12), Field(12, 12), type=FieldType.inaccessible), Rectangle(Field(7, 6), Field(8, 9), type=FieldType.inaccessible), ] a += rectangles return a
def test_init_subsubindexes(self): a = Area(Rectangle(Field(0, 0), Field(7, 7)), 3) self.assertTrue(a.bottom_left.bottom_left.main == Rectangle( Field(0, 0), Field(1, 1))) self.assertTrue(a.bottom_left.bottom_right.main == Rectangle( Field(2, 0), Field(3, 1))) self.assertTrue( a.bottom_left.top_left.main == Rectangle(Field(0, 2), Field(1, 3))) self.assertTrue(a.bottom_left.top_right.main == Rectangle( Field(2, 2), Field(3, 3)))
def area(): a = Area.of_size(8) rectangles = [ Rectangle(Field(0, 0), Field(0, 5), type=FieldType.inaccessible), Rectangle(Field(3, 0), Field(3, 5), type=FieldType.inaccessible), Rectangle(Field(4, 5), Field(6, 5), type=FieldType.inaccessible), Rectangle(Field(6, 3), Field(7, 3), type=FieldType.inaccessible) ] a += rectangles return a
def area(): a = Area.of_size(8) rectangles = [ Rectangle(Field(2, 2), Field(3, 3), type=FieldType.inaccessible), Rectangle(Field(2, 5), Field(3, 6), type=FieldType.inaccessible), Rectangle(Field(5, 2), Field(6, 3), type=FieldType.inaccessible), Rectangle(Field(5, 5), Field(6, 6), type=FieldType.inaccessible) ] a += rectangles return a
def test_add_multiple(self): a = Area(Rectangle(Field(0, 0), Field(15, 15)), 2) r = Rectangle(Field(6, 6), Field(10, 10)) self.assertFalse(r in a.bottom_left.rectangles) self.assertFalse(r in a.bottom_right.rectangles) self.assertFalse(r in a.top_left.rectangles) self.assertFalse(r in a.top_right.rectangles) a += r self.assertTrue(r in a.bottom_left.rectangles) self.assertTrue(r in a.bottom_right.rectangles) self.assertTrue(r in a.top_left.rectangles) self.assertTrue(r in a.top_right.rectangles)
def gen_area(self): # DO NOT CHANGE!!! a = Area(Rectangle(Field(0, 0), Field(15, 15)), 1) rectangles = [ Rectangle(Field(2, 2), Field(3, 3), type=FieldType.inaccessible), Rectangle(Field(2, 5), Field(3, 6), type=FieldType.inaccessible), Rectangle(Field(5, 2), Field(6, 3), type=FieldType.inaccessible), Rectangle(Field(6, 5), Field(6, 5), type=FieldType.inaccessible) ] a += rectangles return a
def test_add_coinciding(self): a = Area(Rectangle(Field(0, 0), Field(15, 15)), 2) base = Rectangle(Field(6, 6), Field(10, 10)) r1 = Rectangle(Field(6, 6), Field(10, 10)) r2 = Rectangle(Field(4, 4), Field(8, 8)) r3 = Rectangle(Field(10, 10), Field(13, 13)) rs = [r1, r2, r3] a += base for r in rs: with self.assertRaises(Exception): a += r
def test_init_area_big(self): a = Area(Rectangle(Field(0, 0), Field(1023, 1023)), 5) rect1 = Rectangle(Field(5, 5), Field(10, 10)) rect2 = Rectangle(Field(200, 500), Field(1001, 700)) rect3 = Rectangle(Field(201, 201), Field(201, 201)) rectangles = {rect1, rect2, rect3} for r in rectangles: a += r for c in r.corners(): self.assertEqual(c.rectangle.area, a)
def test_confine_edge(self): a = Area(Rectangle(Field(0, 0), Field(63, 63)), 4) # Corner rect = Rectangle(Field(0, 0), Field(0, 0), area=a) expected = Rectangle(Field(0, 0), Field(1, 1)) result = rect.confine() self.assertEqual(expected, result) # Bottom edge for i in range(1, 63): rect = Rectangle(Field(i, 0), Field(i, 0), area=a) expected = Rectangle(Field(i - 1, 0), Field(i + 1, 1)) result = rect.confine() self.assertEqual(expected, result) # Corner rect = Rectangle(Field(63, 0), Field(63, 0), area=a) expected = Rectangle(Field(62, 0), Field(63, 1)) result = rect.confine() self.assertEqual(expected, result) # Right edge for i in range(1, 63): rect = Rectangle(Field(63, i), Field(63, i), area=a) expected = Rectangle(Field(62, i - 1), Field(63, i + 1)) result = rect.confine() self.assertEqual(expected, result) # Corner rect = Rectangle(Field(63, 63), Field(63, 63), area=a) expected = Rectangle(Field(62, 62), Field(63, 63)) result = rect.confine() self.assertEqual(expected, result) # Top edge for i in range(1, 63): rect = Rectangle(Field(i, 63), Field(i, 63), area=a) expected = Rectangle(Field(i - 1, 62), Field(i + 1, 63)) result = rect.confine() self.assertEqual(expected, result) # Corner rect = Rectangle(Field(0, 63), Field(0, 63), area=a) expected = Rectangle(Field(0, 62), Field(1, 63)) result = rect.confine() self.assertEqual(expected, result) # Left edge for i in range(1, 63): rect = Rectangle(Field(0, i), Field(0, i), area=a) expected = Rectangle(Field(0, i - 1), Field(1, i + 1)) result = rect.confine() self.assertEqual(expected, result)
def test_valid(self): a = Area(Rectangle(Field(0, 0), Field(15, 15)), 1) rectangles = [ Rectangle(Field(2, 2), Field(3, 3)), Rectangle(Field(2, 5), Field(3, 6)), Rectangle(Field(5, 2), Field(6, 3)), Rectangle(Field(5, 5), Field(6, 6)) ] a += rectangles origin = Point(4, 4) test_cases = { Point(0, 0): False, Point(7, 0): False, Point(0, 7): False, Point(7, 7): False, Point(2, 2): False, Point(2, 6): False, Point(5, 6): False, Point(2, 9): False, Point(6, 10): False, Point(0, 4): True, Point(4, 0): True, Point(7, 4): True, Point(4, 7): True, Point(4, 4): True, Point(1, 4): True, Point(2, 4): True, Point(3, 4): True, Point(4, 7): True, Point(3, 9): True, Point(3, 10): True, Point(4, 10): True, Point(5, 10): True, Point(5, 2): False, } for target, expected in test_cases.items(): ray = Ray(origin, target, area=a) self.assertEqual(ray.valid(), expected)
def test_is_free_corner_diagonally(self): a = Area(Rectangle(Field(0, 0), Field(15, 15)), 1) base = Rectangle(Field(5, 5), Field(8, 8)) for c in base.corners(): with self.assertRaises(Exception): c.is_free_corner_diagonally() a += base for c in base.corners(): self.assertTrue(c.is_free_corner_diagonally()) r1 = Rectangle(Field(1, 1), Field(4, 4)) r2 = Rectangle(Field(9, 9), Field(13, 14)) r3 = Rectangle(Field(4, 9), Field(1, 12)) r4 = Rectangle(Field(6, 4), Field(10, 2)) rs = [r1, r2, r3, r4] for r in rs: # We will add a new rectangle which is in contact with the former a += r for c in base.corners(): if c.is_free_corner(): self.assertTrue(c.is_free_corner_diagonally()) else: self.assertFalse(not c.is_free_corner_diagonally()) # Now for the second for c in r.corners(): if c.is_free_corner(): self.assertTrue(c.is_free_corner_diagonally()) else: self.assertFalse(not c.is_free_corner_diagonally())
def test_complete(self): a = Area(Rectangle(Field(0, 0), Field(7, 7)), 1) rectangles = [ Rectangle(Field(2, 2), Field(3, 3), type=FieldType.inaccessible), Rectangle(Field(2, 5), Field(3, 6), type=FieldType.inaccessible), Rectangle(Field(5, 2), Field(6, 3), type=FieldType.inaccessible), Rectangle(Field(5, 5), Field(6, 6), type=FieldType.inaccessible) ] a += rectangles pm = RandomBehaviour() start = Field(4, 4) # Testing test_rectangle1 = Rectangle(Field(3, 3), Field(5, 5)) test_rectangle2 = Rectangle(Field(2, 2), Field(6, 6)) test_rectangle3 = Rectangle(Field(1, 1), Field(7, 7)) walker = Agent(a, start, pm) walker.step() self.assertTrue(walker.position in test_rectangle1) walker.step() self.assertTrue(walker.position in test_rectangle2) walker.step() self.assertTrue(walker.position in test_rectangle3)
def test_can_step(self): a = Area(Rectangle(Field(0, 0), Field(15, 15)), 2) w = Agent(a, Field(3, 3), RandomBehaviour()) a += Rectangle(Field(0, 0), Field(5, 2), type=FieldType.inaccessible) a += Rectangle(Field(0, 3), Field(2, 7), type=FieldType.inaccessible) positions = { (2, 4): False, (3, 4): True, (4, 4): True, (2, 3): False, (3, 3): True, (4, 3): True, (2, 2): False, (2, 3): False, (2, 4): False } for (x, y), val in positions.items(): self.assertEqual(w.can_step(Field(x, y)), val) w.change_position(Field(4, 4)) positions = { (3, 3): True, (4, 5): True, (5, 5): True, (3, 4): True, (4, 4): True, (5, 4): True, (3, 3): True, (3, 4): True, (3, 5): True } for (x, y), val in positions.items(): self.assertEqual(w.can_step(Field(x, y)), val)
def test_free_corners(self): a = Area(Rectangle(Field(0, 0), Field(63, 63)), 4) r = Rectangle(Field(2, 1), Field(5, 5)) for c in r.corners(): with self.assertRaises(Exception): c.is_free_corner() a += r for c in r.corners(): if c in r.free_corners(): self.assertTrue(c.is_free_corner()) else: self.assertFalse(c.is_free_corner()) # We will add a new rectangle which is in contact with the former r2 = Rectangle(Field(1, 6), Field(5, 7)) a += r2 for c in r.corners(): if c in r.free_corners(): self.assertTrue(c.is_free_corner()) else: self.assertFalse(c.is_free_corner()) # Second rectangle for c in r2.corners(): if c in r2.free_corners(): self.assertTrue(c.is_free_corner()) else: self.assertFalse(c.is_free_corner())
def test_join_area_type(self): a = Area(Rectangle(Field(0, 0), Field(63, 63)), 2) r1 = Rectangle(Field(3, 3), Field(5, 5), type=FieldType.inaccessible, area=None) r2 = Rectangle(Field(6, 6), Field(8, 8), type=FieldType.inaccessible, area=None) r3 = Rectangle(Field(6, 3), Field(8, 5), type=FieldType.inaccessible, area=None) r4 = Rectangle(Field(9, 3), Field(12, 5), type=FieldType.inaccessible, area=None) test = [(r1, r3), (r2, r3), (r3, r4)] for (first, second) in test: a.clear() a += first a += second result = first.join(second) self.assertEqual(result.area, a) self.assertEqual(result.type, FieldType.inaccessible) # Other way around a.clear() a += first a += second result = second.join(first) self.assertEqual(result.area, a) self.assertEqual(result.type, FieldType.inaccessible)