Search in sources :

Example 21 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class ForwardCompatibilityTest method checkSerVer1_32Bytes_tooSmall.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkSerVer1_32Bytes_tooSmall() {
    byte[] byteArray = new byte[32];
    WritableMemory mem = WritableMemory.wrap(byteArray);
    //mdLongs
    mem.putByte(0, (byte) 3);
    //SerVer
    mem.putByte(1, (byte) 1);
    //SketchType = SetSketch
    mem.putByte(2, (byte) 3);
    //byte 3 lgNomLongs not used with SetSketch
    //byte 4 lgArrLongs not used with SetSketch
    //byte 5 lgRR not used with SetSketch
    //byte 6: Flags: b0: BigEnd, b1: ReadOnly
    mem.putByte(6, (byte) 2);
    //byte 7 Not used
    //curCount = 2
    mem.putInt(8, 2);
    mem.putLong(16, Long.MAX_VALUE);
    Memory srcMem = Memory.wrap(byteArray);
    Sketch.heapify(srcMem);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 22 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class HeapQuickSelectSketchTest method checkIllegalSketchID_UpdateSketch.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkIllegalSketchID_UpdateSketch() {
    int k = 512;
    int u = k;
    long seed = DEFAULT_UPDATE_SEED;
    UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setSeed(seed).setNominalEntries(k).build();
    //for internal checks
    HeapQuickSelectSketch sk1 = (HeapQuickSelectSketch) usk;
    assertTrue(usk.isEmpty());
    for (int i = 0; i < u; i++) usk.update(i);
    assertFalse(usk.isEmpty());
    assertEquals(usk.getEstimate(), u, 0.0);
    assertEquals(sk1.getRetainedEntries(false), u);
    byte[] byteArray = usk.toByteArray();
    WritableMemory mem = WritableMemory.wrap(byteArray);
    //corrupt the Sketch ID byte
    mem.putByte(FAMILY_BYTE, (byte) 0);
    //try to heapify the corruped mem
    Sketch.heapify(mem, seed);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 23 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class HeapQuickSelectSketchTest method checkMemSerDeExceptions.

@Test
public void checkMemSerDeExceptions() {
    int k = 1024;
    UpdateSketch sk1 = UpdateSketch.builder().setFamily(QUICKSELECT).setNominalEntries(k).build();
    //forces preLongs to 3
    sk1.update(1L);
    byte[] bytearray1 = sk1.toByteArray();
    WritableMemory mem = WritableMemory.wrap(bytearray1);
    long pre0 = mem.getLong(0);
    //Corrupt PreLongs
    tryBadMem(mem, PREAMBLE_LONGS_BYTE, 2);
    //restore
    mem.putLong(0, pre0);
    //Corrupt SerVer
    tryBadMem(mem, SER_VER_BYTE, 2);
    //restore
    mem.putLong(0, pre0);
    //Corrupt Family
    tryBadMem(mem, FAMILY_BYTE, 1);
    //restore
    mem.putLong(0, pre0);
    //Corrupt READ_ONLY to true
    tryBadMem(mem, FLAGS_BYTE, 2);
    //restore
    mem.putLong(0, pre0);
    //Corrupt, Family to Union
    tryBadMem(mem, FAMILY_BYTE, 4);
    try {
        //Corrupt the theta value
        mem.putDouble(16, 0.5);
        HeapQuickSelectSketch.heapifyInstance(mem, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    //expected
    }
    //restore theta
    mem.putDouble(16, 1.0);
    byte[] byteArray2 = new byte[bytearray1.length - 1];
    WritableMemory mem2 = WritableMemory.wrap(byteArray2);
    mem.copyTo(0, mem2, 0, mem2.getCapacity());
    try {
        HeapQuickSelectSketch.heapifyInstance(mem2, DEFAULT_UPDATE_SEED);
        fail();
    } catch (SketchesArgumentException e) {
    //expected
    }
}
Also used : SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 24 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class HeapQuickSelectSketchTest method checkBadSerVer.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSerVer() {
    int k = 512;
    int u = k;
    long seed = DEFAULT_UPDATE_SEED;
    UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setSeed(seed).setNominalEntries(k).build();
    //for internal checks
    HeapQuickSelectSketch sk1 = (HeapQuickSelectSketch) usk;
    assertTrue(usk.isEmpty());
    for (int i = 0; i < u; i++) sk1.update(i);
    assertFalse(usk.isEmpty());
    assertEquals(usk.getEstimate(), u, 0.0);
    assertEquals(sk1.getRetainedEntries(false), u);
    byte[] byteArray = usk.toByteArray();
    WritableMemory mem = WritableMemory.wrap(byteArray);
    //corrupt the SerVer byte
    mem.putByte(SER_VER_BYTE, (byte) 0);
    Sketch.heapify(mem, seed);
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 25 with WritableMemory

use of com.yahoo.memory.WritableMemory in project sketches-core by DataSketches.

the class HeapQuickSelectSketchTest method checkHQStoCompactEmptyForms.

@Test
public void checkHQStoCompactEmptyForms() {
    int k = 512;
    UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setResizeFactor(X2).setNominalEntries(k).build();
    println("lgArr: " + usk.getLgArrLongs());
    //empty
    usk.toString(false, true, 0, false);
    boolean estimating = false;
    assertEquals(usk.getClass().getSimpleName(), "HeapQuickSelectSketch");
    double uskEst = usk.getEstimate();
    double uskLB = usk.getLowerBound(2);
    double uskUB = usk.getUpperBound(2);
    int currentUSBytes = usk.getCurrentBytes(false);
    // clumsy, but a function of RF and TCF
    assertEquals(currentUSBytes, 32 * 8 + 24);
    //compact form
    int compBytes = usk.getCurrentBytes(true);
    assertEquals(compBytes, 8);
    assertEquals(usk.isEstimationMode(), estimating);
    byte[] arr2 = new byte[compBytes];
    WritableMemory mem2 = WritableMemory.wrap(arr2);
    CompactSketch csk2 = usk.compact(false, mem2);
    assertEquals(csk2.getEstimate(), uskEst);
    assertEquals(csk2.getLowerBound(2), uskLB);
    assertEquals(csk2.getUpperBound(2), uskUB);
    assertEquals(csk2.isEmpty(), true);
    assertEquals(csk2.isEstimationMode(), estimating);
    assertEquals(csk2.getClass().getSimpleName(), "DirectCompactSketch");
    CompactSketch csk3 = usk.compact(true, mem2);
    csk3.toString(false, true, 0, false);
    csk3.toString();
    assertEquals(csk3.getEstimate(), uskEst);
    assertEquals(csk3.getLowerBound(2), uskLB);
    assertEquals(csk3.getUpperBound(2), uskUB);
    assertEquals(csk3.isEmpty(), true);
    assertEquals(csk3.isEstimationMode(), estimating);
    assertEquals(csk3.getClass().getSimpleName(), "DirectCompactOrderedSketch");
}
Also used : WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

WritableMemory (com.yahoo.memory.WritableMemory)264 Test (org.testng.annotations.Test)210 Memory (com.yahoo.memory.Memory)34 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)15 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)11 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)6 WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)5 MemoryRequestServer (com.yahoo.memory.MemoryRequestServer)2 ArrayOfDoublesSerDe (com.yahoo.sketches.ArrayOfDoublesSerDe)2 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)2 PreambleUtil.extractTgtHllType (com.yahoo.sketches.hll.PreambleUtil.extractTgtHllType)2 ResizeFactor (com.yahoo.sketches.ResizeFactor)1 PreambleUtil.extractCurMode (com.yahoo.sketches.hll.PreambleUtil.extractCurMode)1 PreambleUtil.insertTgtHllType (com.yahoo.sketches.hll.PreambleUtil.insertTgtHllType)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1