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

package cx2x.translator.language;

import static org.junit.Assert.*;

import cx2x.xcodeml.exception.IllegalDirectiveException;
import org.junit.Test;

/**
 * JUnit test class for the CLAW language parser and information holding class.
 *
 * @author clementval
 */
public class ClawLanguageTest {

  /**
   * Test various input for the CLAW loop fusion directive.
   */
  @Test
  public void FusionTest(){
    // Valid directives
    analyzeValidClawLoopFusion("claw loop-fusion", null);
    analyzeValidClawLoopFusion("claw loop-fusion group(g1)", "g1");
    analyzeValidClawLoopFusion("claw loop-fusion group( g1 )", "g1");
    analyzeValidClawLoopFusion("claw loop-fusion group ( g1   ) ", "g1");
    analyzeValidClawLoopFusion("claw loop-fusiongroup(g1)", "g1");

    // Unvalid directives
    analyzeUnvalidClawLanguage("claw loop-fusion group");
    analyzeUnvalidClawLanguage("claw loop-fusion (i,j,k)");
    analyzeUnvalidClawLanguage("claw loop-fusion group()");
    analyzeUnvalidClawLanguage("claw loop-fusion group(   )");
  }

  /**
   * Assert the result for valid loop fusion CLAW directive
   * @param raw       Raw string valud of the CLAW directive to be analyzed.
   * @param groupName Group name to be found if any.
   */
  private void analyzeValidClawLoopFusion(String raw, String groupName){
    try {
      ClawLanguage l = ClawLanguage.analyze(raw);
      assertEquals(ClawDirective.LOOP_FUSION, l.getDirective());
      if(groupName != null){
        assertTrue(l.hasGroupOption());
        assertEquals(groupName, l.getGroupName());
      } else {
        assertFalse(l.hasGroupOption());
        assertNull(l.getGroupName());
      }
    } catch(IllegalDirectiveException idex){
      fail();
    }
  }

  /**
   * Assert any unvalid claw raw input
   * @param raw Raw string valud of the CLAW directive to be analyzed.
   */
  private void analyzeUnvalidClawLanguage(String raw){
    ClawLanguage l = null;
    try {
      l = ClawLanguage.analyze(raw);
      fail();
    } catch (IllegalDirectiveException pex){
      assertNull(l);
    }
  }
}

@@ -245,16 +245,93 @@
 .Test;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0A
 /**%0A * J
@@ -2243,10 +2243,1249 @@
   %7D%0A  %7D%0A
+%0A  /**%0A   * Test various input for the CLAW loop interchange directive.%0A   */%0A  @Test%0A  public void InterchangeTest()%7B%0A    // Valid directives%0A    analyzeValidClawLoopInterchange(%22claw loop-interchange%22, null);%0A    analyzeValidClawLoopInterchange(%22claw loop-interchange (i,j,k)%22, Arrays.asList(%22i%22, %22j%22, %22k%22));%0A%0A    // Unvalid directives%0A    analyzeUnvalidClawLanguage(%22claw loop-interchange ()%22);%0A    analyzeUnvalidClawLanguage(%22claw loop-interchange (i,j,k) group%22);%0A  %7D%0A%0A  /**%0A   * Assert the result for valid loop interchange CLAW directive%0A   * @param raw       Raw string valud of the CLAW directive to be analyzed.%0A   * @param indexes   List of indexes to be found if any.%0A   */%0A  private void analyzeValidClawLoopInterchange(String raw,%0A                                               List%3CString%3E indexes)%0A  %7B%0A    try %7B%0A      ClawLanguage l = ClawLanguage.analyze(raw);%0A      assertEquals(ClawDirective.LOOP_INTERCHANGE, l.getDirective());%0A      if(indexes != null)%7B%0A        assertTrue(l.hasIndexes());%0A        assertEquals(indexes.size(), l.getIndexes().size());%0A%0A      %7D else %7B%0A        assertFalse(l.hasIndexes());%0A        assertNull(l.getIndexes());%0A      %7D%0A    %7D catch(IllegalDirectiveException idex)%7B%0A      fail();%0A    %7D%0A  %7D%0A%0A
 %7D%0A

Add unit tests for loop-interchange directive