Search in sources :

Example 1 with ArrayOfNumbersSerDe

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

the class ReservoirItemsSketchTest method checkPolymorphicType.

@Test
public void checkPolymorphicType() {
    final ReservoirItemsSketch<Number> ris = ReservoirItemsSketch.newInstance(6);
    assertNull(ris.getSamples());
    assertNull(ris.getSamples(Number.class));
    // using mixed types
    ris.update(1);
    ris.update(2L);
    ris.update(3.0);
    ris.update((short) (44023 & 0xFFFF));
    ris.update((byte) (68 & 0xFF));
    ris.update(4.0F);
    final Number[] data = ris.getSamples(Number.class);
    assertNotNull(data);
    assertEquals(data.length, 6);
    // copying samples without specifying Number.class should fail
    try {
        ris.getSamples();
        fail();
    } catch (final ArrayStoreException e) {
    // expected
    }
    // likewise for toByteArray() (which uses getDataSamples() internally for type handling)
    final ArrayOfNumbersSerDe serDe = new ArrayOfNumbersSerDe();
    try {
        ris.toByteArray(serDe);
        fail();
    } catch (final ArrayStoreException e) {
    // expected
    }
    final byte[] sketchBytes = ris.toByteArray(serDe, Number.class);
    assertEquals(sketchBytes.length, 49);
    final Memory mem = Memory.wrap(sketchBytes);
    final ReservoirItemsSketch<Number> loadedRis = ReservoirItemsSketch.heapify(mem, serDe);
    assertEquals(ris.getNumSamples(), loadedRis.getNumSamples());
    final Number[] samples1 = ris.getSamples(Number.class);
    final Number[] samples2 = loadedRis.getSamples(Number.class);
    assertNotNull(samples1);
    assertNotNull(samples2);
    assertEquals(samples1.length, samples2.length);
    for (int i = 0; i < samples1.length; ++i) {
        assertEquals(samples1[i], samples2[i]);
    }
}
Also used : ArrayOfNumbersSerDe(com.yahoo.sketches.ArrayOfNumbersSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 2 with ArrayOfNumbersSerDe

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

the class ReservoirItemsSketchTest method checkArrayOfNumbersSerDeErrors.

@Test
public void checkArrayOfNumbersSerDeErrors() {
    // Highly debatable whether this belongs here vs a stand-alone test class
    final ReservoirItemsSketch<Number> ris = ReservoirItemsSketch.newInstance(6);
    assertNull(ris.getSamples());
    assertNull(ris.getSamples(Number.class));
    // using mixed types, but BigDecimal not supported by serde class
    ris.update(1);
    ris.update(new BigDecimal(2));
    // this should work since BigDecimal is an instance of Number
    final Number[] data = ris.getSamples(Number.class);
    assertNotNull(data);
    assertEquals(data.length, 2);
    // toByteArray() should fail
    final ArrayOfNumbersSerDe serDe = new ArrayOfNumbersSerDe();
    try {
        ris.toByteArray(serDe, Number.class);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
    // force entry to a supported type
    data[1] = 3.0;
    final byte[] bytes = serDe.serializeToByteArray(data);
    // change first element to indicate something unsupported
    bytes[0] = 'q';
    try {
        serDe.deserializeFromMemory(Memory.wrap(bytes), 2);
        fail();
    } catch (final SketchesArgumentException e) {
    // expected
    }
}
Also used : ArrayOfNumbersSerDe(com.yahoo.sketches.ArrayOfNumbersSerDe) SketchesArgumentException(com.yahoo.sketches.SketchesArgumentException) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 3 with ArrayOfNumbersSerDe

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

the class ReservoirItemsUnionTest method checkVersionConversionWithGadget.

@Test
public void checkVersionConversionWithGadget() {
    final long n = 32;
    final int k = 256;
    final short encK = ReservoirSize.computeSize(k);
    final ArrayOfNumbersSerDe serDe = new ArrayOfNumbersSerDe();
    final ReservoirItemsUnion<Number> rlu = ReservoirItemsUnion.newInstance(k);
    for (long i = 0; i < n; ++i) {
        rlu.update(i);
    }
    final byte[] unionBytesOrig = rlu.toByteArray(serDe);
    // get a new byte[], manually revert to v1, then reconstruct
    final byte[] unionBytes = rlu.toByteArray(serDe);
    final WritableMemory unionMem = WritableMemory.wrap(unionBytes);
    unionMem.putByte(SER_VER_BYTE, (byte) 1);
    // zero out all 4 bytes
    unionMem.putInt(RESERVOIR_SIZE_INT, 0);
    unionMem.putShort(RESERVOIR_SIZE_SHORT, encK);
    // force gadget header to v1, too
    final int offset = Family.RESERVOIR_UNION.getMaxPreLongs() << 3;
    unionMem.putByte(offset + SER_VER_BYTE, (byte) 1);
    // zero out all 4 bytes
    unionMem.putInt(offset + RESERVOIR_SIZE_INT, 0);
    unionMem.putShort(offset + RESERVOIR_SIZE_SHORT, encK);
    final ReservoirItemsUnion<Number> rebuilt = ReservoirItemsUnion.heapify(unionMem, serDe);
    final byte[] rebuiltBytes = rebuilt.toByteArray(serDe);
    assertEquals(unionBytesOrig.length, rebuiltBytes.length);
    for (int i = 0; i < unionBytesOrig.length; ++i) {
        assertEquals(unionBytesOrig[i], rebuiltBytes[i]);
    }
}
Also used : ArrayOfNumbersSerDe(com.yahoo.sketches.ArrayOfNumbersSerDe) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 4 with ArrayOfNumbersSerDe

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

the class ReservoirItemsUnionTest method checkPolymorphicType.

@Test
public void checkPolymorphicType() {
    final int k = 4;
    final ReservoirItemsUnion<Number> riu = ReservoirItemsUnion.newInstance(k);
    riu.update(2.2);
    riu.update(6L);
    final ReservoirItemsSketch<Number> ris = ReservoirItemsSketch.newInstance(k);
    ris.update(1);
    ris.update(3.7f);
    riu.update(ris);
    final ArrayOfNumbersSerDe serDe = new ArrayOfNumbersSerDe();
    final byte[] sketchBytes = riu.toByteArray(serDe, Number.class);
    final Memory mem = Memory.wrap(sketchBytes);
    final ReservoirItemsUnion<Number> rebuiltRiu = ReservoirItemsUnion.heapify(mem, serDe);
    // validateReservoirEquality can't handle abstract base class
    assertNotNull(riu.getResult());
    assertNotNull(rebuiltRiu.getResult());
    assertEquals(riu.getResult().getNumSamples(), rebuiltRiu.getResult().getNumSamples());
    final Number[] samples1 = riu.getResult().getSamples(Number.class);
    final Number[] samples2 = rebuiltRiu.getResult().getSamples(Number.class);
    assertNotNull(samples1);
    assertNotNull(samples2);
    assertEquals(samples1.length, samples2.length);
    for (int i = 0; i < samples1.length; ++i) {
        assertEquals(samples1[i], samples2[i]);
    }
}
Also used : ArrayOfNumbersSerDe(com.yahoo.sketches.ArrayOfNumbersSerDe) Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 5 with ArrayOfNumbersSerDe

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

the class ReservoirItemsUnionTest method checkBadPreLongs.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadPreLongs() {
    final ReservoirItemsUnion<Number> riu = ReservoirItemsUnion.newInstance(1024);
    final WritableMemory mem = WritableMemory.wrap(riu.toByteArray(new ArrayOfNumbersSerDe()));
    // corrupt the preLongs count
    mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 0);
    ReservoirItemsUnion.heapify(mem, new ArrayOfNumbersSerDe());
    fail();
}
Also used : ArrayOfNumbersSerDe(com.yahoo.sketches.ArrayOfNumbersSerDe) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)5 Test (org.testng.annotations.Test)5 WritableMemory (com.yahoo.memory.WritableMemory)4 Memory (com.yahoo.memory.Memory)2 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)1 BigDecimal (java.math.BigDecimal)1