blob: e757dd02f39c4d7bf2672e4c1fbada22587c7e8c [file] [log] [blame]
Amin Hassanib05a65a2017-12-18 15:15:32 -08001#!/usr/bin/python2
Gilad Arnold553b0ec2013-01-26 01:00:39 -08002#
Amin Hassanif94b6432018-01-26 17:39:47 -08003# Copyright (C) 2013 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Gilad Arnold553b0ec2013-01-26 01:00:39 -080017
18"""Unit tests for histogram.py."""
19
20import unittest
21
Amin Hassanib05a65a2017-12-18 15:15:32 -080022from update_payload import format_utils
23from update_payload import histogram
Gilad Arnold553b0ec2013-01-26 01:00:39 -080024
25
26class HistogramTest(unittest.TestCase):
Amin Hassanib05a65a2017-12-18 15:15:32 -080027 """ Tests histogram"""
Gilad Arnold553b0ec2013-01-26 01:00:39 -080028
29 @staticmethod
30 def AddHumanReadableSize(size):
31 fmt = format_utils.BytesToHumanReadable(size)
32 return '%s (%s)' % (size, fmt) if fmt else str(size)
33
34 def CompareToExpectedDefault(self, actual_str):
35 expected_str = (
36 'Yes |################ | 5 (83.3%)\n'
37 'No |### | 1 (16.6%)'
38 )
39 self.assertEqual(actual_str, expected_str)
40
41 def testExampleHistogram(self):
42 self.CompareToExpectedDefault(str(histogram.Histogram(
43 [('Yes', 5), ('No', 1)])))
44
45 def testFromCountDict(self):
46 self.CompareToExpectedDefault(str(histogram.Histogram.FromCountDict(
47 {'Yes': 5, 'No': 1})))
48
49 def testFromKeyList(self):
50 self.CompareToExpectedDefault(str(histogram.Histogram.FromKeyList(
51 ['Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes'])))
52
53 def testCustomScale(self):
54 expected_str = (
55 'Yes |#### | 5 (83.3%)\n'
56 'No | | 1 (16.6%)'
57 )
58 actual_str = str(histogram.Histogram([('Yes', 5), ('No', 1)], scale=5))
59 self.assertEqual(actual_str, expected_str)
60
61 def testCustomFormatter(self):
62 expected_str = (
63 'Yes |################ | 5000 (4.8 KiB) (83.3%)\n'
64 'No |### | 1000 (16.6%)'
65 )
66 actual_str = str(histogram.Histogram(
67 [('Yes', 5000), ('No', 1000)], formatter=self.AddHumanReadableSize))
68 self.assertEqual(actual_str, expected_str)
69
70
71if __name__ == '__main__':
72 unittest.main()