/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */


package cx2x.translator.common.analysis.dependence;

import cx2x.xcodeml.xnode.Xcode;
import cx2x.xcodeml.xnode.XcodeProgram;
import cx2x.xcodeml.xnode.Xnode;
import helper.TestConstant;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;

/**
 * Test the various features of the DependenceAnalysis class.
 *
 * @author clementval
 */
public class DependenceAnalysisTest {

  @Test
  public void analyzeTest() {
    File f = new File(TestConstant.TEST_DEPENDENCE);
    assertTrue(f.exists());
    XcodeProgram xcodeml = XcodeProgram.createFromFile(TestConstant.TEST_DEPENDENCE);
    assertNotNull(xcodeml);
    List<Xnode> functions = xcodeml.matchAll(Xcode.FFUNCTIONDEFINITION);
    assertEquals(2, functions.size());

    Xnode lw_solver_noscat = functions.get(0);

    List<Xnode> loops = lw_solver_noscat.matchAll(Xcode.FDOSTATEMENT);
    assertEquals(10, loops.size());

    List<DependenceAnalysis> dependencies = new ArrayList<>();
    for(Xnode loop : loops) {
      try {
        dependencies.add(new DependenceAnalysis(loop));
      } catch(Exception e) {
        fail();
      }
    }

    assertTrue(dependencies.get(0).isIndependent());
    assertTrue(dependencies.get(1).isIndependent());
    assertTrue(dependencies.get(2).isIndependent());

    assertFalse(dependencies.get(3).isIndependent());
    assertEquals(1, dependencies.get(3).getDistanceVector());
    assertEquals(DependenceDirection.BACKWARD,
        dependencies.get(3).getDirectionVector());

    assertFalse(dependencies.get(4).isIndependent());
    assertEquals(1, dependencies.get(4).getDistanceVector());
    assertEquals(DependenceDirection.FORWARD,
        dependencies.get(4).getDirectionVector());

    assertFalse(dependencies.get(5).isIndependent());
    assertEquals(1, dependencies.get(5).getDistanceVector());
    assertEquals(DependenceDirection.FORWARD,
        dependencies.get(5).getDirectionVector());

    assertFalse(dependencies.get(6).isIndependent());
    assertEquals(1, dependencies.get(6).getDistanceVector());
    assertEquals(DependenceDirection.BACKWARD,
        dependencies.get(6).getDirectionVector());

    assertTrue(dependencies.get(7).isIndependent());
    assertTrue(dependencies.get(8).isIndependent());
    assertTrue(dependencies.get(9).isIndependent());

    for(DependenceAnalysis dep : dependencies) {
      System.out.println(dep.getInfoMsg());
    }
  }
}

@@ -148,16 +148,667 @@
 dence;%0A%0A
+import cx2x.translator.config.Configuration;%0Aimport cx2x.translator.language.base.ClawLanguage;%0Aimport cx2x.translator.language.helper.accelerator.AcceleratorDirective;%0Aimport cx2x.translator.language.helper.accelerator.AcceleratorGenerator;%0Aimport cx2x.translator.language.helper.accelerator.AcceleratorHelper;%0Aimport cx2x.translator.language.helper.target.Target;%0Aimport cx2x.translator.transformation.loop.LoopFusion;%0Aimport cx2x.translator.transformer.ClawTransformer;%0Aimport cx2x.xcodeml.exception.IllegalTransformationException;%0Aimport cx2x.xcodeml.transformation.DependentTransformationGroup;%0Aimport cx2x.xcodeml.transformation.Transformation;%0A
 import c
@@ -828,24 +828,24 @@
 node.Xcode;%0A
-
 import cx2x.
@@ -1541,24 +1541,575 @@
 s.size());%0A%0A
+    List%3CXnode%3E pragmas = xcodeml.matchAll(Xcode.FPRAGMASTATEMENT);%0A    assertEquals(1, pragmas.size());%0A%0A    Configuration configuration =%0A        new Configuration(AcceleratorDirective.OPENACC, Target.GPU);%0A    ClawTransformer transformer = new ClawTransformer(configuration, 80);%0A    AcceleratorGenerator generator =%0A        AcceleratorHelper.createAcceleratorGenerator(configuration);%0A    ClawLanguage main = null;%0A    try %7B%0A      main = ClawLanguage.analyze(pragmas.get(0), generator, Target.GPU);%0A    %7D catch(Exception e)%7B%0A      fail();%0A    %7D%0A%0A%0A
     Xnode lw
@@ -3659,94 +3659,332 @@
     
-for(DependenceAnalysis dep : dependencies) %7B%0A      System.out.println(dep.getInfoMsg()
+try %7B%0A      IterationSpace is = new IterationSpace(loops);%0A      is.tryFusion(xcodeml, transformer, main);%0A      is.printDebug(true);%0A%0A      loops = lw_solver_noscat.matchAll(Xcode.FDOSTATEMENT);%0A      assertEquals(7, loops.size());%0A%0A      is.reload(loops);%0A      is.printDebug(true);%0A%0A    %7D catch(Exception e) %7B%0A      fail(
 );%0A 
@@ -3988,14 +3988,16 @@
 ;%0A    %7D%0A
+%0A%0A
   %7D%0A%7D%0A

Test iteration space features