Author: schor
Date: Fri May  6 22:19:50 2016
New Revision: 1742662

URL: http://svn.apache.org/viewvc?rev=1742662&view=rev
Log:
[UIMA-4674] add test for v2 backward compatibility with ll set-int and friends.

Modified:
    
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/FeatureStructureTest.java

Modified: 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/FeatureStructureTest.java
URL: 
http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/FeatureStructureTest.java?rev=1742662&r1=1742661&r2=1742662&view=diff
==============================================================================
--- 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/FeatureStructureTest.java
 (original)
+++ 
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/test/java/org/apache/uima/cas/test/FeatureStructureTest.java
 Fri May  6 22:19:50 2016
@@ -25,10 +25,19 @@ import org.apache.uima.cas.CASRuntimeExc
 import org.apache.uima.cas.Feature;
 import org.apache.uima.cas.FeatureStructure;
 import org.apache.uima.cas.Type;
-import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.cas.impl.CASImpl;
+import org.apache.uima.cas.impl.FeatureImpl;
 import org.apache.uima.cas.impl.LowLevelCAS;
 import org.apache.uima.cas.impl.LowLevelTypeSystem;
+import org.apache.uima.cas.impl.TypeImpl;
+import org.apache.uima.cas.impl.TypeSystemConstants;
+import org.apache.uima.cas.impl.TypeSystemImpl;
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.jcas.cas.FSArray;
+import org.apache.uima.jcas.cas.NonEmptyFSList;
+import org.apache.uima.jcas.cas.Sofa;
+import org.apache.uima.jcas.cas.TOP;
+import org.apache.uima.jcas.tcas.Annotation;
 
 import junit.framework.TestCase;
 
@@ -38,19 +47,19 @@ import junit.framework.TestCase;
  */
 public class FeatureStructureTest extends TestCase {
 
-       private CAS cas;
+       private CASImpl cas;
 
-       private TypeSystem ts;
+       private TypeSystemImpl ts;
 
        private Type topType;
 
        private Type stringType;
 
-       private Type tokenType;
+       private TypeImpl tokenType;
 
        private Type intType;
 
-       private Type tokenTypeType;
+       private TypeImpl tokenTypeType;
 
        private Type wordType;
 
@@ -79,6 +88,8 @@ public class FeatureStructureTest extend
        private Feature sentLenFeat;
 
        private Feature tokenFloatFeat;
+       private Feature tokenLongFeat;
+       private Feature tokenDoubleFeat;
 
        private Feature startFeature;
 
@@ -97,9 +108,9 @@ public class FeatureStructureTest extend
 
        public void setUp() {
                try {
-                       this.cas = CASInitializer.initCas(new CASTestSetup(), 
null);
+                       this.cas = (CASImpl) CASInitializer.initCas(new 
CASTestSetup(), null);
                        assertTrue(this.cas != null);
-                       this.ts = this.cas.getTypeSystem();
+                       this.ts = (TypeSystemImpl) this.cas.getTypeSystem();
                        assertTrue(this.ts != null);
                } catch (Exception e) {
                        e.printStackTrace();
@@ -133,6 +144,10 @@ public class FeatureStructureTest extend
                assertTrue(this.sentLenFeat != null);
                this.tokenFloatFeat = 
this.ts.getFeatureByFullName(CASTestSetup.TOKEN_FLOAT_FEAT_Q);
                assertTrue(this.tokenFloatFeat != null);
+    this.tokenDoubleFeat = 
this.ts.getFeatureByFullName(CASTestSetup.TOKEN_DOUBLE_FEAT_Q);
+    assertTrue(this.tokenDoubleFeat != null);
+    this.tokenLongFeat = 
this.ts.getFeatureByFullName(CASTestSetup.TOKEN_LONG_FEAT_Q);
+    assertTrue(this.tokenLongFeat != null);
                this.startFeature = 
this.ts.getFeatureByFullName(CAS.FEATURE_FULL_NAME_BEGIN);
                assertTrue(this.startFeature != null);
                this.langPairType = this.ts.getType(CASTestSetup.LANG_PAIR);
@@ -200,6 +215,92 @@ public class FeatureStructureTest extend
                assertTrue(word.getType().equals(wordType1));
                assertTrue(token.getType().equals(tokenType1));
        }
+       
+       /**
+        * This test tests V2 backwards compatibility 
+        * The goal is to match what V2 did for low level cas access
+        * The area this is testing is the use of the LL int operations to 
change the type of an existing feature structure.
+        */
+       public void testLLsetType() {
+         LowLevelCAS llc = cas.getLowLevelCAS();
+    FSArray fsa = new FSArray(ts.getType(CAS.TYPE_NAME_FS_ARRAY), cas, 3);
+    NonEmptyFSList  fsl = new 
NonEmptyFSList(ts.getType(CAS.TYPE_NAME_NON_EMPTY_FS_LIST), cas);
+    
+    Annotation token = this.cas.createFS(tokenType);
+    // set up some refs; these must be updated if the type changes in a way to 
require a new FS
+    fsa.set(0, token);   // set the 0th  element of a FS Array to point to the 
"token"
+    fsl.setHead(token);  // set the head element of a FS Array to point to the 
"token"
+    int tokId = token.id();
+    
+    // set some feature values; some of these are copied (if there's room, 
etc.)
+    TOP ttfv = cas.createFS(tokenTypeType);
+    token.setFeatureValue(tokenTypeFeat, ttfv);
+    token.setFloatValue(tokenFloatFeat, 1.1f);
+    assertEquals(1.1f, token.getFloatValue(tokenFloatFeat));
+    token.setDoubleValue(tokenDoubleFeat, 1.7d);
+    assertEquals(1.7d, token.getDoubleValue(tokenDoubleFeat));
+    token.setBegin(3);
+    token.setEnd(5);
+    
+    Sofa sofa = (Sofa) token.getSofa();
+    assertTrue(sofa != null);
+    assertTrue(fsa.get(0) == token);
+    assertTrue(fsl.getHead() == token);
+    
+    // change the type to just Annotation
+    // because this is a supertype, it should not create a new FS
+    
+    llc.ll_setIntValue(tokId, 0, TypeSystemConstants.annotTypeCode);
+    Annotation fs = cas.getFsFromId(tokId);
+    assertTrue(fs == token);
+    assertTrue(fs.id() == token.id());
+    assertEquals(ts.annotType, fs._typeImpl);
+    assertEquals(fs.getBegin(), 3);
+    assertEquals(fs.getEnd(), 5);
+    assertEquals(sofa, fs.getSofa());
+    assertTrue(fsa.get(0) == fs);
+    assertTrue(fsl.getHead() == fs);
+    
+    // Change Annotation back to Token type    
+    
+    llc.ll_setIntValue(tokId, 0, tokenType.getCode());
+    token = cas.getFsFromId(tokId);
+    assertTrue(fs == token);
+    assertTrue(fs.id() == token.id());
+    assertEquals(fs.getBegin(), 3);
+    assertEquals(fs.getEnd(), 5);
+    assertEquals(sofa, fs.getSofa());
+    assertEquals(1.1f, token.getFloatValue(tokenFloatFeat));
+    assertEquals(ttfv, token.getFeatureValue(tokenTypeFeat));
+    assertTrue(fsa.get(0) == token);
+    assertTrue(fsl.getHead() == token);
+    
+    // change type where the type forces a copy
+    // token -> token_type_type
+    //  These types are completely orthogonal, one doesn't subsume the other
+    
+    llc.ll_setIntValue(tokId,  0,  tokenTypeType.getCode());
+    TOP ttt = cas.getFsFromId(tokId);
+    assertTrue(ttt != token);
+    assertTrue(ttt.id() == tokId);
+    assertEquals(ttt._typeImpl, tokenTypeType);
+    assertTrue(fsa.get(0) == ttt);
+    assertTrue(fsl.getHead() == ttt);
+    
+    
+    llc.ll_setIntValue(tokId,  0,  tokenType.getCode());
+    token = cas.getFsFromId(tokId);
+    assertTrue(ttt != token);
+    assertTrue(ttt.id() == token.id());
+    assertEquals(token.getBegin(), 0);
+    assertEquals(token.getEnd(), 0);
+    assertEquals(sofa, token.getSofa());
+    assertEquals(0.0f, token.getFloatValue(tokenFloatFeat));
+    assertEquals(null, token.getFeatureValue(tokenTypeFeat));
+    assertTrue(fsa.get(0) == token);
+    assertTrue(fsl.getHead() == token);
+
+       }
 
        public void testSetArrayValuedFeature() {
                FeatureStructure testFS = 
this.cas.createFS(this.arrayFsWithSubtypeType);
@@ -262,6 +363,16 @@ public class FeatureStructureTest extend
                testString = "";
                token.setStringValue(this.lemmaFeat, testString);
                
assertTrue(token.getStringValue(this.lemmaFeat).equals(testString));
+               
+               // test low level
+    LowLevelCAS llcas = cas.getLowLevelCAS();
+               token.setFeatureValue(tokenTypeFeat, word);
+               int fsRef = token.id();
+               int fc = ((FeatureImpl)tokenTypeFeat).getCode();
+               assertEquals(llcas.ll_getIntValue(fsRef, fc), word.id());
+               TOP word2 = cas.createFS(wordType);
+               llcas.ll_setIntValue(fsRef, fc, word2.id());
+               assertEquals(token.getFeatureValue(tokenTypeFeat), word2);
        }
 
        public void testSetFloatValue() {
@@ -291,7 +402,68 @@ public class FeatureStructureTest extend
                }
                assertTrue(caughtExc);
                assertTrue(token.getFloatValue(this.tokenFloatFeat) == f);
+               
+               // low level
+               LowLevelCAS llcas = cas.getLowLevelCAS();
+               int ffc = ((FeatureImpl)tokenFloatFeat).getCode();
+               llcas.ll_setIntValue(token.id(), ffc, 
CASImpl.float2int(123.456f));
+               assertEquals(token.getFloatValue(tokenFloatFeat), 123.456f);
+               assertEquals(llcas.ll_getIntValue(token.id(), ffc), 
CASImpl.float2int(123.456f));
        }
+       
+  public void testSetLongValue() {
+         AnnotationFS token = (AnnotationFS) this.cas.createFS(this.tokenType);
+    assertTrue(token.getLongValue(this.tokenLongFeat) == 0.0f);
+    long f = -34L;
+    token.setLongValue(this.tokenLongFeat, f);
+    assertTrue(token.getLongValue(this.tokenLongFeat) == f);
+    f = 8_000_000_003L;
+    token.setLongValue(this.tokenLongFeat, f);
+    assertTrue(token.getLongValue(this.tokenLongFeat) == f);
+    f = -8_000_000_003L;
+    token.setLongValue(this.tokenLongFeat, f);
+    assertTrue(token.getLongValue(this.tokenLongFeat) == f);
+    
+    // low level
+    LowLevelCAS llcas = cas.getLowLevelCAS();
+    int ffc = ((FeatureImpl)tokenLongFeat).getCode();
+    int h = llcas.ll_getIntValue(token.id(), ffc);
+    assertEquals(1, h);
+    
+    long g = 23;
+    token.setLongValue(this.tokenLongFeat, g);
+    assertEquals(g, token.getLongValue(this.tokenLongFeat));
+    
+    llcas.ll_setIntValue(token.id(), ffc, h);
+    assertEquals(f, token.getLongValue(this.tokenLongFeat));
+  }
+
+  public void testSetDoubleValue() {
+    AnnotationFS token = (AnnotationFS) this.cas.createFS(this.tokenType);
+    assertTrue(token.getDoubleValue(this.tokenDoubleFeat) == 0.0f);
+    double f = -34.56D;
+    token.setDoubleValue(this.tokenDoubleFeat, f);
+    assertTrue(token.getDoubleValue(this.tokenDoubleFeat) == f);
+    f = 8_000_000_003.24852D;
+    token.setDoubleValue(this.tokenDoubleFeat, f);
+    assertTrue(token.getDoubleValue(this.tokenDoubleFeat) == f);
+    f = -8_000_000_003D;
+    token.setDoubleValue(this.tokenDoubleFeat, f);
+    assertTrue(token.getDoubleValue(this.tokenDoubleFeat) == f);
+    
+    // low level
+    LowLevelCAS llcas = cas.getLowLevelCAS();
+    int ffc = ((FeatureImpl)tokenDoubleFeat).getCode();
+    int h = llcas.ll_getIntValue(token.id(), ffc);
+    assertEquals(1, h);
+    
+    double g = 23;
+    token.setDoubleValue(this.tokenDoubleFeat, g);
+    assertEquals(g, token.getDoubleValue(this.tokenDoubleFeat));
+    
+    llcas.ll_setIntValue(token.id(), ffc, h);
+    assertEquals(f, token.getDoubleValue(this.tokenDoubleFeat));
+  }
 
        public void testSetIntValue() {
                AnnotationFS token = (AnnotationFS) 
this.cas.createFS(this.tokenType);
@@ -390,6 +562,27 @@ public class FeatureStructureTest extend
                llc.ll_setStringValue(addr, lemmaFeatCode, "test", true);
                assertTrue(llc.ll_getCharBufferValueSize(addr, lemmaFeatCode) 
== 4);
 
+               // try accessing low level strings using ll_setIntValue
+               
+               final int stringcode = llc.ll_getIntValue(addr,  lemmaFeatCode);
+               assertTrue(stringcode == 1);
+    llc.ll_setStringValue(addr, lemmaFeatCode, "test", true);
+    assertEquals(llc.ll_getIntValue(addr,  lemmaFeatCode), 1);  // should not 
change
+    llc.ll_setStringValue(addr, lemmaFeatCode, "test2", true);
+    assertEquals(llc.ll_getIntValue(addr,  lemmaFeatCode), 2);
+    llc.ll_setIntValue(addr,  lemmaFeatCode,  1);
+    assertEquals(llc.ll_getIntValue(addr,  lemmaFeatCode), 1);
+    assertEquals(llc.ll_getStringValue(addr, lemmaFeatCode), "test");
+    llc.ll_setIntValue(addr,  lemmaFeatCode,  0);
+    assertEquals(llc.ll_getIntValue(addr,  lemmaFeatCode), 0);
+    assertTrue(llc.ll_getStringValue(addr, lemmaFeatCode) == null);
+    llc.ll_setIntValue(addr,  lemmaFeatCode, 2);
+    assertEquals(llc.ll_getStringValue(addr,  lemmaFeatCode), "test2");
+    
+    // check that equal strings are shared
+    
+    llc.ll_setStringValue(addr, lemmaFeatCode, new String("test"));  
+    assertEquals(1, llc.ll_getIntValue(addr, lemmaFeatCode));
        }
 
        public void testEquals() {


Reply via email to