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 UnionVisitorTester {
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  // M_<num> and N_<num> have the same class, vars, and formula, but
028  // different probabilities
029  // map1: A->{W, X1, Y}, B->{Y}, D->{M_001, N_001, O_1}
030  // map2: A->{W, X2, Z}, C->{Z}, D->{M_1, N_0001}
031  // map1 union map2: A->{W, X1, X2, Y, Z}, B->{Y}, C->{Z},
032  //                  D->{M_001, N_0001, O_1}
033  @Test
034  public void testXor() {
035    VarInfo[] vars = {
036      DiffTester.newIntVarInfo("w"),
037      DiffTester.newIntVarInfo("x"),
038      DiffTester.newIntVarInfo("y"),
039      DiffTester.newIntVarInfo("z"),
040      DiffTester.newIntVarInfo("m"),
041      DiffTester.newIntVarInfo("n"),
042      DiffTester.newIntVarInfo("o"),
043    };
044    PptTopLevel A = Common.makePptTopLevel("A:::OBJECT", vars);
045    PptTopLevel B = Common.makePptTopLevel("B:::OBJECT", vars);
046    PptTopLevel C = Common.makePptTopLevel("C:::OBJECT", vars);
047    PptTopLevel D = Common.makePptTopLevel("D:::OBJECT", vars);
048    PptSlice slicew = new PptSlice1(A, new VarInfo[] {vars[0]});
049    PptSlice slicex = new PptSlice1(A, new VarInfo[] {vars[1]});
050    PptSlice slicey = new PptSlice1(A, new VarInfo[] {vars[2]});
051    PptSlice slicez = new PptSlice1(A, new VarInfo[] {vars[3]});
052    PptSlice slicem = new PptSlice1(A, new VarInfo[] {vars[4]});
053    PptSlice slicen = new PptSlice1(A, new VarInfo[] {vars[5]});
054    PptSlice sliceo = new PptSlice1(A, new VarInfo[] {vars[6]});
055    Invariant W = new DiffDummyInvariant(slicew, "W", true);
056    Invariant X1 = new DiffDummyInvariant(slicex, "X1", true);
057    Invariant X2 = new DiffDummyInvariant(slicex, "X2", true);
058    Invariant Y = new DiffDummyInvariant(slicey, "Y", true);
059    Invariant Z = new DiffDummyInvariant(slicez, "Z", true);
060    Invariant M_001 = new DiffDummyInvariant(slicem, "M", .999);
061    Invariant M_1 = new DiffDummyInvariant(slicem, "M", 0);
062    Invariant N_001 = new DiffDummyInvariant(slicen, "N", .999);
063    Invariant N_0001 = new DiffDummyInvariant(slicen, "N", .9999);
064    Invariant O_1 = new DiffDummyInvariant(sliceo, "O", 0);
065
066    InvMap map1 = new InvMap();
067    map1.put(A, Arrays.asList(W, X1, Y));
068    map1.put(B, Arrays.asList(Y));
069    map1.put(D, Arrays.asList(M_001, N_001, O_1));
070
071    InvMap map2 = new InvMap();
072    map2.put(A, Arrays.asList(W, X2, Z));
073    map2.put(C, Arrays.asList(Z));
074    map2.put(D, Arrays.asList(M_1, N_0001));
075
076    RootNode root = diff.diffInvMap(map1, map2);
077    UnionVisitor v = new UnionVisitor();
078    root.accept(v);
079    InvMap result = v.getResult();
080
081    InvMap expected = new InvMap();
082    expected.put(A, Arrays.asList(W, X1, X2, Y, Z));
083    expected.put(B, Arrays.asList(Y));
084    expected.put(C, Arrays.asList(Z));
085    expected.put(D, Arrays.asList(M_001, N_0001, O_1));
086
087    assertEquals(expected.toString(), result.toString());
088  }
089}