1 #!/usr/bin/python2 2 # 3 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 """Unit tests for histogram.py.""" 8 9 import unittest 10 11 from update_payload import format_utils 12 from update_payload import histogram 13 14 15 class HistogramTest(unittest.TestCase): 16 """ Tests histogram""" 17 18 @staticmethod 19 def AddHumanReadableSize(size): 20 fmt = format_utils.BytesToHumanReadable(size) 21 return '%s (%s)' % (size, fmt) if fmt else str(size) 22 23 def CompareToExpectedDefault(self, actual_str): 24 expected_str = ( 25 'Yes |################ | 5 (83.3%)\n' 26 'No |### | 1 (16.6%)' 27 ) 28 self.assertEqual(actual_str, expected_str) 29 30 def testExampleHistogram(self): 31 self.CompareToExpectedDefault(str(histogram.Histogram( 32 [('Yes', 5), ('No', 1)]))) 33 34 def testFromCountDict(self): 35 self.CompareToExpectedDefault(str(histogram.Histogram.FromCountDict( 36 {'Yes': 5, 'No': 1}))) 37 38 def testFromKeyList(self): 39 self.CompareToExpectedDefault(str(histogram.Histogram.FromKeyList( 40 ['Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes']))) 41 42 def testCustomScale(self): 43 expected_str = ( 44 'Yes |#### | 5 (83.3%)\n' 45 'No | | 1 (16.6%)' 46 ) 47 actual_str = str(histogram.Histogram([('Yes', 5), ('No', 1)], scale=5)) 48 self.assertEqual(actual_str, expected_str) 49 50 def testCustomFormatter(self): 51 expected_str = ( 52 'Yes |################ | 5000 (4.8 KiB) (83.3%)\n' 53 'No |### | 1000 (16.6%)' 54 ) 55 actual_str = str(histogram.Histogram( 56 [('Yes', 5000), ('No', 1000)], formatter=self.AddHumanReadableSize)) 57 self.assertEqual(actual_str, expected_str) 58 59 60 if __name__ == '__main__': 61 unittest.main() 62