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

package xelement;

import cx2x.xcodeml.xelement.*;
import helper.XmlHelper;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * @author clementval
 */
public class XbasicTypeTest {

  private static final String type1 =
      "<FbasicType type=\"TYPE_NAME\" ref=\"Fint\">\n" +
      "<kind>8</kind>"+
      "</FbasicType>";

  private static final String type2 =
      "<FbasicType type=\"TYPE_NAME\" ref=\"Fcharacter\"> <len>\n" +
      "<FintConstant type=\"Fint\">10</FintConstant> </len>\n" +
      "</FbasicType>";

  private static final String type3 =
      "<FbasicType type=\"TYPE_NAME\" ref=\"Fint\">" +
      "<arrayIndex>" +
      "<FintConstant type=\"Fint\">10</FintConstant>" +
      "</arrayIndex>" +
      "<indexRange>" +
      "<lowerBound>" +
      "<FintConstant type=\"Fint\">1</FintConstant>" +
      "</lowerBound>" +
      "<upperBound>" +
      "<FintConstant type=\"Fint\">10</FintConstant>" +
      "</upperBound>" +
      "</indexRange>" +
      "</FbasicType>";


  /**
   * Test for a simple integer type
   *
   * integer(kind=8)
   */
  @Test
  public void simpleIntegerTypeTest(){
    XbasicType b = XmlHelper.createXbasicTypeFromString(type1);
    assertNotNull(b);
    assertTrue(b.hasKind());
    assertNotNull(b.getKind());
    assertNotNull(b.getRef());
    assertNotNull(b.getType());
    assertNull(b.getLength());
    assertFalse(b.hasLength());
    assertFalse(b.isAllocatable());
    assertFalse(b.isArray());
    assertFalse(b.isExternal());
    assertFalse(b.isIntrinsic());
    assertFalse(b.isOptional());
    assertFalse(b.isParameter());
    assertFalse(b.isPointer());
    assertFalse(b.isPrivate());
    assertFalse(b.isPublic());
    assertFalse(b.isSave());
    assertFalse(b.isTarget());
    assertEquals(0, b.getDimensions());

    assertEquals("Fint", b.getRef());
    assertEquals("TYPE_NAME", b.getType());
    assertEquals("8", b.getKind().getValue());
  }

  /**
   * Test for a simple character declarations
   *
   * character(len=10)
   */
  @Test
  public void simpleCharTypeTest(){
    XbasicType b = XmlHelper.createXbasicTypeFromString(type2);
    assertNotNull(b);
    assertFalse(b.hasKind());
    assertNull(b.getKind());
    assertNotNull(b.getRef());
    assertNotNull(b.getType());
    assertTrue(b.hasLength());
    assertNotNull(b.getLength());
    assertFalse(b.isAllocatable());
    assertFalse(b.isArray());
    assertFalse(b.isExternal());
    assertFalse(b.isIntrinsic());
    assertFalse(b.isOptional());
    assertFalse(b.isParameter());
    assertFalse(b.isPointer());
    assertFalse(b.isPrivate());
    assertFalse(b.isPublic());
    assertFalse(b.isSave());
    assertFalse(b.isTarget());
    assertEquals(0, b.getDimensions());

    assertEquals("Fcharacter", b.getRef());
    assertEquals("TYPE_NAME", b.getType());
    assertTrue(b.getLength().getExprModel().isIntConst());
    assertEquals("10", b.getLength().getExprModel().getIntConstant().getValue());
  }


  /**
   * Test for a more complex integer type with dimension
   *
   * integer dimension(10, 1:10)
   */
  @Test
  public void complexIntTypeTest() {
    XbasicType b = XmlHelper.createXbasicTypeFromString(type3);
    assertNotNull(b);
    assertFalse(b.hasKind());
    assertNull(b.getKind());
    assertNotNull(b.getRef());
    assertNotNull(b.getType());
    assertNull(b.getLength());
    assertFalse(b.hasLength());
    assertFalse(b.isAllocatable());
    assertTrue(b.isArray());
    assertFalse(b.isExternal());
    assertFalse(b.isIntrinsic());
    assertFalse(b.isOptional());
    assertFalse(b.isParameter());
    assertFalse(b.isPointer());
    assertFalse(b.isPrivate());
    assertFalse(b.isPublic());
    assertFalse(b.isSave());
    assertFalse(b.isTarget());
    assertEquals(2, b.getDimensions());
    assertNotNull(b.getDimensions(0));
    assertNotNull(b.getDimensions(1));
    assertNull(b.getDimensions(2));

    assertEquals("Fint", b.getRef());
    assertEquals("TYPE_NAME", b.getType());
  }
}

@@ -3988,24 +3988,818 @@
 sions(2));%0A%0A
+    Xindex dim0 = b.getDimensions(0);%0A    Xindex dim1 = b.getDimensions(1);%0A%0A    assertTrue(dim0.isArrayIndex());%0A    assertTrue(dim1.isIndexRange());%0A%0A    XarrayIndex arrayIndex = (XarrayIndex)dim0;%0A    assertNotNull(arrayIndex.getExprModel());%0A    assertTrue(arrayIndex.getExprModel().isIntConst());%0A    assertEquals(%2210%22, arrayIndex.getExprModel().getIntConstant().getValue());%0A%0A    XindexRange indexRange = (XindexRange)dim1;%0A    assertNotNull(indexRange.getLowerBound());%0A    assertNotNull(indexRange.getUpperBound());%0A    assertTrue(indexRange.getLowerBound().getExprModel().isIntConst());%0A    assertEquals(%221%22, indexRange.getLowerBound().getValue());%0A    assertTrue(indexRange.getUpperBound().getExprModel().isIntConst());%0A    assertEquals(%2210%22, indexRange.getUpperBound().getValue());%0A%0A
     assertEq

Add more assertion to validate inner values