def test_div_bin(self): for fir, sec, res in test_div_bin: func_res = div_(fir, sec) self.assertEqual(func_res, res) func_another_res = Calculator().calculate('0b' + fir, '0b' + sec, '/') self.assertEqual(func_another_res, res)
def test_div_dec(self): for fir, sec, res in test_div_dec: func_res = div_( Number(fir, 10).convert_to_binary(), Number(sec, 10).convert_to_binary()) func_res = int( func_res, 2 ) # Перевожу в нужную СС для сравнения с результатами других функций self.assertEqual(str(func_res), res) func_another_res = Calculator().calculate(fir, sec, '/') self.assertEqual(func_another_res, res)
def test_div_oct(self): for fir, sec, res in test_div_oct: func_res = div_( Number(fir, 8).convert_to_binary(), Number(sec, 8).convert_to_binary()) func_res = int(func_res, 2) # Перевожу в 10 СС func_res = str( '%o' % func_res ) # Перевожу в нужную СС для сравнения с результатами других функций self.assertEqual(func_res, res) func_another_res = Calculator().calculate('0' + fir, '0' + sec, '/') self.assertEqual(func_another_res, res)
def test_mul_hex(self): for fir, sec, res in test_mul_hex: func_res = mul_( Number(fir, 16).convert_to_binary(), Number(sec, 16).convert_to_binary()) func_res = int(func_res, 2) # Перевожу в 10 СС func_res = str( '%X' % func_res ) # Перевожу в нужную СС для сравнения с результатами других функций self.assertEqual(func_res.upper(), res) func_another_res = Calculator().calculate('0x' + fir, '0x' + sec, '*') self.assertEqual(func_another_res, res)
calc_funcs[op](Number(x, base).convert_to_binary(), Number(y, base).convert_to_binary()) else: calc_funcs[op](x, y, base) profiler.disable() tottime_list[op] = round(collect_info(profiler), 3) return tottime_list if __name__ == "__main__": CALC_NAME_1 = 'calculator_roman' FUNCS_1 = [sum_, sub_, mul_, div_] CALC_NAME_2 = 'calculator_kate' FUNCS_2 = [ Calculator().add, Calculator().subtract, Calculator().multiply, Calculator().divide ] res_timing_roman = test_performance(FUNCS_1, wrap=True) res_timing_kate = test_performance(FUNCS_2) table = BeautifulTable() table.column_headers = ['name', 'add', 'sub', 'mul', 'div'] table.append_row(['Roman'] + res_timing_roman) table.append_row(['Kate'] + res_timing_kate) table.append_row(['Marat', '4.401', '4.353', '1072.925', '189.233']) table.append_row(['Svetozar', '7.944', '7.870', '986.770', '2936.387']) print(table)
plt.figure() plt.plot(n_cur, time_cur) plt.xlabel('Sum of operands digits') plt.ylabel('Total time, s') plt.plot(n_cur, time_cur, linestyle='-', marker='+') plt.title(calc_funcs[op].__name__) plt.grid(True) plt.savefig(calc_name + '_' + calc_funcs[op].__name__ + '.png') if __name__ == "__main__": CALC_NAME_1 = 'calculator_roman' FUNCS_1 = [sum_, sub_, mul_, div_] CALC_NAME_2 = 'calculator_kate' FUNCS_2 = [Calculator().add, Calculator().subtract, Calculator().multiply, Calculator().divide] BASE = 10 PAIRS_KEYS = ['sum', 'sub', 'mul', 'div'] x = '1015165122054415011111113515146834551681648554684351268011111110151656153513515146834551681648543541' y = '5424542415458457584254811111111111111110151615351351514683455168164854354654115161065122054415015458' PAIRS = { 'sum': [(x, y, BASE), (x * 66, y * 66, BASE), (x * 99, y * 99, BASE), (x * 132, y * 132, BASE)], 'sub': [(x, y, BASE), (x * 66, y * 66, BASE), (x * 99, y * 99, BASE),