From 8679ae410a941d9edbb908ca43261f9650e3bd03 Mon Sep 17 00:00:00 2001 From: Alex Birkett Date: Sat, 30 Jan 2016 12:48:21 +0100 Subject: [PATCH] Port remaining tests from Cassowary project --- src/test/java/no/birkett/kiwi/Tests.java | 141 +++++++++++++---------- 1 file changed, 78 insertions(+), 63 deletions(-) diff --git a/src/test/java/no/birkett/kiwi/Tests.java b/src/test/java/no/birkett/kiwi/Tests.java index 9d88f11..1173ca1 100644 --- a/src/test/java/no/birkett/kiwi/Tests.java +++ b/src/test/java/no/birkett/kiwi/Tests.java @@ -15,34 +15,31 @@ public class Tests { Variable x = new Variable("x"); - solver.addConstraint(Symbolics.equals(Symbolics.add(x,2), 20)); + solver.addConstraint(Symbolics.equals(Symbolics.add(x, 2), 20)); solver.updateVariables(); assertEquals(x.getValue(), 18, EPSILON); } - + @Test public void simple0() throws UnsatisfiableConstraintException, DuplicateConstraintException { Solver solver = new Solver(); Variable x = new Variable("x"); Variable y = new Variable("y"); - // 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(); - + System.out.println("x " + x.getValue() + " y " + y.getValue()); assertEquals(y.getValue(), 12, EPSILON); assertEquals(x.getValue(), 20, EPSILON); } - + @Test public void simple1() throws DuplicateConstraintException, UnsatisfiableConstraintException { Variable x = new Variable("x"); @@ -65,7 +62,7 @@ public class Tests { solver.addConstraint(Symbolics.equals(y, 10.0).setStrength(Strength.WEAK)); solver.updateVariables(); - + if (Math.abs(x.getValue() - 10.0) < EPSILON) { assertEquals(10, x.getValue(), EPSILON); assertEquals(13, y.getValue(), EPSILON); @@ -75,105 +72,122 @@ public class Tests { } } - - /*@Test - public void addDelete1() { + @Test + public void addDelete1() throws DuplicateConstraintException, UnsatisfiableConstraintException, UnknownConstraintException { Variable x = new Variable("x"); 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); - Constraint c20 = new Constraint(x, Constraint.Operator.LEQ, 20.0); + solver.updateVariables(); + assertEquals(100, x.getValue(), EPSILON); + + Constraint c10 = Symbolics.lessThanOrEqualTo(x, 10.0); + Constraint c20 = Symbolics.lessThanOrEqualTo(x, 20.0); solver.addConstraint(c10); solver.addConstraint(c20); - assertEquals(10, x.value(), EPSILON); + solver.updateVariables(); + + assertEquals(10, x.getValue(), EPSILON); solver.removeConstraint(c10); - assertEquals(20, x.value(), EPSILON); + + solver.updateVariables(); + + assertEquals(20, x.getValue(), EPSILON); 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(c10); + solver.updateVariables(); - assertEquals(10, x.value(), EPSILON); + assertEquals(10, x.getValue(), EPSILON); solver.removeConstraint(c10); - assertEquals(10, x.value(), EPSILON); + solver.updateVariables(); + assertEquals(10, x.getValue(), EPSILON); solver.removeConstraint(c10again); - assertEquals(100, x.value(), EPSILON); + solver.updateVariables(); + assertEquals(100, x.getValue(), EPSILON); } - @Test - public void addDelete2() { + public void addDelete2() throws DuplicateConstraintException, UnsatisfiableConstraintException, UnknownConstraintException { Variable x = new Variable("x"); Variable y = new Variable("y"); Solver solver = new Solver(); - solver.addConstraint(new Constraint(x, Constraint.Operator.EQ, 100.0, Strength.WEAK)); - solver.addConstraint(new Constraint(y, Constraint.Operator.EQ, 120.0, Strength.STRONG)); + solver.addConstraint(Symbolics.equals(x, 100).setStrength(Strength.WEAK)); + solver.addConstraint(Symbolics.equals(y, 120).setStrength(Strength.STRONG)); - - Constraint c10 = new Constraint(x, Constraint.Operator.LEQ, 10.0); - Constraint c20 = new Constraint(x, Constraint.Operator.LEQ, 20.0); + Constraint c10 = Symbolics.lessThanOrEqualTo(x, 10.0); + Constraint c20 = Symbolics.lessThanOrEqualTo(x, 20.0); solver.addConstraint(c10); solver.addConstraint(c20); + solver.updateVariables(); - assertEquals(10, x.value(), EPSILON); - assertEquals(120, y.value(), EPSILON); + assertEquals(10, x.getValue(), EPSILON); + assertEquals(120, y.getValue(), EPSILON); solver.removeConstraint(c10); - assertEquals(20, x.value(), EPSILON); - assertEquals(120, y.value(), EPSILON); + solver.updateVariables(); - 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); - assertEquals(20, x.value(), EPSILON); - assertEquals(40, y.value(), EPSILON); + solver.updateVariables(); + + assertEquals(20, x.getValue(), EPSILON); + assertEquals(40, y.getValue(), EPSILON); solver.removeConstraint(c20); - assertEquals(60, x.value(), EPSILON); - assertEquals(120, y.value(), EPSILON); + solver.updateVariables(); + + assertEquals(60, x.getValue(), EPSILON); + assertEquals(120, y.getValue(), EPSILON); solver.removeConstraint(cxy); - assertEquals(100, x.value(), EPSILON); - assertEquals(120, y.value(), EPSILON); + solver.updateVariables(); + + assertEquals(100, x.getValue(), EPSILON); + assertEquals(120, y.getValue(), EPSILON); } - - - @Test(expected = RequiredFailure.class) - public void inconsistent1() throws InternalError { + @Test(expected = UnsatisfiableConstraintException.class) + public void inconsistent1() throws InternalError, DuplicateConstraintException, UnsatisfiableConstraintException { Variable x = new Variable("x"); Solver solver = new Solver(); - solver.addConstraint(new Constraint(x, Constraint.Operator.EQ, 10.0)); - solver.addConstraint(new Constraint(x, Constraint.Operator.EQ, 5.0)); + solver.addConstraint(Symbolics.equals(x, 10.0)); + solver.addConstraint(Symbolics.equals(x, 5.0)); + + solver.updateVariables(); } - - @Test(expected = RequiredFailure.class) - public void inconsistent2() { + @Test(expected = UnsatisfiableConstraintException.class) + public void inconsistent2() throws DuplicateConstraintException, UnsatisfiableConstraintException { Variable x = new Variable("x"); Solver solver = new Solver(); - solver.addConstraint(new Constraint(x, Constraint.Operator.GEQ, 10.0)); - solver.addConstraint(new Constraint(x, Constraint.Operator.LEQ, 5.0)); + solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, 10.0)); + solver.addConstraint(Symbolics.lessThanOrEqualTo(x, 5.0)); + solver.updateVariables(); } - - - @Test(expected = RequiredFailure.class) - public void inconsistent3() { + @Test(expected = UnsatisfiableConstraintException.class) + public void inconsistent3() throws DuplicateConstraintException, UnsatisfiableConstraintException { Variable w = new Variable("w"); Variable x = new Variable("x"); @@ -181,12 +195,13 @@ public class Tests { Variable z = new Variable("z"); Solver solver = new Solver(); - solver.addConstraint(new Constraint(w, Constraint.Operator.GEQ, 10.0)); - solver.addConstraint(new Constraint(x, Constraint.Operator.GEQ, w)); - solver.addConstraint(new Constraint(y, Constraint.Operator.GEQ, x)); - solver.addConstraint(new Constraint(z, Constraint.Operator.GEQ, y)); - solver.addConstraint(new Constraint(z, Constraint.Operator.GEQ, 8.0)); - solver.addConstraint(new Constraint(z, Constraint.Operator.LEQ, 4.0)); - }*/ + solver.addConstraint(Symbolics.greaterThanOrEqualTo(w, 10.0)); + solver.addConstraint(Symbolics.greaterThanOrEqualTo(x, w)); + solver.addConstraint(Symbolics.greaterThanOrEqualTo(y, x)); + solver.addConstraint(Symbolics.greaterThanOrEqualTo(z, y)); + solver.addConstraint(Symbolics.greaterThanOrEqualTo(z, 8.0)); + solver.addConstraint(Symbolics.lessThanOrEqualTo(z, 4.0)); + solver.updateVariables(); + } }