add more evilness to compressed streams test

This commit is contained in:
Robert Muir 2014-08-07 18:30:12 -04:00
parent e4d35c458a
commit 59aabf30f8
1 changed files with 66 additions and 6 deletions

View File

@ -40,9 +40,9 @@ import java.util.Random;
public class CompressedStreamTests extends ElasticsearchTestCase { public class CompressedStreamTests extends ElasticsearchTestCase {
public void testRandom() throws IOException { public void testRandom() throws IOException {
for (int i = 0; i < 2000; i++) { Random r = getRandom();
Random r = getRandom(); for (int i = 0; i < 100; i++) {
byte bytes[] = new byte[TestUtil.nextInt(r, 1, 10000)]; byte bytes[] = new byte[TestUtil.nextInt(r, 1, 100000)];
r.nextBytes(bytes); r.nextBytes(bytes);
doTest("lzf", bytes); doTest("lzf", bytes);
} }
@ -51,8 +51,8 @@ public class CompressedStreamTests extends ElasticsearchTestCase {
public void testLineDocs() throws IOException { public void testLineDocs() throws IOException {
Random r = getRandom(); Random r = getRandom();
LineFileDocs lineFileDocs = new LineFileDocs(r); LineFileDocs lineFileDocs = new LineFileDocs(r);
for (int i = 0; i < 200; i++) { for (int i = 0; i < 100; i++) {
int numDocs = TestUtil.nextInt(r, 1, 100); int numDocs = TestUtil.nextInt(r, 1, 200);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int j = 0; j < numDocs; j++) { for (int j = 0; j < numDocs; j++) {
String s = lineFileDocs.nextDoc().get("body"); String s = lineFileDocs.nextDoc().get("body");
@ -63,6 +63,65 @@ public class CompressedStreamTests extends ElasticsearchTestCase {
lineFileDocs.close(); lineFileDocs.close();
} }
public void testRepetitionsL() throws IOException {
Random r = getRandom();
for (int i = 0; i < 1000; i++) {
int numLongs = TestUtil.nextInt(r, 1, 1000);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
long theValue = r.nextLong();
for (int j = 0; j < numLongs; j++) {
if (r.nextInt(10) == 0) {
theValue = r.nextLong();
}
bos.write((byte) (theValue >>> 56));
bos.write((byte) (theValue >>> 48));
bos.write((byte) (theValue >>> 40));
bos.write((byte) (theValue >>> 32));
bos.write((byte) (theValue >>> 24));
bos.write((byte) (theValue >>> 16));
bos.write((byte) (theValue >>> 8));
bos.write((byte) theValue);
}
doTest("lzf", bos.toByteArray());
}
}
public void testRepetitionsI() throws IOException {
Random r = getRandom();
for (int i = 0; i < 1000; i++) {
int numInts = TestUtil.nextInt(r, 1, 1000);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int theValue = r.nextInt();
for (int j = 0; j < numInts; j++) {
if (r.nextInt(10) == 0) {
theValue = r.nextInt();
}
bos.write((byte) (theValue >>> 24));
bos.write((byte) (theValue >>> 16));
bos.write((byte) (theValue >>> 8));
bos.write((byte) theValue);
}
doTest("lzf", bos.toByteArray());
}
}
public void testRepetitionsS() throws IOException {
Random r = getRandom();
for (int i = 0; i < 1000; i++) {
int numShorts = TestUtil.nextInt(r, 1, 1000);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
short theValue = (short) r.nextInt(65535);
for (int j = 0; j < numShorts; j++) {
if (r.nextInt(10) == 0) {
theValue = (short) r.nextInt(65535);
}
bos.write((byte) (theValue >>> 8));
bos.write((byte) theValue);
}
doTest("lzf", bos.toByteArray());
}
}
private void doTest(String compressor, byte bytes[]) throws IOException { private void doTest(String compressor, byte bytes[]) throws IOException {
CompressorFactory.configure(ImmutableSettings.settingsBuilder().put("compress.default.type", compressor).build()); CompressorFactory.configure(ImmutableSettings.settingsBuilder().put("compress.default.type", compressor).build());
ByteBuffer bb = ByteBuffer.wrap(bytes); ByteBuffer bb = ByteBuffer.wrap(bytes);
@ -73,7 +132,8 @@ public class CompressedStreamTests extends ElasticsearchTestCase {
OutputStreamStreamOutput rawOs = new OutputStreamStreamOutput(bos); OutputStreamStreamOutput rawOs = new OutputStreamStreamOutput(bos);
StreamOutput os = c.streamOutput(rawOs); StreamOutput os = c.streamOutput(rawOs);
int bufferSize = TestUtil.nextInt(getRandom(), 1, 2048); Random r = getRandom();
int bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(getRandom(), 1, 70000);
byte buffer[] = new byte[bufferSize]; byte buffer[] = new byte[bufferSize];
int len; int len;
while ((len = rawIn.read(buffer)) != -1) { while ((len = rawIn.read(buffer)) != -1) {