Exemplo n.º 1
0
 def testGivenExpressionWithTwoVariablesWithSameNameWhenApplyAndSimplifyThenGetValueReturnsCorrectValue(
         self):
     expression = ExpressionBuilder().term(VariableBuilder().name('x').value(3.0).build()). \
         term(VariableBuilder().name('x').value(2.0).build()).build()
     expression.apply('x', 1.0)
     expression.simplify_constant()
     self.assertEqual(expression.get_value_constant(), 5.0)
Exemplo n.º 2
0
 def testGivenWhenThen0(self):
     eqs = EquationSystemBuilder().build()
     eqs.add(EquationBuilder.y_equals_x())
     a = EquationBuilder.one_equals_x()
     b = EquationBuilder().left_term(
         VariableBuilder().name('z').build()).right_term(
             VariableBuilder().name('x').build()).right_term(
                 VariableBuilder().name('y').build()).build()
     a.add_equation(EquationBuilder.one_equals_y())
     eqs.add(a)
     eqs.add(b)
     reduction_method = ReductionMethod()
     equation_system_solver = EquationSystemSolver(eqs, reduction_method)
     equation_system_solver.resolve()
     self.assertTrue(
         equation_system_solver.get_solution('x').equal(
             EquationBuilder.x_equals_1()))
     self.assertTrue(
         equation_system_solver.get_solution('y').equal(
             EquationBuilder.y_equals_1()))
     self.assertTrue(
         equation_system_solver.get_solution('z').equal(
             EquationBuilder().left_term(
                 VariableBuilder().name('z').build()).right_term(
                     ConstantBuilder().value(2).build()).build()))
Exemplo n.º 3
0
 def testGivenExpressionWithTwoVariablesWithDifferentNameWhenGetNameSetThenSetWithVariablesNamesIsReturned(
         self):
     variable1 = VariableBuilder().name('x').build()
     variable2 = VariableBuilder().name('y').build()
     expression = ExpressionBuilder().term(variable1).term(
         variable2).build()
     self.assertEqual(expression.get_name_set(),
                      {variable1.name, variable2.name})
 def testEqualWithConstantDifferentValueNegative(self):
     variable1 = VariableBuilder().value(3.0).build()
     variable2 = Mock()
     variable2.value = 2.0
     variable2.dispatch = Mock(
         side_effect=lambda x: x.visit_constant(variable2))
     self.assertFalse(variable1.equal(variable2))
     self.assertNotEqual(variable1, variable2)
Exemplo n.º 5
0
 def testGivenExpressionWithTwoVariablesSameNameWhenSimplifyThenExpressionWithOneVariableWithCorrectValueIsObtained(
         self):
     variable1 = VariableBuilder().name('x').build()
     expression = ExpressionBuilder().term(variable1).term(variable1).term(
         VariableBuilder().name('y').build()).default_constant().build()
     expression.simplify_variable(variable1.name)
     self.assertTrue(expression.get_value_variable(variable1.name),
                     2 * variable1.value)
Exemplo n.º 6
0
 def testGivenExpressionWithTwoVariablesWithInverseValuesWhenSimplifyThenExpressionWithNoVariablesAndConstantWithValue0IsObtained(
         self):
     variable1 = VariableBuilder().build()
     variable2 = variable1.clon()
     variable2.multiply(-1.0)
     expression = ExpressionBuilder().term(variable1).term(
         variable2).build()
     expression.simplify_variable(variable1.name)
     self.assertEqual(expression.get_value_constant(), 0.0)
     self.assertEqual(expression.get_value_variable(variable1.name), 0.0)
 def testGivenXIs0EquationWhenAddOIsYEquationThenYIsXEquationIsObtained(
         self):
     equation = EquationBuilder.x_equals_0()
     equation.add_equation(EquationBuilder.zero_equals_y())
     expected_equation = EquationBuilder().left_term(
         VariableBuilder().name('x').value(1.0).build()).left_term(
             ConstantBuilder().value(0).build()).right_term(
                 ConstantBuilder().value(0).build()).right_term(
                     VariableBuilder().name('y').value(
                         1.0).build()).build()
     self.assertTrue(equation.equal(expected_equation))
Exemplo n.º 8
0
 def testGivenExpressionWithOneVariableWhenAddExpressionWithOtherVariableAndConstantThenExpressionContainsAllTerms(
         self):
     variable1 = VariableBuilder().name('x').build()
     variable2 = VariableBuilder().name('y').build()
     constant = ConstantBuilder().value(1.0).build()
     expression1 = ExpressionBuilder().term(variable1).build()
     expression2 = ExpressionBuilder().term(variable2).term(
         constant).build()
     expression1.add_expression(expression2)
     self.assertEqual(expression1.get_name_set(),
                      {variable1.name, variable2.name})
     self.assertEqual(expression1.get_value_constant(), constant.value)
Exemplo n.º 9
0
 def testGivenExpressionWithConstantAndVariableWhenMultiplyThenGetValuesReturnsMultipliedTermsValues(
         self):
     constant = ConstantBuilder().build()
     variable = VariableBuilder().build()
     expression = ExpressionBuilder().term(constant).term(variable).build()
     multiply_value = 2.0
     expression.multiply(multiply_value)
     constant.multiply(multiply_value)
     variable.multiply(multiply_value)
     self.assertEqual(expression.get_value_constant(), constant.value)
     self.assertEqual(expression.get_value_variable(variable.name),
                      variable.value)
 def testGivenEmptyEquationWhenAddTermThenTermIsAddedInBothSides(self):
     equation = EquationBuilder().build()
     term = VariableBuilder().build()
     inverse_term = term.clon()
     inverse_term.multiply(-1.0)
     equation.add(term)
     self.assertEqual(equation.get_name_set(), {term.name})
     equation.add_side(Side.left, inverse_term)
     equation.simplify_variable(Side.left, term.name)
     self.assertEqual(equation.get_value_constant(Side.left), 0.0)
     self.assertEqual(equation.get_name_set(), {term.name})
     equation.add_side(Side.right, inverse_term)
     equation.simplify_variable(Side.right, term.name)
     self.assertEqual(equation.get_value_constant(Side.right), 0.0)
     self.assertEqual(equation.get_name_set(), set())
 def testGivenEquationWithSameVariableTwiceAtRightSidesWhenGetValueThenRaisesNotSimplified(
         self):
     variable = VariableBuilder().build()
     equation = EquationBuilder().right_term(variable).right_term(
         variable).left_default_constant().build()
     self.assertRaises(NotSimplified, equation.get_value_variable,
                       Side.right, variable.name)
 def testGivenEquationWithOneVariableAtLeftWhenGetValueThenVariableValueIsReturned(
         self):
     variable = VariableBuilder().build()
     equation = EquationBuilder().left_term(
         variable).right_default_constant().build()
     self.assertEqual(equation.get_value_variable(Side.left, variable.name),
                      variable.value)
Exemplo n.º 13
0
 def testGivenExpressionWithOneVariableWhenSimplifyNameThenSameExpressionIsObtained(
         self):
     variable = VariableBuilder().build()
     expression = ExpressionBuilder().term(variable).build()
     expression2 = expression.clon()
     expression.simplify_variable(variable.name)
     self.assertTrue(expression.equal(expression2))
Exemplo n.º 14
0
 def testGivenExpressionWithVariableWhenMultiplyBy0ThenExpressionWithConstant0IsObtained(
         self):
     variable = VariableBuilder().build()
     expression = ExpressionBuilder().term(variable).build()
     expression.multiply(0.0)
     self.assertEqual(expression.get_value_constant(), 0)
     self.assertEqual(expression.get_value_variable(variable.name), 0.0)
Exemplo n.º 15
0
 def testGivenExpressionWithVariableWhenAddZeroValueVariableThenNoVariableIsAdded(
         self):
     expression = ExpressionBuilder().default_variable().build()
     expression_original = expression.clon()
     zero = VariableBuilder().value(0).build()
     expression.add_term(zero)
     self.assertEqual(str(expression), str(expression_original))
 def testGivenEquationSameVariableTwiceAtRightWithNameXWhenSimplifyNameXThenVariableIsSimplified(
         self):
     term = VariableBuilder().build()
     equation = EquationBuilder().right_term(term).right_term(
         term).left_default_constant().build()
     equation.simplify_variable(Side.right, term.name)
     self.assertEqual(equation.get_value_variable(Side.right, term.name),
                      2 * term.value)
 def testStrVariable(self):
     self.assertEqual(
         str(VariableBuilder().name('x').value(Fraction(1, 2)).build()),
         '+(1/2)*x')
     self.assertEqual(
         str(VariableBuilder().name('x').value(Fraction(-1, 2)).build()),
         '-(1/2)*x')
     self.assertEqual(
         str(VariableBuilder().name('x').value(Fraction(1, 1)).build()),
         '+x')
     self.assertEqual(
         str(VariableBuilder().name('x').value(Fraction(-1, 1)).build()),
         '-x')
     self.assertEqual(
         str(VariableBuilder().name('x').value(Fraction(-4, 2)).build()),
         '-2*x')
     self.assertEqual(
         str(VariableBuilder().name('x').value(Fraction(4, 2)).build()),
         '+2*x')
     self.assertEqual(
         str(VariableBuilder().name('x').value(Fraction(0, 2)).build()),
         '+0*x')
Exemplo n.º 18
0
 def testGivenExpressionWithVariableWhenApplyWrongValueThenGetValueRaisesLookupError(
         self):
     expression = ExpressionBuilder().term(
         VariableBuilder().name('x').build()).build()
     self.assertRaises(LookupError, expression.apply, 'y', 1.0)
 def testHasNameSetPositive(self):
     variable1 = VariableBuilder().name('x').build()
     name_set = ['x', 'y']
     self.assertTrue(variable1.has_name_set(name_set))
 def testHasNameSetNegative(self):
     variable1 = VariableBuilder().name('x').build()
     name_set = ['z', 'y']
     self.assertFalse(variable1.has_name_set(name_set))
 def testValue(self):
     variable = VariableBuilder().value(3.0).build()
     self.assertEqual(variable.value, 3.0)
Exemplo n.º 22
0
 def testGivenExpressionWithTwoVariablesWithSameNameWhenGetNameSetThenSetWithVariablesNameIsReturned(
         self):
     variable = VariableBuilder().name('x').build()
     expression = ExpressionBuilder().term(variable).term(
         variable.clon()).build()
     self.assertEqual(expression.get_name_set(), {variable.name})
Exemplo n.º 23
0
 def testGivenExpressionWithTwoVariablesWithDifferentNamesWhenHasNameWithVariableNameThenReturnsTrue(
         self):
     variable = VariableBuilder().name('x').build()
     expression = ExpressionBuilder().term(variable).term(
         VariableBuilder().name('y').build()).build()
     self.assertTrue(expression.has_name(variable.name))
Exemplo n.º 24
0
 def testGivenExpressionTwoWithVariablesWithDifferentNamesWhenGetValueNameThenCorrectVariableValueIsReturned(
         self):
     variable = VariableBuilder().name('x').build()
     expression = ExpressionBuilder().term(
         VariableBuilder().name('y').build()).term(variable).build()
     self.assertEqual(expression.get_value_variable('x'), variable.value)
Exemplo n.º 25
0
 def testGivenExpressionWithTwoVariablesWithSameNameWhenGetValueNameThenNotSimplifiedIsRaised(
         self):
     variable = VariableBuilder().name('x').build()
     expression = ExpressionBuilder().term(variable).term(
         variable.clon()).build()
     self.assertRaises(NotSimplified, expression.get_value_variable, 'x')
Exemplo n.º 26
0
 def testGivenExpressionWithVariableWhenApplyThenGetValueReturnsCorrectValue(
         self):
     expression = ExpressionBuilder().term(
         VariableBuilder().name('x').value(3.0).build()).build()
     expression.apply('x', 1.0)
     self.assertEqual(expression.get_value_constant(), 3.0)
Exemplo n.º 27
0
 def testGivenExpressionWithVariableAndConstantWhenGetValueNameThenVariableValueIsReturned(
         self):
     variable = VariableBuilder().name('x').build()
     expression = ExpressionBuilder().default_constant().term(
         variable).build()
     self.assertEqual(expression.get_value_variable('x'), variable.value)
Exemplo n.º 28
0
 def testGivenExpressionWithVariableOnlyWhenHasNameWithVariableNameThenReturnsTrue(
         self):
     variable = VariableBuilder().build()
     expression = ExpressionBuilder().term(variable).build()
     self.assertTrue(expression.has_name(variable.name))
Exemplo n.º 29
0
 def testGivenExpressionWithVariablesWithSpecificNamesWhenGetValueNameWithOtherVariableNameThen0IsReturned(
         self):
     expression = ExpressionBuilder().term(
         VariableBuilder().name('y').build()).term(VariableBuilder().name(
             'z').build()).default_constant().build()
     self.assertEqual(expression.get_value_variable('x'), 0)
Exemplo n.º 30
0
 def testGivenExpressionWithVariableOnlyWhenHasNameWithDifferentVariableNameThenReturnsFalse(
         self):
     variable = VariableBuilder().name('x').build()
     expression = ExpressionBuilder().term(variable).build()
     self.assertFalse(expression.has_name('y'))