FileDocCategorySizeDatePackage
TestBufferedIndexInput.javaAPI DocApache Lucene 2.1.04232Wed Feb 14 10:46:34 GMT 2007org.apache.lucene.store

TestBufferedIndexInput

public class TestBufferedIndexInput extends TestCase

Fields Summary
Constructors Summary
Methods Summary
private static bytebyten(long n)

    	return (byte)(n*n%256);
    
private voidcheckReadBytes(org.apache.lucene.store.BufferedIndexInput input, int size, int pos)

	   // Just to see that "offset" is treated properly in readBytes(), we
	   // add an arbitrary offset at the beginning of the array
	   int offset = size % 10; // arbitrary
	   byte[] b = new byte[offset+size];
	   input.readBytes(b, offset, size);
	   for(int i=0; i<size; i++){
		   assertEquals(b[offset+i], byten(pos+i));
	   }
   
public voidtestEOF()

	   MyBufferedIndexInput input = new MyBufferedIndexInput(1024);
	   // see that we can read all the bytes at one go:
	   checkReadBytes(input, (int)input.length(), 0);  
	   // go back and see that we can't read more than that, for small and
	   // large overflows:
	   int pos = (int)input.length()-10;
	   input.seek(pos);
	   checkReadBytes(input, 10, pos);  
	   input.seek(pos);
	   try {
		   checkReadBytes(input, 11, pos);
           fail("Block read past end of file");
       } catch (IOException e) {
           /* success */
       }
	   input.seek(pos);
	   try {
		   checkReadBytes(input, 50, pos);
           fail("Block read past end of file");
       } catch (IOException e) {
           /* success */
       }
	   input.seek(pos);
	   try {
		   checkReadBytes(input, 100000, pos);
           fail("Block read past end of file");
       } catch (IOException e) {
           /* success */
       }
  
public voidtestReadByte()

    	MyBufferedIndexInput input = new MyBufferedIndexInput(); 
    	for(int i=0; i<BufferedIndexInput.BUFFER_SIZE*10; i++){
     		assertEquals(input.readByte(), byten(i));
    	}
    
public voidtestReadBytes()

    	MyBufferedIndexInput input = new MyBufferedIndexInput();
    	int pos=0;
    	// gradually increasing size:
    	for(int size=1; size<BufferedIndexInput.BUFFER_SIZE*10; size=size+size/200+1){
    		checkReadBytes(input, size, pos);
    		pos+=size;
    	}
    	// wildly fluctuating size:
    	for(long i=0; i<1000; i++){
    		// The following function generates a fluctuating (but repeatable)
    		// size, sometimes small (<100) but sometimes large (>10000)
    		int size1 = (int)( i%7 + 7*(i%5)+ 7*5*(i%3) + 5*5*3*(i%2));
    		int size2 = (int)( i%11 + 11*(i%7)+ 11*7*(i%5) + 11*7*5*(i%3) + 11*7*5*3*(i%2) );
    		int size = (i%3==0)?size2*10:size1; 
    		checkReadBytes(input, size, pos);
    		pos+=size;
    	}
    	// constant small size (7 bytes):
    	for(int i=0; i<BufferedIndexInput.BUFFER_SIZE; i++){
    		checkReadBytes(input, 7, pos);
    		pos+=7;
    	}