def testGivenXIs1EquationAndXIs0WhenAskedForEqualityThenFalseIsReturned( self): equation1 = EquationBuilder.x_equals_0() equation2 = EquationBuilder.x_equals_1() self.assertFalse(equation1.equal(equation2)) self.assertFalse(equation1 == equation2) self.assertTrue(equation1 != equation2)
def testGivenEquationSameConstantTwiceAtRightWhenSimplifyThenConstantIsSimplified( self): term = ConstantBuilder().build() equation = EquationBuilder().right_term(term).right_term(term).build() equation.simplify_constant(Side.right) self.assertEqual(equation.get_value_constant(Side.right), 2 * term.value)
def testEqualsEquationSystem3(self): eq_system = EquationSystemBuilder().build() eq_system.add(EquationBuilder.y_equals_1()) eq_system.add(EquationBuilder.one_equals_x()) eq_system2 = eq_system.clon() eq_system.add(EquationBuilder().left_variable('z', 1, 0).right_default_constant().build()) self.assertNotEqual(eq_system, eq_system2)
def testVariableIsolator(self): eq = EquationBuilder().left_constant(1).left_variable( 'x', 2, 1).right_variable('x', 1, 2).build() eq.apply_operation(Equation.VariableIsolator('x')) isolated_eq = EquationBuilder().left_variable( 'x', 1, 1).right_constant_fraction(-2, 3).build() self.assertEqual(eq, isolated_eq)
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)
def testEquationSimplifyer(self): eq = EquationBuilder().left_constant(1).left_variable( 'x', 2, 1).left_variable('x', 1, 2).right_default_constant().build() eq.apply_operation(Equation.EquationSimplifyer()) simplified_eq = EquationBuilder().left_constant(1).left_variable( 'x', 5, 2).right_default_constant().build() self.assertEqual(eq, simplified_eq)
def testVariableIsolatorWithMoreVariables(self): eq = EquationBuilder().left_constant(1).left_variable( 'x', 2, 1).left_variable('y', 1, 1).right_variable('x', 1, 1).build() eq.apply_operation(Equation.VariableIsolator('x')) isolated_eq = EquationBuilder().left_variable( 'x', 1, 1).right_constant(-1).right_variable('y', -1, 1).build() self.assertEqual(eq, isolated_eq)
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 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))
def testGivenWhenThen1(self): eqs = EquationSystemBuilder().build() eqs.add(EquationBuilder.y_equals_x()) a = EquationBuilder.one_equals_x() eqs.add(a) 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()))
def testGivenDefaultEquationWhenMultiplyBy0ThenValue0IsObtainedInBothTerms( self): equation = EquationBuilder.x_equals_1() equation.multiply(0.0) self.assertEqual(equation.get_name_set(), set()) self.assertEqual(equation.get_value_constant(Side.left), 0.0) self.assertEqual(equation.get_value_constant(Side.right), 0.0)
def testGivenEquationWithSameConstantTwiceAtRightSidesWhenGetValueThenRaisesNotSimplified( self): constant = ConstantBuilder().build() equation = EquationBuilder().right_term(constant).right_term( constant).build() self.assertRaises(NotSimplified, equation.get_value_constant, Side.right)
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 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()))
def testGivenEmptyEquationWhenAddEquationThenEquationIsEqualToAddedEquation( self): equation1 = EquationBuilder().build() equation2 = EquationBuilder.x_equals_1() equation1.add_equation(equation2) self.assertTrue(equation1.equal(equation2)) self.assertTrue(equation1 == equation2)
def tesEquationApplier(self): # 1 + 2x + y = x eq = EquationBuilder().left_constant(1).left_variable( 'x', 2, 1).left_variable('y', 1, 1).right_variable('x', 1, 1).build() # 1 +x -y = y +1 -> x = y + y -> x = 2y eq2 = EquationBuilder().left_constant(1).left_variable( 'x', 1, 1).left_variable('y', -1, 1).right_variable( 'y', 1, 1).right_constant_fraction(1, 1).build() # eq: 1 + 4y + y = 2y eq.apply_operation(Equation.EquationApplier('x', eq2)) resultant_eq = EquationBuilder().left_constant(1).left_variable( 'y', 4, 1).left_variable('y', 1, 1).right_variable('y', 2, 1).build() self.assertEqual(eq, resultant_eq)
def testStrEquationSystem(self): eq_system = EquationSystemBuilder().build() eq_system.add(EquationBuilder.one_equals_x()) eq_system.add(EquationBuilder.y_equals_1()) self.assertEqual(str(eq_system), '+1 = +x\n+y = +1')
def testGivenSystemWithTwoEquationsWithDifferentVariablesWhenRemoveOneVariableEquationThenSystemTheOtherVariableName(self): eq_system = EquationSystemBuilder().build() eq_system.add(EquationBuilder.one_equals_x()) eq_system.add(EquationBuilder.y_equals_1()) eq_system.remove(EquationBuilder.one_equals_x()) self.assertEqual(eq_system.get_name_set(), {'y'})
def testGivenSystemWithOneEuationWhenRemoveThisEquationThenSystemHasNoNames(self): eq_system = EquationSystemBuilder().build() eq_system.add(EquationBuilder.one_equals_x()) eq_system.remove(EquationBuilder.one_equals_x()) self.assertEqual(eq_system.get_name_set(), set())
def testGivenEmptySystemWhenAddEquationWithXAndAddWithYThenXAndYarePresent(self): eq_system = EquationSystemBuilder().build() eq_system.add(EquationBuilder.one_equals_x()) eq_system.add(EquationBuilder.y_equals_1()) self.assertEqual(eq_system.get_name_set(), {'x', 'y'})
def testGivenEmptySystemWhenAddEquationWithXThenXisPresent(self): eq_system = EquationSystemBuilder().build() eq_system.add(EquationBuilder.x_equals_1()) self.assertEqual(eq_system.get_name_set(), {'x'})
def testReprEquation(self): self.assertEqual( repr(EquationBuilder.x_equals_1()), 'Equation(Expression([Variable(\'x\', Fraction(1, 1))]), Expression([Constant(Fraction(1, 1))]))' )
def testGivenDefaultEquationWhenMultiplyBy1ThenSameEquationIsObtained( self): equation1 = EquationBuilder.x_equals_1() equation2 = EquationBuilder.x_equals_1() equation1.multiply(1.0) self.assertTrue(equation1.equal(equation2))
def testGivenEmptyEquationWhenAddConstantThenItIsAddedInBothSides(self): equation = EquationBuilder().build() term = ConstantBuilder().build() equation.add(term) self.assertEqual(equation.get_value_constant(Side.left), term.value) self.assertEqual(equation.get_value_constant(Side.right), term.value)
def testGivenEmptyEquationWhenAddConstantSideRightThenItIsAddedInRightSide( self): equation = EquationBuilder().build() term = ConstantBuilder().value(1.0).build() equation.add_side(Side.right, term) self.assertEqual(equation.get_value_constant(Side.right), term.value)
def testReprEquationSyetem(self): eq_system = EquationSystemBuilder().build() eq_system.add(EquationBuilder.one_equals_x()) eq_system.add(EquationBuilder.y_equals_1()) self.assertEqual(repr(eq_system), 'EquationSystem([Equation(Expression([Constant(Fraction(1, 1))]), Expression([Variable(\'x\', Fraction(1, 1))])), Equation(Expression([Variable(\'y\', Fraction(1, 1))]), Expression([Constant(Fraction(1, 1))]))])')
def testEqualsEquationSystem(self): eq_system = EquationSystemBuilder().build() eq_system.add(EquationBuilder.one_equals_x()) eq_system.add(EquationBuilder.y_equals_1()) eq_system2 = eq_system.clon() self.assertEqual(eq_system, eq_system2)
def testStrEquation(self): self.assertEqual(str(EquationBuilder.x_equals_1()), '+x = +1')
def testGivenEmptyEquationWhenGetValueThen0IsReturned(self): equation = EquationBuilder().build() self.assertEqual(equation.get_value_variable(Side.left, 'x'), 0.0) self.assertEqual(equation.get_value_variable(Side.right, 'x'), 0.0)
def testGivenXEqualsOneEquationWhenInvertThenEquationOneEqualsXIsReturned( self): eq = EquationBuilder.x_equals_1() eq.invert() self.assertEqual(eq.get_value_constant(Side.left), 1.0)