lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ehatc...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene/index MockInputStream.java TestInputStream.java
Date Tue, 12 Aug 2003 10:41:56 GMT
ehatcher    2003/08/12 03:41:56

  Added:       src/test/org/apache/lucene/index MockInputStream.java
                        TestInputStream.java
  Log:
  Low-level InputStream test w/mock
  
  Revision  Changes    Path
  1.1                  jakarta-lucene/src/test/org/apache/lucene/index/MockInputStream.java
  
  Index: MockInputStream.java
  ===================================================================
  package org.apache.lucene.index;
  
  import org.apache.lucene.store.InputStream;
  
  import java.io.IOException;
  
  public class MockInputStream extends InputStream {
      private byte[] buffer;
      private int pointer = 0;
  
      public MockInputStream(byte[] bytes) {
          buffer = bytes;
          length = bytes.length;
      }
  
      protected void readInternal(byte[] dest, int destOffset, int len)
              throws IOException {
          int remainder = len;
          int start = pointer;
          while (remainder != 0) {
  //          int bufferNumber = start / buffer.length;
            int bufferOffset = start % buffer.length;
            int bytesInBuffer = buffer.length - bufferOffset;
            int bytesToCopy = bytesInBuffer >= remainder ? remainder : bytesInBuffer;
            System.arraycopy(buffer, bufferOffset, dest, destOffset, bytesToCopy);
            destOffset += bytesToCopy;
            start += bytesToCopy;
            remainder -= bytesToCopy;
          }
          pointer += len;
      }
  
      public void close() throws IOException {
          // ignore
      }
  
      protected void seekInternal(long pos) throws IOException {
          pointer = (int) pos;
      }
  }
  
  
  
  1.1                  jakarta-lucene/src/test/org/apache/lucene/index/TestInputStream.java
  
  Index: TestInputStream.java
  ===================================================================
  package org.apache.lucene.index;
  
  import junit.framework.TestCase;
  import org.apache.lucene.store.InputStream;
  
  import java.io.IOException;
  
  public class TestInputStream extends TestCase {
      public void testRead() throws IOException {
          InputStream is = new MockInputStream(new byte[] { (byte) 0x80, 0x01,
                                                            (byte) 0xFF, 0x7F,
                                                            (byte) 0x80, (byte) 0x80, 0x01,
                                                            (byte) 0x81, (byte) 0x80, 0x01,
                                                            0x06, 'L', 'u', 'c', 'e', 'n',
'e'});
          assertEquals(128,is.readVInt());
          assertEquals(16383,is.readVInt());
          assertEquals(16384,is.readVInt());
          assertEquals(16385,is.readVInt());
          assertEquals("Lucene",is.readString());
      }
  }
  
  
  

Mime
View raw message