| /* |
| * Copyright 2016, Yahoo! Inc. |
| * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms. |
| */ |
| package com.yahoo.sketches.pig.quantiles; |
| |
| import com.yahoo.sketches.ArrayOfItemsSerDe; |
| import com.yahoo.sketches.ArrayOfStringsSerDe; |
| import com.yahoo.sketches.quantiles.ItemsSketch; |
| |
| import java.util.Arrays; |
| import java.util.Comparator; |
| |
| import org.apache.pig.EvalFunc; |
| import org.apache.pig.data.DataByteArray; |
| import org.apache.pig.data.Tuple; |
| import org.apache.pig.data.TupleFactory; |
| |
| import org.testng.annotations.Test; |
| import org.testng.Assert; |
| |
| public class GetPmfFromStringsSketchTest { |
| private static final TupleFactory TUPLE_FACTORY = TupleFactory.getInstance(); |
| |
| private static final Comparator<String> COMPARATOR = Comparator.naturalOrder(); |
| private static final ArrayOfItemsSerDe<String> SER_DE = new ArrayOfStringsSerDe(); |
| |
| @Test |
| public void emptySketch() throws Exception { |
| EvalFunc<Tuple> func = new GetPmfFromStringsSketch(); |
| ItemsSketch<String> sketch = ItemsSketch.getInstance(COMPARATOR); |
| Tuple resultTuple = func.exec(TUPLE_FACTORY.newTuple(Arrays.asList(new DataByteArray(sketch.toByteArray(SER_DE)), "a"))); |
| Assert.assertNotNull(resultTuple); |
| Assert.assertEquals(resultTuple.size(), 2); |
| Assert.assertEquals(((double) resultTuple.get(0)), Double.NaN); |
| Assert.assertEquals(((double) resultTuple.get(1)), Double.NaN); |
| } |
| |
| @Test |
| public void normalCase() throws Exception { |
| EvalFunc<Tuple> func = new GetPmfFromStringsSketch(); |
| ItemsSketch<String> sketch = ItemsSketch.getInstance(COMPARATOR); |
| for (int i = 1; i <= 10; i++) sketch.update(String.format("%02d", i)); |
| Tuple resultTuple = func.exec(TUPLE_FACTORY.newTuple(Arrays.asList(new DataByteArray(sketch.toByteArray(SER_DE)), "02", "07"))); |
| Assert.assertNotNull(resultTuple); |
| Assert.assertEquals(resultTuple.size(), 3); |
| Assert.assertEquals(((double) resultTuple.get(0)), 0.1); |
| Assert.assertEquals(((double) resultTuple.get(1)), 0.5); |
| Assert.assertEquals(((double) resultTuple.get(2)), 0.4); |
| } |
| |
| @Test(expectedExceptions = IllegalArgumentException.class) |
| public void wrongNumberOfInputs() throws Exception { |
| EvalFunc<Tuple> func = new GetPmfFromStringsSketch(); |
| func.exec(TUPLE_FACTORY.newTuple(1)); |
| } |
| |
| @Test(expectedExceptions = IllegalArgumentException.class) |
| public void wrongTypeForSketch() throws Exception { |
| EvalFunc<Tuple> func = new GetPmfFromStringsSketch(); |
| func.exec(TUPLE_FACTORY.newTuple(Arrays.asList(1.0, 1.0))); |
| } |
| |
| @Test(expectedExceptions = IllegalArgumentException.class) |
| public void wrongTypeOfSplitPoint() throws Exception { |
| EvalFunc<Tuple> func = new GetPmfFromStringsSketch(); |
| ItemsSketch<String> sketch = ItemsSketch.getInstance(COMPARATOR); |
| func.exec(TUPLE_FACTORY.newTuple(Arrays.asList(new DataByteArray(sketch.toByteArray(SER_DE)), 1))); |
| } |
| } |