class ConstraintTests(TestCase): def setUp(self): self.constraint_system = ConstraintSystem() self.variables = self.constraint_system.create_variables( ["v1", "v2", "v3"], [4, 5, 3]) self.v1, self.v2, self.v3 = self.variables method1_2 = Method(self.v1, self.v2, lambda x: x // 2) method1_3 = Method(self.v1, self.v3, lambda x: x // 3) self.constraint = Constraint(lambda v1, v2, v3: True, Strength.STRONG, self.variables, [method1_3, method1_2]) self.constraint_system.add_constraint(self.constraint) def tearDown(self): pass def test_adding_enforced_to_pplan(self): self.assertIsNone(self.constraint.mark) mark = self.constraint_system.marker.new_mark() pplan = self.constraint.add_to_pplan([], mark) self.assertEqual([self.constraint], pplan) self.assertEqual(mark, self.constraint.mark) def test_adding_unenforced_to_pplan(self): self.constraint.selected_method = None self.assertIsNone(self.constraint.mark) pplan = self.constraint.add_to_pplan( [], self.constraint_system.marker.new_mark()) self.assertEqual([], pplan) self.assertIsNone(self.constraint.mark) def test_adding_with_the_same_mark(self): mark = self.constraint_system.marker.new_mark() self.constraint.mark = mark pplan = self.constraint.add_to_pplan([], mark) self.assertEqual([], pplan) self.assertEqual(mark, self.constraint.mark) def test_adding_with_other_mark(self): mark1 = self.constraint_system.marker.new_mark() mark2 = self.constraint_system.marker.new_mark() self.constraint.mark = mark1 pplan = self.constraint.add_to_pplan([], mark2) self.assertEqual([self.constraint], pplan) self.assertEqual(mark2, self.constraint.mark)
class ConstraintTests(TestCase): def setUp(self): self.constraint_system = ConstraintSystem() self.variables = self.constraint_system.create_variables(["v1", "v2", "v3"], [4, 5, 3]) self.v1, self.v2, self.v3 = self.variables method1_2 = Method(self.v1, self.v2, lambda x: x // 2) method1_3 = Method(self.v1, self.v3, lambda x: x // 3) self.constraint = Constraint(lambda v1, v2, v3: True, Strength.STRONG, self.variables, [method1_3, method1_2]) self.constraint_system.add_constraint(self.constraint) def tearDown(self): pass def test_adding_enforced_to_pplan(self): self.assertIsNone(self.constraint.mark) mark = self.constraint_system.marker.new_mark() pplan = self.constraint.add_to_pplan([], mark) self.assertEqual([self.constraint], pplan) self.assertEqual(mark, self.constraint.mark) def test_adding_unenforced_to_pplan(self): self.constraint.selected_method = None self.assertIsNone(self.constraint.mark) pplan = self.constraint.add_to_pplan([], self.constraint_system.marker.new_mark()) self.assertEqual([], pplan) self.assertIsNone(self.constraint.mark) def test_adding_with_the_same_mark(self): mark = self.constraint_system.marker.new_mark() self.constraint.mark = mark pplan = self.constraint.add_to_pplan([], mark) self.assertEqual([], pplan) self.assertEqual(mark, self.constraint.mark) def test_adding_with_other_mark(self): mark1 = self.constraint_system.marker.new_mark() mark2 = self.constraint_system.marker.new_mark() self.constraint.mark = mark1 pplan = self.constraint.add_to_pplan([], mark2) self.assertEqual([self.constraint], pplan) self.assertEqual(mark2, self.constraint.mark)
def test_pplan_add_for_one_constraint(self): v = Variable("v", 1, self.constraint_system) constraint = Constraint(lambda x: x == 5, Strength.STRONG, v, Method([], v, lambda: 5)) self.constraint_system.add_constraint(constraint) self.assertTrue(constraint, v.determined_by) self.assertEqual( constraint.add_to_pplan([], self.constraint_system.marker.new_mark()), [constraint], "should contain only the constraint")
def test_pplan_add_for_one_constraint(self): v = Variable("v", 1, self.constraint_system) constraint = Constraint(lambda x: x == 5, Strength.STRONG, v, Method([], v, lambda: 5)) self.constraint_system.add_constraint(constraint) self.assertTrue(constraint, v.determined_by) self.assertEqual( constraint.add_to_pplan([], self.constraint_system.marker.new_mark()), [constraint], "should contain only the constraint")
def test_pplan_add_for_two_constraint(self): v = Variable("v", 1, self.constraint_system) constraint1 = Constraint(lambda x: x == 5, Strength.WEAK, v, Method([], v, lambda: 5)) constraint2 = Constraint(lambda x: x == 6, Strength.STRONG, v, Method([], v, lambda: 6)) self.constraint_system.add_constraint(constraint1) self.constraint_system.add_constraint(constraint2) self.assertFalse(constraint1.is_enforced()) self.assertTrue(constraint2.is_enforced()) self.assertTrue(constraint2, v.determined_by) self.assertEqual( constraint2.add_to_pplan([], self.constraint_system.marker.new_mark()), [constraint2], "does not add other constraint of \ a variable if it is not enforced") self.assertEqual( constraint1.add_to_pplan([], self.constraint_system.marker.new_mark()), [], "does not add unenforced constraints")
def test_pplan_add_for_two_constraint(self): v = Variable("v", 1, self.constraint_system) constraint1 = Constraint(lambda x: x == 5, Strength.WEAK, v, Method([], v, lambda: 5)) constraint2 = Constraint(lambda x: x == 6, Strength.STRONG, v, Method([], v, lambda: 6)) self.constraint_system.add_constraint(constraint1) self.constraint_system.add_constraint(constraint2) self.assertFalse(constraint1.is_enforced()) self.assertTrue(constraint2.is_enforced()) self.assertTrue(constraint2, v.determined_by) self.assertEqual( constraint2.add_to_pplan([], self.constraint_system.marker.new_mark()), [constraint2], "does not add other constraint of \ a variable if it is not enforced") self.assertEqual( constraint1.add_to_pplan([], self.constraint_system.marker.new_mark()), [], "does not add unenforced constraints")
class ConstraintTests(TestCase): def setUp(self): self.cs = ConstraintSystem() self.vars = self.cs.create_variables(["v1", "v2", "v3"], [4, 5, 3]) self.v1, self.v2, self.v3 = self.vars m1_2 = Method(self.v1, self.v2, lambda x: x // 2) m1_3 = Method(self.v1, self.v3, lambda x: x // 3) self.cn = Constraint(lambda v1, v2, v3: True, Strength.STRONG, self.vars, [m1_3, m1_2]) self.cs.add_constraint(self.cn) def tearDown(self): pass def test_adding_enforced_to_pplan(self): self.assertIsNone(self.cn.mark) mark = self.cs.marker.new_mark() pplan = self.cn.add_to_pplan([], mark) self.assertEqual([self.cn], pplan) self.assertEqual(mark, self.cn.mark) def test_adding_unenforced_to_pplan(self): self.cn.selected_method = None self.assertIsNone(self.cn.mark) pplan = self.cn.add_to_pplan([], self.cs.marker.new_mark()) self.assertEqual([], pplan) self.assertIsNone(self.cn.mark) def test_adding_with_the_same_mark(self): mark = self.cs.marker.new_mark() self.cn.mark = mark pplan = self.cn.add_to_pplan([], mark) self.assertEqual([], pplan) self.assertEqual(mark, self.cn.mark) def test_adding_with_other_mark(self): mark1 = self.cs.marker.new_mark() mark2 = self.cs.marker.new_mark() self.cn.mark = mark1 pplan = self.cn.add_to_pplan([], mark2) self.assertEqual([self.cn], pplan) self.assertEqual(mark2, self.cn.mark) def test_adding_iterative_should_be_in_the_right_order(self): cs = ConstraintSystem() all_vars = cs.create_variables(["a", "b", "c"], [1, 2, 3]) a, b, c = all_vars m1 = Method([a, c], [b], lambda a,c: a + c) m2 = Method([b, c], [a], lambda b,c: b - c) cn = Constraint(lambda a,b,c: a + c == b, Strength.STRONG, [a, b, c], [m1, m2], "cn1") self.cs.add_constraint(cn) a.stay() a_stay_cn = a.stay_constraint c.set_value(8) c_set_cn = cs.forced_constraint cs.exec_roots = [c_set_cn, a_stay_cn] self.assertEqual([cn, c_set_cn, a_stay_cn], cs.exec_pplan_create())