• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
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
18"""Unit tests for format_utils.py."""
19
20# Disable check for function names to avoid errors based on old code
21# pylint: disable-msg=invalid-name
22
23from __future__ import absolute_import
24
25import unittest
26
27from update_payload import format_utils
28
29
30class NumToPercentTest(unittest.TestCase):
31  """ Tests number conversion to percentage format."""
32  def testHundredPercent(self):
33    self.assertEqual(format_utils.NumToPercent(1, 1), '100%')
34
35  def testOverHundredPercent(self):
36    self.assertEqual(format_utils.NumToPercent(5, 2), '250%')
37
38  def testWholePercent(self):
39    self.assertEqual(format_utils.NumToPercent(3, 10), '30%')
40
41  def testDefaultMinPrecision(self):
42    self.assertEqual(format_utils.NumToPercent(3, 9), '33.3%')
43    self.assertEqual(format_utils.NumToPercent(3, 900), '0.3%')
44
45  def testDefaultMaxPrecision(self):
46    self.assertEqual(format_utils.NumToPercent(3, 9000000), '0.00003%')
47    self.assertEqual(format_utils.NumToPercent(3, 90000000), '0%')
48
49  def testCustomMinPrecision(self):
50    self.assertEqual(format_utils.NumToPercent(3, 9, min_precision=3),
51                     '33.333%')
52    self.assertEqual(format_utils.NumToPercent(3, 9, min_precision=0),
53                     '33%')
54
55  def testCustomMaxPrecision(self):
56    self.assertEqual(format_utils.NumToPercent(3, 900, max_precision=1),
57                     '0.3%')
58    self.assertEqual(format_utils.NumToPercent(3, 9000, max_precision=1),
59                     '0%')
60
61
62class BytesToHumanReadableTest(unittest.TestCase):
63  """ Tests number conversion to human readable format."""
64  def testBaseTwo(self):
65    self.assertEqual(format_utils.BytesToHumanReadable(0x1000), '4 KiB')
66    self.assertEqual(format_utils.BytesToHumanReadable(0x400000), '4 MiB')
67    self.assertEqual(format_utils.BytesToHumanReadable(0x100000000), '4 GiB')
68    self.assertEqual(format_utils.BytesToHumanReadable(0x40000000000), '4 TiB')
69
70  def testDecimal(self):
71    self.assertEqual(format_utils.BytesToHumanReadable(5000, decimal=True),
72                     '5 kB')
73    self.assertEqual(format_utils.BytesToHumanReadable(5000000, decimal=True),
74                     '5 MB')
75    self.assertEqual(format_utils.BytesToHumanReadable(5000000000,
76                                                       decimal=True),
77                     '5 GB')
78
79  def testDefaultPrecision(self):
80    self.assertEqual(format_utils.BytesToHumanReadable(5000), '4.8 KiB')
81    self.assertEqual(format_utils.BytesToHumanReadable(500000), '488.2 KiB')
82    self.assertEqual(format_utils.BytesToHumanReadable(5000000), '4.7 MiB')
83
84  def testCustomPrecision(self):
85    self.assertEqual(format_utils.BytesToHumanReadable(5000, precision=3),
86                     '4.882 KiB')
87    self.assertEqual(format_utils.BytesToHumanReadable(500000, precision=0),
88                     '488 KiB')
89    self.assertEqual(format_utils.BytesToHumanReadable(5000000, precision=5),
90                     '4.76837 MiB')
91
92
93if __name__ == '__main__':
94  unittest.main()
95