def testSettingConfiguration(self): """ Test that setting configuration to different values has effect. """ s = Size(64, GiB) SizeConfig.set_display_config(DisplayConfig(strip=False)) prev = str(s) SizeConfig.set_display_config(DisplayConfig(strip=True)) subs = str(s) self.assertTrue(subs != prev)
def testStrWithSmallDeviations(self): """ Behavior when deviation from whole value is small. """ SizeConfig.set_display_config(DisplayConfig(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 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), "@16.01 KiB")
def testStrConfigs(self): """ Test str with various configuration options. """ SizeConfig.set_display_config(DisplayConfig(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") SizeConfig.set_str_config(StrConfig(max_places=3)) SizeConfig.set_display_config(DisplayConfig(strip=True)) s = Size('23.7874', TiB) self.assertEqual(str(s), "@23.787 TiB") SizeConfig.set_str_config(StrConfig(min_value=10)) SizeConfig.set_display_config(DisplayConfig(strip=True)) s = Size(8193) self.assertEqual(str(s), ("8193 B")) # if max_places is set to None, all digits are displayed SizeConfig.set_str_config(StrConfig(max_places=None)) SizeConfig.set_display_config(DisplayConfig(strip=True)) s = Size(0xfffffffffffff) self.assertEqual( str(s), "3.99999999999999911182158029987476766109466552734375 PiB" ) s = Size(0x10000) self.assertEqual(str(s), ("64 KiB")) s = Size(0x10001) self.assertEqual(str(s), "64.0009765625 KiB") SizeConfig.set_str_config(StrConfig(max_places=2)) SizeConfig.set_display_config(DisplayConfig(strip=False)) s = Size(1024**9 + 1) self.assertEqual(str(s), "@1024.00 YiB") s = Size(0xfffff) self.assertEqual(str(s), "@1024.00 KiB")
def tearDown(self): """ Reset configuration to default. """ SizeConfig.set_str_config(self.str_config) SizeConfig.set_display_config(self.display_config)
def testSettingStrConfig(self, config): """ Test that new str config is the correct one. """ SizeConfig.set_str_config(config) self.assertEqual(str(config), str(SizeConfig.STR_CONFIG))
def testSettingDisplayConfig(self, config): """ Test that new str config is the correct one. """ SizeConfig.set_display_config(config) self.assertEqual(str(config), str(SizeConfig.DISPLAY_CONFIG))
def testSettingInputConfig(self, config): """ That that new input config is the correct one. """ SizeConfig.set_input_config(config) self.assertEqual(str(config), str(SizeConfig.INPUT_CONFIG))