1# Copyright 2012 Google Inc. All Rights Reserved. 2"""Tests for the tabulator module.""" 3 4from __future__ import print_function 5 6__author__ = 'asharif@google.com (Ahmad Sharif)' 7 8# System modules 9import unittest 10 11# Local modules 12import tabulator 13 14 15class TabulatorTest(unittest.TestCase): 16 """Tests for the Tabulator class.""" 17 18 def testResult(self): 19 table = ['k1', ['1', '3'], ['55']] 20 result = tabulator.Result() 21 cell = tabulator.Cell() 22 result.Compute(cell, table[2], table[1]) 23 expected = ' '.join([str(float(v)) for v in table[2]]) 24 self.assertTrue(cell.value == expected) 25 26 result = tabulator.AmeanResult() 27 cell = tabulator.Cell() 28 result.Compute(cell, table[2], table[1]) 29 self.assertTrue(cell.value == float(table[2][0])) 30 31 def testStringMean(self): 32 smr = tabulator.StringMeanResult() 33 cell = tabulator.Cell() 34 value = 'PASS' 35 values = [value for _ in range(3)] 36 smr.Compute(cell, values, None) 37 self.assertTrue(cell.value == value) 38 values.append('FAIL') 39 smr.Compute(cell, values, None) 40 self.assertTrue(cell.value == '?') 41 42 def testStorageFormat(self): 43 sf = tabulator.StorageFormat() 44 cell = tabulator.Cell() 45 base = 1024.0 46 cell.value = base 47 sf.Compute(cell) 48 self.assertTrue(cell.string_value == '1.0K') 49 cell.value = base**2 50 sf.Compute(cell) 51 self.assertTrue(cell.string_value == '1.0M') 52 cell.value = base**3 53 sf.Compute(cell) 54 self.assertTrue(cell.string_value == '1.0G') 55 56 def testLerp(self): 57 c1 = tabulator.Color(0, 0, 0, 0) 58 c2 = tabulator.Color(255, 0, 0, 0) 59 c3 = tabulator.Color.Lerp(0.5, c1, c2) 60 self.assertTrue(c3.r == 127.5) 61 self.assertTrue(c3.g == 0) 62 self.assertTrue(c3.b == 0) 63 self.assertTrue(c3.a == 0) 64 c3.Round() 65 self.assertTrue(c3.r == 127) 66 67 def testGmean(self): 68 a = [1.0e+308] * 3 69 # pylint: disable=protected-access 70 b = tabulator.Result()._GetGmean(a) 71 self.assertTrue(b >= 0.99e+308 and b <= 1.01e+308) 72 73 def testTableGenerator(self): 74 runs = [[{'k1': '10', 75 'k2': '12'}, {'k1': '13', 76 'k2': '14', 77 'k3': '15'}], [{'k1': '50', 78 'k2': '51', 79 'k3': '52', 80 'k4': '53'}]] 81 labels = ['vanilla', 'modified'] 82 tg = tabulator.TableGenerator(runs, labels) 83 table = tg.GetTable() 84 header = table.pop(0) 85 86 self.assertTrue(header == ['keys', 'vanilla', 'modified']) 87 row = table.pop(0) 88 self.assertTrue(row == ['k1', ['10', '13'], ['50']]) 89 row = table.pop(0) 90 self.assertTrue(row == ['k2', ['12', '14'], ['51']]) 91 row = table.pop(0) 92 self.assertTrue(row == ['k3', [None, '15'], ['52']]) 93 row = table.pop(0) 94 self.assertTrue(row == ['k4', [None, None], ['53']]) 95 96 table = tg.GetTable() 97 columns = [ 98 tabulator.Column(tabulator.AmeanResult(), tabulator.Format()), 99 tabulator.Column(tabulator.AmeanRatioResult(), 100 tabulator.PercentFormat()), 101 ] 102 tf = tabulator.TableFormatter(table, columns) 103 table = tf.GetCellTable() 104 self.assertTrue(table) 105 106 def testColspan(self): 107 simple_table = [ 108 ['binary', 'b1', 'b2', 'b3'], 109 ['size', 100, 105, 108], 110 ['rodata', 100, 80, 70], 111 ['data', 100, 100, 100], 112 ['debug', 100, 140, 60], 113 ] 114 columns = [ 115 tabulator.Column(tabulator.AmeanResult(), tabulator.Format()), 116 tabulator.Column(tabulator.MinResult(), tabulator.Format()), 117 tabulator.Column(tabulator.AmeanRatioResult(), 118 tabulator.PercentFormat()), 119 tabulator.Column(tabulator.AmeanRatioResult(), 120 tabulator.ColorBoxFormat()), 121 ] 122 our_table = [simple_table[0]] 123 for row in simple_table[1:]: 124 our_row = [row[0]] 125 for v in row[1:]: 126 our_row.append([v]) 127 our_table.append(our_row) 128 129 tf = tabulator.TableFormatter(our_table, columns) 130 cell_table = tf.GetCellTable() 131 self.assertTrue(cell_table[0][0].colspan == 1) 132 self.assertTrue(cell_table[0][1].colspan == 2) 133 self.assertTrue(cell_table[0][2].colspan == 4) 134 self.assertTrue(cell_table[0][3].colspan == 4) 135 for row in cell_table[1:]: 136 for cell in row: 137 self.assertTrue(cell.colspan == 1) 138 139 140if __name__ == '__main__': 141 unittest.main() 142