def testCmpBytes(self): x = Size.new_from_str("1 KiB") # result > 0 y = 1023 cmpResult = Size.cmp_bytes(x, y, False) self.assertGreater(cmpResult, 0) # result < 0 y = 1025 cmpResult = Size.cmp_bytes(x, y, False) self.assertLess(cmpResult, 0) # result == 0 y = 1024 cmpResult = Size.cmp_bytes(x, y, False) self.assertEqual(cmpResult, 0) # test with abs == True x = Size.new_from_str("-1 KiB") # result > 0 y = 1023 cmpResult = Size.cmp_bytes(x, y, True) self.assertGreater(cmpResult, 0) # result < 0 y = 1025 cmpResult = Size.cmp_bytes(x, y, True) self.assertLess(cmpResult, 0) # result == 0 y = 1024 cmpResult = Size.cmp_bytes(x, y, True) self.assertEqual(cmpResult, 0)
def testMod(self): x = Size.new_from_str("1024 B") y = Size.new_from_str("1000 B") actual = x.mod(y).get_bytes() expected = (24, 1) self.assertEqual(actual, expected) # when modding the signs are ignored x = Size.new_from_str("1024 B") y = Size.new_from_str("-1000 B") actual = x.mod(y).get_bytes() expected = (24, 1) self.assertEqual(actual, expected) x = Size.new_from_str("-1024 B") y = Size.new_from_str("1000 B") actual = x.mod(y).get_bytes() expected = (24, 1) self.assertEqual(actual, expected) x = Size.new_from_str("-1024 B") y = Size.new_from_str("-1000 B") actual = x.mod(y).get_bytes() expected = (24, 1) self.assertEqual(actual, expected) x = Size.new_from_str("1024 B") y = Size.new_from_str("1024 B") actual = x.mod(y).get_bytes() expected = (0, 0) self.assertEqual(actual, expected)
def testSgn(self): sgn = Size.new_from_str("12 KiB").sgn() self.assertEqual(sgn, 1) sgn = Size.new_from_str("0 MB").sgn() self.assertEqual(sgn, 0) sgn = Size.new_from_str("-12 GiB").sgn() self.assertEqual(sgn, -1)
def testTrueDiv(self): x = Size.new_from_str("1024 B") y = Size.new_from_str("-102.4 B") # rounds to whole bytes divResult = float(x.true_div(y)[:15]) # just some number to cover accurancy and not cross max float range self.assertAlmostEqual(divResult, 1024.0/-102.0) x = Size.new_from_str("1 MiB") y = Size.new_from_str("1 KiB") divResult = float(x.true_div(y)[:15]) # just some number to cover accurancy and not cross max float range self.assertAlmostEqual(divResult, 1024.0)
def testMulFloatStr(self): x = Size.new_from_str("8 B") actual = x.mul_float_str("1.51").get_bytes() self.assertEqual(actual, (12, 1)) x = Size.new_from_str("-8 B") actual = x.mul_float_str("1.51").get_bytes() self.assertEqual(actual, (12, -1)) x = Size.new_from_str("8 B") actual = x.mul_float_str("-1.51").get_bytes() self.assertEqual(actual, (12, -1))
def testCmp(self): x = Size.new_from_str("1 KiB") y = Size.new_from_str("-1 KiB") # params: Size x, Size y, bool abs # result > 0 cmpResult = Size.cmp(x, y, False) self.assertGreater(cmpResult, 0) # result < 0 cmpResult = Size.cmp(y, x, False) self.assertLess(cmpResult, 0) # result == 0 cmpResult = Size.cmp(y, x, True) self.assertEqual(cmpResult, 0)
def testMulInt(self): x = Size.new_from_str("8 B") y = 2 actual = x.mul_int(y).get_bytes() expected = (16, 1) self.assertEqual(actual, expected) x = Size.new_from_str("0 B") y = 1 actual = x.mul_int(y).get_bytes() expected = (0, 0) self.assertEqual(actual, expected) x = Size.new_from_str("10 B") y = 0 actual = x.mul_int(y).get_bytes() expected = (0, 0) self.assertEqual(actual, expected)
def testDiv(self): x = Size.new_from_str("1 KiB") y = Size.new_from_str("-0.1 KiB") divResult = x.div(y) self.assertEqual(divResult, 10) x = Size.new_from_str("1 MiB") y = Size.new_from_str("1 KiB") divResult = x.div(y) self.assertEqual(divResult, 1024) x = Size.new_from_str("1 GB") y = Size.new_from_str("0.7 GB") divResult = x.div(y) self.assertEqual(divResult, 1) x = Size.new_from_str("-1 KiB") y = Size.new_from_str("0.1 KiB") divResult = x.div(y) self.assertEqual(divResult, 10)
def testRoundToNearest(self): x = Size.new_from_str("1500 B") roundTo = Size.new_from_str("1 KiB") actual = x.round_to_nearest(roundTo, ByteSize.RoundDir.UP).get_bytes() expected = (2048, 1) self.assertEqual(actual, expected) x = Size.new_from_str("1500 B") roundTo = Size.new_from_str("1 KiB") actual = x.round_to_nearest(roundTo, ByteSize.RoundDir.DOWN).get_bytes() expected = (1024, 1) self.assertEqual(actual, expected) x = Size.new_from_str("1500 B") roundTo = Size.new_from_str("10 KiB") actual = x.round_to_nearest(roundTo, ByteSize.RoundDir.DOWN).get_bytes() expected = (0, 0) self.assertEqual(actual, expected)
def testHumanReadable(self): strSize = Size.new_from_str("12 KiB").human_readable(ByteSize.Bunit.KIB, 2, True) self.assertEqual(strSize, "12 KiB") strSize = Size.new_from_str("1 KB").human_readable(ByteSize.Bunit.KIB, 2, False) self.assertEqual(strSize, "0.98 KiB")
def testGetBytesStr(self): strSize = Size.new_from_str("-1 KiB").get_bytes_str() self.assertEqual(strSize, "-1024")
def testDivInt(self): x = Size.new_from_str("1 MiB") y = 1024 divResult = x.div_int(y).get_bytes() self.assertEqual(divResult, (1024, 1))
def testConvertTo(self): x = Size.new_from_str("1 KiB") x.convert_to(ByteSize.Bunit.KIB)
def testNewFromStr(self): actual = Size.new_from_str('0 B').get_bytes() expected = (0, 0) self.assertEqual(actual, expected) actual = Size.new_from_str('1 KiB').get_bytes() expected = (1024, 1) self.assertEqual(actual, expected) actual = Size.new_from_str('1KB').get_bytes() expected = (1000, 1) self.assertEqual(actual, expected) actual = Size.new_from_str('1 MiB').get_bytes() expected = (1048576, 1) self.assertEqual(actual, expected) actual = Size.new_from_str('-1.5 GiB').get_bytes() expected = (1610612736, -1) self.assertEqual(actual, expected) locale.setlocale(locale.LC_ALL,'cs_CZ.UTF-8') actual = Size.new_from_str('1,5 KiB').get_bytes() expected = (1536, 1) self.assertEqual(actual, expected) actual = Size.new_from_str('-1,5 KiB').get_bytes() expected = (1536, -1) self.assertEqual(actual, expected) actual = Size.new_from_str('1.5 KiB').get_bytes() expected = (1536, 1) self.assertEqual(actual, expected) actual = Size.new_from_str('-1.5 KiB').get_bytes() expected = (1536, -1) self.assertEqual(actual, expected) # this persian locale uses a two-byte unicode character for the radix locale.setlocale(locale.LC_ALL, 'ps_AF.UTF-8') actual = Size.new_from_str('1٫5 KiB').get_bytes() expected = (1536, 1) self.assertEqual(actual, expected) actual = Size.new_from_str('-1٫5 KiB').get_bytes() expected = (1536, -1) self.assertEqual(actual, expected) actual = Size.new_from_str('1.5 KiB').get_bytes() expected = (1536, 1) self.assertEqual(actual, expected) actual = Size.new_from_str('-1.5 KiB').get_bytes() expected = (1536, -1) self.assertEqual(actual, expected) locale.setlocale(locale.LC_ALL,'en_US.UTF-8')