Port remaining tests from Cassowary project

This commit is contained in:
Alex Birkett 2016-01-30 12:48:21 +01:00
parent 6fd9d8f275
commit 8679ae410a
1 changed files with 78 additions and 63 deletions

View File

@ -28,11 +28,8 @@ public class Tests {
Variable x = new Variable("x"); Variable x = new Variable("x");
Variable y = new Variable("y"); Variable y = new Variable("y");
// x = 20
solver.addConstraint(Symbolics.equals(x, 20)); solver.addConstraint(Symbolics.equals(x, 20));
// x + 2 == y + 10
//
solver.addConstraint(Symbolics.equals(Symbolics.add(x, 2), Symbolics.add(y, 10))); solver.addConstraint(Symbolics.equals(Symbolics.add(x, 2), Symbolics.add(y, 10)));
solver.updateVariables(); solver.updateVariables();
@ -75,105 +72,122 @@ public class Tests {
} }
} }
@Test
/*@Test public void addDelete1() throws DuplicateConstraintException, UnsatisfiableConstraintException, UnknownConstraintException {
public void addDelete1() {
Variable x = new Variable("x"); Variable x = new Variable("x");
Solver solver = new Solver(); Solver solver = new Solver();
solver.addConstraint(new Constraint(x, Constraint.Operator.EQ, 100, Strength.WEAK)); solver.addConstraint(Symbolics.lessThanOrEqualTo(x, 100).setStrength(Strength.WEAK));
Constraint c10 = new Constraint(x, Constraint.Operator.LEQ, 10.0); solver.updateVariables();
Constraint c20 = new Constraint(x, Constraint.Operator.LEQ, 20.0); assertEquals(100, x.getValue(), EPSILON);
Constraint c10 = Symbolics.lessThanOrEqualTo(x, 10.0);
Constraint c20 = Symbolics.lessThanOrEqualTo(x, 20.0);
solver.addConstraint(c10); solver.addConstraint(c10);
solver.addConstraint(c20); solver.addConstraint(c20);
assertEquals(10, x.value(), EPSILON); solver.updateVariables();
assertEquals(10, x.getValue(), EPSILON);
solver.removeConstraint(c10); solver.removeConstraint(c10);
assertEquals(20, x.value(), EPSILON);
solver.updateVariables();
assertEquals(20, x.getValue(), EPSILON);
solver.removeConstraint(c20); solver.removeConstraint(c20);
assertEquals(100, x.value(), EPSILON); solver.updateVariables();
Constraint c10again = new Constraint(x, Constraint.Operator.LEQ, 10.0); assertEquals(100, x.getValue(), EPSILON);
Constraint c10again = Symbolics.lessThanOrEqualTo(x, 10.0);
solver.addConstraint(c10);
solver.addConstraint(c10again); solver.addConstraint(c10again);
solver.addConstraint(c10);
solver.updateVariables();
assertEquals(10, x.value(), EPSILON); assertEquals(10, x.getValue(), EPSILON);
solver.removeConstraint(c10); solver.removeConstraint(c10);
assertEquals(10, x.value(), EPSILON); solver.updateVariables();
assertEquals(10, x.getValue(), EPSILON);
solver.removeConstraint(c10again); solver.removeConstraint(c10again);
assertEquals(100, x.value(), EPSILON); solver.updateVariables();
assertEquals(100, x.getValue(), EPSILON);
} }
@Test @Test
public void addDelete2() { public void addDelete2() throws DuplicateConstraintException, UnsatisfiableConstraintException, UnknownConstraintException {
Variable x = new Variable("x"); Variable x = new Variable("x");
Variable y = new Variable("y"); Variable y = new Variable("y");
Solver solver = new Solver(); Solver solver = new Solver();
solver.addConstraint(new Constraint(x, Constraint.Operator.EQ, 100.0, Strength.WEAK)); solver.addConstraint(Symbolics.equals(x, 100).setStrength(Strength.WEAK));
solver.addConstraint(new Constraint(y, Constraint.Operator.EQ, 120.0, Strength.STRONG)); solver.addConstraint(Symbolics.equals(y, 120).setStrength(Strength.STRONG));
Constraint c10 = Symbolics.lessThanOrEqualTo(x, 10.0);
Constraint c10 = new Constraint(x, Constraint.Operator.LEQ, 10.0); Constraint c20 = Symbolics.lessThanOrEqualTo(x, 20.0);
Constraint c20 = new Constraint(x, Constraint.Operator.LEQ, 20.0);
solver.addConstraint(c10); solver.addConstraint(c10);
solver.addConstraint(c20); solver.addConstraint(c20);
solver.updateVariables();
assertEquals(10, x.value(), EPSILON); assertEquals(10, x.getValue(), EPSILON);
assertEquals(120, y.value(), EPSILON); assertEquals(120, y.getValue(), EPSILON);
solver.removeConstraint(c10); solver.removeConstraint(c10);
assertEquals(20, x.value(), EPSILON); solver.updateVariables();
assertEquals(120, y.value(), EPSILON);
Constraint cxy = new Constraint(x.times(2.0), Constraint.Operator.EQ, y); assertEquals(20, x.getValue(), EPSILON);
assertEquals(120, y.getValue(), EPSILON);
Constraint cxy = Symbolics.equals(Symbolics.multiply(x, 2.0), y);
solver.addConstraint(cxy); solver.addConstraint(cxy);
assertEquals(20, x.value(), EPSILON); solver.updateVariables();
assertEquals(40, y.value(), EPSILON);
assertEquals(20, x.getValue(), EPSILON);
assertEquals(40, y.getValue(), EPSILON);
solver.removeConstraint(c20); solver.removeConstraint(c20);
assertEquals(60, x.value(), EPSILON); solver.updateVariables();
assertEquals(120, y.value(), EPSILON);
assertEquals(60, x.getValue(), EPSILON);
assertEquals(120, y.getValue(), EPSILON);
solver.removeConstraint(cxy); solver.removeConstraint(cxy);
assertEquals(100, x.value(), EPSILON); solver.updateVariables();
assertEquals(120, y.value(), EPSILON);
assertEquals(100, x.getValue(), EPSILON);
assertEquals(120, y.getValue(), EPSILON);
} }
@Test(expected = UnsatisfiableConstraintException.class)
public void inconsistent1() throws InternalError, DuplicateConstraintException, UnsatisfiableConstraintException {
@Test(expected = RequiredFailure.class)
public void inconsistent1() throws InternalError {
Variable x = new Variable("x"); Variable x = new Variable("x");
Solver solver = new Solver(); Solver solver = new Solver();
solver.addConstraint(new Constraint(x, Constraint.Operator.EQ, 10.0)); solver.addConstraint(Symbolics.equals(x, 10.0));
solver.addConstraint(new Constraint(x, Constraint.Operator.EQ, 5.0)); solver.addConstraint(Symbolics.equals(x, 5.0));
solver.updateVariables();
} }
@Test(expected = UnsatisfiableConstraintException.class)
@Test(expected = RequiredFailure.class) public void inconsistent2() throws DuplicateConstraintException, UnsatisfiableConstraintException {
public void inconsistent2() {
Variable x = new Variable("x"); Variable x = new Variable("x");
Solver solver = new Solver(); Solver solver = new Solver();
solver.addConstraint(new Constraint(x, Constraint.Operator.GEQ, 10.0)); solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, 10.0));
solver.addConstraint(new Constraint(x, Constraint.Operator.LEQ, 5.0)); solver.addConstraint(Symbolics.lessThanOrEqualTo(x, 5.0));
solver.updateVariables();
} }
@Test(expected = UnsatisfiableConstraintException.class)
public void inconsistent3() throws DuplicateConstraintException, UnsatisfiableConstraintException {
@Test(expected = RequiredFailure.class)
public void inconsistent3() {
Variable w = new Variable("w"); Variable w = new Variable("w");
Variable x = new Variable("x"); Variable x = new Variable("x");
@ -181,12 +195,13 @@ public class Tests {
Variable z = new Variable("z"); Variable z = new Variable("z");
Solver solver = new Solver(); Solver solver = new Solver();
solver.addConstraint(new Constraint(w, Constraint.Operator.GEQ, 10.0)); solver.addConstraint(Symbolics.greaterThanOrEqualTo(w, 10.0));
solver.addConstraint(new Constraint(x, Constraint.Operator.GEQ, w)); solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, w));
solver.addConstraint(new Constraint(y, Constraint.Operator.GEQ, x)); solver.addConstraint(Symbolics.greaterThanOrEqualTo(y, x));
solver.addConstraint(new Constraint(z, Constraint.Operator.GEQ, y)); solver.addConstraint(Symbolics.greaterThanOrEqualTo(z, y));
solver.addConstraint(new Constraint(z, Constraint.Operator.GEQ, 8.0)); solver.addConstraint(Symbolics.greaterThanOrEqualTo(z, 8.0));
solver.addConstraint(new Constraint(z, Constraint.Operator.LEQ, 4.0)); solver.addConstraint(Symbolics.lessThanOrEqualTo(z, 4.0));
}*/ solver.updateVariables();
}
} }