コード例 #1
0
 def test_in_duration(self):
     information = Information(10, Information.MEGABYTES)
     self.assertEqual(information.in_duration(Duration(seconds=5)),
                      Speed(Information(2, Information.MEGABYTES)))
コード例 #2
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_eq_true(self):
     self.assertEqual(
         Speed.GIGABIT,
         Speed(Information(10, Information.GIGABITS), Duration(seconds=10)))
コード例 #3
0
ファイル: test_speed.py プロジェクト: gebn/nibble
class TestSpeed(unittest.TestCase):

    _INFORMATION = Information(10)
    _SPEED = Speed(_INFORMATION)

    def test_init_instant(self):
        with self.assertRaises(ValueError):
            Speed(Information(1), Duration.ZERO)

    def test_from_quantity_units(self):
        self.assertEqual(
            Speed.from_quantity_units(1.35, 'kB', 'weeks'),
            Speed(Information(1.35, Information.KILOBYTES), Duration(weeks=1)))

    def test_per_second(self):
        self.assertEqual(Speed.FORTY_GIGABIT._per_second,
                         Information(40000, Information.MEGABITS))

    def test_for_duration(self):
        self.assertEqual(self._SPEED.for_duration(Duration(minutes=1)),
                         self._INFORMATION * 60)

    def test_lt_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.TEN_GIGABIT < 1

    def test_lt_false(self):
        self.assertFalse(Speed.TEN_GIGABIT < Speed.GIGABIT)

    def test_lt_true(self):
        self.assertLess(Speed.GIGABIT, Speed.TEN_GIGABIT)

    def test_le_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT <= 1

    def test_le_false(self):
        self.assertFalse(Speed.TEN_GIGABIT <= Speed.GIGABIT)

    def test_le_true_less(self):
        self.assertLessEqual(Speed.GIGABIT, Speed.TEN_GIGABIT)

    def test_le_true_equal(self):
        self.assertLessEqual(Speed.TEN_GIGABIT, Speed.TEN_GIGABIT)

    def test_eq_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT == 1

    def test_eq_false(self):
        self.assertFalse(Speed.GIGABIT == Speed.TEN_GIGABIT)

    def test_eq_true(self):
        self.assertEqual(
            Speed.GIGABIT,
            Speed(Information(10, Information.GIGABITS), Duration(seconds=10)))

    def test_ne_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT != 1

    def test_ne_false(self):
        self.assertFalse(Speed.FORTY_GIGABIT != Speed(
            Information(80, Information.GIGABITS), Duration(seconds=2)))

    def test_ne_true(self):
        self.assertNotEqual(Speed.FORTY_GIGABIT, Speed.HUNDRED_GIGABIT)

    def test_ge_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT >= 1

    def test_ge_false(self):
        self.assertFalse(Speed.GIGABIT >= Speed.TEN_GIGABIT)

    def test_ge_true_less(self):
        self.assertGreaterEqual(Speed.TEN_GIGABIT, Speed.GIGABIT)

    def test_ge_true_equal(self):
        self.assertGreaterEqual(Speed.TEN_GIGABIT, Speed.TEN_GIGABIT)

    def test_gt_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT > 1

    def test_gt_false(self):
        self.assertFalse(Speed.GIGABIT > Speed.TEN_GIGABIT)

    def test_gt_true(self):
        self.assertGreater(Speed.TEN_GIGABIT, Speed.GIGABIT)

    def test_add_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT + 1

    def test_add(self):
        self.assertEqual(
            Speed(Information(500, Information.MEGABITS)) +
            Speed(Information(2.5, Information.GIGABITS), Duration(seconds=5)),
            Speed.GIGABIT)

    def test_sub_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT - 1

    def test_sub_zero(self):
        with self.assertRaises(ArithmeticError):
            _ = Speed.GIGABIT * 10 - Speed.TEN_GIGABIT

    def test_sub_negative(self):
        with self.assertRaises(ArithmeticError):
            _ = Speed.GIGABIT - Speed.TEN_GIGABIT

    def test_sub(self):
        self.assertEqual(
            Speed.TEN_GIGABIT - Speed.GIGABIT,
            Speed(Information(90, Information.GIGABITS), Duration(seconds=10)))

    def test_mul_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT * ''

    def test_mul(self):
        self.assertEqual(Speed.TEN_GIGABIT * 4, Speed.FORTY_GIGABIT)

    def test_truediv_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT / ''

    def test_truediv_zero(self):
        with self.assertRaises(ZeroDivisionError):
            _ = Speed.GIGABIT / 0

    def test_truediv_low(self):  # 1.33 should go down
        self.assertEqual(
            Speed(Information(4), Duration(1)) / 3,
            Speed(Information(1), Duration(1)))

    def test_truediv_high(self):  # 1.66 should go up
        self.assertEqual(
            Speed(Information(10), Duration(1)) / 6,
            Speed(Information(20), Duration(10)))

    def test_floordiv_bad_class(self):
        with self.assertRaises(TypeError):
            _ = Speed.GIGABIT // ''

    def test_floordiv_zero(self):
        with self.assertRaises(ZeroDivisionError):
            _ = Speed.GIGABIT // 0

    def test_floordiv_low(self):  # 1.33 should go down
        self.assertEqual(
            Speed(Information(4), Duration(1)) // 3,
            Speed(Information(1), Duration(1)))

    def test_floordiv_high(self):  # 1.66 should go down
        self.assertEqual(
            Speed(Information(10), Duration(1)) // 6,
            Speed(Information(1), Duration(1)))

    def test_bool_true(self):
        self.assertTrue(Speed.HUNDRED_MEGABIT)

    def test_bool_false(self):
        self.assertFalse(Speed.ZERO)

    def test_format_default(self):
        self.assertEqual('{0}'.format(Speed.GIGABIT), '119.21 MiB/s')

    def test_format_zero(self):
        self.assertEqual('{0}'.format(Speed.ZERO), '0 B/s')

    def test_format_info_unit(self):
        # force Gb
        self.assertEqual('{0:Gb}'.format(Speed.GIGABIT), '1Gb/s')

    def test_format_invalid_time_unit(self):
        with self.assertRaises(TypeError):
            '{0:/z}'.format(self._SPEED)

    def test_format_invalid_info_unit(self):
        with self.assertRaises(TypeError):
            '{0:foo}'.format(Duration.SECOND)

    def test_format_separator_info_unit(self):
        speed = Speed(Information(1, Information.TERABITS))
        self.assertEqual('{0: Gb}'.format(speed), '1,000 Gb/s')

    def test_format_time_unit_singular(self):
        # use per minute instead of seconds, and work out the quantity in binary
        # bytes
        self.assertEqual('{0:/m}'.format(Speed.GIGABIT), '6.98GiB/m')

    def test_format_time_unit_integral(self):
        self.assertEqual('{0:/5m}'.format(Speed.TEN_GIGABIT), '349.25GiB/5m')

    def test_format_time_unit_fractional(self):
        self.assertEqual('{0:/2.3d}'.format(Speed.TEN_MEGABIT),
                         '231.34GiB/2.3d')

    def test_format_separator_time_unit(self):
        self.assertEqual('{0: /m}'.format(Speed.GIGABIT), '6.98 GiB/m')

    def test_format_info_category_time_unit(self):
        # work out the quantity in binary bit units per hour, putting a space
        # between the quantity and unit, and using default formatting for the
        # quantity,
        self.assertEqual('{0: bb/h}'.format(Speed.GIGABIT), '3.27 Tib/h')

    def test_format_info_unit_time_unit(self):
        # use per minute instead of seconds, and work out the quantity in MiB
        self.assertEqual('{0: MiB/m}'.format(Speed.HUNDRED_MEGABIT),
                         '715.26 MiB/m')

    def test_format(self):
        # show the quantity of information processed per month to 2dp with comma
        # separated thousands, with a space after, then a decimal bytes unit
        self.assertEqual('{0:,.2f|dB/mo}'.format(Speed.GIGABIT), '328.50TB/mo')

    def test_repr(self):
        self.assertEqual(repr(self._SPEED),
                         '<Speed(<Information(10)>, <Duration(1000000000)>)>')

    def test_str(self):
        self.assertEqual(str(self._SPEED), '1.25 B/s')
コード例 #4
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_from_quantity_units(self):
     self.assertEqual(
         Speed.from_quantity_units(1.35, 'kB', 'weeks'),
         Speed(Information(1.35, Information.KILOBYTES), Duration(weeks=1)))
コード例 #5
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_format_separator_info_unit(self):
     speed = Speed(Information(1, Information.TERABITS))
     self.assertEqual('{0: Gb}'.format(speed), '1,000 Gb/s')
コード例 #6
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_init_instant(self):
     with self.assertRaises(ValueError):
         Speed(Information(1), Duration.ZERO)
コード例 #7
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_truediv_high(self):  # 1.66 should go up
     self.assertEqual(
         Speed(Information(10), Duration(1)) / 6,
         Speed(Information(20), Duration(10)))
コード例 #8
0
 def test_eq_bad_class(self):
     with self.assertRaises(TypeError):
         _ = Information(1) == 1
コード例 #9
0
 def test_eq_false(self):
     self.assertFalse(
         Information(22, Information.MEBIBYTES) == Information(
             2528, Information.KIBIBYTES))
コード例 #10
0
 def test_le_false(self):
     self.assertFalse(Information(10) <= Information(1))
コード例 #11
0
 def test_le_true_equal(self):
     self.assertLessEqual(Information(10), Information(10))
コード例 #12
0
 def test_lt_true(self):
     self.assertLess(Information(1), Information(10))
コード例 #13
0
 def test_parse_decimal(self):
     self.assertEqual(Information.parse('1.4 GB'),
                      Information(10**9 * 1.4 * 8, Information.BITS))
コード例 #14
0
 def test_parse_integer(self):
     self.assertEqual(Information.parse('123 YiB'),
                      Information(2**80 * 123 * 8, Information.BITS))
コード例 #15
0
ファイル: test_parser.py プロジェクト: gebn/nibble
 def test_speed_information_duration(self):
     self.assertEqual(self.parser.parse('11 b in 2 nanoseconds'),
                      Speed(Information(11), Duration(nanoseconds=2)))
コード例 #16
0
 def test_eq_true(self):
     self.assertEqual(Information(22, Information.MEBIBYTES),
                      Information(22528, Information.KIBIBYTES))
コード例 #17
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_add(self):
     self.assertEqual(
         Speed(Information(500, Information.MEGABITS)) +
         Speed(Information(2.5, Information.GIGABITS), Duration(seconds=5)),
         Speed.GIGABIT)
コード例 #18
0
ファイル: test_parser.py プロジェクト: gebn/nibble
 def test_expression_information(self):
     self.assertEqual(self.parser.parse('10Gb'),
                      Information(10, Information.GIGABITS))
コード例 #19
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_sub(self):
     self.assertEqual(
         Speed.TEN_GIGABIT - Speed.GIGABIT,
         Speed(Information(90, Information.GIGABITS), Duration(seconds=10)))
コード例 #20
0
ファイル: test_parser.py プロジェクト: gebn/nibble
 def test_expression_speed(self):
     self.assertEqual(
         self.parser.parse('11.25Yib/8years'),
         Speed(Information(13600415470664578215444480), Duration(years=8)))
コード例 #21
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_floordiv_low(self):  # 1.33 should go down
     self.assertEqual(
         Speed(Information(4), Duration(1)) // 3,
         Speed(Information(1), Duration(1)))
コード例 #22
0
ファイル: test_parser.py プロジェクト: gebn/nibble
 def test_information_constructor(self):
     self.assertEqual(self.parser.parse('10.4Gb'),
                      Information(10.4, Information.GIGABITS))
コード例 #23
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_floordiv_high(self):  # 1.66 should go down
     self.assertEqual(
         Speed(Information(10), Duration(1)) // 6,
         Speed(Information(1), Duration(1)))
コード例 #24
0
ファイル: test_parser.py プロジェクト: gebn/nibble
 def test_information_speed_duration(self):
     self.assertEqual(self.parser.parse('10Gb/s for 11 minutes'),
                      Information(10 * 60 * 11, Information.GIGABITS))
コード例 #25
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_per_second(self):
     self.assertEqual(Speed.FORTY_GIGABIT._per_second,
                      Information(40000, Information.MEGABITS))
コード例 #26
0
ファイル: test_parser.py プロジェクト: gebn/nibble
 def test_duration_information_speed(self):
     duration = Information(17.3, Information.GIGABYTES).at_speed(
         Speed(Information(688.3, Information.KILOBYTES)))
     self.assertEqual(self.parser.parse('17.3GB at 688.3kB/s'), duration)
コード例 #27
0
ファイル: test_speed.py プロジェクト: gebn/nibble
 def test_ne_false(self):
     self.assertFalse(Speed.FORTY_GIGABIT != Speed(
         Information(80, Information.GIGABITS), Duration(seconds=2)))
コード例 #28
0
ファイル: test_parser.py プロジェクト: gebn/nibble
 def test_speed_constructor(self):
     self.assertEqual(self.parser.parse('12 MiB/3s'),
                      Speed(Information(4, Information.MEBIBYTES)))
コード例 #29
0
ファイル: speed.py プロジェクト: gebn/nibble
        except ValueError as e:
            raise TypeError(e)

        information = self.information * nanos / self.duration.nanoseconds

        time_fmt = unit if quantity == 1 else '{0}{1}'.format(quantity, unit)
        return '{0:{1}}/{2}'.format(information, lhs, time_fmt)

    def __str__(self):
        return '{0}'.format(self)


Speed.ZERO = Speed(Information.ZERO)

# Ethernet
Speed.TEN_MEGABIT = Speed(Information(10, Information.MEGABITS))
Speed.HUNDRED_MEGABIT = Speed.TEN_MEGABIT * 10
Speed.GIGABIT = Speed.HUNDRED_MEGABIT * 10
Speed.TEN_GIGABIT = Speed.GIGABIT * 10
Speed.FORTY_GIGABIT = Speed.TEN_GIGABIT * 4
Speed.HUNDRED_GIGABIT = Speed.TEN_GIGABIT * 10

# E-carrier
Speed.E0 = Speed(Information(64, Information.KILOBITS))
Speed.E1 = Speed(Information(2.048, Information.MEGABITS))
Speed.E2 = Speed(Information(8.448, Information.MEGABITS))
Speed.E3 = Speed(Information(34.368, Information.MEGABITS))
Speed.E4 = Speed(Information(139.264, Information.MEGABITS))
Speed.E5 = Speed(Information(565.148, Information.MEGABITS))

# T-carrier signaling
コード例 #30
0
 def test_at_speed(self):
     speed = Speed(Information(100, Information.GIBIBYTES))
     information = Information(10, Information.GIBIBYTES)
     self.assertEqual(information.at_speed(speed),
                      Duration(milliseconds=100))