blob: 643bb321791e657f8c8c783ec83d4bb631ffbf2b [file] [log] [blame]
Amin Hassanib05a65a2017-12-18 15:15:32 -08001#!/usr/bin/python2
Gilad Arnold553b0ec2013-01-26 01:00:39 -08002#
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
9import unittest
10
Amin Hassanib05a65a2017-12-18 15:15:32 -080011from update_payload import format_utils
12from update_payload import histogram
Gilad Arnold553b0ec2013-01-26 01:00:39 -080013
14
15class HistogramTest(unittest.TestCase):
Amin Hassanib05a65a2017-12-18 15:15:32 -080016 """ Tests histogram"""
Gilad Arnold553b0ec2013-01-26 01:00:39 -080017
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
60if __name__ == '__main__':
61 unittest.main()