def test_sum_negative(self): for _sum, (_b1, _b2) in summ_n.items(): s, b1, b2 = DataPacket(_sum), DataPacket(_b1), DataPacket(_b2) r_s = sum([b1, b2]) assert r_s != s, "Wrong output: {} + {} == {} (Actual: {})".format( b1, b2, s, r_s) logging.info("{} + {} != {} (Actual: {})".format(b1, b2, s, r_s))
def test_isub(self): p = DataPacket('1M') p_sub = DataPacket('1K') logging.info(f"{p:.1m}") p -= p_sub logging.info(f"{p:.4m}") p -= '0.1M' logging.info(f"{p:.4m}")
def test_iadd(self): p = DataPacket('1M') p_add = DataPacket('1K') logging.info(f"1M - {p:.1m}") p += p_add logging.info(f"8m - {p:.4m}") p += '1M' logging.info(f"12m - {p:.4m}")
def packet_sum(*packet_list, **kwargs): _list = list(packet_list) if _list.__len__() == 0: return DataPacket(number=0, **kwargs) _res = DataPacket(_list.pop()) while _list.__len__() > 0: _next = _list.pop() _next.format = _res.rate _res += DataPacket(_next, **kwargs) return _res
def packet_max(*packet_list, **kwargs): _list = list(packet_list) if _list.__len__() == 0: return DataPacket(number=0, **kwargs) _res = DataPacket(_list.pop(), **kwargs) while _list.__len__() > 0: _next = _list.pop() _next.format = _res.format if _next > _res: _res = _next return _res
def test_sum_positive(self): errors = [] for _sum, (_b1, _b2) in summ_p.items(): try: s, b1, b2 = DataPacket(_sum), DataPacket(_b1), DataPacket(_b2) _b = [b1, b2] r_s = sum(_b) assert r_s == s, "Wrong output: {} + {} == {} (Actual: {})".format( b1, b2, s, r_s) logging.info("{} + {} == {}".format(b1, b2, s)) except AssertionError as e: errors.append(e) assert len(errors) == 0, "Following iterations failed:\n{}".format( '\n\t'.join([str(e) for e in errors]))
def test_format_conversion(self): v = 8000000.8 p = DataPacket(number=v) logging.info(f"{p}") logging.info(f"{p:.1b}") logging.info(f"{p:.1k}") logging.info(f"{p:.2m}") logging.info(f"{p:.1B}") logging.info(f"{p:.1K}") logging.info(f"{p:.1M}")
def test_bit_value(self): b0 = DataPacket(0) assert b0 == 0, "Wrong output: {}".format(b0) logging.info("Type: {}, Value: {}".format(type(b0).__name__, b0)) b00 = DataPacket(float(0)) assert str(b00) == '0.0b', "Wrong output: {}".format(b00) logging.info("Type: {}, Value: {}".format(type(b0).__name__, b0)) b1 = DataPacket('1K') assert str(b1) == '1.0K', "Wrong output: {}".format(b1) logging.info("Type: {}, Value: {}".format(type(b1).__name__, b1)) b2 = DataPacket('1M') assert str(b2) == '1.0M', "Wrong output: {}".format(b2) logging.info("Type: {}, Value: {}".format(type(b2).__name__, b2)) b3 = DataPacket(number=1, rate='K') assert str(b3) == '1.0K', "Wrong output: {}".format(b3) logging.info("Type: {}, Value: {}".format(type(b3).__name__, b3)) b4 = DataPacket('1G') assert str(b4) == '1.0G', "Wrong output: {}".format(b4) logging.info("Type: {}, Value: {}".format(type(b4).__name__, b4)) # b4 = PacketSize('1.1G') # assert str(b4) == '1.1G', "Wrong output: {}".format(b4) # logging.info("Type: {}, Value: {}".format(type(b4), b4)) b5 = DataPacket('1.1446564G') logging.info("Format: {0} vs. {0:.1M}".format(b5))
def test_ne(self): for _b1, _b2 in ne.items(): b1, b2 = DataPacket(_b1), DataPacket(_b2) assert b1 != b2, "Wrong output: {} != {}".format( b1.bit_value, b2.bit_value) logging.info("{} != {}".format(b1, b2))
def test_eq(self): for _b1, _b2 in eq.items(): b1, b2 = DataPacket(_b1), DataPacket(_b2) assert b1 == b2, "Wrong output: {} == {}".format(b1, b2) logging.info("{} == {}".format(b1, b2))
def test_percent(self): packet = DataPacket('10M') percent = Percent('10%') packet += percent logging.info(f"{packet}")