001package daikon.test.diff;
002
003import static java.util.logging.Level.INFO;
004import static org.junit.Assert.assertEquals;
005
006import daikon.*;
007import daikon.diff.*;
008import daikon.inv.*;
009import daikon.test.*;
010import java.util.Arrays;
011import junit.framework.*;
012import org.junit.BeforeClass;
013import org.junit.Test;
014
015public class XorVisitorTester {
016
017  private Diff diff = new Diff(true, new Invariant.ClassVarnameFormulaComparator());
018
019  /** prepare for tests */
020  @BeforeClass
021  public static void setUpClass() {
022    daikon.LogHelper.setupLogs(INFO);
023    FileIO.new_decl_format = true;
024  }
025
026  // X1 and X2 have the same class and vars, but different formula
027  // map1: A->{W, X1, Y}, B->{Y}, D->{M, N_unjusitifed, O_unjustified}
028  // map2: A->{W, X2, Z}, C->{Z}, D->{M_unjustified, N}
029  // map1 xor map2: A->{X1, X2, Y, Z}, B->{Y}, C->{Z}, D->{M, N}
030  @Test
031  public void testXor() {
032    VarInfo[] vars = {
033      DiffTester.newIntVarInfo("w"),
034      DiffTester.newIntVarInfo("x"),
035      DiffTester.newIntVarInfo("y"),
036      DiffTester.newIntVarInfo("z"),
037      DiffTester.newIntVarInfo("m"),
038      DiffTester.newIntVarInfo("n"),
039      DiffTester.newIntVarInfo("o"),
040    };
041    PptTopLevel A = Common.makePptTopLevel("A:::OBJECT", vars);
042    PptTopLevel B = Common.makePptTopLevel("B:::OBJECT", vars);
043    PptTopLevel C = Common.makePptTopLevel("C:::OBJECT", vars);
044    PptTopLevel D = Common.makePptTopLevel("D:::OBJECT", vars);
045    PptSlice slicew = new PptSlice1(A, new VarInfo[] {vars[0]});
046    PptSlice slicex = new PptSlice1(A, new VarInfo[] {vars[1]});
047    PptSlice slicey = new PptSlice1(A, new VarInfo[] {vars[2]});
048    PptSlice slicez = new PptSlice1(A, new VarInfo[] {vars[3]});
049    PptSlice slicem = new PptSlice1(A, new VarInfo[] {vars[4]});
050    PptSlice slicen = new PptSlice1(A, new VarInfo[] {vars[5]});
051    PptSlice sliceo = new PptSlice1(A, new VarInfo[] {vars[6]});
052    Invariant W = new DiffDummyInvariant(slicew, "W", true);
053    Invariant X1 = new DiffDummyInvariant(slicex, "X1", true);
054    Invariant X2 = new DiffDummyInvariant(slicex, "X2", true);
055    Invariant Y = new DiffDummyInvariant(slicey, "Y", true);
056    Invariant Z = new DiffDummyInvariant(slicez, "Z", true);
057    Invariant M = new DiffDummyInvariant(slicem, "M", true);
058    Invariant unjM = new DiffDummyInvariant(slicem, "M", false);
059    Invariant N = new DiffDummyInvariant(slicen, "N", true);
060    Invariant unjN = new DiffDummyInvariant(slicen, "N", false);
061    Invariant unjO = new DiffDummyInvariant(sliceo, "O", false);
062
063    InvMap map1 = new InvMap();
064    map1.put(A, Arrays.asList(W, X1, Y));
065    map1.put(B, Arrays.asList(Y));
066    map1.put(D, Arrays.asList(M, unjN, unjO));
067
068    InvMap map2 = new InvMap();
069    map2.put(A, Arrays.asList(W, X2, Z));
070    map2.put(C, Arrays.asList(Z));
071    map2.put(D, Arrays.asList(unjM, N));
072
073    RootNode root = diff.diffInvMap(map1, map2, false);
074    XorVisitor v = new XorVisitor();
075    root.accept(v);
076    InvMap result = v.getResult();
077
078    InvMap expected = new InvMap();
079    expected.put(A, Arrays.asList(X1, X2, Y, Z));
080    expected.put(B, Arrays.asList(Y));
081    expected.put(C, Arrays.asList(Z));
082    expected.put(D, Arrays.asList(M, N));
083
084    assertEquals(expected.toString(), result.toString());
085  }
086}