Ejemplo n.º 1
0
    def test_weakest_list_order(self):
        """Test weakest list order"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        weakest = [el for el in c_eq._weakest]
        a.value = 4

        self.assertEqual(
            c_eq._weakest,
            weakest)  # does not change if non-weak variable changed

        b.value = 5
        self.assertEqual(c_eq.weakest(), c)

        b.value = 6
        self.assertEqual(c_eq.weakest(), c)

        # b changed above, now change a - all weakest variables changed
        # return the oldest changed variable then
        c.value = 6
        self.assertEqual(c_eq.weakest(), b)

        b.value = 6
        self.assertEqual(c_eq.weakest(), c)
Ejemplo n.º 2
0
    def test_weakest_list_order(self):
        """Test weakest list order"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        weakest = [el for el in c_eq._weakest]
        a.value = 4

        self.assertEqual(c_eq._weakest, weakest) # does not change if non-weak variable changed

        b.value = 5
        self.assertEqual(c_eq.weakest(), c)

        b.value = 6
        self.assertEqual(c_eq.weakest(), c)

        # b changed above, now change a - all weakest variables changed
        # return the oldest changed variable then
        c.value = 6
        self.assertEqual(c_eq.weakest(), b)

        b.value = 6
        self.assertEqual(c_eq.weakest(), c)
Ejemplo n.º 3
0
 def __init__(self):
     self.solver = Solver()
     self.a = Variable(1, 30)
     self.b = Variable(2, 10)
     self.c = Variable(3, 10)
     self.c_eq = EquationConstraint(
         lambda a, b, c: a + b + c, a=self.a, b=self.b, c=self.c
     )
     self.solver.add_constraint(self.c_eq)
Ejemplo n.º 4
0
class SolverFixture(object):
    def __init__(self):
        self.solver = Solver()
        self.a = Variable(1, 30)
        self.b = Variable(2, 10)
        self.c = Variable(3, 10)
        self.c_eq = EquationConstraint(
            lambda a, b, c: a + b + c, a=self.a, b=self.b, c=self.c
        )
        self.solver.add_constraint(self.c_eq)
Ejemplo n.º 5
0
    def test_strength_change(self):
        """Test strength change"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        b.strength = 9
        self.assertEqual(c_eq._weakest, [b])
Ejemplo n.º 6
0
    def test_strength_change(self):
        """Test strength change"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        b.strength = 9
        self.assertEqual(c_eq._weakest, [b])
Ejemplo n.º 7
0
    def test_weakest_list(self):
        """Test weakest list"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        # because of kwargs above we cannot test by the order of arguments
        self.assertTrue(b in c_eq._weakest)
        self.assertTrue(c in c_eq._weakest)
Ejemplo n.º 8
0
    def test_weakest_list(self):
        """Test weakest list"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        # because of kwargs above we cannot test by the order of arguments
        self.assertTrue(b in c_eq._weakest)
        self.assertTrue(c in c_eq._weakest)
Ejemplo n.º 9
0
    def __init__(self, solver: Optional[Solver] = None) -> None:
        self._solver = solver or Solver()
        self._connections: table.Table[Connection] = table.Table(
            Connection, tuple(range(5)))
        self._handlers: Set[Callable[[Connection], None]] = set()

        self._solver.add_handler(self._on_constraint_solved)
Ejemplo n.º 10
0
def test_juggle_error_is_raised_when_constraints_can_not_be_resolved():
    solver = Solver()
    a = Variable()
    b = Variable()
    c = Variable(40, strength=REQUIRED)
    d = Variable(30, strength=REQUIRED)

    solver.add_constraint(EqualsConstraint(a, b))
    solver.add_constraint(EqualsConstraint(a, c))
    solver.add_constraint(EqualsConstraint(b, d))

    with pytest.raises(JuggleError):
        solver.solve()
Ejemplo n.º 11
0
def test_notify_for_nested_constraint():
    events = []
    solver = Solver()
    a = Variable()
    b = Variable()
    nested = EqualsConstraint(a, b)
    multi = MultiConstraint(nested)

    solver.add_constraint(multi)
    solver.solve()

    def handler(constraint):
        events.append(constraint)

    solver.add_handler(handler)

    a.value = 10

    solver.solve()

    assert multi in events
    assert nested not in events
Ejemplo n.º 12
0
def test_weakest_list_order():
    solver = Solver()
    a = Variable(1, 30)
    b = Variable(2, 10)
    c = Variable(3, 10)
    c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
    solver.add_constraint(c_eq)
    a.value = 4

    b.value = 5
    assert c_eq.weakest() == c

    b.value = 6
    assert c_eq.weakest() == c

    # b changed above, now change a - all weakest variables changed return the
    # oldest changed variable
    c.value = 6
    assert c_eq.weakest() == b

    b.value = 6
    assert c_eq.weakest() == c
Ejemplo n.º 13
0
    def test_min_size(self):
        """Test minimal size constraint"""
        solver = Solver()
        v1 = Variable(0)
        v2 = Variable(10)
        v3 = Variable(10)
        c1 = EqualsConstraint(a=v2, b=v3)
        c2 = LessThanConstraint(smaller=v1, bigger=v3, delta=10)
        solver.add_constraint(c1)
        solver.add_constraint(c2)

        # check everyting is ok on start
        solver.solve()
        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)

        # change v1 to 2, after solve it should be 0 again due to LT
        # constraint
        v1.value = 2
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)

        # change v3 to 20, after solve v2 will follow thanks to EQ
        # constraint
        v3.value = 20
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(20, v2)
        self.assertEquals(20, v3)

        # change v3 to 0, after solve it shoul be 10 due to LT.delta = 10,
        # v2 should also be 10 due to EQ constraint
        v3.value = 0
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)
Ejemplo n.º 14
0
def test_connections_with_custom_solver():
    solver = Solver()
    connections = Connections(solver)

    assert connections.solver is solver
Ejemplo n.º 15
0
def test_minimal_size_constraint():
    solver = Solver()
    v1 = Variable(0)
    v2 = Variable(10)
    v3 = Variable(10)
    c1 = EqualsConstraint(a=v2, b=v3)
    c2 = LessThanConstraint(smaller=v1, bigger=v3, delta=10)
    solver.add_constraint(c1)
    solver.add_constraint(c2)

    # Check everything is ok on start
    solver.solve()
    assert 0 == v1
    assert 10 == v2
    assert 10 == v3

    # Change v1 to 2, after solve it should be 0 again due to LT constraint
    v1.value = 2
    solver.solve()

    assert 0 == v1
    assert 10 == v2
    assert 10 == v3

    # Change v3 to 20, after solve v2 will follow thanks to EQ constraint
    v3.value = 20
    solver.solve()

    assert 0 == v1
    assert 20 == v2
    assert 20 == v3

    # Change v3 to 0, after solve it should be 10 due to LT.delta = 10, v2
    # should also be 10 due to EQ constraint
    v3.value = 0
    solver.solve()

    assert 0 == v1
    assert 10 == v2
    assert 10 == v3
Ejemplo n.º 16
0
def test_solver_implements_constraint_protocol():
    solver = Solver()

    assert isinstance(solver, Constraint)
    assert isinstance(solver, ContainsConstraints)
Ejemplo n.º 17
0
def solver():
    return Solver()
Ejemplo n.º 18
0
    def test_min_size(self):
        """Test minimal size constraint"""
        solver = Solver()
        v1 = Variable(0)
        v2 = Variable(10)
        v3 = Variable(10)
        c1 = EqualsConstraint(a=v2, b=v3)
        c2 = LessThanConstraint(smaller=v1, bigger=v3, delta=10)
        solver.add_constraint(c1)
        solver.add_constraint(c2)

        # check everyting is ok on start
        solver.solve()
        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)

        # change v1 to 2, after solve it should be 0 again due to LT
        # constraint
        v1.value = 2
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)

        # change v3 to 20, after solve v2 will follow thanks to EQ
        # constraint
        v3.value = 20
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(20, v2)
        self.assertEquals(20, v3)

        # change v3 to 0, after solve it shoul be 10 due to LT.delta = 10,
        # v2 should also be 10 due to EQ constraint
        v3.value = 0
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)