You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2012/04/01 15:52:17 UTC

svn commit: r1308117 - in /lucene/dev/trunk/lucene/core/src: java/org/apache/lucene/store/DataOutput.java test/org/apache/lucene/index/TestIndexInput.java

Author: uschindler
Date: Sun Apr  1 13:52:16 2012
New Revision: 1308117

URL: http://svn.apache.org/viewvc?rev=1308117&view=rev
Log:
LUCENE-3738: Add random test for vints and vlongs, cleanup code and make more clear for longs

Modified:
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/DataOutput.java
    lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/DataOutput.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/DataOutput.java?rev=1308117&r1=1308116&r2=1308117&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/DataOutput.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/store/DataOutput.java Sun Apr  1 13:52:16 2012
@@ -76,7 +76,7 @@ public abstract class DataOutput {
    */
   public final void writeVInt(int i) throws IOException {
     while ((i & ~0x7F) != 0) {
-      writeByte((byte)((i & 0x7f) | 0x80));
+      writeByte((byte)((i & 0x7F) | 0x80));
       i >>>= 7;
     }
     writeByte((byte)i);
@@ -97,8 +97,8 @@ public abstract class DataOutput {
    */
   public final void writeVLong(long i) throws IOException {
     assert i >= 0L;
-    while ((i & ~0x7F) != 0) {
-      writeByte((byte)((i & 0x7f) | 0x80));
+    while ((i & ~0x7FL) != 0L) {
+      writeByte((byte)((i & 0x7FL) | 0x80L));
       i >>>= 7;
     }
     writeByte((byte)i);

Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java?rev=1308117&r1=1308116&r2=1308117&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java Sun Apr  1 13:52:16 2012
@@ -19,11 +19,15 @@ package org.apache.lucene.index;
 
 import org.apache.lucene.util.LuceneTestCase;
 import org.apache.lucene.store.ByteArrayDataInput;
+import org.apache.lucene.store.ByteArrayDataOutput;
 import org.apache.lucene.store.DataInput;
 import org.apache.lucene.store.IndexInput;
 import org.apache.lucene.store.IndexOutput;
 import org.apache.lucene.store.RAMDirectory;
 
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
 import java.io.IOException;
 
 public class TestIndexInput extends LuceneTestCase {
@@ -74,6 +78,41 @@ public class TestIndexInput extends Luce
     (byte) 0x01, // guard value
   };
   
+  static final int COUNT = RANDOM_MULTIPLIER * 65536;
+  static int[] INTS;
+  static long[] LONGS;
+  static byte[] RANDOM_TEST_BYTES;
+  
+  @BeforeClass
+  public static void beforeClass() throws IOException {
+    INTS = new int[COUNT];
+    LONGS = new long[COUNT];
+    RANDOM_TEST_BYTES = new byte[COUNT * (5 + 4 + 9 + 8)];
+    final ByteArrayDataOutput bdo = new ByteArrayDataOutput(RANDOM_TEST_BYTES);
+    for (int i = 0; i < COUNT; i++) {
+      final int i1 = INTS[i] = random.nextInt();
+      bdo.writeVInt(i1);
+      bdo.writeInt(i1);
+
+      final long l1;
+      if (rarely()) {
+        // a long with lots of zeroes at the end
+        l1 = LONGS[i] = ((long) Math.abs(random.nextInt())) << 32;
+      } else {
+        l1 = LONGS[i] = Math.abs(random.nextLong());
+      }
+      bdo.writeVLong(l1);
+      bdo.writeLong(l1);
+    }
+  }
+
+  @AfterClass
+  public static void afterClass() {
+    INTS = null;
+    LONGS = null;
+    RANDOM_TEST_BYTES = null;
+  }
+
   private void checkReads(DataInput is, Class<? extends Exception> expectedEx) throws IOException {
     assertEquals(128,is.readVInt());
     assertEquals(16383,is.readVInt());
@@ -116,29 +155,50 @@ public class TestIndexInput extends Luce
     }
     assertEquals(1L, is.readVLong()); // guard value
   }
+  
+  private void checkRandomReads(DataInput is) throws IOException {
+    for (int i = 0; i < COUNT; i++) {
+      assertEquals(INTS[i], is.readVInt());
+      assertEquals(INTS[i], is.readInt());
+      assertEquals(LONGS[i], is.readVLong());
+      assertEquals(LONGS[i], is.readLong());
+    }
+  }
 
   // this test only checks BufferedIndexInput because MockIndexInput extends BufferedIndexInput
   public void testBufferedIndexInputRead() throws IOException {
-    final IndexInput is = new MockIndexInput(READ_TEST_BYTES);
+    IndexInput is = new MockIndexInput(READ_TEST_BYTES);
     checkReads(is, IOException.class);
     is.close();
+    is = new MockIndexInput(RANDOM_TEST_BYTES);
+    checkRandomReads(is);
+    is.close();
   }
 
   // this test checks the raw IndexInput methods as it uses RAMIndexInput which extends IndexInput directly
   public void testRawIndexInputRead() throws IOException {
     final RAMDirectory dir = new RAMDirectory();
-    final IndexOutput os = dir.createOutput("foo", newIOContext(random));
+    IndexOutput os = dir.createOutput("foo", newIOContext(random));
     os.writeBytes(READ_TEST_BYTES, READ_TEST_BYTES.length);
     os.close();
-    final IndexInput is = dir.openInput("foo", newIOContext(random));
+    IndexInput is = dir.openInput("foo", newIOContext(random));
     checkReads(is, IOException.class);
     is.close();
+    
+    os = dir.createOutput("bar", newIOContext(random));
+    os.writeBytes(RANDOM_TEST_BYTES, RANDOM_TEST_BYTES.length);
+    os.close();
+    is = dir.openInput("bar", newIOContext(random));
+    checkRandomReads(is);
+    is.close();
     dir.close();
   }
 
   public void testByteArrayDataInput() throws IOException {
-    final ByteArrayDataInput is = new ByteArrayDataInput(READ_TEST_BYTES);
+    ByteArrayDataInput is = new ByteArrayDataInput(READ_TEST_BYTES);
     checkReads(is, RuntimeException.class);
+    is = new ByteArrayDataInput(RANDOM_TEST_BYTES);
+    checkRandomReads(is);
   }
 
 }