You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2013/03/26 22:28:45 UTC

svn commit: r1461322 [1/2] - in /uima/uimaj/trunk/uimaj-core/src/test: java/org/apache/uima/cas/impl/ resources/SerDes4/

Author: schor
Date: Tue Mar 26 21:28:44 2013
New Revision: 1461322

URL: http://svn.apache.org/r1461322
Log:
[UIMA-2774] improve test case so it is no longer sensitive to changes in random impl, or different Java impl of random. 

Added:
    uima/uimaj/trunk/uimaj-core/src/test/resources/SerDes4/SavedInts.binary
Removed:
    uima/uimaj/trunk/uimaj-core/src/test/resources/SerDes4/RandomSeed.binary
Modified:
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesTest4.java
    uima/uimaj/trunk/uimaj-core/src/test/resources/SerDes4/AllKinds.binary
    uima/uimaj/trunk/uimaj-core/src/test/resources/SerDes4/DeltaWithIndexMods.binary

Modified: uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesTest4.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesTest4.java?rev=1461322&r1=1461321&r2=1461322&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesTest4.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesTest4.java Tue Mar 26 21:28:44 2013
@@ -20,14 +20,17 @@ package org.apache.uima.cas.impl;
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
+import java.nio.CharBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -65,8 +68,56 @@ import org.apache.uima.util.impl.Seriali
  */
 public class SerDesTest4 extends TestCase {
   
-  private final Random random = new Random();;
+  class MyRandom extends Random {
+
+    @Override
+    public int nextInt(int n) {
+      int r = usePrevData ? readNextSavedInt() : super.nextInt(n);
+      if (capture) writeSavedInt(r);
+      return r;
+    }
+
+    @Override
+    public int nextInt() {
+      int r = usePrevData ? readNextSavedInt() : super.nextInt();
+      if (capture) writeSavedInt(r);
+      return r;
+    }
+
+    @Override
+    public long nextLong() {
+      int r = usePrevData ? readNextSavedInt() : super.nextInt();
+      if (capture) writeSavedInt(r);      
+      return r;
+    }
+
+    @Override
+    public boolean nextBoolean() {
+      int r = usePrevData ? readNextSavedInt() : super.nextInt(2);
+      if (capture) writeSavedInt(r);      
+      return r == 0;
+    }
+
+    @Override
+    public float nextFloat() {
+      int r = usePrevData ? readNextSavedInt() : super.nextInt(0x7ffff);
+      if (capture) writeSavedInt(r);
+      return Float.intBitsToFloat(r);
+    }
+
+    @Override
+    public double nextDouble() {
+      int r = usePrevData ? readNextSavedInt() : super.nextInt(0x7ffff);
+      if (capture) writeSavedInt(r);
+      return Double.longBitsToDouble((long) r);
+    }
+  }
+  
+  private final Random random = new MyRandom();
   private long seed;
+  private char[] sbSavedInts = new char[20];
+  private BufferedReader savedIntsStream;
+  private OutputStreamWriter savedIntsOutStream;
 
   private Type akof;
   private Type topType;
@@ -197,7 +248,7 @@ public class SerDesTest4 extends TestCas
   }
   
   public void setUp() {
-    long seed = random.nextLong();
+    long seed = (new Random()).nextLong();
     random.setSeed(seed);
 //    System.out.format("RandomSeed: %,d%n", seed);
 
@@ -403,8 +454,8 @@ public class SerDesTest4 extends TestCas
   }
 
   public void testWithPrevGenerated() throws IOException {
-    random.setSeed(readinRandomSeed());
     usePrevData = true;
+    initReadSavedInts();
     testDeltaWithMods();
     tearDown(); setUp();
     testDeltaWithRefsBelow();
@@ -415,15 +466,15 @@ public class SerDesTest4 extends TestCas
     tearDown(); setUp();
     testAllKinds();
     tearDown(); setUp();
-    testArrayAux();    
+    testArrayAux();
+    usePrevData = false;
+    savedIntsStream.close();
   }
 
   public void captureGenerated() throws IOException {
     capture = true;
-    long seed = random.nextLong();
-    writeoutRandomSeed(seed);
+    initWriteSavedInts();
     setUp();
-    random.setSeed(seed);
     testDeltaWithMods();
     tearDown(); setUp();
     testDeltaWithRefsBelow();
@@ -435,6 +486,7 @@ public class SerDesTest4 extends TestCas
     testAllKinds();
     tearDown(); setUp();
     testArrayAux();
+    savedIntsOutStream.close();
   }
 
   
@@ -533,7 +585,11 @@ public class SerDesTest4 extends TestCas
     "abc", "abcdef", null, "", "ghijklm", "a", "b"
   };
   private String randomString(Random r) {
-    return stringValues[r.nextInt(7)];
+    int i = r.nextInt(7);
+    if (i >= 7) {
+      System.out.println("debug");
+    }
+    return stringValues[i];
   }
 
   private StringArrayFS randomStringA(Random r) {
@@ -881,27 +937,39 @@ public class SerDesTest4 extends TestCas
     
   }
   
-  private void writeoutRandomSeed(long seed) {
+  private void initWriteSavedInts() {
     try {
-      OutputStreamWriter w = new OutputStreamWriter(setupFileOut("RandomSeed"));
-      w.append(Long.toString(seed));
-      w.close();
+      savedIntsOutStream = new OutputStreamWriter(setupFileOut("SavedInts"));
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
   
-  private long readinRandomSeed() {
+  private void initReadSavedInts() {
     try {
-      InputStreamReader i = new InputStreamReader(new BufferedInputStream(new FileInputStream(new File("src/test/resources/SerDes4/RandomSeed.binary"))));
-      char[] cbuf = new char[20];
-      int len = i.read(cbuf);
-      return Long.parseLong(new String(cbuf, 0, len));
+      savedIntsStream = new BufferedReader(new FileReader("src/test/resources/SerDes4/SavedInts.binary"));
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
   
+  private void writeSavedInt(int i) {
+    try {
+      savedIntsOutStream.write(Integer.toString(i) + '\n');
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+  
+  private int readNextSavedInt() {
+    try {
+      String s = savedIntsStream.readLine();
+      return Integer.parseInt(s);
+   } catch (IOException e) {
+      throw new RuntimeException(e);
+   }
+  }
+  
   private void makeRandomUpdatesBelowMark(List<FeatureStructure> fs, int belowMarkSize, Random r) {
     for (int i = 0; i < belowMarkSize; i++ ) {
       makeRandomUpdate(lfs.get(i), r);

Modified: uima/uimaj/trunk/uimaj-core/src/test/resources/SerDes4/AllKinds.binary
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/resources/SerDes4/AllKinds.binary?rev=1461322&r1=1461321&r2=1461322&view=diff
==============================================================================
Binary files - no diff available.

Modified: uima/uimaj/trunk/uimaj-core/src/test/resources/SerDes4/DeltaWithIndexMods.binary
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/resources/SerDes4/DeltaWithIndexMods.binary?rev=1461322&r1=1461321&r2=1461322&view=diff
==============================================================================
Binary files - no diff available.