lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From uschind...@apache.org
Subject svn commit: r1308119 - in /lucene/dev/branches/branch_3x: ./ lucene/ lucene/core/src/ lucene/core/src/java/org/apache/lucene/store/DataOutput.java lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
Date Sun, 01 Apr 2012 13:56:05 GMT
Author: uschindler
Date: Sun Apr  1 13:56:04 2012
New Revision: 1308119

URL: http://svn.apache.org/viewvc?rev=1308119&view=rev
Log:
Merged revision(s) 1308117 from lucene/dev/trunk:
LUCENE-3738: Add random test for vints and vlongs, cleanup code and make more clear for longs

Modified:
    lucene/dev/branches/branch_3x/   (props changed)
    lucene/dev/branches/branch_3x/lucene/   (props changed)
    lucene/dev/branches/branch_3x/lucene/core/src/   (props changed)
    lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/store/DataOutput.java
    lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java

Modified: lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/store/DataOutput.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/store/DataOutput.java?rev=1308119&r1=1308118&r2=1308119&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/store/DataOutput.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/store/DataOutput.java
Sun Apr  1 13:56:04 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/branches/branch_3x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java?rev=1308119&r1=1308118&r2=1308119&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
Sun Apr  1 13:56:04 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");
+    IndexOutput os = dir.createOutput("foo");
     os.writeBytes(READ_TEST_BYTES, READ_TEST_BYTES.length);
     os.close();
-    final IndexInput is = dir.openInput("foo");
+    IndexInput is = dir.openInput("foo");
     checkReads(is, IOException.class);
     is.close();
+    
+    os = dir.createOutput("bar");
+    os.writeBytes(RANDOM_TEST_BYTES, RANDOM_TEST_BYTES.length);
+    os.close();
+    is = dir.openInput("bar");
+    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);
   }
 
   /**



Mime
View raw message