LUCENE-5849: try to stress slicing better in BaseDirectoryTestCase

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1614339 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2014-07-29 13:17:44 +00:00
parent 892131b0d1
commit ad8aa3826c
1 changed files with 47 additions and 0 deletions

View File

@ -914,5 +914,52 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {
input.close(); input.close();
dir.close(); dir.close();
} }
/** try to stress slices of slices */
public void testSliceOfSlice() throws Exception {
Directory dir = getDirectory(createTempDir("sliceOfSlice"));
IndexOutput output = dir.createOutput("bytes", newIOContext(random()));
int num = TestUtil.nextInt(random(), 50, 2500);
byte bytes[] = new byte[num];
random().nextBytes(bytes);
for (int i = 0; i < bytes.length; i++) {
output.writeByte(bytes[i]);
}
output.close();
IndexInput input = dir.openInput("bytes", newIOContext(random()));
// seek to a random spot shouldnt impact slicing.
input.seek(TestUtil.nextLong(random(), 0, input.length()));
for (int i = 0; i < num; i += 16) {
IndexInput slice1 = input.slice("slice1", i, num-i);
assertEquals(0, slice1.getFilePointer());
assertEquals(num-i, slice1.length());
// seek to a random spot shouldnt impact slicing.
slice1.seek(TestUtil.nextLong(random(), 0, slice1.length()));
for (int j = 0; j < slice1.length(); j += 16) {
IndexInput slice2 = slice1.slice("slice2", j, num-i-j);
assertEquals(0, slice2.getFilePointer());
assertEquals(num-i-j, slice2.length());
byte data[] = new byte[num];
System.arraycopy(bytes, 0, data, 0, i+j);
if (random().nextBoolean()) {
// read the bytes for this slice-of-slice
slice2.readBytes(data, i+j, num-i-j);
} else {
// seek to a random spot in between, read some, seek back and read the rest
long seek = TestUtil.nextLong(random(), 0, slice2.length());
slice2.seek(seek);
slice2.readBytes(data, (int)(i+j+seek), (int)(num-i-j-seek));
slice2.seek(0);
slice2.readBytes(data, i+j, (int)seek);
}
assertArrayEquals(bytes, data);
}
}
input.close();
dir.close();
}
} }