def testStrWithSmallDeviations(self): """ Behavior when deviation from whole value is small. """ Config.set_display_config( DisplayConfig( strip_config=StripConfig(strip=True) ) ) eps = 1024 * Fraction(1, 100) * Fraction(1, 2) # deviation is less than 1/2 of 1% of 1024 s = Range(16384 - (eps - 1)) self.assertEqual(str(s), "< 16 KiB") # deviation is greater than 1/2 of 1% of 1024 s = Range(16384 - (eps + 1)) self.assertEqual(str(s), "> 15.99 KiB") # deviation is less than 1/2 of 1% of 1024 s = Range(16384 + (eps - 1)) self.assertEqual(str(s), "> 16 KiB") # deviation is greater than 1/2 of 1% of 1024 s = Range(16384 + (eps + 1)) self.assertEqual(str(s), "< 16.01 KiB")
def testSettingValueConfig(self, config): """ Test that new str config is the correct one. """ Config.set_value_config(config) self.assertEqual(str(config), str(Config.STRING_CONFIG.VALUE_CONFIG))
def testSettingDisplayConfig(self, config): """ Test that new str config is the correct one. """ Config.set_display_config(config) self.assertEqual(str(config), str(Config.STRING_CONFIG.DISPLAY_CONFIG))
def testValueConfigs(self): """ Test str with various configuration options. """ Config.set_display_config( DisplayConfig( strip_config=StripConfig(strip=True) ) ) # exactly 4 Pi s = Range(0x10000000000000) self.assertEqual(str(s), "4 PiB") s = Range(300, MiB) self.assertEqual(str(s), "300 MiB") s = Range('12.6998', TiB) self.assertEqual(str(s), "< 12.7 TiB") # byte values close to multiples of 2 are shown without trailing zeros s = Range(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 = Range(16384 - (Fraction(1024)/100 + 1)) self.assertEqual(str(s), "< 15.99 KiB") # test a very large quantity with no associated abbreviation or prefix s = Range(1024**9) self.assertEqual(str(s), "1024 YiB") s = Range(1024**9 - 1) self.assertEqual(str(s), "< 1024 YiB") s = Range(1024**10) self.assertEqual(str(s), "1048576 YiB") s = Range(0xfffffffffffff) self.assertEqual(str(s), "< 4 PiB") s = Range(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") Config.set_value_config(ValueConfig(max_places=3)) Config.set_display_config( DisplayConfig( strip_config=StripConfig(strip=True) ) ) s = Range('23.7874', TiB) self.assertEqual(str(s), "> 23.787 TiB") Config.set_value_config(ValueConfig(min_value=10)) Config.set_display_config( DisplayConfig( strip_config=StripConfig(strip=True) ) ) s = Range(8193) self.assertEqual(str(s), ("8193 B")) # if max_places is set to None, all digits are displayed Config.set_value_config(ValueConfig(max_places=None)) Config.set_display_config( DisplayConfig( strip_config=StripConfig(strip=True) ) ) s = Range(0xfffffffffffff) self.assertEqual( str(s), "3.99999999999999911182158029987476766109466552734375 PiB" ) s = Range(0x10000) self.assertEqual(str(s), ("64 KiB")) s = Range(0x10001) self.assertEqual(str(s), "64.0009765625 KiB") Config.set_value_config(ValueConfig(max_places=2)) Config.set_display_config( DisplayConfig( strip_config=StripConfig(strip=False) ) ) s = Range(1024**9 + 1) self.assertEqual(str(s), "> 1024.00 YiB") s = Range(0xfffff) self.assertEqual(str(s), "< 1024.00 KiB")
def tearDown(self): """ Reset configuration to default. """ Config.set_value_config(self.str_config) Config.set_display_config(self.display_config)