package org.clafer.ir.analysis;

import org.clafer.ir.IrIntVar;
import org.clafer.ir.IrModule;
import org.clafer.ir.Irs;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
 *
 * @author jimmy
 */
public class LinearEquationOptimizerTest {

    @Test
    public void testNotOptimizable() {
        IrModule module = new IrModule();
        IrIntVar i = Irs.boundInt("i", -3, 4);
        IrIntVar j = Irs.boundInt("j", -3, 4);
        module.addConstraint(Irs.lessThan(i, Irs.add(j, 1)));

        module = LinearEquationOptimizer.optimize(module);

        assertEquals(1, module.getConstraints().size());
    }

    @Test
    public void testEqualitySubsumesInequality() {
        IrModule module = new IrModule();
        IrIntVar i = Irs.boundInt("i", -3, 4);
        IrIntVar j = Irs.boundInt("j", -3, 4);
        module.addConstraint(Irs.equal(i, Irs.add(j, 1)));
        module.addConstraint(Irs.lessThanEqual(i, Irs.add(j, 2)));

        module = LinearEquationOptimizer.optimize(module);

        assertEquals(1, module.getConstraints().size());
    }
}

@@ -26,16 +26,50 @@
 lysis;%0A%0A
+import org.clafer.domain.Domains;%0A
 import o
@@ -91,16 +91,16 @@
 IntVar;%0A
-
 import o
@@ -193,16 +193,59 @@
 Equals;%0A
+import static org.junit.Assert.assertTrue;%0A
 import o
@@ -1080,32 +1080,32 @@
 imize(module);%0A%0A
-
         assertEq
@@ -1150,11 +1150,764 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testCombineInequalties() %7B%0A        IrModule module = new IrModule();%0A        IrIntVar i0 = Irs.boundInt(%22i0%22, 0, 4);%0A        IrIntVar i1 = Irs.boundInt(%22i1%22, 0, 4);%0A        IrIntVar i2 = Irs.boundInt(%22i2%22, 0, 4);%0A        IrIntVar i3 = Irs.boundInt(%22i3%22, 0, 4);%0A        module.addConstraint(Irs.greaterThan(Irs.add(i0, i1, i2, i3), 0));%0A        module.addConstraint(Irs.greaterThanEqual(i0, i1));%0A        module.addConstraint(Irs.greaterThanEqual(i1, i2));%0A        module.addConstraint(Irs.greaterThanEqual(i2, i3));%0A%0A        module = LinearEquationOptimizer.optimize(module);%0A%0A        assertTrue(module.getConstraints().contains(%0A                Irs.lessThanEqual(Irs.One, Irs.mul(4, i0, Domains.boundDomain(0, 16)))));%0A    %7D
 %0A%7D%0A

Test linear optimizer optimizes sum on sorted operands.