Skip to content

Commit

Permalink
aaronshan#4, Test Cases for UDFArrayIntersect
Browse files Browse the repository at this point in the history
  • Loading branch information
minhoryang committed Jul 10, 2018
1 parent 61c4bc3 commit 483a969
Showing 1 changed file with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cc.shanruifeng.functions.array;

import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.ArrayList;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.junit.Test;

import static org.junit.Assert.*;


public class UDFArrayIntersectTest {
@Test
public void testArrayIntersect() throws Exception {
UDFArrayIntersect udf = new UDFArrayIntersect();

ObjectInspector arrayOI = ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
ObjectInspector[] arguments = {arrayOI, arrayOI};

udf.initialize(arguments);
List<String> array = ImmutableList.of("a", "b", "c");
DeferredObject arrayObj = new DeferredJavaObject(array);
DeferredObject[] args = {arrayObj, arrayObj};
List output = (List) udf.evaluate(args);

assertEquals("array_intersect() test", array, output);

// Try with null args
DeferredObject[] nullArgs = { new DeferredJavaObject(null), new DeferredJavaObject(null) };
output = (List) udf.evaluate(nullArgs);
assertEquals("array_intersect() test", null, output);

// Try with example args
ArrayList<Integer> arrayA = new ArrayList<Integer>();
arrayA.add(16);
arrayA.add(12);
arrayA.add(18);
arrayA.add(9);
arrayA.add(null);
ArrayList<Integer> arrayB = new ArrayList<Integer>();
arrayB.add(14);
arrayB.add(9);
arrayB.add(6);
arrayB.add(18);
arrayB.add(null);
ArrayList<Integer> expectedAB = new ArrayList<Integer>();
expectedAB.add(null);
//expectedAB.add(9); // TODO: Unexpected Alphabetical Ordering.
expectedAB.add(18);
expectedAB.add(9); // XXX: Unexpected Alphabetical Ordering.
System.out.println("hi");
DeferredObject[] exampleArgs = { new DeferredJavaObject(arrayA), new DeferredJavaObject(arrayB) };
output = (List) udf.evaluate(exampleArgs);
assertEquals("array_intersect() test", expectedAB, output);

// Edge Case 1. Data Missing.
arrayB.add(18);
arrayB.add(18);
DeferredObject[] edgeArgs = { new DeferredJavaObject(arrayA), new DeferredJavaObject(arrayB) };
output = (List) udf.evaluate(edgeArgs);
assertEquals("array_intersect() test", expectedAB, output);

// Edge Case 2. Array IndexOutOfBoundsException.
DeferredObject[] edge2Args = { new DeferredJavaObject(ImmutableList.of(0,1,2,3,4,5)), new DeferredJavaObject(ImmutableList.of(1,1,2,2,5,5)) };
output = (List) udf.evaluate(edge2Args);
assertEquals("array_intersect() test", ImmutableList.of(1,2,5), output);
}
}

0 comments on commit 483a969

Please sign in to comment.