Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
47   125   20   4.27
14   89   0.43   5.5
11     1.82  
2    
 
  TestBufferedIndexInput       Line # 7 40 13 94.7% 0.94736844
  TestBufferedIndexInput.MyBufferedIndexInput       Line # 98 7 7 93.3% 0.93333334
 
  (3)
 
1    package org.apache.lucene.store;
2   
3    import java.io.IOException;
4   
5    import junit.framework.TestCase;
6   
 
7    public class TestBufferedIndexInput extends TestCase {
8    // Call readByte() repeatedly, past the buffer boundary, and see that it
9    // is working as expected.
10    // Our input comes from a dynamically generated/ "file" - see
11    // MyBufferedIndexInput below.
 
12  1 toggle public void testReadByte() throws Exception {
13  1 MyBufferedIndexInput input = new MyBufferedIndexInput();
14  10241 for(int i=0; i<BufferedIndexInput.BUFFER_SIZE*10; i++){
15  10240 assertEquals(input.readByte(), byten(i));
16    }
17    }
18   
19    // Call readBytes() repeatedly, with various chunk sizes (from 1 byte to
20    // larger than the buffer size), and see that it returns the bytes we expect.
21    // Our input comes from a dynamically generated "file" -
22    // see MyBufferedIndexInput below.
 
23  1 toggle public void testReadBytes() throws Exception {
24  1 MyBufferedIndexInput input = new MyBufferedIndexInput();
25  1 int pos=0;
26    // gradually increasing size:
27  906 for(int size=1; size<BufferedIndexInput.BUFFER_SIZE*10; size=size+size/200+1){
28  905 checkReadBytes(input, size, pos);
29  905 pos+=size;
30    }
31    // wildly fluctuating size:
32  1001 for(long i=0; i<1000; i++){
33    // The following function generates a fluctuating (but repeatable)
34    // size, sometimes small (<100) but sometimes large (>10000)
35  1000 int size1 = (int)( i%7 + 7*(i%5)+ 7*5*(i%3) + 5*5*3*(i%2));
36  1000 int size2 = (int)( i%11 + 11*(i%7)+ 11*7*(i%5) + 11*7*5*(i%3) + 11*7*5*3*(i%2) );
37  1000 int size = (i%3==0)?size2*10:size1;
38  1000 checkReadBytes(input, size, pos);
39  1000 pos+=size;
40    }
41    // constant small size (7 bytes):
42  1025 for(int i=0; i<BufferedIndexInput.BUFFER_SIZE; i++){
43  1024 checkReadBytes(input, 7, pos);
44  1024 pos+=7;
45    }
46    }
 
47  2934 toggle private void checkReadBytes(BufferedIndexInput input, int size, int pos) throws IOException{
48    // Just to see that "offset" is treated properly in readBytes(), we
49    // add an arbitrary offset at the beginning of the array
50  2934 int offset = size % 10; // arbitrary
51  2934 byte[] b = new byte[offset+size];
52  2934 input.readBytes(b, offset, size);
53  4612392 for(int i=0; i<size; i++){
54  4609461 assertEquals(b[offset+i], byten(pos+i));
55    }
56    }
57   
58    // This tests that attempts to readBytes() past an EOF will fail, while
59    // reads up to the EOF will succeed. The EOF is determined by the
60    // BufferedIndexInput's arbitrary length() value.
 
61  1 toggle public void testEOF() throws Exception {
62  1 MyBufferedIndexInput input = new MyBufferedIndexInput(1024);
63    // see that we can read all the bytes at one go:
64  1 checkReadBytes(input, (int)input.length(), 0);
65    // go back and see that we can't read more than that, for small and
66    // large overflows:
67  1 int pos = (int)input.length()-10;
68  1 input.seek(pos);
69  1 checkReadBytes(input, 10, pos);
70  1 input.seek(pos);
71  1 try {
72  1 checkReadBytes(input, 11, pos);
73  0 fail("Block read past end of file");
74    } catch (IOException e) {
75    /* success */
76    }
77  1 input.seek(pos);
78  1 try {
79  1 checkReadBytes(input, 50, pos);
80  0 fail("Block read past end of file");
81    } catch (IOException e) {
82    /* success */
83    }
84  1 input.seek(pos);
85  1 try {
86  1 checkReadBytes(input, 100000, pos);
87  0 fail("Block read past end of file");
88    } catch (IOException e) {
89    /* success */
90    }
91    }
92   
93    // byten emulates a file - byten(n) returns the n'th byte in that file.
94    // MyBufferedIndexInput reads this "file".
 
95  9239412 toggle private static byte byten(long n){
96  9239412 return (byte)(n*n%256);
97    }
 
98    private static class MyBufferedIndexInput extends BufferedIndexInput {
99    private long pos;
100    private long len;
 
101  3 toggle public MyBufferedIndexInput(long len){
102  3 this.len = len;
103  3 this.pos = 0;
104    }
 
105  2 toggle public MyBufferedIndexInput(){
106    // an infinite file
107  2 this(Long.MAX_VALUE);
108    }
 
109  1192 toggle protected void readInternal(byte[] b, int offset, int length) throws IOException {
110  4620903 for(int i=offset; i<offset+length; i++)
111  4619711 b[i] = byten(pos++);
112    }
113   
 
114  2 toggle protected void seekInternal(long pos) throws IOException {
115  2 this.pos = pos;
116    }
117   
 
118  0 toggle public void close() throws IOException {
119    }
120   
 
121  1199 toggle public long length() {
122  1199 return len;
123    }
124    }
125    }