myui commented on a change in pull request #193: [WIP][HIVEMALL-253-2] map_roulette UDF URL: https://github.com/apache/incubator-hivemall/pull/193#discussion_r297036896
########## File path: core/src/test/java/hivemall/tools/map/MapRouletteUDFTest.java ########## @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package hivemall.tools.map; + +import hivemall.TestUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; +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.Assert; +import org.junit.Test; + +/** + * Unit test for {@link hivemall.tools.map.MapRouletteUDF} + */ +public class MapRouletteUDFTest { + + /** + * Tom, Jerry, Amy, Wong, Zhao joined a roulette. Jerry has 0.2 weight to win. Zhao's weight is + * highest, he has more chance to win. During data processing ,Tom 's weight was Lost. Algorithm + * treat Tom 's weight as average. After 1000000 times of roulette, Zhao wins the most. Jerry + * wins less than Zhao but more than the other. + * + * @throws HiveException fmp.initialize may throws UDFArgumentException when checking parameter, + * org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector#getMap(java.lang.Object) + * may throw Hive Exception + */ + @Test + public void testRoulette() throws HiveException { + MapRouletteUDF fmp = new MapRouletteUDF(); + fmp.initialize(new ObjectInspector[] {ObjectInspectorFactory.getStandardMapObjectInspector( + PrimitiveObjectInspectorFactory.javaStringObjectInspector, + PrimitiveObjectInspectorFactory.javaDoubleObjectInspector)}); + Map<Object, Integer> solve = new HashMap<>(); + solve.put("Tom", 0); + solve.put("Jerry", 0); + solve.put("Amy", 0); + solve.put("Wong", 0); + solve.put("Zhao", 0); + int T = 1000000; + while (T-- > 0) { + Map<Object, Double> m = new HashMap<>(); + m.put("Tom", null); + m.put("Jerry", 0.2); + m.put("Amy", 0.1); + m.put("Wong", 0.1); + m.put("Zhao", 0.5); + GenericUDF.DeferredObject[] arguments = + new GenericUDF.DeferredObject[] {new GenericUDF.DeferredJavaObject(m)}; + Object key = fmp.evaluate(arguments); + solve.put(key, solve.get(key) + 1); + } + List<Map.Entry<Object, Integer>> solveList = new ArrayList<>(solve.entrySet()); + Collections.sort(solveList, new KvComparator()); + Object highestSolve = solveList.get(solveList.size() - 1).getKey(); + Assert.assertEquals(highestSolve.toString(), "Zhao"); + Object secondarySolve = solveList.get(solveList.size() - 2).getKey(); + Assert.assertEquals(secondarySolve.toString(), "Jerry"); Review comment: oops `(0.2+0.1+0.1+0.5)/4=0.225`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services