Search in sources :

Example 46 with SketchesArgumentException

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
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) ResizeFactor(org.apache.datasketches.ResizeFactor) PreambleUtil.insertLgResizeFactor(org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor) Test(org.testng.annotations.Test)

Example 47 with SketchesArgumentException

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);
        }
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 48 with SketchesArgumentException

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);
        }
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 49 with SketchesArgumentException

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);
        }
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) SketchesReadOnlyException(org.apache.datasketches.SketchesReadOnlyException) Test(org.testng.annotations.Test)

Example 50 with SketchesArgumentException

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
    }
}
Also used : SketchesArgumentException(org.apache.datasketches.SketchesArgumentException) WritableMemory(org.apache.datasketches.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

SketchesArgumentException (org.apache.datasketches.SketchesArgumentException)64 WritableMemory (org.apache.datasketches.memory.WritableMemory)38 Test (org.testng.annotations.Test)29 Family (org.apache.datasketches.Family)14 Memory (org.apache.datasketches.memory.Memory)13 Family.idToFamily (org.apache.datasketches.Family.idToFamily)10 SketchesReadOnlyException (org.apache.datasketches.SketchesReadOnlyException)7 WritableHandle (org.apache.datasketches.memory.WritableHandle)6 ResizeFactor (org.apache.datasketches.ResizeFactor)5 ArrayOfLongsSerDe (org.apache.datasketches.ArrayOfLongsSerDe)4 AnotbAction (org.apache.datasketches.SetOperationCornerCases.AnotbAction)3 CornerCase (org.apache.datasketches.SetOperationCornerCases.CornerCase)3 PreambleUtil.extractResizeFactor (org.apache.datasketches.sampling.PreambleUtil.extractResizeFactor)3 ArrayList (java.util.ArrayList)2 SketchesStateException (org.apache.datasketches.SketchesStateException)2 PreambleUtil.insertLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor)2 UpdateSketch (org.apache.datasketches.theta.UpdateSketch)2 UpdateSketchBuilder (org.apache.datasketches.theta.UpdateSketchBuilder)2 BigDecimal (java.math.BigDecimal)1 ArrayOfBooleansSerDe (org.apache.datasketches.ArrayOfBooleansSerDe)1