package tester; import edu.neu.ccs.satsolver.*; import edu.neu.ccs.demeter.dj.*; import java.util.Set; import java.util.HashSet; public class TestVisitor extends Visitor { public double DELTA = 0.00005d; public ClassGraph m_cg = new ClassGraph("tester", true, false); public String m_testname; public HashSet m_added_pairs; public HashSet m_subtracted_pairs; public TestPolynomial m_poly_before; public double m_bias_expected; public TestPolynomial m_poly_expected; public boolean withinDelta(double a, double b) { boolean t = a < (b + DELTA) && a > (b - DELTA); if (!t) { System.out.println("a: " + a + " b: " + b + " not equal"); } return t; } public void check(OutputI output) { System.out.println("\ntest: " + m_testname); double result = output.getMaxBias(); if (withinDelta(result, m_bias_expected)) { System.out.println("PASS: " + result + " = " + m_bias_expected); } else { System.out.println("FAIL: " + result + " != " + m_bias_expected); } PolynomialI p = output.getPolynomial(); if (withinDelta(p.getCoefficient(3), m_poly_expected.getCoefficient(3)) && withinDelta(p.getCoefficient(2), m_poly_expected.getCoefficient(2)) && withinDelta(p.getCoefficient(1), m_poly_expected.getCoefficient(1)) && withinDelta(p.getCoefficient(0), m_poly_expected.getCoefficient(0))) { System.out.println("PASS: " + p.toString() + " = " + m_poly_expected.toString()); } else { System.out.println("FAIL: " + p.toString() + " != " + m_poly_expected.toString()); } } public void before(ComputeBiasTest host) { m_testname = host.testname.get_s().toString(); m_added_pairs = new HashSet(); Result result = (Result) m_cg.fetch(host, "from tester.ComputeBiasTest " + "through -> *,result,* " + "to tester.Result"); m_bias_expected = result.v.doubleValue(); double x[] = { result.poly.x3.doubleValue(), result.poly.x2.doubleValue(), result.poly.x1.doubleValue(), result.poly.x0.doubleValue() }; m_poly_expected = new TestPolynomial(x); } public void before(Pair host) { TestPair tp = new TestPair(host.r.intValue(), host.frac.doubleValue()); m_added_pairs.add(tp); } public void after(ComputeBiasTest host) { TestInputInitial input = new TestInputInitial(m_added_pairs); OutputI output = SATSolverUtil.calculateBias(input); check(output); } public void before(UpdateBiasTest host) { m_testname = host.testname.get_s().toString(); m_added_pairs = new HashSet(); m_subtracted_pairs = new HashSet(); // grab the expected results Result result = (Result) m_cg.fetch(host, "from tester.UpdateBiasTest " + "through -> *,result,* " + "to tester.Result"); m_bias_expected = result.v.doubleValue(); double x[] = { result.poly.x3.doubleValue(), result.poly.x2.doubleValue(), result.poly.x1.doubleValue(), result.poly.x0.doubleValue() }; m_poly_expected = new TestPolynomial(x); // grab the polynomial before Polynomial p = (Polynomial) m_cg.fetch(host, "from tester.UpdateBiasTest " + "through -> *,beforepoly,* " + "to tester.Polynomial"); double x2[] = { p.x3.doubleValue(), p.x2.doubleValue(), p.x1.doubleValue(), p.x0.doubleValue() }; m_poly_before = new TestPolynomial(x2); } public void before(OPair host) { TestPair tp = new TestPair(host.get_r().intValue(), host.get_frac().doubleValue()); if (host.get_op() instanceof Added) { m_added_pairs.add(tp); } else { m_subtracted_pairs.add(tp); } } public void after(UpdateBiasTest host) { InputUpdateI update = new TestInputUpdate(m_poly_before, m_added_pairs, m_subtracted_pairs); OutputI output = SATSolverUtil.updateBias(update); check(output); } }