def test_repr(self): self.assertEqual(repr(units.BitRate('1.1 Gbps')), 'Transfer Rate(1.1 Gbit/s)') self.assertEqual(repr(units.BitRate('1.2 kbit/s')), 'Transfer Rate(1.2 kbit/s)') self.assertEqual(repr(units.BitRate('400 Tbps')), 'Transfer Rate(400.0 Tbit/s)')
def test_number_constructor(self): t = units.BitRate(1.0) self.assertEqual(t.raw_value, 1.0) t = units.BitRate(3) self.assertEqual(t.raw_value, 3.0) t = units.BitRate(477777.54) self.assertEqual(t.raw_value, 477777.54)
def test_numpy(self): Stat = NumPyUnitsStatisticsArray array = map(units.BitRate, [1, 2, 3, 4]) stats = Stat(array) self.assertEqual(stats.min(), units.BitRate(1)) self.assertEqual(stats.max(), units.BitRate(4)) self.assertEqual(stats.mean(), units.BitRate(2.5)) self.assertTrue(abs(stats.std().raw_value - 1.11803398875) < 0.000001)
def test_mutator(self): d = OptionsDescriptor() d.add_option('test', 'Hello', float) d.add_option('test2', 'Yeah', units.Time, 0) o = Options(d) with self.assertRaises(UnknownOptionError): o['unknown'] = 5 o['test'] = 5 self.assertEqual(o['test'], 5) o['test2'] = 7.1 self.assertEqual(o['test2'], units.Time(7.1)) # Request for impossible casting with self.assertRaises(TypeError): o['test2'] = units.BitRate(0) # Iterator access optids = [] values = [] for opt_id in o: optids.append(opt_id) values.append(o[opt_id]) self.assertEqual(optids, ['test', 'test2']) self.assertEqual(values, [5, units.Time(7.1)])
def parse_and_store_output(self): output = self.get_subprocess_output().split("\n") received = output[1].split(',') self.store_result('transfer_rate', units.BitRate(received[8])) self.store_result('jitter', units.Time(received[9] + "ms")) self.store_result('lost_packets', units.Packet(received[10])) self.store_result('total_packets', units.Packet(received[11])) self.store_result('percentage_lost', units.Percentage(received[12]))
def run(self): min_trans = self.context.options['min_transfer'].raw_value max_trans = self.context.options['max_transfer'].raw_value min_time = self.context.options['min_time'].raw_value max_time = self.context.options['max_time'].raw_value self.store_result('random_transfer', units.BitRate(random.uniform(min_trans, max_trans))) self.store_result('random_time', units.Time(random.uniform(min_time, max_time))) self.logger.debug( "Results have been generated. Sending them to client...") self.propagate_results()
def test_scale(self): t = units.BitRate(356500000) self.assertEqual(t.scale('Kbps'), 356500) self.assertEqual(t.scale('kbit/s'), 356500) self.assertEqual(t.scale('Mbit/s'), 356.5) self.assertEqual(t.scale('Mbits/s'), 356.5) self.assertEqual(t.scale('Mbps'), 356.5) self.assertEqual(t.scale('Gbit/s'), 0.3565) self.assertEqual(t.scale('Gbits/s'), 0.3565) self.assertEqual(t.scale('Gbps'), 0.3565) self.assertEqual(t.scale('Tbit/s'), 0.0003565) self.assertEqual(t.scale('Tbits/s'), 0.0003565) self.assertEqual(t.scale('Tbps'), 0.0003565)
def test_optimal_scale(self): self.assertEqual(units.BitRate('0 bps').optimal_scale(), (0, 'bits/s')) self.assertEqual( units.BitRate('100 bps').optimal_scale(), (100, 'bits/s')) self.assertEqual( units.BitRate('1000 bps').optimal_scale(), (1, 'kbit/s')) self.assertEqual( units.BitRate('1100 bps').optimal_scale(), (1.1, 'kbit/s')) self.assertEqual( units.BitRate('0.00054 bps').optimal_scale(), (0.00054, 'bits/s')) self.assertEqual( units.BitRate('1 Gbit/s').optimal_scale(), (1, 'Gbit/s')) self.assertEqual( units.BitRate('1 Tbit/s').optimal_scale(), (1, 'Tbit/s'))
def test_propage_results(self): p = Profile('profid', 'profname', ProfileExecutorA, ProfileExecutorB) p.add_result('testdt', 'name', units.Time) p.add_result('testbit', 'name', units.BitRate) c = NullNSTSConnection() ctxa = ProfileExecution(p, ExecutionDirection('s'), Options(p.supported_options), c) a = ctxa.executor ctxb = ProfileExecution(p, ExecutionDirection('r'), Options(p.supported_options), c) b = ctxb.executor a.store_result('testdt', '10 sec') a.store_result('testbit', '32 bps') a.propagate_results() b.collect_results() self.assertEqual(b.results['testdt'], units.Time('10 sec')) self.assertEqual(b.results['testbit'], units.BitRate('32 bps'))
def parse_and_store_output(self): output = self.get_subprocess_output() self.store_result('transfer_rate', units.BitRate(float(output.split(',')[8])))
def test_sub(self): a = units.BitRate(4.5) b = units.BitRate(1.2) self.assertEqual(a - b, units.BitRate(3.3))
def test_add(self): a = units.BitRate(4.5) b = units.BitRate(1.2) self.assertEqual(a + b, units.BitRate(5.7))
def test_nonzero(self): self.assertFalse(units.BitRate(0)) self.assertTrue(units.BitRate(1))
def test_str(self): self.assertEqual(str(units.BitRate('1 Gbps')), '1.0 Gbit/s') self.assertEqual(str(units.BitRate('1.2 kbit/s')), '1.2 kbit/s') self.assertEqual(str(units.BitRate('400 Tbps')), '400.0 Tbit/s')
def test_parse_constructor(self): self.assertEqual(units.BitRate('0').raw_value, 0) self.assertEqual(units.BitRate('2.2 bits/s').raw_value, 2.2) self.assertEqual(units.BitRate('3.3 bit/s').raw_value, 3.3) self.assertEqual(units.BitRate('4.4 bps').raw_value, 4.4) self.assertEqual(units.BitRate('1.4 Gbit/s').raw_value, 1.4 * (10**9)) self.assertEqual(units.BitRate('1.5 Gbits/s').raw_value, 1.5 * (10**9)) self.assertEqual(units.BitRate('1.6 Gbps').raw_value, 1.6 * (10**9)) self.assertEqual(units.BitRate('1.4 Mbit/s').raw_value, 1.4 * (10**6)) self.assertEqual(units.BitRate('1.5 Mbits/s').raw_value, 1.5 * (10**6)) self.assertEqual(units.BitRate('1.6 Mbps').raw_value, 1.6 * (10**6)) self.assertEqual(units.BitRate('1.4 Kbit/s').raw_value, 1.4 * (10**3)) self.assertEqual(units.BitRate('1.5 Kbits/s').raw_value, 1.5 * (10**3)) self.assertEqual(units.BitRate('1.6 Kbps').raw_value, 1.6 * (10**3)) self.assertEqual(units.BitRate('1.4 Tbit/s').raw_value, 1.4 * (10**12)) self.assertEqual( units.BitRate('1.5 Tbits/s').raw_value, 1.5 * (10**12)) self.assertEqual(units.BitRate('1.6 Tbps').raw_value, 1.6 * (10**12)) with self.assertRaises(units.ParseError): units.BitRate('46 dummy') with self.assertRaises(units.ParseError): units.BitRate('46.5.6') with self.assertRaises(units.ParseError): units.BitRate('46.5.6 kb ps')
def test_copy_constructor(self): b1 = units.BitRate(15) b2 = units.BitRate(b1) self.assertEqual(b1, b2) self.assertEqual(b2.raw_value, 15)
def test_cmp(self): self.assertFalse(units.BitRate(1) == units.BitRate(0)) self.assertTrue(units.BitRate(1) != units.BitRate(0)) self.assertFalse(units.BitRate(1) < units.BitRate(0)) self.assertFalse(units.BitRate(1) <= units.BitRate(0)) self.assertTrue(units.BitRate(1) > units.BitRate(0)) self.assertTrue(units.BitRate(1) >= units.BitRate(0)) self.assertTrue(units.BitRate(1) == units.BitRate(1)) self.assertFalse(units.BitRate(1) != units.BitRate(1)) self.assertFalse(units.BitRate(1) < units.BitRate(1)) self.assertTrue(units.BitRate(1) <= units.BitRate(1)) self.assertFalse(units.BitRate(1) > units.BitRate(1)) self.assertTrue(units.BitRate(1) >= units.BitRate(1)) self.assertFalse(units.BitRate(0) == units.BitRate(1)) self.assertTrue(units.BitRate(0) != units.BitRate(1)) self.assertTrue(units.BitRate(0) < units.BitRate(1)) self.assertTrue(units.BitRate(0) <= units.BitRate(1)) self.assertFalse(units.BitRate(0) > units.BitRate(1)) self.assertFalse(units.BitRate(0) >= units.BitRate(1))
def test_emtpy_constructor(self): t = units.BitRate() self.assertEqual(t.raw_value, 0)