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 format_utils.py."""
8
9import unittest
10
11import format_utils
12
13
14class NumToPercentTest(unittest.TestCase):
15  def testHundredPercent(self):
16    self.assertEqual(format_utils.NumToPercent(1, 1), '100%')
17
18  def testOverHundredPercent(self):
19    self.assertEqual(format_utils.NumToPercent(5, 2), '250%')
20
21  def testWholePercent(self):
22    self.assertEqual(format_utils.NumToPercent(3, 10), '30%')
23
24  def testDefaultMinPrecision(self):
25    self.assertEqual(format_utils.NumToPercent(3, 9), '33.3%')
26    self.assertEqual(format_utils.NumToPercent(3, 900), '0.3%')
27
28  def testDefaultMaxPrecision(self):
29    self.assertEqual(format_utils.NumToPercent(3, 9000000), '0.00003%')
30    self.assertEqual(format_utils.NumToPercent(3, 90000000), '0%')
31
32  def testCustomMinPrecision(self):
33    self.assertEqual(format_utils.NumToPercent(3, 9, min_precision=3),
34                     '33.333%')
35    self.assertEqual(format_utils.NumToPercent(3, 9, min_precision=0),
36                     '33%')
37
38  def testCustomMaxPrecision(self):
39    self.assertEqual(format_utils.NumToPercent(3, 900, max_precision=1),
40                     '0.3%')
41    self.assertEqual(format_utils.NumToPercent(3, 9000, max_precision=1),
42                     '0%')
43
44
45class BytesToHumanReadableTest(unittest.TestCase):
46  def testBaseTwo(self):
47    self.assertEqual(format_utils.BytesToHumanReadable(0x1000), '4 KiB')
48    self.assertEqual(format_utils.BytesToHumanReadable(0x400000), '4 MiB')
49    self.assertEqual(format_utils.BytesToHumanReadable(0x100000000), '4 GiB')
50    self.assertEqual(format_utils.BytesToHumanReadable(0x40000000000), '4 TiB')
51
52  def testDecimal(self):
53    self.assertEqual(format_utils.BytesToHumanReadable(5000, decimal=True),
54                     '5 kB')
55    self.assertEqual(format_utils.BytesToHumanReadable(5000000, decimal=True),
56                     '5 MB')
57    self.assertEqual(format_utils.BytesToHumanReadable(5000000000,
58                                                       decimal=True),
59                     '5 GB')
60
61  def testDefaultPrecision(self):
62    self.assertEqual(format_utils.BytesToHumanReadable(5000), '4.8 KiB')
63    self.assertEqual(format_utils.BytesToHumanReadable(500000), '488.2 KiB')
64    self.assertEqual(format_utils.BytesToHumanReadable(5000000), '4.7 MiB')
65
66  def testCustomPrecision(self):
67    self.assertEqual(format_utils.BytesToHumanReadable(5000, precision=3),
68                     '4.882 KiB')
69    self.assertEqual(format_utils.BytesToHumanReadable(500000, precision=0),
70                     '488 KiB')
71    self.assertEqual(format_utils.BytesToHumanReadable(5000000, precision=5),
72                     '4.76837 MiB')
73
74
75if __name__ == '__main__':
76  unittest.main()
77