Home | History | Annotate | Download | only in update_payload
      1 #!/usr/bin/python
      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 import format_utils
     12 import histogram
     13 
     14 
     15 class HistogramTest(unittest.TestCase):
     16 
     17   @staticmethod
     18   def AddHumanReadableSize(size):
     19     fmt = format_utils.BytesToHumanReadable(size)
     20     return '%s (%s)' % (size, fmt) if fmt else str(size)
     21 
     22   def CompareToExpectedDefault(self, actual_str):
     23     expected_str = (
     24         'Yes |################    | 5 (83.3%)\n'
     25         'No  |###                 | 1 (16.6%)'
     26     )
     27     self.assertEqual(actual_str, expected_str)
     28 
     29   def testExampleHistogram(self):
     30     self.CompareToExpectedDefault(str(histogram.Histogram(
     31         [('Yes', 5), ('No', 1)])))
     32 
     33   def testFromCountDict(self):
     34     self.CompareToExpectedDefault(str(histogram.Histogram.FromCountDict(
     35         {'Yes': 5, 'No': 1})))
     36 
     37   def testFromKeyList(self):
     38     self.CompareToExpectedDefault(str(histogram.Histogram.FromKeyList(
     39         ['Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes'])))
     40 
     41   def testCustomScale(self):
     42     expected_str = (
     43         'Yes |#### | 5 (83.3%)\n'
     44         'No  |     | 1 (16.6%)'
     45     )
     46     actual_str = str(histogram.Histogram([('Yes', 5), ('No', 1)], scale=5))
     47     self.assertEqual(actual_str, expected_str)
     48 
     49   def testCustomFormatter(self):
     50     expected_str = (
     51         'Yes |################    | 5000 (4.8 KiB) (83.3%)\n'
     52         'No  |###                 | 1000 (16.6%)'
     53     )
     54     actual_str = str(histogram.Histogram(
     55         [('Yes', 5000), ('No', 1000)], formatter=self.AddHumanReadableSize))
     56     self.assertEqual(actual_str, expected_str)
     57 
     58 
     59 if __name__ == '__main__':
     60   unittest.main()
     61