use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkConstructorSrcMemCorruptions.
@Test
public void checkConstructorSrcMemCorruptions() {
// lgNomLongs = 10
int k = 1024;
// exact mode, lgArrLongs = 11
int u = k;
int bytes = Sketches.getMaxUpdateSketchBytes(k);
byte[] arr1 = new byte[bytes];
WritableMemory mem1 = WritableMemory.writableWrap(arr1);
// 0
ResizeFactor rf = ResizeFactor.X1;
UpdateSketch usk1 = UpdateSketch.builder().setNominalEntries(k).setResizeFactor(rf).build(mem1);
for (int i = 0; i < u; i++) {
usk1.update(i);
}
// println(PreambleUtil.toString(mem1));
@SuppressWarnings("unused") UpdateSketch usk2;
// corrupt Family by setting to Compact
mem1.putByte(FAMILY_BYTE, (byte) 3);
try {
usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
fail("Expected SketchesArgumentException");
} catch (SketchesArgumentException e) {
// Pass
}
// fix Family
mem1.putByte(FAMILY_BYTE, (byte) 2);
// corrupt preLongs
mem1.putByte(PREAMBLE_LONGS_BYTE, (byte) 1);
try {
usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
fail("Expected SketchesArgumentException");
} catch (SketchesArgumentException e) {
// pass
}
// fix preLongs
mem1.putByte(PREAMBLE_LONGS_BYTE, (byte) 3);
// corrupt serVer
mem1.putByte(SER_VER_BYTE, (byte) 2);
try {
usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
fail("Expected SketchesArgumentException");
} catch (SketchesArgumentException e) {
// pass
}
// fix serVer
mem1.putByte(SER_VER_BYTE, (byte) 3);
// corrupt theta and
mem1.putLong(THETA_LONG, Long.MAX_VALUE >>> 1);
// corrupt lgArrLongs
mem1.putByte(LG_ARR_LONGS_BYTE, (byte) 10);
try {
usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
fail("Expected SketchesArgumentException");
} catch (SketchesArgumentException e) {
// pass
}
// fix theta and
mem1.putLong(THETA_LONG, Long.MAX_VALUE);
// fix lgArrLongs
mem1.putByte(LG_ARR_LONGS_BYTE, (byte) 11);
byte badFlags = (byte) (BIG_ENDIAN_FLAG_MASK | COMPACT_FLAG_MASK | READ_ONLY_FLAG_MASK | ORDERED_FLAG_MASK);
mem1.putByte(FLAGS_BYTE, badFlags);
try {
usk2 = DirectQuickSelectSketch.writableWrap(mem1, DEFAULT_UPDATE_SEED);
fail("Expected SketchesArgumentException");
} catch (SketchesArgumentException e) {
// pass
}
// corrupt length
byte[] arr2 = Arrays.copyOfRange(arr1, 0, bytes - 1);
WritableMemory mem2 = WritableMemory.writableWrap(arr2);
try {
usk2 = DirectQuickSelectSketch.writableWrap(mem2, DEFAULT_UPDATE_SEED);
fail("Expected SketchesArgumentException");
} catch (SketchesArgumentException e) {
// pass
}
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkHeapifySeedConflict.
// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkHeapifySeedConflict() {
int k = 512;
long seed1 = 1021;
long seed2 = DEFAULT_UPDATE_SEED;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch usk = UpdateSketch.builder().setSeed(seed1).setNominalEntries(k).build(mem);
byte[] byteArray = usk.toByteArray();
Memory srcMem = Memory.wrap(byteArray);
Sketch.heapify(srcMem, seed2);
} catch (final Exception e) {
if (e instanceof SketchesArgumentException) {
} else {
throw new RuntimeException(e);
}
}
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkCorruptLgNomLongs.
// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkCorruptLgNomLongs() {
int k = 16;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch.builder().setNominalEntries(k).build(mem);
// corrupt
mem.putByte(LG_NOM_LONGS_BYTE, (byte) 2);
Sketch.heapify(mem, DEFAULT_UPDATE_SEED);
} catch (final Exception e) {
if (e instanceof SketchesArgumentException) {
} else {
throw new RuntimeException(e);
}
}
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class DirectQuickSelectSketchTest method checkConstructorKtooSmall.
// (expectedExceptions = SketchesArgumentException.class)
@Test
public void checkConstructorKtooSmall() {
int k = 8;
try (WritableHandle h = makeNativeMemory(k)) {
WritableMemory mem = h.getWritable();
UpdateSketch.builder().setNominalEntries(k).build(mem);
} catch (final Exception e) {
if (e instanceof SketchesArgumentException) {
} else {
throw new RuntimeException(e);
}
}
}
use of org.apache.datasketches.SketchesArgumentException in project sketches-core by DataSketches.
the class HllSketchTest method checkMemoryNotLargeEnough.
@SuppressWarnings("unused")
@Test
public void checkMemoryNotLargeEnough() {
int bytes = getMaxUpdatableSerializationBytes(8, TgtHllType.HLL_8);
WritableMemory wmem = WritableMemory.allocate(bytes - 1);
try {
HllSketch sk = new HllSketch(8, TgtHllType.HLL_8, wmem);
fail();
} catch (SketchesArgumentException e) {
// OK
}
}
Aggregations