Exemplo n.º 1
0
    def testCompare(self):
        size1 = Size("1 GiB")
        size2 = Size("2 GiB")

        self.assertTrue(size2 > size1)
        self.assertFalse(size1 > size2)
        self.assertTrue(size2 >= size1)
        self.assertFalse(size1 >= size2)
        self.assertTrue(size1 >= size1)

        self.assertFalse(size2 < size1)
        self.assertTrue(size1 < size2)
        self.assertFalse(size2 <= size1)
        self.assertTrue(size1 <= size2)
        self.assertTrue(size1 <= size1)

        self.assertTrue(size1 > None)
        self.assertTrue(size1 >= None)
        self.assertFalse(size1 < None)
        self.assertFalse(size1 <= None)
        self.assertFalse(size1 == None)
        self.assertTrue(size1 != None)

        size3 = Size(0)
        self.assertTrue(size3 > None)
        self.assertFalse(size3 < None)
        self.assertTrue(size3 != None)
Exemplo n.º 2
0
 def testSettingConfiguration(self):
     """ Test that setting configuration to different values has effect. """
     s = Size(64, GiB)
     s.set_str_config(StrConfig(strip=False))
     prev = str(s)
     s.set_str_config(StrConfig(strip=True))
     subs = str(s)
     self.assertTrue(subs != prev)
    def testOperatorMinus(self):
        result = Size("1000 B") - Size("100 B")
        actual = result.get_bytes()
        expected = 900
        self.assertEqual(actual, expected)

        result = Size("1000 B") - 10
        actual = result.get_bytes()
        expected = 990
        self.assertEqual(actual, expected)

        result = 10 - Size("100 B")
        actual = result.get_bytes()
        expected = -90
        self.assertEqual(actual, expected)

        size1 = Size("100 B")
        size1 -= Size("10 B")
        actual = size1.get_bytes()
        expected = 90
        self.assertEqual(actual, expected)

        size1 = Size("100 B")
        size1 -= 10
        actual = size1.get_bytes()
        expected = 90
        self.assertEqual(actual, expected)

        # TODO shouldn't be the result int?
        int1 = 1000
        int1 -= Size("100 B")
        actual = int1.get_bytes()
        expected = 900
        self.assertEqual(actual, expected)
    def testOperatorPlus(self):
        result = Size("1000 B") + Size("100 B")
        actual = result.get_bytes()
        expected = 1100
        self.assertEqual(actual, expected)

        result = Size("1000 B") + 10
        actual = result.get_bytes()
        expected = 1010
        self.assertEqual(actual, expected)

        result =  10 + Size("1000 B")
        actual = result.get_bytes()
        expected = 1010
        self.assertEqual(actual, expected)

        size1 = Size("100 B")
        size1 += Size("100 B")
        actual = size1.get_bytes()
        expected = 200
        self.assertEqual(actual, expected)

        size1 = Size("100 B")
        size1 += 100
        actual = size1.get_bytes()
        expected = 200
        self.assertEqual(actual, expected)

        # TODO shouldn't be the result int?
        int1 = 10
        int1 += Size("100 B")
        actual = int1.get_bytes()
        expected = 110
        self.assertEqual(actual, expected)
    def testOperatorDiv(self):
        actual = Size("100 B") / Size("10 B")
        expected = Decimal("10")
        self.assertEqual(actual, expected)

        actual = Size("120 B") / Size("100 B")
        expected = Decimal("1.2")
        self.assertEqual(actual, expected)

        actual = Size("120 B") // Size("100 B")
        expected = Decimal("1")
        self.assertEqual(actual, expected)

        actual = Size("128 EiB") // Size("64 EiB")
        expected = 2
        self.assertEqual(actual, expected)

        actual = Size("100 B") / 10
        expected = Decimal("10")
        self.assertEqual(actual, expected)

        actual = Size("120 B") / 100
        expected = Decimal("1.2")
        self.assertEqual(actual, expected)

        actual = Size("128 EiB") // 2
        expected = Size("64 EiB")
        self.assertEqual(actual, expected)

        result = Size("120 B") // 100
        actual = result.get_bytes()
        expected = 1
        self.assertEqual(actual, expected)

        size1 = Size("120 B")
        size1 /= Size("100 B")
        actual = size1
        expected = Decimal("1.2")
        self.assertEqual(actual, expected)

        size1 = Size("120 B")
        size1 /= 100
        actual = size1
        expected = Decimal("1.2")
        self.assertEqual(actual, expected)

        size1 = Size("120 B")
        size1 //= Size("100 B")
        actual = size1
        expected = Decimal("1")
        self.assertEqual(actual, expected)
Exemplo n.º 6
0
    def testRoundToNearest(self):
        size = Size("1.5 KiB")
        conv = size.round_to_nearest(Size("1 KiB"), rounding=ROUND_UP)
        self.assertEqual(conv, Size("2 KiB"))

        conv = size.round_to_nearest(KiB, rounding=ROUND_DOWN)
        self.assertEqual(conv, Size("1 KiB"))

        with self.assertRaises(ValueError):
            size.round_to_nearest(-1, rounding=ROUND_UP)
Exemplo n.º 7
0
    def testEquality(self):
        size1 = Size("1 GiB")
        size2 = Size("2 GiB")

        self.assertTrue(size1 == size1)
        self.assertTrue(size2 == size2)
        self.assertFalse(size1 == size2)
        self.assertFalse(size2 == size1)

        self.assertFalse(size1 == None)
        self.assertFalse(size1 == 0)

        size3 = Size(0)
        self.assertTrue(size3 == 0)
Exemplo n.º 8
0
    def testStrWithSmallDeviations(self):
        """ Behavior when deviation from whole value is small. """
        Size.set_str_config(StrConfig(strip=True))

        eps = Decimal(1024)/100/2 # 1/2 of 1% of 1024

        # deviation is less than 1/2 of 1% of 1024
        s = Size(16384 - (eps - 1))
        self.assertEqual(str(s), "16 KiB")

        # deviation is greater than 1/2 of 1% of 1024
        s = Size(16384 - (eps + 1))
        self.assertEqual(str(s), "15.99 KiB")
        # deviation is greater than 1/2 of 1% of 1024
        s = Size(16384 + (eps + 1))
        self.assertEqual(str(s), "16.01 KiB")

        # deviation is less than 1/2 of 1% of 1024
        s = Size(16384 + (eps - 1))
        self.assertEqual(str(s), "16 KiB")
Exemplo n.º 9
0
    def testConvertTo(self):
        size = Size("1.5 KiB")
        conv = size.convert_to("KiB")
        self.assertEqual(conv, Decimal("1.5"))

        locale.setlocale(locale.LC_ALL, 'cs_CZ.UTF-8')
        size = Size("1.5 KiB")
        conv = size.convert_to("KiB")
        self.assertEqual(conv, Decimal("1.5"))

        # this persian locale uses a two-byte unicode character for the radix
        locale.setlocale(locale.LC_ALL, 'ps_AF.UTF-8')
        size = Size("1.5 KiB")
        conv = size.convert_to("KiB")
        self.assertEqual(conv, Decimal("1.5"))

        locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
Exemplo n.º 10
0
    def testHashable(self):
        size = Size("1 KiB")
        hs = hash(size)
        self.assertIsNotNone(hs)

        size_set = set((Size("1 KiB"), Size("1 KiB"), Size("1 KiB"), Size("2 KiB"), Size(0)))
        self.assertEqual(len(size_set), 3)
    def testOperatorMul(self):
        result = Size("4 B") * 3
        actual = result.get_bytes()
        expected = 12
        self.assertEqual(actual, expected)

        result = 3 * Size("4 B")
        actual = result.get_bytes()
        expected = 12
        self.assertEqual(actual, expected)

        size1 = Size("4 B")
        size1 *= 3
        actual = size1.get_bytes()
        expected = 12
        self.assertEqual(actual, expected)

        int1 = 4
        int1 *= Size("3 B")
        actual = int1.get_bytes()
        expected = 12
        self.assertEqual(actual, expected)
Exemplo n.º 12
0
    def testDivMod(self):
        size1 = Size("120 B")
        q, mod = divmod(size1, Size("100 B"))

        self.assertEqual(q, 1)
        self.assertEqual(mod, Size("20 B"))

        size1 = Size("250 MiB")
        q, mod = divmod(size1, Size("100 MiB"))

        self.assertEqual(q, 2)
        self.assertEqual(mod, Size("50 MiB"))

        size1 = Size("10 GiB") + Size("5 GiB")
        q, mod = divmod(size1, 7)
        self.assertEqual(q, 2300875337)
        self.assertEqual(mod, 1)
Exemplo n.º 13
0
    def testOperatorMul(self):
        result = Size("4 B") * 3
        actual = result.get_bytes()
        expected = 12
        self.assertEqual(actual, expected)

        result = 3 * Size("4 B")
        actual = result.get_bytes()
        expected = 12
        self.assertEqual(actual, expected)

        size1 = Size("4 B")
        size1 *= 3
        actual = size1.get_bytes()
        expected = 12
        self.assertEqual(actual, expected)

        int1 = 4
        int1 *= Size("3 B")
        actual = int1.get_bytes()
        expected = 12
        self.assertEqual(actual, expected)
    def testConvertTo(self):
        size = Size("1.5 KiB")
        conv = size.convert_to("KiB")
        self.assertEqual(conv, Decimal("1.5"))

        locale.setlocale(locale.LC_ALL,'cs_CZ.UTF-8')
        size = Size("1.5 KiB")
        conv = size.convert_to("KiB")
        self.assertEqual(conv, Decimal("1.5"))

        # this persian locale uses a two-byte unicode character for the radix
        locale.setlocale(locale.LC_ALL, 'ps_AF.UTF-8')
        size = Size("1.5 KiB")
        conv = size.convert_to("KiB")
        self.assertEqual(conv, Decimal("1.5"))

        locale.setlocale(locale.LC_ALL,'en_US.UTF-8')
Exemplo n.º 15
0
    def testNeg(self):
        size1 = Size("1 KiB")
        self.assertEqual(-1024, -size1)

        size1 = Size("-1 KiB")
        self.assertEqual(1024, -size1)
Exemplo n.º 16
0
    def testAbs(self):
        size1 = Size("1 GiB")
        self.assertEqual(size1, abs(size1))

        size2 = Size("-2 GiB")
        self.assertEqual(size2, -1 * abs(size2))
Exemplo n.º 17
0
    def testBool(self):
        size1 = Size("1 GiB")
        size2 = Size(0)

        self.assertTrue(size1)
        self.assertFalse(size2)
Exemplo n.º 18
0
 def uploaded(self):
     return Size(int(self._dictionary['uploaded']))
Exemplo n.º 19
0
 def downloaded(self):
     return Size(int(self._dictionary['downloaded']))
Exemplo n.º 20
0
 def testSIUnits(self):
     """ Test binary_units param. """
     s = Size(1000)
     self.assertEqual(s.components(binary_units=False), (1, KB))
Exemplo n.º 21
0
 def testExceptionValues(self):
     """ Test that exceptions are properly raised on bad params. """
     s = Size(500)
     with self.assertRaises(SizeValueError):
         s.components(min_value=-1)
Exemplo n.º 22
0
 def testInt(self):
     # just to make sure Size can be correctly interpreted as int in python 3.10
     ctypes.c_int(Size("1 GiB"))
Exemplo n.º 23
0
    def testRMethods(self):
        """ Test certain r* methods. These methods must be invoked
            explicitly, rather than by means of an operator, in order
            to be executed. Otherwise, their companion non-R method
            will be invoked instead.
        """
        s = Size(2, GiB)

        # rtruediv, retains fractional quantities
        self.assertEqual(s.__rtruediv__(s), Fraction(1))
        with self.assertRaises(SizeNonsensicalOpError):
            s.__rtruediv__("str") # pylint: disable=pointless-statement

        # rdivmod
        self.assertEqual(
            Size(16, MiB).__rdivmod__(Size(24, MiB)),
            (1, Size(8, MiB))
        )
        with self.assertRaises(SizeNonsensicalOpError):
            divmod(s.__rdivmod__("str"))

        # // rfloordiv
        self.assertEqual(s.__rfloordiv__(s), 1)
        with self.assertRaises(SizeNonsensicalOpError):
            s.__rfloordiv__("str") # pylint: disable=pointless-statement

        # rmod
        self.assertEqual(s.__rmod__(s), Size(0))
        with self.assertRaises(SizeNonsensicalOpError):
            s.__rmod__("str") # pylint: disable=expression-not-assigned, pointless-statement

        # rsub
        self.assertEqual(s.__rsub__(s), Size(0))
        with self.assertRaises(SizeNonsensicalOpError):
            s.__rsub__(2) # pylint: disable=pointless-statement
Exemplo n.º 24
0
 def download(self):
     return Size(self._dictionary['Download'])
Exemplo n.º 25
0
    def testMinValue(self):
        """ Test behavior on min_value parameter. """
        s = Size(9, MiB)
        self.assertEqual(s.components(), (Fraction(9, 1), MiB))
        self.assertEqual(s.components(min_value=10), (Fraction(9216, 1), KiB))

        s = Size("0.5", GiB)
        self.assertEqual(s.components(min_value=1), (Fraction(512, 1), MiB))
        self.assertEqual(
           s.components(min_value=Decimal("0.1")),
           (Fraction(1, 2), GiB)
        )
        self.assertEqual(
           s.components(min_value=Decimal(1)),
           (Fraction(512, 1), MiB)
        )

        # when min_value is 10 and single digit on left of decimal, display
        # with smaller unit.
        s = Size('7.18', KiB)
        self.assertEqual(s.components(min_value=10)[1], B)
        s = Size('9.68', TiB)
        self.assertEqual(s.components(min_value=10)[1], GiB)
        s = Size('4.29', MiB)
        self.assertEqual(s.components(min_value=10)[1], KiB)

        # when min value is 100 and two digits on left of decimal
        s = Size('14', MiB)
        self.assertEqual(
           s.components(min_value=100),
           (Fraction(14 * 1024, 1), KiB)
        )
Exemplo n.º 26
0
    def testStrConfigs(self):
        """ Test str with various configuration options. """
        Size.set_str_config(StrConfig(strip=True))

        # exactly 4 Pi
        s = Size(0x10000000000000)
        self.assertEqual(str(s), "4 PiB")

        s = Size(300, MiB)
        self.assertEqual(str(s), "300 MiB")

        s = Size('12.6998', TiB)
        self.assertEqual(str(s), "12.7 TiB")

        # byte values close to multiples of 2 are shown without trailing zeros
        s = Size(0xff)
        self.assertEqual(str(s), "255 B")

        # a fractional quantity is shown if the value deviates
        # from the whole number of units by more than 1%
        s = Size(16384 - (Decimal(1024)/100 + 1))
        self.assertEqual(str(s), "15.99 KiB")

        # test a very large quantity with no associated abbreviation or prefix
        s = Size(1024**9)
        self.assertEqual(str(s), "1024 YiB")
        s = Size(1024**9 - 1)
        self.assertEqual(str(s), "1024 YiB")
        s = Size(1024**10)
        self.assertEqual(str(s), "1048576 YiB")

        s = Size(0xfffffffffffff)
        self.assertEqual(str(s), "4 PiB")

        s = Size(0xffff)
        # value is not exactly 64 KiB, but w/ 2 places, value is 64.00 KiB
        # so the trailing 0s are stripped.
        self.assertEqual(str(s), "64 KiB")

        Size.set_str_config(StrConfig(max_places=3, strip=True))
        s = Size('23.7874', TiB)
        self.assertEqual(str(s), "23.787 TiB")

        Size.set_str_config(StrConfig(min_value=10, strip=True))
        s = Size(8193)
        self.assertEqual(str(s), ("8193 B"))

        # if max_places is set to None, all digits are displayed
        Size.set_str_config(StrConfig(max_places=None, strip=True))
        s = Size(0xfffffffffffff)
        self.assertEqual(str(s), "3.9999999999999991118215803 PiB")
        s = Size(0x10000)
        self.assertEqual(str(s), ("64 KiB"))
        s = Size(0x10001)
        self.assertEqual(str(s), "64.0009765625 KiB")

        Size.set_str_config(StrConfig(strip=False))
        s = Size(1024**9 + 1)
        self.assertEqual(str(s), "1024.00 YiB")

        s = Size(0xfffff)
        self.assertEqual(str(s), "1024.00 KiB")
Exemplo n.º 27
0
    def testDeepCopy(self):
        size1 = Size("1 GiB")
        size2 = copy.deepcopy(size1)

        self.assertIsNot(size1, size2)
        self.assertEqual(size1, size2)
Exemplo n.º 28
0
    def testOperatorMinus(self):
        result = Size("1000 B") - Size("100 B")
        actual = result.get_bytes()
        expected = 900
        self.assertEqual(actual, expected)

        result = Size("1000 B") - 10
        actual = result.get_bytes()
        expected = 990
        self.assertEqual(actual, expected)

        result = 10 - Size("100 B")
        actual = result.get_bytes()
        expected = -90
        self.assertEqual(actual, expected)

        size1 = Size("100 B")
        size1 -= Size("10 B")
        actual = size1.get_bytes()
        expected = 90
        self.assertEqual(actual, expected)

        size1 = Size("100 B")
        size1 -= 10
        actual = size1.get_bytes()
        expected = 90
        self.assertEqual(actual, expected)

        # TODO shouldn't be the result int?
        int1 = 1000
        int1 -= Size("100 B")
        actual = int1.get_bytes()
        expected = 900
        self.assertEqual(actual, expected)

        size1 = Size("100 B") - None
        self.assertEqual(size1.get_bytes(), 100)
Exemplo n.º 29
0
    def testOperatorPlus(self):
        result = Size("1000 B") + Size("100 B")
        actual = result.get_bytes()
        expected = 1100
        self.assertEqual(actual, expected)

        result = Size("1000 B") + 10
        actual = result.get_bytes()
        expected = 1010
        self.assertEqual(actual, expected)

        result = 10 + Size("1000 B")
        actual = result.get_bytes()
        expected = 1010
        self.assertEqual(actual, expected)

        size1 = Size("100 B")
        size1 += Size("100 B")
        actual = size1.get_bytes()
        expected = 200
        self.assertEqual(actual, expected)

        size1 = Size("100 B")
        size1 += 100
        actual = size1.get_bytes()
        expected = 200
        self.assertEqual(actual, expected)

        # TODO shouldn't be the result int?
        int1 = 10
        int1 += Size("100 B")
        actual = int1.get_bytes()
        expected = 110
        self.assertEqual(actual, expected)

        size1 = Size("100 B") + None
        self.assertEqual(size1.get_bytes(), 100)
Exemplo n.º 30
0
 def tearDown(self):
     """ Reset configuration to default. """
     Size.set_str_config(Defaults.STR_CONFIG)
Exemplo n.º 31
0
 def size(self):
     return Size(int(self._dictionary["Size"]))
Exemplo n.º 32
0
    def testOperatorDiv(self):
        actual = Size("100 B") / Size("10 B")
        expected = Decimal("10")
        self.assertEqual(actual, expected)

        actual = Size("120 B") / Size("100 B")
        expected = Decimal("1.2")
        self.assertEqual(actual, expected)

        actual = Size("120 B") // Size("100 B")
        expected = Decimal("1")
        self.assertEqual(actual, expected)

        actual = Size("128 EiB") // Size("64 EiB")
        expected = 2
        self.assertEqual(actual, expected)

        actual = Size("100 B") / 10
        expected = Decimal("10")
        self.assertEqual(actual, expected)

        actual = Size("120 B") / 100
        expected = Decimal("1.2")
        self.assertEqual(actual, expected)

        actual = Size("128 EiB") // 2
        expected = Size("64 EiB")
        self.assertEqual(actual, expected)

        result = Size("120 B") // 100
        actual = result.get_bytes()
        expected = 1
        self.assertEqual(actual, expected)

        size1 = Size("120 B")
        size1 /= Size("100 B")
        actual = size1
        expected = Decimal("1.2")
        self.assertEqual(actual, expected)

        size1 = Size("120 B")
        size1 /= 100
        actual = size1
        expected = Decimal("1.2")
        self.assertEqual(actual, expected)

        size1 = Size("120 B")
        size1 //= Size("100 B")
        actual = size1
        expected = Decimal("1")
        self.assertEqual(actual, expected)
Exemplo n.º 33
0
 def testNegative(self):
     """ Test construction of negative sizes. """
     s = Size(-500, MiB)
     self.assertEqual(s.components(), (Fraction(-500, 1), MiB))
     self.assertEqual(s.convertTo(B), -524288000)
Exemplo n.º 34
0
 def testRoundingToBytes(self):
     """ Test that second part is B when rounding to bytes. """
     s = Size(500)
     self.assertEqual(s.components()[1], B)
Exemplo n.º 35
0
 def size(self):
     return Size(int(self._dictionary['Size']))
Exemplo n.º 36
0
 def upload(self):
     return Size(self._dictionary['Upload'])