| /* |
| * 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.TupleFactory; |
| |
| import org.testng.annotations.Test; |
| import org.testng.Assert; |
| |
| public class GetQuantileFromStringsSketchTest { |
| |
| 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<String> func = new GetQuantileFromStringsSketch(); |
| ItemsSketch<String> sketch = ItemsSketch.getInstance(COMPARATOR); |
| String result = func.exec(TUPLE_FACTORY.newTuple(Arrays.asList(new DataByteArray(sketch.toByteArray(SER_DE)), 0.0))); |
| Assert.assertNull(result); |
| } |
| |
| @Test |
| public void normalCase() throws Exception { |
| EvalFunc<String> func = new GetQuantileFromStringsSketch(); |
| ItemsSketch<String> sketch = ItemsSketch.getInstance(COMPARATOR); |
| sketch.update("a"); |
| String result = func.exec(TUPLE_FACTORY.newTuple(Arrays.asList(new DataByteArray(sketch.toByteArray(SER_DE)), 0.5))); |
| Assert.assertEquals(result, "a"); |
| } |
| |
| @Test(expectedExceptions = IllegalArgumentException.class) |
| public void wrongNumberOfInputs() throws Exception { |
| EvalFunc<String> func = new GetQuantileFromStringsSketch(); |
| func.exec(TUPLE_FACTORY.newTuple(1)); |
| } |
| |
| @Test(expectedExceptions = IllegalArgumentException.class) |
| public void wrongTypeForSketch() throws Exception { |
| EvalFunc<String> func = new GetQuantileFromStringsSketch(); |
| func.exec(TUPLE_FACTORY.newTuple(Arrays.asList(1.0, 1.0))); |
| } |
| |
| @Test(expectedExceptions = IllegalArgumentException.class) |
| public void wrongTypeForFraction() throws Exception { |
| EvalFunc<String> func = new GetQuantileFromStringsSketch(); |
| ItemsSketch<String> sketch = ItemsSketch.getInstance(COMPARATOR); |
| func.exec(TUPLE_FACTORY.newTuple(Arrays.asList(new DataByteArray(sketch.toByteArray(SER_DE)), 1))); |
| } |
| } |