def computing(self): first_poly = Polynomial.from_string(self.first_poly) second_poly = Polynomial.from_string(self.second_poly) if first_poly and second_poly: if self.operation == '+': self.result = str(first_poly + second_poly) elif self.operation == '-': self.result = str(first_poly - second_poly) elif self.operation == '*': self.result = str(first_poly * second_poly) else: self.result = 'Coeff has no type int'
def test_eq_poly_with_zero(self): self.assertTrue( Polynomial([0, 1, 2, 3, 4]) == Polynomial([1, 2, 3, 4]))
def test_sub_poly_and_poly(self): self.assertEqual(Polynomial([-4, -4, -4, -4]), Polynomial([1, 2, 3]) - Polynomial([4, 5, 6, 7]))
def test_sub_fail_poly_and_incorrect_type(self): with self.assertRaises(TypeError): Polynomial([1, 2, 3]) - 'str'
def test_sub_number_and_poly(self): self.assertEqual(Polynomial([-1, -2, -2]), 1 - Polynomial([1, 2, 3]))
def test_sum_poly_and_poly_with_check_zero(self): self.assertEqual(Polynomial([1, 0]), Polynomial([1, 2, 3]) + Polynomial([-1, -1, -3]))
def test_sum_fail_incorrect_type_and_poly(self): with self.assertRaises(TypeError): 'str' + Polynomial([1, 2, 3])
def test_sum_poly_and_number(self): self.assertEqual(Polynomial([1, 2, 4]), Polynomial([1, 2, 3]) + 1)
def test_from_string(self): self.assertEqual(Polynomial([1, 2, 3]), Polynomial.from_string('1,2,3'))
def test_str_with_zeros(self): self.assertEqual('x^2 + 2x + 3', str(Polynomial([0, 0, 1, 2, 3])))
def test_str(self): self.assertEqual('x^2 + x + 1', str(Polynomial([1, 1, 1])))
def test_repr_with_zeros(self): self.assertEqual('Polynomial([1, 2, 3])', repr(Polynomial([0, 1, 2, 3])))
def test_repr(self): self.assertEqual('Polynomial([1, 2, 3])', repr(Polynomial([1, 2, 3])))
def test_eq_fail_incorrect_type(self): with self.assertRaises(TypeError): Polynomial([0, 1, 4, 0, -8]) == 'test'
def test_not_eq_poly(self): self.assertFalse(Polynomial([1, 2, 3, 4]) == Polynomial([1, 2, 3, 5]))
def test_init_fails_on_incorrect_coeffs_type_string(self): with self.assertRaises(TypeError): Polynomial(['42453', '24'])
def test_init_fails_on_incorrect_coeffs_type_float(self): with self.assertRaises(TypeError): Polynomial([2.0, 6.1])
def test_error_val(self): self.assertEqual(False, Polynomial.from_string('1,2,a'))
def test_sum_number_and_poly(self): self.assertEqual(Polynomial([1, 2, 4]), 1 + Polynomial([1, 2, 3]))
def test_init_param_polynomial(self): pol1 = Polynomial([1, 2, 3]) pol2 = Polynomial(pol1) self.assertEqual(pol1.coeffs, pol2.coeffs)
def test_sum_poly_and_poly(self): self.assertEqual(Polynomial([4, 6, 8, 10]), Polynomial([1, 2, 3]) + Polynomial([4, 5, 6, 7]))
def test_init_with_start_zeros_in_param(self): self.assertEqual([1, 2, -4], Polynomial((0, 0, 0, 1, 2, -4)).coeffs)
def test_sub_poly_and_number(self): self.assertEqual(Polynomial([1, 2, 2]), Polynomial([1, 2, 3]) - 1)
def test_init_param_zero(self): self.assertEqual([0], Polynomial([0]).coeffs)
def test_sub_poly_and_zero(self): self.assertEqual(Polynomial([1, 2, 3]), Polynomial([1, 2, 3]) - 0)
def test_init_param_only_zeros(self): self.assertEqual([0], Polynomial([0, 0, 0]).coeffs)
def test_init_param_list(self): _list = list([1, 2, 3]) self.assertEqual(_list, Polynomial(_list).coeffs)
def test_init_fails_on_empty_param(self): with self.assertRaises(AttributeError): Polynomial([])
def test_sub_poly_and_poly_with_check_zero(self): self.assertEqual(Polynomial([1, 0]), Polynomial([1, 2, 3]) - Polynomial([1, 1, 3]))
def test_init_fails_on_incorrect_input_param(self): with self.assertRaises(TypeError): Polynomial(1)