From 95b4ec59228da8ab987d9591bf75c379614afa8b Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Wed, 16 Nov 2011 20:39:01 -0500 Subject: [PATCH] wrote some unit tests! --- test/test_all.py | 14 +++++ test/test_dirtytiles.py | 120 ++++++++++++++++++++++++++++++++++++++++ test/test_tileobj.py | 63 +++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 test/test_all.py create mode 100644 test/test_dirtytiles.py create mode 100644 test/test_tileobj.py diff --git a/test/test_all.py b/test/test_all.py new file mode 100644 index 0000000..cc82f84 --- /dev/null +++ b/test/test_all.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +import unittest + +# For convenience +import sys,os +sys.path.insert(0, os.getcwd()) +sys.path.insert(0, os.path.join(os.getcwd(), os.pardir)) + +# Import unit test cases or suites here +from test_tileobj import TileTest +from test_dirtytiles import DirtyTilesTest + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_dirtytiles.py b/test/test_dirtytiles.py new file mode 100644 index 0000000..7a0cfc6 --- /dev/null +++ b/test/test_dirtytiles.py @@ -0,0 +1,120 @@ +import unittest + +from overviewer_core.quadtree import DirtyTiles, iterate_base4 + +class DirtyTilesTest(unittest.TestCase): + dirty_paths = frozenset([ + # Entire subtree 0/0 is dirty, nothing else under 0 + (0,0,0), + (0,0,1), + (0,0,2), + (0,0,3), + # A few tiles under quadrant 1 + (1,0,3), + (1,1,3), + (1,2,0), + # Entire subtree under quadrant 2 is dirty + (2,0,0), + (2,0,1), + (2,0,2), + (2,0,3), + (2,1,0), + (2,1,1), + (2,1,2), + (2,1,3), + (2,2,0), + (2,2,1), + (2,2,2), + (2,2,3), + (2,3,0), + (2,3,1), + (2,3,2), + (2,3,3), + # Nothing under quadrant 3 + ]) + + def setUp(self): + self.tree = DirtyTiles(3) + for t in self.dirty_paths: + self.tree.set_dirty(t) + + def test_query(self): + """Make sure the correct tiles are marked as dirty""" + for path in iterate_base4(3): + if path in self.dirty_paths: + self.assertTrue( self.tree.query_path(path) ) + else: + self.assertFalse( self.tree.query_path(path) ) + + def test_iterate(self): + """Make sure iterating over the tree returns each dirty tile exactly once""" + dirty = set(self.dirty_paths) + for p in self.tree.iterate_dirty(): + # Can't use assertIn, was only added in 2.7 + self.assertTrue(p in dirty) + + # Should not see this one again + dirty.remove(p) + + # Make sure they were all returned + self.assertEqual(len(dirty), 0) + + def test_iterate_fail(self): + """Meta-test: Make sure test_iterate() would actually fail""" + # if an extra item were returned""" + self.tree.set_dirty((1,1,1)) + self.assertRaises(AssertionError, self.test_iterate) + + # If something was supposed to be returned but didn't + tree = DirtyTiles(3) + c = len(self.dirty_paths) // 2 + for t in self.dirty_paths: + tree.set_dirty(t) + c -= 1 + if c <= 0: + break + self.tree = tree + self.assertRaises(AssertionError, self.test_iterate) + + def test_count(self): + self.assertEquals(self.tree.count(), len(self.dirty_paths)) + + def test_bool(self): + "Tests the boolean status of a node" + self.assertTrue(self.tree) + t = DirtyTiles(3) + self.assertFalse(t) + t.set_dirty((0,0,0)) + self.assertTrue(t) + + def test_query_level(self): + "Tests querying at a level other than max" + # level 2 + l2 = set() + for p in self.dirty_paths: + l2.add(p[0:2]) + for path in iterate_base4(2): + if path in l2: + self.assertTrue( self.tree.query_path(path) ) + else: + self.assertFalse( self.tree.query_path(path) ) + + # level 1: + self.assertTrue( self.tree.query_path((0,))) + self.assertTrue( self.tree.query_path((1,))) + self.assertTrue( self.tree.query_path((2,))) + self.assertFalse( self.tree.query_path((3,))) + + def test_iterate_level(self): + """Test iterating at a level other than max""" + # level 2 + l2 = set() + for p in self.dirty_paths: + l2.add(p[0:2]) + for p in self.tree.iterate_dirty(2): + self.assertTrue(p in l2, "%s was not supposed to be returned!" % (p,)) + l2.remove(p) + self.assertEqual(len(dirty), 0, "Never iterated over these items: %s" % l2) + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_tileobj.py b/test/test_tileobj.py new file mode 100644 index 0000000..87277cf --- /dev/null +++ b/test/test_tileobj.py @@ -0,0 +1,63 @@ +import unittest + +from overviewer_core import quadtree +from overviewer_core.quadtree import Tile + +items = [ + ((-4,-8), (0,0)), + ((-2,-8), (0,1)), + ((0,-8), (1,0)), + ((2,-8), (1,1)), + ((-4,-4), (0,2)), + ((-2,-4), (0,3)), + ((0,-4), (1,2)), + ((2,-4), (1,3)), + ((-4,0), (2,0)), + ((-2,0), (2,1)), + ((0,0), (3,0)), + ((2,0), (3,1)), + ((-4,4), (2,2)), + ((-2,4), (2,3)), + ((0,4), (3,2)), + ((2,4), (3,3)), + ] + +class TileTest(unittest.TestCase): + def test_compute_path(self): + """Tests that the correct path is computed when a col,row,depth is + given to compute_path + + """ + for path in quadtree.iterate_base4(7): + t1 = Tile.from_path(path) + col = t1.col + row = t1.row + depth = len(path) + + t2 = Tile.compute_path(col, row, depth) + self.assertEqual(t1, t2) + + def test_equality(self): + t1 = Tile(-6, -20, (0,1,2,3)) + + self.assertEqual(t1, Tile(-6, -20, (0,1,2,3))) + self.assertNotEqual(t1, Tile(-4, -20, (0,1,2,3))) + self.assertNotEqual(t1, Tile(-6, -24, (0,1,2,3))) + self.assertNotEqual(t1, Tile(-6, -20, (0,1,2,0))) + + def test_depth2_from_path(self): + """Test frompath on all 16 tiles of a depth 2 tree""" + for (col, row), path in items: + t = Tile.from_path(path) + self.assertEqual(t.col, col) + self.assertEqual(t.row, row) + + def test_depth2_compute_path(self): + """Test comptue_path on all 16 tiles of a depth 2 tree""" + for (col, row), path in items: + t = Tile.compute_path(col, row, 2) + self.assertEqual(t.path, path) + + +if __name__ == "__main__": + unittest.main()