public void testAVGFinal() throws Exception {
String[] avgTypes = {"AVGFinal", "DoubleAvgFinal", "LongAvgFinal", "IntAvgFinal", "FloatAvgFinal"};
String[] avgIntermediateTypes = {"AVGIntermediate", "DoubleAvgIntermediate", "LongAvgIntermediate", "IntAvgIntermediate", "FloatAvgIntermediate"};
for(int k = 0; k < avgTypes.length; k++) {
EvalFunc<?> avg = evalFuncMap.get(avgTypes[k]);
Tuple tup = inputMap.get(getInputType(avgTypes[k]));
// To test AVGFinal, AVGIntermediate should first be called and
// the output of AVGIntermediate should be supplied as input to
// AVGFinal. To simulate this, we will call Intermediate twice
// on the above tuple and collect the outputs and pass it to
// Final.
// get the right "Intermediate" EvalFunc
EvalFunc<?> avgIntermediate = evalFuncMap.get(avgIntermediateTypes[k]);
// The tuple we got above has a bag with input
// values. Input to the Intermediate.exec() however comes
// from the map which would put each value and a count of
// 1 in a tuple and send it down. So lets create a bag with
// tuples that have two fields - the value and a count 1.
// The input has 10 values - lets put the first five of them
// in the input to the first call of AVGIntermediate and the
// remaining five in the second call.
DataBag bg = (DataBag) tup.get(0);
DataBag bg1 = bagFactory.newDefaultBag();
DataBag bg2 = bagFactory.newDefaultBag();
int i = 0;
for (Tuple t: bg) {
Tuple newTuple = tupleFactory.newTuple(2);
newTuple.set(0, t.get(0));
if ( t.get(0) == null)
newTuple.set(1, new Long(0));
else
newTuple.set(1, new Long(1));
if(i < 5) {
bg1.add(newTuple);
} else {
bg2.add(newTuple);
}
i++;
}
Tuple intermediateInput1 = tupleFactory.newTuple();
intermediateInput1.append(bg1);
Object output1 = avgIntermediate.exec(intermediateInput1);
Tuple intermediateInput2 = tupleFactory.newTuple();
intermediateInput2.append(bg2);
Object output2 = avgIntermediate.exec(intermediateInput2);
DataBag bag = Util.createBag(new Tuple[]{(Tuple)output1, (Tuple)output2});
Tuple finalTuple = TupleFactory.getInstance().newTuple(1);
finalTuple.set(0, bag);
Object output = avg.exec(finalTuple);
String msg = "[Testing " + avgTypes[k] + " on input type: " + getInputType(avgTypes[k]) + " ( (output) " +
output + " == " + getExpected(avgTypes[k]) + " (expected) )]";
assertEquals(msg, (Double)getExpected(avgTypes[k]), (Double)output, 0.00001);
}