add prefix sum render tests
authorJacob Lifshay <programmerjake@gmail.com>
Sat, 9 Apr 2022 03:01:24 +0000 (20:01 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Sat, 9 Apr 2022 03:01:24 +0000 (20:01 -0700)
src/nmutil/test/test_prefix_sum.py

index 73e93d8b5e8373c43fadf82a8b45172667fa0153..63aa68ee94c998102f40d3c16ab161c40f08dcb3 100644 (file)
@@ -7,7 +7,7 @@
 from nmutil.formaltest import FHDLTestCase
 from itertools import accumulate
 import operator
-from nmutil.prefix_sum import prefix_sum
+from nmutil.prefix_sum import prefix_sum, render_prefix_sum_diagram
 import unittest
 
 
@@ -16,6 +16,8 @@ def reference_prefix_sum(items, fn):
 
 
 class TestPrefixSum(FHDLTestCase):
+    maxDiff = None
+
     def test_prefix_sum_str(self):
         input_items = ("a", "b", "c", "d", "e", "f", "g", "h", "i")
         expected = reference_prefix_sum(input_items, operator.add)
@@ -26,6 +28,145 @@ class TestPrefixSum(FHDLTestCase):
             work_efficient = prefix_sum(input_items, work_efficient=True)
             self.assertEqual(expected, work_efficient)
 
+    def test_render_work_efficient(self):
+        text = render_prefix_sum_diagram(16, work_efficient=True, plus="@")
+        expected = r"""
+ |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
+ ●  |  ●  |  ●  |  ●  |  ●  |  ●  |  ●  |  ●  |
+ |\ |  |\ |  |\ |  |\ |  |\ |  |\ |  |\ |  |\ |
+ | \|  | \|  | \|  | \|  | \|  | \|  | \|  | \|
+ |  @  |  @  |  @  |  @  |  @  |  @  |  @  |  @
+ |  |\ |  |  |  |\ |  |  |  |\ |  |  |  |\ |  |
+ |  | \|  |  |  | \|  |  |  | \|  |  |  | \|  |
+ |  |  X  |  |  |  X  |  |  |  X  |  |  |  X  |
+ |  |  |\ |  |  |  |\ |  |  |  |\ |  |  |  |\ |
+ |  |  | \|  |  |  | \|  |  |  | \|  |  |  | \|
+ |  |  |  @  |  |  |  @  |  |  |  @  |  |  |  @
+ |  |  |  |\ |  |  |  |  |  |  |  |\ |  |  |  |
+ |  |  |  | \|  |  |  |  |  |  |  | \|  |  |  |
+ |  |  |  |  X  |  |  |  |  |  |  |  X  |  |  |
+ |  |  |  |  |\ |  |  |  |  |  |  |  |\ |  |  |
+ |  |  |  |  | \|  |  |  |  |  |  |  | \|  |  |
+ |  |  |  |  |  X  |  |  |  |  |  |  |  X  |  |
+ |  |  |  |  |  |\ |  |  |  |  |  |  |  |\ |  |
+ |  |  |  |  |  | \|  |  |  |  |  |  |  | \|  |
+ |  |  |  |  |  |  X  |  |  |  |  |  |  |  X  |
+ |  |  |  |  |  |  |\ |  |  |  |  |  |  |  |\ |
+ |  |  |  |  |  |  | \|  |  |  |  |  |  |  | \|
+ |  |  |  |  |  |  |  @  |  |  |  |  |  |  |  @
+ |  |  |  |  |  |  |  |\ |  |  |  |  |  |  |  |
+ |  |  |  |  |  |  |  | \|  |  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  X  |  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |\ |  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  | \|  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  X  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |\ |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  | \|  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  X  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  |\ |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  | \|  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  |  X  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  |  |\ |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  |  | \|  |  |  |
+ |  |  |  |  |  |  |  |  |  |  |  |  X  |  |  |
+ |  |  |  |  |  |  |  |  |  |  |  |  |\ |  |  |
+ |  |  |  |  |  |  |  |  |  |  |  |  | \|  |  |
+ |  |  |  |  |  |  |  |  |  |  |  |  |  X  |  |
+ |  |  |  |  |  |  |  |  |  |  |  |  |  |\ |  |
+ |  |  |  |  |  |  |  |  |  |  |  |  |  | \|  |
+ |  |  |  |  |  |  |  |  |  |  |  |  |  |  X  |
+ |  |  |  |  |  |  |  |  |  |  |  |  |  |  |\ |
+ |  |  |  |  |  |  |  |  |  |  |  |  |  |  | \|
+ |  |  |  |  |  |  |  ●  |  |  |  |  |  |  |  @
+ |  |  |  |  |  |  |  |\ |  |  |  |  |  |  |  |
+ |  |  |  |  |  |  |  | \|  |  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  X  |  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |\ |  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  | \|  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  X  |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |\ |  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  | \|  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  X  |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  |\ |  |  |  |  |
+ |  |  |  |  |  |  |  |  |  |  | \|  |  |  |  |
+ |  |  |  ●  |  |  |  ●  |  |  |  @  |  |  |  |
+ |  |  |  |\ |  |  |  |\ |  |  |  |\ |  |  |  |
+ |  |  |  | \|  |  |  | \|  |  |  | \|  |  |  |
+ |  |  |  |  X  |  |  |  X  |  |  |  X  |  |  |
+ |  |  |  |  |\ |  |  |  |\ |  |  |  |\ |  |  |
+ |  |  |  |  | \|  |  |  | \|  |  |  | \|  |  |
+ |  ●  |  ●  |  @  |  ●  |  @  |  ●  |  @  |  |
+ |  |\ |  |\ |  |\ |  |\ |  |\ |  |\ |  |\ |  |
+ |  | \|  | \|  | \|  | \|  | \|  | \|  | \|  |
+ |  |  @  |  @  |  @  |  @  |  @  |  @  |  @  |
+ |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
+"""
+        expected = expected[1:-1]  # trim newline at start and end
+        if text != expected:
+            print("text:")
+            print(text)
+            print()
+        self.assertEqual(expected, text)
+
+    def test_render_not_work_efficient(self):
+        text = render_prefix_sum_diagram(16, work_efficient=False, plus="@")
+        expected = r"""
+ |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
+ ●  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●  |
+ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |
+ | \| \| \| \| \| \| \| \| \| \| \| \| \| \| \|
+ ●  @  @  @  @  @  @  @  @  @  @  @  @  @  @  @
+ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |  |
+ | \| \| \| \| \| \| \| \| \| \| \| \| \| \|  |
+ |  X  X  X  X  X  X  X  X  X  X  X  X  X  X  |
+ |  |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |
+ |  | \| \| \| \| \| \| \| \| \| \| \| \| \| \|
+ ●  ●  @  @  @  @  @  @  @  @  @  @  @  @  @  @
+ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |  |  |  |
+ | \| \| \| \| \| \| \| \| \| \| \| \|  |  |  |
+ |  X  X  X  X  X  X  X  X  X  X  X  X  |  |  |
+ |  |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |  |  |
+ |  | \| \| \| \| \| \| \| \| \| \| \| \|  |  |
+ |  |  X  X  X  X  X  X  X  X  X  X  X  X  |  |
+ |  |  |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |  |
+ |  |  | \| \| \| \| \| \| \| \| \| \| \| \|  |
+ |  |  |  X  X  X  X  X  X  X  X  X  X  X  X  |
+ |  |  |  |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |\ |
+ |  |  |  | \| \| \| \| \| \| \| \| \| \| \| \|
+ ●  ●  ●  ●  @  @  @  @  @  @  @  @  @  @  @  @
+ |\ |\ |\ |\ |\ |\ |\ |\ |  |  |  |  |  |  |  |
+ | \| \| \| \| \| \| \| \|  |  |  |  |  |  |  |
+ |  X  X  X  X  X  X  X  X  |  |  |  |  |  |  |
+ |  |\ |\ |\ |\ |\ |\ |\ |\ |  |  |  |  |  |  |
+ |  | \| \| \| \| \| \| \| \|  |  |  |  |  |  |
+ |  |  X  X  X  X  X  X  X  X  |  |  |  |  |  |
+ |  |  |\ |\ |\ |\ |\ |\ |\ |\ |  |  |  |  |  |
+ |  |  | \| \| \| \| \| \| \| \|  |  |  |  |  |
+ |  |  |  X  X  X  X  X  X  X  X  |  |  |  |  |
+ |  |  |  |\ |\ |\ |\ |\ |\ |\ |\ |  |  |  |  |
+ |  |  |  | \| \| \| \| \| \| \| \|  |  |  |  |
+ |  |  |  |  X  X  X  X  X  X  X  X  |  |  |  |
+ |  |  |  |  |\ |\ |\ |\ |\ |\ |\ |\ |  |  |  |
+ |  |  |  |  | \| \| \| \| \| \| \| \|  |  |  |
+ |  |  |  |  |  X  X  X  X  X  X  X  X  |  |  |
+ |  |  |  |  |  |\ |\ |\ |\ |\ |\ |\ |\ |  |  |
+ |  |  |  |  |  | \| \| \| \| \| \| \| \|  |  |
+ |  |  |  |  |  |  X  X  X  X  X  X  X  X  |  |
+ |  |  |  |  |  |  |\ |\ |\ |\ |\ |\ |\ |\ |  |
+ |  |  |  |  |  |  | \| \| \| \| \| \| \| \|  |
+ |  |  |  |  |  |  |  X  X  X  X  X  X  X  X  |
+ |  |  |  |  |  |  |  |\ |\ |\ |\ |\ |\ |\ |\ |
+ |  |  |  |  |  |  |  | \| \| \| \| \| \| \| \|
+ |  |  |  |  |  |  |  |  @  @  @  @  @  @  @  @
+ |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
+"""
+        expected = expected[1:-1]  # trim newline at start and end
+        if text != expected:
+            print("text:")
+            print(text)
+            print()
+        self.assertEqual(expected, text)
+
     # TODO: add more tests