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_every_operator(self): a = solution.create_variable('a') b = solution.create_variable('b') x = ((((5 // b) << (5 % b))**b) >> (((5 | b) - (5 & b)) ^ b)) y = ((((a // 3) << (a % 3))**3) >> (((a | 3) - (a & 3)) ^ 3)) z = ((((a // b) << (a % b))**b) >> (((a | b) - (a & b)) ^ b)) self.assertEqual(x.evaluate(b=3), 2) self.assertEqual(y.evaluate(a=5), 2) self.assertEqual(z.evaluate(a=5, b=3), 2)
def test_every_operator(self): a = solution.create_variable('a') b = solution.create_variable('b') x = ((((5 // b) << (5 % b)) ** b) >> (((5 | b) - (5 & b)) ^ b)) y = ((((a // 3) << (a % 3)) ** 3) >> (((a | 3) - (a & 3)) ^ 3)) z = ((((a // b) << (a % b)) ** b) >> (((a | b) - (a & b)) ^ b)) self.assertEqual(x.evaluate(b=3), 2) self.assertEqual(y.evaluate(a=5), 2) self.assertEqual(z.evaluate(a=5, b=3), 2)
def test_expression_variable_names(self): x = s.create_variable('x') y = s.create_variable('y') z = s.create_variable('z') f = (z - 2 * (x + 1j)) + 3.5*(y-2) variables = f.variable_names expected = ['x', 'y', 'z'] for v in variables: self.assertTrue(v in expected) for v in expected: self.assertTrue(v in variables)
def test_expression_variable_names(self): x = s.create_variable('x') y = s.create_variable('y') z = s.create_variable('z') f = (z - 2 * (x + 1j)) + 3.5 * (y - 2) variables = f.variable_names expected = ['x', 'y', 'z'] for v in variables: self.assertTrue(v in expected) for v in expected: self.assertTrue(v in variables)
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_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_literals(self): x = solution.create_variable('x') y = solution.create_variable('y') expression_result = (x + 3 * (y - 2)).evaluate(x=1, y=4) self.assertEqual(expression_result, 7)
def test_variable_evaluation(self): self.assertEqual(solution.create_variable('x').evaluate(x=42), 42)
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 = solution.create_variable('y') twelve = solution.create_constant(12) expression = y + twelve self.assertEqual(expression.evaluate(y=3), 15)
def test_five_plus_three(self): plus = solution.create_operator('+', lambda lhs, rhs: lhs + rhs) x = solution.create_variable('x') y = solution.create_variable('y') added_expression = solution.create_expression((x, plus, y)) self.assertEqual(added_expression.evaluate(x=5, y=3), 8)
def test_five_plus_three(self): plus = s.create_operator('+', lambda lhs, rhs: lhs + rhs) x = s.create_variable('x') y = s.create_variable('y') added_expression = s.create_expression((x, plus, y)) self.assertEqual(added_expression.evaluate(x=5, y=3), 8)
def test_operators(self): y = s.create_variable('y') twelve = s.create_constant(12) expression = y + twelve self.assertEqual(expression.evaluate(y=3), 15)