Commits

Anonymous committed 7068c3e

Add test script and the dev tools

  • Participants
  • Parent commits 29fc1ac

Comments (0)

Files changed (3)

development_and_test/optimize_dirty_rects_convert.py

+#! /usr/bin/env python
+# coding: ascii
+# optimize_dirty_rects_convert
+"""Help the coder choose the correct collision from the chart
+
+usage: optimize_dirty_rects_convert.py tcmp bcmp lcmp rcmp
+   where
+      tcmp is < for r.top < r2.top
+         "    = for r.top == r2.top
+         "    > for r.top > r2.top
+      bcmp is < for r.bottom < r2.bottom
+         "    = for r.bottom == r2.bottom
+              > for r.bottom > r2.bottom
+      lcmp is < for r.left < r2.left
+         "    = for r.left == r2.left
+         "    > for r.left > r2.left
+      rcmp    < for r.right < r2.right
+         "    = for r.right == r2.right
+         "    > for r.right > r2.right
+
+This is useful for determining which block of rectangle processing code
+should apply to a given pair of overlapping rectangles. Output is in the
+notation used on this page:
+http://gandraxa.com/detect_overlapping_subrectangles.xml
+
+Command line example (Windows): optimize_dirty_rects_convert.py = ^< ^> =
+(The escape character on Windows is ^. On POSIX platforms, the escape
+character is the backslash.)
+
+The MIT License (MIT)
+
+Copyright (c) 2013 Jason Marshall
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+"""
+
+def convert_cmp_to_RBTLRBTL(tcmp, bcmp, lcmp, rcmp):
+    """help the coder choose the correct collision from the chart"""
+    for s in (tcmp, bcmp, lcmp, rcmp):
+        assert s in ('<', '=', '>', '=='), 'Bad input %r' % s
+    ret = ['-', '-', '-', '-', '-', '-', '-', '-']
+    R, B, T, L, R2, B2, T2, L2 = 0, 1, 2, 3, 4, 5, 6, 7
+    if tcmp == '=' or tcmp == '==':
+        ret[T] = 'T'
+        ret[T2] = 'T'
+    elif tcmp == '<':
+        ret[T2] = 'T'
+    else:
+        ret[T] = 'T'
+    if lcmp == '=' or lcmp == '==':
+        ret[L] = 'L'
+        ret[L2] = 'L'
+    elif lcmp == '<':
+        ret[L2] = 'L'
+    else:
+        ret[L] = 'L'
+    if bcmp == '=' or bcmp == '==':
+        ret[B] = 'B'
+        ret[B2] = 'B'
+    elif bcmp == '>':
+        ret[B2] = 'B'
+    else:
+        ret[B] = 'B'
+    if rcmp == '=' or rcmp == '==':
+        ret[R] = 'R'
+        ret[R2] = 'R'
+    elif rcmp == '>':
+        ret[R2] = 'R'
+    else:
+        ret[R] = 'R'
+    return ''.join(ret)
+
+if __name__ == "__main__":
+    import sys
+    args = sys.argv[-4:]
+    print (convert_cmp_to_RBTLRBTL(*args))

development_and_test/optimize_dirty_rects_test.py

+#! /usr/bin/env python
+# coding: ascii
+# optimize_dirty_rects_test
+"""Test script for optimize_dirty_rects
+
+The MIT License (MIT)
+
+Copyright (c) 2013 Jason Marshall
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+"""
+import os
+import sys
+import pygame
+from pygame.tests.test_utils import unittest, unordered_equality
+import optimize_dirty_rects
+#####from optimize_dirty_rects import debug
+
+class OptimizeDirtyRectsTest(unittest.TestCase):
+    """verify handling of all possible rect collision arrangements
+
+    The test methods just pass a string depicting rects to the _verify
+    method. This string must depict two 5x5 grids, side-by-side. Key:
+
+    .   No rect
+    |   Rect 1
+    -   Rect 2
+    =   Rect 3
+    +   Rects 1 & 2 overlap (input only)
+
+    The _verify method contains all of the necessary code to convert
+    the string to rects, pass the input rects to optimize_dirty_rects,
+    compare the actual output of optimize_dirty_rects to the expected
+    output and update the test result.
+
+    """
+
+    def test_000_display_update_compatibility(self):
+        """verify that pygame.display.update accepts output"""
+
+        #####debug('t000')
+
+        # Initialize video subsystem
+        display_initialized_previously = pygame.display.get_init()
+        if not display_initialized_previously:
+            previous_video_driver = os.environ.get('SDL_VIDEODRIVER')
+            if previous_video_driver is None:
+                os.environ['SDL_VIDEODRIVER'] = 'dummy'
+            pygame.display.init()
+        screen = pygame.display.set_mode((100, 100))
+
+        try:
+
+            queue = optimize_dirty_rects.optimize_dirty_rects([])
+            pygame.display.update(queue)
+
+            # Pass
+            self.failUnless(True)
+
+        except:
+
+            # Fail
+            self.failUnless(False, 'empty list')
+
+        try:
+
+            queue = optimize_dirty_rects.optimize_dirty_rects(
+                                              [pygame.Rect(10, 10, 10, 10)])
+            pygame.display.update(queue)
+
+            # Pass
+            self.failUnless(True)
+
+        except:
+
+            # Fail
+            self.failUnless(False, 'single item list')
+
+        try:
+
+            values_list = optimize_dirty_rects.optimize_dirty_rects(
+                                              [pygame.Rect(10, 10, 10, 10),
+                                               pygame.Rect(20, 20, 20, 20)])
+            pygame.display.update(values_list)
+
+            # Pass
+            self.failUnless(True)
+
+        except:
+
+            # Fail
+            self.failUnless(False, 'multiple items list')
+
+        # Deinitialize video subsystem
+        if not display_initialized_previously:
+            pygame.display.quit()
+            if previous_video_driver is None:
+                del os.environ['SDL_VIDEODRIVER']
+            else:
+                os.environ['SDL_VIDEODRIVER'] = previous_video_driver
+
+    def test_001_RBTLRBTL(self):
+        #####debug('t001')
+        self._verify("""....=    ....=
+                        .+++.    .---.
+                        .+++. -> .---.
+                        .+++.    .---.
+                        .....    .....""")
+
+    def test_002_RBTL_BTL(self):
+        #####debug('t002')
+        self._verify("""....=    ....=
+                        .++-.    .---.
+                        .++-. -> .---.
+                        .++-.    .---.
+                        .....    .....""")
+
+    def test_003__BTLRBTL(self):
+        #####debug('t003')
+        self._verify("""....=    ....=
+                        .++|.    .|||.
+                        .++|. -> .|||.
+                        .++|.    .|||.
+                        .....    .....""")
+
+    def test_004_RBT_RBTL(self):
+        #####debug('t004')
+        self._verify("""....=    ....=
+                        .|++.    .|||.
+                        .|++. -> .|||.
+                        .|++.    .|||.
+                        .....    .....""")
+
+    def test_005__BT_RBTL(self):
+        #####debug('t005')
+        self._verify("""....=    ....=
+                        .|+|.    .|||.
+                        .|+|. -> .|||.
+                        .|+|.    .|||.
+                        .....    .....""")
+
+    def test_006_RBT__BTL(self):
+        #####debug('t006')
+        self._verify("""....=    ....=
+                        .|+-.    .|||.
+                        .|+-. -> .|||.
+                        .|+-.    .|||.
+                        .....    .....""")
+        #####debug('t006-shortcut')
+        self._verify(""".....    .....
+                        .|+-.    .|||.
+                        .|+-. -> .|||.
+                        .|+-.    .|||.
+                        .....    .....""")
+
+    def test_007__BT__BT_a(self):
+        #####debug('t007')
+        self._verify("""....=    ....=
+                        .|--.    .|||.
+                        .|--. -> .|||.
+                        .|--.    .|||.
+                        .....    .....""")
+        #####debug('t007-shortcut')
+        self._verify(""".....    .....
+                        .|--.    .|||.
+                        .|--. -> .|||.
+                        .|--.    .|||.
+                        .....    .....""")
+
+    def test_008_RBTLRBT_(self):
+        #####debug('t008')
+        self._verify("""....=    ....=
+                        .-++.    .---.
+                        .-++. -> .---.
+                        .-++.    .---.
+                        .....    .....""")
+
+    def test_009_RBTL_BT_(self):
+        #####debug('t009')
+        self._verify("""....=    ....=
+                        .-+-.    .---.
+                        .-+-. -> .---.
+                        .-+-.    .---.
+                        .....    .....""")
+
+    def test_010__BTLRBT_(self):
+        #####debug('t010')
+        self._verify("""....=    ....=
+                        .-+|.    .|||.
+                        .-+|. -> .|||.
+                        .-+|.    .|||.
+                        .....    .....""")
+        #####debug('t010-shortcut')
+        self._verify(""".....    .....
+                        .-+|.    .|||.
+                        .-+|. -> .|||.
+                        .-+|.    .|||.
+                        .....    .....""")
+
+    def test_011__BT__BT_b(self):
+        #####debug('t011')
+        self._verify("""....=    ....=
+                        .--|.    .|||.
+                        .--|. -> .|||.
+                        .--|.    .|||.
+                        .....    .....""")
+        #####debug('t011-shortcut')
+        self._verify(""".....    .....
+                        .--|.    .|||.
+                        .--|. -> .|||.
+                        .--|.    .|||.
+                        .....    .....""")
+
+    def test_012_RBTLR_TL(self):
+        #####debug('t012')
+        self._verify("""....=    ....=
+                        .+++.    .---.
+                        .---. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_013_RBTL__TL(self):
+        #####debug('t013')
+        self._verify("""....=    ....=
+                        .++-.    .---.
+                        .++-. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_014__BTLR_TL(self):
+        #####debug('t014')
+        self._verify(""".....    .....
+                        .+||.    .|||.
+                        .-... -> .-...
+                        .-...    .-...
+                        .....    .....""")
+
+    def test_015_RBT_R_TL(self):
+        #####debug('t015')
+        self._verify(""".....    .....
+                        .|++.    .|||.
+                        .|++. -> .|||.
+                        ..--.    ..--.
+                        .....    .....""")
+
+    def test_016_RBT___TL(self):
+        #####debug('t016')
+        self._verify(""".....    .....
+                        .|+-.    .|||.
+                        .|+-. -> .|||.
+                        ..--.    ..--.
+                        .....    .....""")
+
+    def test_017__BT___T_a(self):
+        #####debug('t017')
+        self._verify(""".....    .....
+                        .|--.    .|||.
+                        .|--. -> .|||.
+                        ..--.    ..--.
+                        .....    .....""")
+
+    def test_018__BT_R_TL(self):
+        #####debug('t018')
+        self._verify(""".....    .....
+                        .|+|.    .|||.
+                        .|+|. -> .|||.
+                        ..-..    ..-..
+                        .....    .....""")
+
+
+    def test_019_RBTLR_T_(self):
+        #####debug('t019')
+        self._verify("""....=    ....=
+                        .-++.    .---.
+                        .-++. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_020_RBTL__T_(self):
+        #####debug('t020')
+        self._verify("""....=    ....=
+                        .-+-.    .---.
+                        .-+-. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_021__BTLR_T_(self):
+        #####debug('t021')
+        self._verify(""".....    .....
+                        .-+|.    .|||.
+                        .-+|. -> .|||.
+                        .--..    .--..
+                        .....    .....""")
+
+    def test_022__BT___T_b(self):
+        #####debug('t022')
+        self._verify(""".....    .....
+                        .--|.    .|||.
+                        .--|. -> .|||.
+                        .--..    .--..
+                        .....    .....""")
+
+    def test_023_R_TLRBTL(self):
+        #####debug('t023')
+        self._verify("""....=    ....=
+                        .+++.    .|||.
+                        .+++. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_024_R_TL_BTL(self):
+        #####debug('t024')
+        self._verify(""".....    .....
+                        .++-.    .---.
+                        .++-. -> .---.
+                        .||..    .||..
+                        .....    .....""")
+
+    def test_025___TLRBTL(self):
+        #####debug('t025')
+        self._verify("""....=    ....=
+                        .++|.    .|||.
+                        .++|. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_026_R_T_RBTL(self):
+        #####debug('t026')
+        self._verify("""....=    ....=
+                        .|++.    .|||.
+                        .|++. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_027_R_T__BTL(self):
+        #####debug('t027')
+        self._verify(""".....    .....
+                        .|+-.    .|||.
+                        .|+-. -> .|||.
+                        .||..    .==..
+                        .....    .....""")
+
+    def test_028___T__BT_b(self):
+        #####debug('t028')
+        self._verify(""".....    .....
+                        .||-.    .|||.
+                        .||-. -> .|||.
+                        .||..    .==..
+                        .....    .....""")
+
+    def test_029___T_RBTL(self):
+        #####debug('t029')
+        self._verify("""....=    ....=
+                        .|+|.    .|||.
+                        .|+|. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_030_R_TLRBT_(self):
+        #####debug('t030')
+        self._verify(""".....    .....
+                        .-++.    .---.
+                        .-++. -> .---.
+                        ..||.    ..||.
+                        .....    .....""")
+
+    def test_031_R_TL_BT_(self):
+        #####debug('t031')
+        self._verify(""".....    .....
+                        .-+-.    .---.
+                        .-+-. -> .---.
+                        ..|..    ..|..
+                        .....    .....""")
+
+    def test_032___TLRBT_(self):
+        #####debug('t032')
+        self._verify(""".....    .....
+                        .-+|.    .|||.
+                        .-+|. -> .|||.
+                        ..||.    ..==.
+                        .....    .....""")
+
+    def test_033___T__BT_a(self):
+        #####debug('t033')
+        self._verify(""".....    .....
+                        .-||.    .|||.
+                        .-||. -> .|||.
+                        ..||.    ..==.
+                        .....    .....""")
+
+    def test_034_RB_LRBTL(self):
+        #####debug('t034')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .+++. -> .|||.
+                        .+++.    .|||.
+                        .....    .....""")
+
+    def test_035_RB_L_BTL(self):
+        #####debug('t034')
+        self._verify(""".....    .....
+                        .||..    .||..
+                        .++-. -> .---.
+                        .++-.    .---.
+                        .....    .....""")
+
+    def test_036__B_LRBTL(self):
+        #####debug('t036')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .++|. -> .|||.
+                        .++|.    .|||.
+                        .....    .....""")
+
+    def test_037_RB__RBTL(self):
+        #####debug('t037')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .|++. -> .|||.
+                        .|++.    .|||.
+                        .....    .....""")
+
+    def test_038_RB___BTL(self):
+        #####debug('t038')
+        self._verify(""".....    .....
+                        .||..    .==..
+                        .|+-. -> .|||.
+                        .|+-.    .|||.
+                        .....    .....""")
+
+    def test_039__B___BT_b(self):
+        #####debug('t039')
+        self._verify(""".....    .....
+                        .||..    .==..
+                        .||-. -> .|||.
+                        .||-.    .|||.
+                        .....    .....""")
+
+    def test_040__B__RBTL(self):
+        #####debug('t040')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .|+|. -> .|||.
+                        .|+|.    .|||.
+                        .....    .....""")
+
+    def test_041_RB_LRBT_(self):
+        #####debug('t041')
+        self._verify(""".....    .....
+                        ..||.    ..||.
+                        .-++. -> .---.
+                        .-++.    .---.
+                        .....    .....""")
+
+    def test_042_RB_L_BT_(self):
+        #####debug('t042')
+        self._verify(""".....    .....
+                        ..|..    ..|..
+                        .-+-. -> .---.
+                        .-+-.    .---.
+                        .....    .....""")
+
+    def test_043__B_LRBT_(self):
+        #####debug('t043')
+        self._verify(""".....    .....
+                        ..||.    ..==.
+                        .-+|. -> .|||.
+                        .-+|.    .|||.
+                        .....    .....""")
+
+    def test_044__B___BT_a(self):
+        #####debug('t044')
+        self._verify(""".....    .....
+                        ..||.    ..==.
+                        .-||. -> .|||.
+                        .-||.    .|||.
+                        .....    .....""")
+
+    def test_045_RB_LR_TL(self):
+        #####debug('t045')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .+++. -> .|||.
+                        .---.    .|||.
+                        .....    .....""")
+        #####debug('t045-shortcut')
+        self._verify(""".....    .....
+                        .|||.    .|||.
+                        .+++. -> .|||.
+                        .---.    .|||.
+                        .....    .....""")
+
+    def test_046_RB_L__TL(self):
+        #####debug('t046')
+        self._verify(""".....    .....
+                        .||..    .||..
+                        .++-. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_047__B_LR_TL(self):
+        #####debug('t047')
+        self._verify(""".....    .....
+                        .|||.    .|||.
+                        .++|. -> .|||.
+                        .--..    .--..
+                        .....    .....""")
+
+    def test_048_RB__R_TL(self):
+        #####debug('t048')
+        self._verify(""".....    .....
+                        .|||.    .|||.
+                        .|++. -> .|||.
+                        ..--.    ..--.
+                        .....    .....""")
+
+    def test_049_RB__R_TL(self):
+        #####debug('t049')
+        self._verify(""".....    .....
+                        .||..    .||..
+                        .|+-. -> .===.
+                        ..--.    ..--.
+                        .....    .....""")
+
+    def test_050__B____T_a(self):
+        #####debug('t050')
+        self._verify(""".....    .....
+                        .||..    .||..
+                        .||-. -> .===.
+                        ...-.    ...-.
+                        .....    .....""")
+
+    def test_051__B__R_TL(self):
+        #####debug('t051')
+        self._verify(""".....    .....
+                        .|||.    .|||.
+                        .|+|. -> .|||.
+                        ..-..    ..-..
+                        .....    .....""")
+
+    def test_052_RB_LR_T_(self):
+        #####debug('t052')
+        self._verify(""".....    .....
+                        ..||.    ..||.
+                        .-++. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_053_RB_L__T_(self):
+        #####debug('t053')
+        self._verify(""".....    .....
+                        ..|..    ..|..
+                        .-+-. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_054__B_LR_T_(self):
+        #####debug('t054')
+        self._verify(""".....    .....
+                        ..||.    ..||.
+                        .-+.. -> .===.
+                        .--..    .--..
+                        .....    .....""")
+
+    def test_055__B____T_b(self):
+        #####debug('t055')
+        self._verify(""".....    .....
+                        ..||.    ..||.
+                        .-||. -> .===.
+                        .-...    .-...
+                        .....    .....""")
+
+    def test_056_R__LRBTL(self):
+        #####debug('t056')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .+++. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_057_R__L_BTL(self):
+        #####debug('t057')
+        self._verify(""".....    .....
+                        .||..    .||..
+                        .++-. -> .---.
+                        .||..    .==..
+                        .....    .....""")
+
+    def test_058____LRBTL(self):
+        #####debug('t058')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .++|. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_059_R___RBTL(self):
+        #####debug('t059')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .|++. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_060_R____BTL(self):
+        #####debug('t060')
+        self._verify(""".....    .....
+                        .||..    .||..
+                        .|+-. -> .---.
+                        .||..    .==..
+                        .....    .....""")
+
+    def test_061_____RBTL(self):
+        #####debug('t061')
+        self._verify("""....=    ....=
+                        .|||.    .|||.
+                        .|+|. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_062_R__LRBT_(self):
+        #####debug('t062')
+        self._verify(""".....    .....
+                        ..||.    ..||.
+                        .-++. -> .---.
+                        ..||.    ..==.
+                        .....    .....""")
+
+    def test_063_R__L_BT_(self):
+        #####debug('t063')
+        self._verify(""".....    .....
+                        ..|..    ..|..
+                        .-+-. -> .---.
+                        ..|..    ..=..
+                        .....    .....""")
+
+    def test_064____LRBT_(self):
+        #####debug('t064')
+        self._verify(""".....    .....
+                        ..||.    ..||.
+                        .-+|. -> .---.
+                        ..||.    ..==.
+                        .....    .....""")
+
+    def test_065______BT_a(self):
+        #####debug('t065')
+        self._verify(""".....    .....
+                        ...|.    ...|.
+                        .--|. -> .---.
+                        ...|.    ...=.
+                        .....    .....""")
+
+    def test_066______BT_b(self):
+        #####debug('t066')
+        self._verify(""".....    .....
+                        .|...    .|...
+                        .|--. -> .---.
+                        .|...    .=...
+                        .....    .....""")
+
+    def test_067_RBTLRB_L(self):
+        #####debug('t067')
+        self._verify("""....=    ....=
+                        .---.    .---.
+                        .+++. -> .---.
+                        .+++.    .---.
+                        .....    .....""")
+
+    def test_068_RBTL_B_L(self):
+        #####debug('t068')
+        self._verify("""....=    ....=
+                        .---.    .---.
+                        .++-. -> .---.
+                        .++-.    .---.
+                        .....    .....""")
+
+    def test_069__BTLRB_L(self):
+        #####debug('t069')
+        self._verify(""".....    .....
+                        .-...    .-...
+                        .-... -> .-...
+                        .+||.    .|||.
+                        .....    .....""")
+
+    def test_070_RBT_RB_L(self):
+        #####debug('t070')
+        self._verify(""".....    .....
+                        ...-.    ...-.
+                        ...-. -> ...-.
+                        .||+.    .|||.
+                        .....    .....""")
+
+    def test_071_RBT__B_L(self):
+        #####debug('t071')
+        self._verify(""".....    .....
+                        ..--.    ..--.
+                        .|+-. -> .|||.
+                        .|+-.    .|||.
+                        .....    .....""")
+
+    def test_072__BT__B__a(self):
+        #####debug('t072')
+        self._verify(""".....    .....
+                        ...-.    ...-.
+                        .||-. -> .|||.
+                        .||-.    .|||.
+                        .....    .....""")
+
+    def test_073__BT_RB_L(self):
+        #####debug('t073')
+        self._verify(""".....    .....
+                        ..-..    ..-..
+                        ..-.. -> ..-..
+                        .|+|.    .|||.
+                        .....    .....""")
+
+    def test_074_RBTLRB__(self):
+        #####debug('t074')
+        self._verify("""....=    ....=
+                        .---.    .---.
+                        .-++. -> .---.
+                        .-++.    .---.
+                        .....    .....""")
+
+    def test_075_RBTL_B__(self):
+        #####debug('t075')
+        self._verify("""....=    ....=
+                        .---.    .---.
+                        .-+-. -> .---.
+                        .-+-.    .---.
+                        .....    .....""")
+
+    def test_076__BTLRB__(self):
+        #####debug('t076')
+        self._verify(""".....    .....
+                        .--..    .--..
+                        .-+|. -> .|||.
+                        .-+|.    .|||.
+                        .....    .....""")
+
+    def test_077__BT__B__b(self):
+        #####debug('t077')
+        self._verify(""".....    .....
+                        .-...    .-...
+                        .-||. -> .|||.
+                        .-||.    .|||.
+                        .....    .....""")
+
+    def test_078_RBTLR__L(self):
+        #####debug('t078')
+        self._verify("""....=    ....=
+                        .---.    .---.
+                        .+++. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_079_RBTL___L(self):
+        #####debug('t079')
+        self._verify("""....=    ....=
+                        .---.    .---.
+                        .+--. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_080__BTLR__L(self):
+        #####debug('t080')
+        self._verify(""".....    .....
+                        .-...    .-...
+                        .+||. -> .|||.
+                        .-...    .=...
+                        .....    .....""")
+
+    def test_081_RBT_R__L(self):
+        #####debug('t081')
+        self._verify(""".....    .....
+                        ...-.    ...-.
+                        .||+. -> .|||.
+                        ...-.    ...=.
+                        .....    .....""")
+
+    def test_082_RBT____L(self):
+        #####debug('t082')
+        self._verify(""".....    .....
+                        ..--.    ..--.
+                        .|+-. -> .|||.
+                        ..--.    ..==.
+                        .....    .....""")
+
+    def test_083__BT_____a(self):
+        #####debug('t083')
+        self._verify(""".....    .....
+                        ..--.    ..--.
+                        .|--. -> .|||.
+                        ..--.    ..==.
+                        .....    .....""")
+
+    def test_084__BT_R__L(self):
+        #####debug('t084')
+        self._verify(""".....    .....
+                        ..-..    ..-..
+                        .|+|. -> .|||.
+                        ..-..    ..=..
+                        .....    .....""")
+
+    def test_085_RBTLR___(self):
+        #####debug('t085')
+        self._verify("""....=    ....=
+                        .---.    .---.
+                        .--+. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_086_RBTL____(self):
+        #####debug('t086')
+        self._verify("""....=    ....=
+                        .---.    .---.
+                        .-+-. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_087_RBT____L(self):
+        #####debug('t087')
+        self._verify(""".....    .....
+                        .--..    .--..
+                        .-+|. -> .|||.
+                        .--..    .==..
+                        .....    .....""")
+
+    def test_088__BT_____b(self):
+        #####debug('t088')
+        self._verify(""".....    .....
+                        .--..    .--..
+                        .--|. -> .|||.
+                        .--..    .==..
+                        .....    .....""")
+
+    def test_089_R_TLRB_L(self):
+        #####debug('t089')
+        self._verify("""....=    ....=
+                        .---.    .|||.
+                        .+++. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+        #####debug('t089-shortcut')
+        self._verify(""".....    .....
+                        .---.    .|||.
+                        .+++. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_090_R_TL_B_L(self):
+        #####debug('t090')
+        self._verify(""".....    .....
+                        .---.    .---.
+                        .++-. -> .---.
+                        .||..    .||..
+                        .....    .....""")
+
+    def test_091___TLRB_L(self):
+        #####debug('t091')
+        self._verify(""".....    .....
+                        .--..    .--..
+                        .++|. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_092_R_T_RB_L(self):
+        #####debug('t092')
+        self._verify(""".....    .....
+                        ..--.    ..--.
+                        .|++. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_093_R_T__B_L(self):
+        #####debug('t093')
+        self._verify(""".....    .....
+                        ..--.    ..--.
+                        .|+-. -> .===.
+                        .||..    .||..
+                        .....    .....""")
+
+    def test_094___T__B__b(self):
+        #####debug('t094')
+        self._verify(""".....    .....
+                        ..--.    ..--.
+                        .|--. -> .|||.
+                        .|...    .=...
+                        .....    .....""")
+
+    def test_095___T_RB_L(self):
+        #####debug('t095')
+        self._verify(""".....    .....
+                        ..-..    ..-..
+                        .|+|. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_096_R_TLRB__(self):
+        #####debug('t096')
+        self._verify(""".....    .....
+                        .---.    .---.
+                        .-++. -> .---.
+                        ..||.    ..||.
+                        .....    .....""")
+
+    def test_097_R_TL_B__(self):
+        #####debug('t097')
+        self._verify(""".....    .....
+                        .---.    .---.
+                        .-+-. -> .---.
+                        ..|..    ..|..
+                        .....    .....""")
+
+    def test_098___T__B__a(self):
+        #####debug('t__T__B__a')
+        self._verify(""".....    .....
+                        .--..    .--..
+                        .--|. -> .===.
+                        ...|.    ...|.
+                        .....    .....""")
+
+    def test_099___TLRB__(self):
+        #####debug('t099')
+        self._verify(""".....    .....
+                        .--..    .--..
+                        .-+|. -> .===.
+                        ..||.    ..||.
+                        .....    .....""")
+
+    def _verify(self, input_and_output):
+
+        # Convert 5x5 grids depicting input and output rects into rects
+        r_input_x = set()
+        r_input_y = set()
+        r2_input_x = set()
+        r2_input_y = set()
+        r3_input_x = set()
+        r3_input_y = set()
+        r_output_x = set()
+        r_output_y = set()
+        r2_output_x = set()
+        r2_output_y = set()
+        r3_output_x = set()
+        r3_output_y = set()
+
+        for y, line in enumerate(input_and_output.splitlines()):
+
+            line = line.strip()
+
+            for x in range(5):
+
+                c = line[x]
+                if c == '.':
+                    pass
+                elif c == '|':
+                    r_input_x.add(x)
+                    r_input_y.add(y)
+                elif c == '-':
+                    r2_input_x.add(x)
+                    r2_input_y.add(y)
+                elif c == '+':
+                    r_input_x.add(x)
+                    r_input_y.add(y)
+                    r2_input_x.add(x)
+                    r2_input_y.add(y)
+                elif c == '=':
+                    r3_input_x.add(x)
+                    r3_input_y.add(y)
+                else:
+                    raise SyntaxError('bad grid input character at %i, %i: %s' % (x, y, c))
+
+                c = line[-5 + x]
+                if c == '.':
+                    pass
+                elif c == '|':
+                    r_output_x.add(x)
+                    r_output_y.add(y)
+                elif c == '-':
+                    r2_output_x.add(x)
+                    r2_output_y.add(y)
+                elif c == '=':
+                    r3_output_x.add(x)
+                    r3_output_y.add(y)
+                else:
+                    raise SyntaxError('bad grid output character at %i, %i: %s' % (x, y, c))
+
+        input_rects = []
+        for (rn_input_x, rn_input_y) in ((r3_input_x, r3_input_y),
+                                         (r2_input_x, r2_input_y),
+                                         (r_input_x, r_input_y)):
+            if rn_input_x:
+                rn_input = self._rect_from_sets(rn_input_x, rn_input_y)
+                input_rects.append(rn_input)
+
+        output_rects = []
+        for (rn_output_x, rn_output_y) in ((r_output_x, r_output_y),
+                                           (r2_output_x, r2_output_y),
+                                           (r3_output_x, r3_output_y)):
+            if rn_output_x:
+                rn_output = self._rect_from_sets(rn_output_x, rn_output_y)
+                output_rects.append(rn_output)
+
+        # Verify that the actual output matches the test case output
+        optimized_rects = optimize_dirty_rects.optimize_dirty_rects(input_rects)
+        rects_match = unordered_equality(optimized_rects, output_rects)
+
+        if rects_match:
+            msg = None
+        else:
+            msg = 'expected rects:\n' + repr(output_rects) + \
+                    '\noptimized rects:\n' + repr(optimized_rects)
+
+        self.failUnless(rects_match, msg)
+
+    def test_985_non_intersecting(self):
+        #####debug('t985')
+        self._verify(""".....    .....
+                        ..|..    ..|..
+                        .---. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_986_non_intersecting(self):
+        #####debug('t986')
+        self._verify(""".....    .....
+                        .|||.    .|||.
+                        ..-.. -> ..-..
+                        ..-..    ..-..
+                        .....    .....""")
+
+    def test_987_non_intersecting(self):
+        #####debug('t987')
+        self._verify(""".....    .....
+                        ..||.    ..||.
+                        .---. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_988_non_intersecting(self):
+        #####debug('t988')
+        self._verify(""".....    .....
+                        .||..    .||..
+                        .---. -> .---.
+                        .---.    .---.
+                        .....    .....""")
+
+    def test_989_non_intersecting(self):
+        #####debug('t989')
+        self._verify(""".....    .....
+                        ..||.    ..||.
+                        .--.. -> .--..
+                        .--..    .--..
+                        .....    .....""")
+
+    def test_990_non_intersecting(self):
+        #####debug('t990')
+        self._verify(""".....    .....
+                        .||..    .||..
+                        ..--. -> ..--.
+                        ..--.    ..--.
+                        .....    .....""")
+
+    def test_991_non_intersecting(self):
+        #####debug('t991')
+        self._verify(""".....    .....
+                        ...|.    ...|.
+                        .--.. -> .--..
+                        .--..    .--..
+                        .....    .....""")
+
+    def test_992_non_intersecting(self):
+        #####debug('t992')
+        self._verify(""".....    .....
+                        .|...    .|...
+                        ..--. -> ..--.
+                        ..--.    ..--.
+                        .....    .....""")
+
+    def test_993_non_intersecting(self):
+        #####debug('t993')
+        self._verify(""".....    .....
+                        .|.-.    .|.-.
+                        .|.-. -> .|.-.
+                        .|.-.    .|.-.
+                        .....    .....""")
+
+    def test_994_vertical_expansion(self):
+        #####debug('t994')
+        self._verify(""".....    .....
+                        .---.    .|||.
+                        .|||. -> .|||.
+                        .===.    .|||.
+                        .....    .....""")
+
+    def test_995_vertical_expansion(self):
+        #####debug('t995')
+        self._verify(""".....    .....
+                        .---.    .|||.
+                        .|||. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def test_996_vertical_expansion(self):
+        #####debug('t996')
+        self._verify(""".....    .....
+                        .|||.    .|||.
+                        .|||. -> .|||.
+                        .---.    .|||.
+                        .....    .....""")
+
+    def test_997_vertical_expansion_to_the_extreme(self):
+        #####debug('t997')
+        # 7777777777 # row 0 # When added, does not touch any other rects
+        # 8888888888 # row 1 # When added, needs to merge with rects above & below
+        # 6666666666 # row 2 # When added, needs to merge with rect below
+        # 0000000000 # row 3
+        # 4444444444 # row 4 # When added, needs to merge with rects above & below
+        # 3333333333 # row 5 # When added, needs to merge with rect below
+        # 1111111111 # row 6 # When added, does not touch any other rects
+        # 1111111111 # row 7
+        # 2222222222 # row 8 # When added, needs to merge with rect above
+        # 5555555555 # row 9 # When added, needs to merge with rect above
+        input_rects = [pygame.Rect(0, 1, 10, 1), # 8
+                       pygame.Rect(0, 0, 10, 1), # 7
+                       pygame.Rect(0, 2, 10, 1), # 6
+                       pygame.Rect(0, 9, 10, 1), # 5
+                       pygame.Rect(0, 4, 10, 1), # 4
+                       pygame.Rect(0, 5, 10, 1), # 3
+                       pygame.Rect(0, 8, 10, 1), # 2
+                       pygame.Rect(0, 6, 10, 2), # 1
+                       pygame.Rect(0, 3, 10, 1)] # 0
+        output_rects = [pygame.Rect(0, 0, 10, 10)]
+        optimized_rects = optimize_dirty_rects.optimize_dirty_rects(input_rects)
+        rects_match = unordered_equality(optimized_rects, output_rects)
+
+        if rects_match:
+            msg = None
+        else:
+            msg = 'expected rects:\n' + repr(output_rects) + \
+                    '\noptimized rects:\n' + repr(optimized_rects)
+
+        self.failUnless(rects_match, msg)
+
+    def test_998_empty_sequence_input(self):
+        #####debug('t998')
+        self._verify(""".....    .....
+                        .....    .....
+                        ..... -> .....
+                        .....    .....
+                        .....    .....""")
+
+    def test_999_one_rect(self):
+        #####debug('t999')
+        self._verify(""".....    .....
+                        .|||.    .|||.
+                        .|||. -> .|||.
+                        .|||.    .|||.
+                        .....    .....""")
+
+    def _rect_from_sets(self, x_set, y_set):
+        """helper method for _verify"""
+        left = min(x_set)
+        top = min(y_set)
+        width = max(x_set) + 1 - left
+        height = max(y_set) + 1 - top
+        return pygame.Rect(left, top, width, height)
+
+if __name__ == "__main__":
+    #####debug('Python ' + sys.version)
+    unittest.main()

development_and_test/table_checkoff.jpg

Added
New image