def test_complex_expression(self): six = solution.create_constant(6) nine = solution.create_constant(9) times = solution.create_operator('*', lambda lhs, rhs: lhs * rhs) minus = solution.create_operator('-', lambda lhs, rhs: lhs - rhs) plus = solution.create_operator('+', lambda lhs, rhs: lhs + rhs) x = solution.create_variable('x') y = solution.create_variable('y') expression = solution.create_expression( (six, times, ((x, minus, y), plus, nine)) ) self.assertEqual(expression.evaluate(x=5, y=2), 72)
def test_str_method_of_classes(self): four = s.create_constant(4) y = s.create_variable('y') f = four + 3.5*(y-2) self.assertEqual(str(four), '4') self.assertEqual(str(y), 'y') self.assertEqual(str(f), '(4 + (3.5 * (y - 2)))')
def test_variable_names(self): x = solution.create_variable('x') y = solution.create_variable('y') eight = solution.create_constant(8) expression = x + y + eight self.assertEqual(expression.variable_names, ('x', 'y'))
def test_str_method_of_classes(self): four = s.create_constant(4) y = s.create_variable('y') f = four + 3.5 * (y - 2) self.assertEqual(str(four), '4') self.assertEqual(str(y), 'y') self.assertEqual(str(f), '(4 + (3.5 * (y - 2)))')
def test_expressions(self): four = s.create_constant(4) y = s.create_variable('y') f = four / 2 + 3.5 * (y - 2) d = (3 + 1j) + y z = 15 - 2 / four * (12 / f) self.assertEqual(d.evaluate(y=2), 5 + 1j) self.assertEqual(f.evaluate(y=2), 2) self.assertEqual(f.evaluate(y=0), -5) self.assertEqual(z.evaluate(y=2, x=3), 12.0)
def test_expressions(self): four = s.create_constant(4) y = s.create_variable('y') f = four/2 + 3.5*(y-2) d = (3+1j) + y z = 15 - 2/four * (12 / f) self.assertEqual(d.evaluate(y=2), 5+1j) self.assertEqual(f.evaluate(y=2), 2) self.assertEqual(f.evaluate(y=0), -5) self.assertEqual(z.evaluate(y=2, x=3), 12.0)
def test_constant_evaluation(self): self.assertEqual(solution.create_constant(5).evaluate(), 5) self.assertEqual(solution.create_constant(10 + 5).evaluate(), 15)
def test_operators(self): y = solution.create_variable('y') twelve = solution.create_constant(12) expression = y + twelve self.assertEqual(expression.evaluate(y=3), 15)
def test_create_constant(self): const = s.create_constant(4.5) self.assertEqual(str(const), '4.5') self.assertEqual(const.evaluate(), 4.5)
def test_constant_evaluation(self): self.assertEqual(s.create_variable('x').evaluate(x=42), 42) self.assertEqual(s.create_constant(5).evaluate(), 5)
def test_operators(self): y = s.create_variable('y') twelve = s.create_constant(12) expression = y + twelve self.assertEqual(expression.evaluate(y=3), 15)