use of kodkod.engine.Evaluator in project org.alloytools.alloy by AlloyTools.
the class IntTest method testCompOp.
private final void testCompOp(IntCompOperator op, IntExpression ei, IntExpression ej, int i, int j, boolean result) {
final Formula e = ei.compare(op, ej);
final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(result ? e : e.not());
final Solution s = solve(f);
if (overflows(ei, i, 0) || overflows(ej, j, 0)) {
assertNull(f.toString(), s.instance());
} else {
assertNotNull(f.toString(), s.instance());
final Evaluator eval = new Evaluator(s.instance(), solver.options());
assertFalse(result ^ eval.evaluate(e));
}
}
use of kodkod.engine.Evaluator in project org.alloytools.alloy by AlloyTools.
the class IntTest method testBinOp.
private final void testBinOp(IntOperator op, IntExpression ei, IntExpression ej, int i, int j, int result, int realResult, int mask) {
final IntExpression e = ei.compose(op, ej);
final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(e.eq(constant(result)));
final Solution s = solve(f);
Instance inst = s.instance();
if (overflows(op, ei, ej, i, j, realResult)) {
assertNull(f.toString(), inst);
} else {
assertNotNull(f.toString(), inst);
final Evaluator eval = new Evaluator(inst, solver.options());
assertEquals(f.toString(), result & mask, eval.evaluate(e) & mask);
}
}
use of kodkod.engine.Evaluator in project org.alloytools.alloy by AlloyTools.
the class IntTest method testBitsetCast.
public void testBitsetCast() {
final int width = 4, msb = width - 1;
solver.options().setBitwidth(width);
final List<Integer> atoms = new ArrayList<Integer>(width);
for (int i = 0; i < msb; i++) {
atoms.add(Integer.valueOf(1 << i));
}
atoms.add(Integer.valueOf(-1 << msb));
final Bounds b = new Bounds(new Universe(atoms));
final TupleFactory f = b.universe().factory();
for (Integer i : atoms) {
b.boundExactly(i, f.setOf(i));
}
b.bound(r1, f.allOf(1));
for (int i = -1 << msb, max = 1 << msb; i < max; i++) {
Formula test = r1.sum().toBitset().eq(IntConstant.constant(i).toBitset());
Solution sol = solver.solve(test, b);
Instance inst = sol.instance();
assertNotNull(inst);
Evaluator eval = new Evaluator(inst, solver.options());
assertEquals(i, eval.evaluate(r1.sum()));
}
}
use of kodkod.engine.Evaluator in project org.alloytools.alloy by AlloyTools.
the class IntTest method testUnOp.
private final void testUnOp(IntOperator op, IntExpression ei, int i, int result, int mask) {
final IntExpression e = ei.apply(op);
final Formula f = ei.eq(constant(i)).and(e.eq(constant(result)));
final Solution s = solve(f);
if (overflows(ei, i, result)) {
assertNull(f.toString(), s.instance());
} else {
assertNotNull(f.toString(), s.instance());
final Evaluator eval = new Evaluator(s.instance(), solver.options());
assertEquals(result & mask, eval.evaluate(e) & mask);
}
}
use of kodkod.engine.Evaluator in project org.alloytools.alloy by AlloyTools.
the class EvaluatorTest method testEvalTransitiveClosure.
public final void testEvalTransitiveClosure() {
// // ^(Hilary->Jocelyn) = Hilary->Jocelyn
// assertEquals(eval(hilary.product(jocelyn).closure()),
// eval(hilary.product(jocelyn)));
// // ^spouse = spouse + spouse.spouse
// assertEquals(eval(spouse.closure()),
// eval(spouse.union(spouse.join(spouse))));
// // Hilary.^shaken = univ - (univ - Person.shaken)
// assertEquals(eval(hilary.join(shaken.closure())),
// eval(univ.difference(univ.difference(person.join(shaken)))));
// try {
// eval(person.closure());
// fail("Expected IllegalArgumentException");
// } catch (IllegalArgumentException iae) {}
// ^r = value(^r)
final Relation r = Relation.binary("r");
final Universe u = evaluator.instance().universe();
final TupleFactory f = u.factory();
final Instance instance = new Instance(u);
// value(r) = u[0]->u[1] + u[1]->u[2] + u[2]->u[3] + u[3]->u[4]
TupleSet s = f.noneOf(r.arity());
for (int i = 0; i < 4; i++) s.add(f.tuple(u.atom(i), u.atom(i + 1)));
instance.add(r, s);
// value(^r) = value(r) + u[0]->u[2] + u[0]->u[3] + u[0]->u[4] +
// u[1]->u[3] u[1]->u[4] + u[2]->u[4]
Set<Tuple> result = new HashSet<Tuple>();
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 5; j++) {
result.add(f.tuple(u.atom(i), u.atom(j)));
}
}
assertEquals((new Evaluator(instance)).evaluate(r.closure()), result);
// value(*r) = value(^r) + iden
for (int i = 0; i < 10; i++) {
result.add(f.tuple(u.atom(i), u.atom(i)));
}
assertEquals((new Evaluator(instance)).evaluate(r.reflexiveClosure()), result);
}
Aggregations