Пример #1
0
    def test_cycle_variables_should_be_invalid(self):
        a, b, c, d = self.constraint_system.create_variables(
            ["a", "b", "c", "d"], [1, 2, 3, 0])
        one = Variable("1", 1, self.constraint_system)

        constraint1 = ConstraintFactory().scale_constraint(
            b, a, one, one, Strength.STRONG, "constraint1")
        constraint2 = ConstraintFactory().scale_constraint(
            c, b, one, one, Strength.STRONG, "constraint2")
        constraint3 = ConstraintFactory().scale_constraint(
            a, c, one, one, Strength.STRONG, "constraint3")
        constraint4 = ConstraintFactory().scale_constraint(
            a, d, one, one, Strength.STRONG, "constraint4")

        try:
            for constraint in [
                    constraint1, constraint2, constraint3, constraint4
            ]:
                self.constraint_system.add_constraint(constraint)
        except CycleException as c_exc:
            self.assertEqual(set([constraint1, constraint2, constraint3]),
                             set(c_exc.cycled_constraints))
            for variable in c_exc.cycled_variables:
                self.assertFalse(variable.valid,
                                 "%s should be invalid" % (variable))
        else:
            raise Exception("should have risen an CycleException")
Пример #2
0
    def test_cycle_should_be_detected(self):
        a, b, c, d = self.constraint_system.create_variables(
            ["a", "b", "c", "d"], [1, 2, 3, 0])
        one = Variable("1", 1, self.constraint_system)

        constraint1 = ConstraintFactory().scale_constraint(
            b, a, one, one, Strength.STRONG)
        constraint2 = ConstraintFactory().scale_constraint(
            c, b, one, one, Strength.STRONG)
        constraint3 = ConstraintFactory().scale_constraint(
            a, c, one, one, Strength.STRONG)
        constraint4 = ConstraintFactory().scale_constraint(
            a, d, one, one, Strength.STRONG)

        with self.assertRaises(CycleException) as e:
            for constraint in [
                    constraint1, constraint2, constraint3, constraint4
            ]:
                self.constraint_system.add_constraint(constraint)
Пример #3
0
    def test_equality_constraint(self):
        variable1 = Variable("variable1", 10, self.constraint_system)
        variable2 = Variable("variable2", 20, self.constraint_system)

        constraint = ConstraintFactory.equality_constraint(
            variable1, variable2, Strength.STRONG)

        self.constraint_system.add_constraint(constraint)

        self.assertTrue(variable1.get_value() == variable2.get_value())
Пример #4
0
  def test_equality_constraint(self):
    variable1 = Variable("variable1", 10, self.constraint_system)
    variable2 = Variable("variable2", 20, self.constraint_system)

    constraint = ConstraintFactory.equality_constraint(
        variable1,
        variable2,
        Strength.STRONG
      )

    self.constraint_system.add_constraint(constraint)

    self.assertTrue(variable1.get_value()==variable2.get_value())
Пример #5
0
    def test_scale_constraint(self):
        destination = Variable("destination", 1, self.constraint_system)
        source = Variable("source", 2, self.constraint_system)
        scale = Variable("scale", 3, self.constraint_system)
        offset = Variable("offset", 4, self.constraint_system)

        constraint = ConstraintFactory.scale_constraint(
            destination, source, scale, offset, Strength.STRONG)

        self.constraint_system.add_constraint(constraint)

        self.assertTrue(
            destination.get_value() == scale.get_value() * source.get_value() +
            offset.get_value())
        destination.set_value(100)
        self.assertTrue(
            destination.get_value() == scale.get_value() * source.get_value() +
            offset.get_value())
Пример #6
0
  def test_scale_constraint(self):
    destination = Variable("destination", 1, self.constraint_system)
    source = Variable("source", 2, self.constraint_system)
    scale = Variable("scale", 3, self.constraint_system)
    offset = Variable("offset", 4, self.constraint_system)

    constraint = ConstraintFactory.scale_constraint(
        destination,
        source,
        scale,
        offset,
        Strength.STRONG
      )

    self.constraint_system.add_constraint(constraint)

    self.assertTrue(destination.get_value() == scale.get_value() * source.get_value() + offset.get_value())
    destination.set_value(100)
    self.assertTrue(destination.get_value() == scale.get_value() * source.get_value() + offset.get_value())