histogram_unittest.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/python2
  2. #
  3. # 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. #
  17. """Unit tests for histogram.py."""
  18. import unittest
  19. from update_payload import format_utils
  20. from update_payload import histogram
  21. class HistogramTest(unittest.TestCase):
  22. """ Tests histogram"""
  23. @staticmethod
  24. def AddHumanReadableSize(size):
  25. fmt = format_utils.BytesToHumanReadable(size)
  26. return '%s (%s)' % (size, fmt) if fmt else str(size)
  27. def CompareToExpectedDefault(self, actual_str):
  28. expected_str = (
  29. 'Yes |################ | 5 (83.3%)\n'
  30. 'No |### | 1 (16.6%)'
  31. )
  32. self.assertEqual(actual_str, expected_str)
  33. def testExampleHistogram(self):
  34. self.CompareToExpectedDefault(str(histogram.Histogram(
  35. [('Yes', 5), ('No', 1)])))
  36. def testFromCountDict(self):
  37. self.CompareToExpectedDefault(str(histogram.Histogram.FromCountDict(
  38. {'Yes': 5, 'No': 1})))
  39. def testFromKeyList(self):
  40. self.CompareToExpectedDefault(str(histogram.Histogram.FromKeyList(
  41. ['Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes'])))
  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. 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. if __name__ == '__main__':
  58. unittest.main()