class VariableTest(unittest.TestCase): """ This only tests basic getters and setters, no constraint solving technology. """ def setUp(self): self.variable = Variable("variable", 10, Mock()) self.variable.system.remove_constraint = Mock() self.variable.system._add_stay_constraint = Mock() def test_get_and_set_variable(self): self.assertEqual(self.variable.get_value(), 10) self.variable.set_value(5) self.assertEqual(5, self.variable.get_value()) def test_stay(self): self.assertTrue(self.variable.stay_constraint is None) self.variable.stay() self.assertFalse(self.variable.stay_constraint is None) self.variable.system._add_stay_constraint.assert_called_with( self.variable, Strength.WEAK) def test_stay_tow_times(self): self.variable.stay() self.assertFalse(self.variable.system.remove_constraint.called) self.variable.stay(Strength.STRONG) self.assertTrue(self.variable.system.remove_constraint.called) self.variable.system._add_stay_constraint.assert_called_with( self.variable, Strength.STRONG)
class VariableTest(unittest.TestCase): """ This only tests basic getters and setters, no constraint solving technology. """ def setUp(self): self.variable = Variable("variable", 10, Mock()) self.variable.system.remove_constraint = Mock() self.variable.system.add_stay_constraint = Mock() def test_get_and_set_variable(self): self.assertEqual(self.variable.get_value(), 10) self.variable.set_value(5) self.assertEqual(5, self.variable.get_value()) def test_stay(self): self.assertTrue(self.variable.stay_constraint is None) self.variable.stay() self.assertFalse(self.variable.stay_constraint is None) self.variable.system.add_stay_constraint.assert_called_with(self.variable,Strength.WEAK) def test_stay_tow_times(self): self.variable.stay() self.assertFalse(self.variable.system.remove_constraint.called) self.variable.stay(Strength.STRONG) self.assertTrue(self.variable.system.remove_constraint.called) self.variable.system.add_stay_constraint.assert_called_with(self.variable,Strength.STRONG)
def test_exec_pplan_create_with_one_undetermined_var(self): v = Variable("v", 1, self.cs) cn = Constraint(lambda x: x == 5, Strength.STRONG, v, Method([], v, lambda: 5)) self.cs.add_constraint(cn) v.determined_by = None self.cs.exec_roots = [v] self.assertEqual([cn], self.cs.exec_pplan_create())
def setUp(self): self.constraint_system = ConstraintSystem() self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system) self.point2 = Variable("Point 2", Point(10, 30), self.constraint_system) self.point3 = Variable("Point 3", Point(50, 20), self.constraint_system) self.point4 = Variable("Point 4", Point(100, 30), self.constraint_system) self.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system) mpmp3p4 = Method([self.midpoint, self.point3, self.point4], [self.point1, self.point2], lambda pm, p3, p4: ( Point( pm.X - (p4.X - p3.X), pm.Y - (p4.Y - p3.Y) ), Point( pm.X + (p4.X - p3.X), pm.Y + (p4.Y - p3.Y) ) ) ) mp1pmp3 = Method([self.point1, self.midpoint, self.point3], [self.point2, self.point4], lambda p1, pm, p3: ( Point( 2 * pm.X - p1.X, 2 * pm.Y - p1.Y ), Point( p3.X + (pm.X - p1.X), p3.Y + (pm.Y - p1.Y) ) ) ) mpmp2p4 = Method([self.midpoint, self.point2, self.point4], [self.point1, self.point3], lambda pm, p2, p4: ( Point( 2 * pm.X - p2.X, 2 * pm.Y - p2.Y ), Point( p4.X + (pm.X - p2.X), p4.Y + (pm.Y - p2.Y) ) ) ) constraint = Constraint( lambda p1, p2, p3, p4, pm: pm.is_midpoint(p1, p2) and p1.distance(pm) == p3.distance(p4), Strength.STRONG, [self.point1, self.point2, self.point3, self.point4, self.midpoint], [mpmp3p4, mp1pmp3, mpmp2p4]) self.constraint_system.add_constraint(constraint) self.point3.stay() self.point4.stay()
def __init__(self): self.constraint_system = ConstraintSystem() self.a = Variable("a", 1, self.constraint_system) self.b = Variable("b", 2, self.constraint_system) self.c = Variable("c", 3, self.constraint_system) self.a_equals_b_plus_2_constraint = self.create_a_equals_b_plus_2_constraint( ) self.a_equals_c_minus_1_constraint = self.create_a_equals_c_minus_1_constraint( ) self.a_plus_b_equals_c_constraint = self.create_a_plus_b_equals_c_constraint( )
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())
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")
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 setUp(self): self.constraint_system = ConstraintSystem() self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system) self.point2 = Variable("Point 2", Point(10, 30), self.constraint_system) self.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system) mMp = Method([self.point1, self.point2], [self.midpoint], lambda p1, p2: Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2)) mP1 = Method([self.midpoint, self.point2], [self.point1], lambda mp, p2: Point((2 * mp.X - p2.X), (2 * mp.Y - p2.Y))) mP2 = Method([self.midpoint, self.point1], [self.point2], lambda mp, p1: Point((2 * mp.X - p1.X), (2 * mp.Y - p1.Y))) constraint = Constraint( lambda point1, point2, midpoint: midpoint.is_midpoint( point1, point2), Strength.STRONG, [self.point1, self.point2, self.midpoint], [mMp, mP1, mP2]) self.constraint_system.add_constraint(constraint)
def setUp(self): self.constraint_system = ConstraintSystem() self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system) self.point2 = Variable("Point 2", Point(10, 30), self.constraint_system) self.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system) mMp = Method([self.point1, self.point2], [self.midpoint], lambda p1, p2: Point((p1.X + p2.X) / 2 , (p1.Y + p2.Y) / 2)) mP1 = Method([self.midpoint, self.point2], [self.point1], lambda mp, p2: Point((2 * mp.X - p2.X) , (2 * mp.Y - p2.Y))) mP2 = Method([self.midpoint, self.point1], [self.point2], lambda mp, p1: Point((2 * mp.X - p1.X) , (2 * mp.Y - p1.Y))) constraint = Constraint( lambda point1, point2, midpoint: midpoint.is_midpoint(point1, point2), Strength.STRONG, [self.point1, self.point2, self.midpoint], [mMp, mP1, mP2]) self.constraint_system.add_constraint(constraint)
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())
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)
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 MidpointTest(unittest.TestCase): __name__ = "MidpointTest" def setUp(self): self.constraint_system = ConstraintSystem() self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system) self.point2 = Variable("Point 2", Point(10, 30), self.constraint_system) self.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system) mMp = Method([self.point1, self.point2], [self.midpoint], lambda p1, p2: Point((p1.X + p2.X) / 2 , (p1.Y + p2.Y) / 2)) mP1 = Method([self.midpoint, self.point2], [self.point1], lambda mp, p2: Point((2 * mp.X - p2.X) , (2 * mp.Y - p2.Y))) mP2 = Method([self.midpoint, self.point1], [self.point2], lambda mp, p1: Point((2 * mp.X - p1.X) , (2 * mp.Y - p1.Y))) constraint = Constraint( lambda point1, point2, midpoint: midpoint.is_midpoint(point1, point2), Strength.STRONG, [self.point1, self.point2, self.midpoint], [mMp, mP1, mP2]) self.constraint_system.add_constraint(constraint) def test_contraint(self): midpoint = self.midpoint.get_value() point1 = self.point1.get_value() point2 = self.point2.get_value() self.assertTrue(midpoint.is_midpoint(point1,point2)) def test_change_point1(self): self.point1.set_value(Point(0, 0)) point1 = self.point1.get_value() self.test_contraint() self.assertEqual(point1.X, 0) self.assertEqual(point1.Y, 0) def test_change_midpoint(self): self.midpoint.set_value(Point(0, 0)) midpoint = self.midpoint.get_value() self.test_contraint() self.assertEqual(midpoint.X, 0) self.assertEqual(midpoint.Y, 0) def test_change_several_points(self): self.midpoint.set_value(Point(0, 0)) self.point1.set_value(Point(10, 10)) self.point2.set_value(Point(50, 50)) point2 = self.point2.get_value() self.test_contraint() self.assertEqual(point2.X, 50) self.assertEqual(point2.Y, 50) def test_change_two_points_at_the_same_time(self): self.constraint_system.change_variable_values( [self.point1, self.point2], [Point(0, 0), Point(20, 20)] ) point1 = self.point1.get_value() point2 = self.point2.get_value() self.test_contraint() self.assertEqual(point1.X, 0) self.assertEqual(point1.Y, 0) self.assertEqual(point2.X, 20) self.assertEqual(point2.Y, 20)
from __future__ import division import sys sys.path.append("../src") sys.path.append("../tests") from skypyblue.core import ConstraintSystem from skypyblue.models import Constraint, Strength, Variable, Method cs = ConstraintSystem() v1 = Variable("v1", 16, cs) v2 = Variable("v2", 16, cs) v3 = Variable("v3", 16, cs) v4 = Variable("v4", 10, cs) v4.stay() # v1 + v2 = v3, v1 - v2 = v4 m12_34 = Method([v1, v2], [v3, v4], lambda v1, v2: (v1 + v2 , v1 - v2)) m34_12 = Method([v3, v4], [v1, v2], lambda v3, v4: ((v3 + v4) / 2 , (v3 - v4) / 2)) cn1 = Constraint( lambda v1, v2, v3, v4: v1 + v2 == v3 and v1 - v2 == v4, Strength.STRONG, [v1, v2, v3, v4], [m12_34, m34_12])
def setUp(self): self.variable = Variable("variable", 10, Mock()) self.variable.system.remove_constraint = Mock() self.variable.system._add_stay_constraint = Mock()
def setUp(self): self.cs = ConstraintSystem() self.v1 = Variable("v1", 15, self.cs) self.c1 = Constraint(lambda x: True, Strength.REQUIRED, [self.v1], [])
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())
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())
class MidpointTest(unittest.TestCase): __name__ = "MidpointTest" def setUp(self): self.constraint_system = ConstraintSystem() self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system) self.point2 = Variable("Point 2", Point(10, 30), self.constraint_system) self.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system) mMp = Method([self.point1, self.point2], [self.midpoint], lambda p1, p2: Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2)) mP1 = Method([self.midpoint, self.point2], [self.point1], lambda mp, p2: Point((2 * mp.X - p2.X), (2 * mp.Y - p2.Y))) mP2 = Method([self.midpoint, self.point1], [self.point2], lambda mp, p1: Point((2 * mp.X - p1.X), (2 * mp.Y - p1.Y))) constraint = Constraint( lambda point1, point2, midpoint: midpoint.is_midpoint( point1, point2), Strength.STRONG, [self.point1, self.point2, self.midpoint], [mMp, mP1, mP2]) self.constraint_system.add_constraint(constraint) def test_contraint(self): midpoint = self.midpoint.get_value() point1 = self.point1.get_value() point2 = self.point2.get_value() self.assertTrue(midpoint.is_midpoint(point1, point2)) def test_change_point1(self): self.point1.set_value(Point(0, 0)) point1 = self.point1.get_value() self.test_contraint() self.assertEqual(point1.X, 0) self.assertEqual(point1.Y, 0) def test_change_midpoint(self): self.midpoint.set_value(Point(0, 0)) midpoint = self.midpoint.get_value() self.test_contraint() self.assertEqual(midpoint.X, 0) self.assertEqual(midpoint.Y, 0) def test_change_several_points(self): self.midpoint.set_value(Point(0, 0)) self.point1.set_value(Point(10, 10)) self.point2.set_value(Point(50, 50)) point2 = self.point2.get_value() self.test_contraint() self.assertEqual(point2.X, 50) self.assertEqual(point2.Y, 50) def test_change_two_points_at_the_same_time(self): self.constraint_system.change_variable_values( [self.point1, self.point2], [Point(0, 0), Point(20, 20)]) point1 = self.point1.get_value() point2 = self.point2.get_value() self.test_contraint() self.assertEqual(point1.X, 0) self.assertEqual(point1.Y, 0) self.assertEqual(point2.X, 20) self.assertEqual(point2.Y, 20)
from __future__ import division import sys sys.path.append("../src") sys.path.append("../tests") from skypyblue.core import ConstraintSystem from skypyblue.models import Constraint, Strength, Variable, Method cs = ConstraintSystem() v1 = Variable("v1", 16, cs) v2 = Variable("v2", 16, cs) v3 = Variable("v3", 16, cs) v4 = Variable("v4", 10, cs) v4.stay() # v1 + v2 = v3, v1 - v2 = v4 m12_34 = Method([v1, v2], [v3, v4], lambda v1, v2: (v1 + v2, v1 - v2)) m34_12 = Method([v3, v4], [v1, v2], lambda v3, v4: ((v3 + v4) / 2, (v3 - v4) / 2)) cn1 = Constraint(lambda v1, v2, v3, v4: v1 + v2 == v3 and v1 - v2 == v4, Strength.STRONG, [v1, v2, v3, v4], [m12_34, m34_12]) cs.add_constraint(cn1) def print_values():
def test_pplan_add_for_variable(self): v = Variable("v", 1, self.constraint_system) self.assertEqual( v.add_to_pplan([], self.constraint_system.marker.new_mark()), [], "plain variable return pplan as it was")
def setUp(self): self.variable = Variable("variable", 10, Mock()) self.variable.system.remove_constraint = Mock() self.variable.system.add_stay_constraint = Mock()
class ExtendedMidpointTest(unittest.TestCase): __name__ = "ExtendedMidpointTest" def setUp(self): self.constraint_system = ConstraintSystem() self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system) self.point2 = Variable("Point 2", Point(10, 30), self.constraint_system) self.point3 = Variable("Point 3", Point(50, 20), self.constraint_system) self.point4 = Variable("Point 4", Point(100, 30), self.constraint_system) self.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system) mpmp3p4 = Method([self.midpoint, self.point3, self.point4], [self.point1, self.point2], lambda pm, p3, p4: ( Point( pm.X - (p4.X - p3.X), pm.Y - (p4.Y - p3.Y) ), Point( pm.X + (p4.X - p3.X), pm.Y + (p4.Y - p3.Y) ) ) ) mp1pmp3 = Method([self.point1, self.midpoint, self.point3], [self.point2, self.point4], lambda p1, pm, p3: ( Point( 2 * pm.X - p1.X, 2 * pm.Y - p1.Y ), Point( p3.X + (pm.X - p1.X), p3.Y + (pm.Y - p1.Y) ) ) ) mpmp2p4 = Method([self.midpoint, self.point2, self.point4], [self.point1, self.point3], lambda pm, p2, p4: ( Point( 2 * pm.X - p2.X, 2 * pm.Y - p2.Y ), Point( p4.X + (pm.X - p2.X), p4.Y + (pm.Y - p2.Y) ) ) ) constraint = Constraint( lambda p1, p2, p3, p4, pm: pm.is_midpoint(p1, p2) and p1.distance(pm) == p3.distance(p4), Strength.STRONG, [self.point1, self.point2, self.point3, self.point4, self.midpoint], [mpmp3p4, mp1pmp3, mpmp2p4]) self.constraint_system.add_constraint(constraint) self.point3.stay() self.point4.stay() def test_contraint(self): pm = self.midpoint.get_value() p1 = self.point1.get_value() p2 = self.point2.get_value() p3 = self.point3.get_value() p4 = self.point4.get_value() self.assertTrue(pm.is_midpoint(p1,p2)) self.assertTrue(p1.distance(pm) == p3.distance(p4)) def test_change_point1(self): pm_old = self.midpoint.get_value() self.point1.set_value(Point(0, 0)) self.test_contraint() self.assertTrue(self.point1.get_value().equals(Point(0,0))) self.assertTrue(self.midpoint.get_value().equals(pm_old)) def test_change_midpoint(self): self.midpoint.set_value(Point(0, 0)) midpoint = self.midpoint.get_value() self.test_contraint() self.assertTrue(self.midpoint.get_value(), Point(0,0)) def test_change_point3(self): pm_old = self.midpoint.get_value() self.point3.set_value(Point(0, 0)) self.test_contraint() self.assertTrue(self.point3.get_value().equals(Point(0,0))) self.assertTrue(self.midpoint.get_value().equals(pm_old))