Commits

Joe LaFata committed 6f1f0e6

split out some functions for better testing
added write_results to the function profiler
added more tests

Comments (0)

Files changed (2)

src/tests/test_unitbench.py

         def adder(x, y):
             return x + y
         
+        eq_(plus2(5), 7)
         eq_(plus2(8), 10)
         eq_(sum23(), 5)
         eq_(adder(7, 9), 16)
-        eq_(len(fp.results), 3)
+        eq_(len(fp.results), 4)
     
 class TestBenchmark(TestCase):
     def test_warmup(self):
         eq_(Benchmark._function_name_to_title("XMLBenchmark"), "Xml Benchmark")
 
 class TestConsoleReporter(TestCase):
+    def test_count_digits(self):
+        eq_(ConsoleReporter.count_digits(1), 1)
+        eq_(ConsoleReporter.count_digits(5), 1)
+        eq_(ConsoleReporter.count_digits(10), 2)
+        eq_(ConsoleReporter.count_digits(19), 2)
+        eq_(ConsoleReporter.count_digits(99), 2)
+        eq_(ConsoleReporter.count_digits(100), 3)
+        eq_(ConsoleReporter.count_digits(999), 3)
+        eq_(ConsoleReporter.count_digits(1000), 4)
+        eq_(ConsoleReporter.count_digits(9999), 4)
+        
+    def test_make_key(self):
+        cr = ConsoleReporter()
+        title = "012345678901234567890123456789"
+        key = []
+        t2 = cr._make_key(title, 30, key)
+        eq_(len(key), 1)
+        eq_(t2, title[23:] + '...[1]')
+        
+        key = []
+        t2 = cr._make_key(title, 31, key)
+        eq_(len(key), 0)
+        eq_(t2, title)
+        
+        key = []
+        t2 = cr._make_key(title, 25, key)
+        eq_(len(key), 1)
+        eq_(t2, title[24-6:] + "...[1]")
+        
     def test_write_results(self):
         class sample(OneRun):
             def input(self):
             return func_call.ret_val
         return wrapper
     
-    def report_results(self, reporter=None):
+    def write_results(self, reporter=None):
         if reporter == None:
-            reporter = ConsoleReporter()
+            reporter = ConsoleReporter(group_by=ConsoleReporter.GroupType.FUNCTION)
+        results = sorted(self.results, key = lambda x : len(self.results[x]))
+        reporter.write_results(results)
     
     def _args_to_str(self, *args, **kwargs):
         value = ''
                 return ConsoleReporter.GroupType.VALUE
             else:
                 return ConsoleReporter.GroupType.FUNCTION
+    
+    @staticmethod
+    def count_digits(a):
+        ret = 0
+        while a > 0:
+            a //= 10
+            ret += 1
+        return ret
         
     def __init__(self, output_stream=sys.stdout, group_by=GroupType.VALUE):
         self.stream = output_stream
         self.group_by = group_by
     
-    @staticmethod
-    def __count_digits(a):
-        ret = 0
-        while a > 0:
-            a //= 10
-            ret += 1
-        return ret
-
+    def _make_key(self, title, length, key):
+        if len(title) >= length:
+            number = len(key)+1
+            digits = ConsoleReporter.count_digits(number)
+            key.append(title)            
+            return title[length-6-digits:] + "...[{0}]".format(number)
+        return title
+        
     def write_results(self, results):
         
         results = map(lambda x: x.calculate_stats(), results)
         title_len = len(first_title)
         while len(results) > 0:
             key = []
-            r = results[0]
-            
             
             length = 40 - title_len
             title_format = first_title + "{0:<" + str(length) + "}{1:>10}{2:>10}{3:>10}\n"
             
-            title_name = getter(r)
-            display_name = title_name
-            if len(title_name) >= length:
-                key.append(display_name)
-                display_name = display_name[length-6:] + "...[1]"
-                
+            title_name = getter(results[0])
+            display_name = self._make_key(title_name, length, key)
             
             self.stream.write(title_format.format(display_name, "user", "sys", "real"))
             self.stream.write("=" * 70 + "\n")
                 if (hasattr(r, "user_mean") and
                     hasattr(r, "system_mean") and hasattr(r, "wall_mean")):
                     
-                    o = other(r)
-                    if len(o) >= 40:
-                        key.append(o)
-                        digits = ConsoleReporter.__count_digits(len(key))
-                        o = o[35-digits:] + "...[{0}]".format(len(key))
-                        
+                    o = self._make_key(other(r), 40, key)
                     self.stream.write("{0:<40} {1:>9.4} {2:>9.4} {3:>9.4}\n"
                                       .format(o, r.user_mean,
                                               r.system_mean, r.wall_mean))