Tuple tup2 = Util.loadNestTuple(TupleFactory.getInstance().newTuple(1),
input);
// System.out.println("Input = " + tup1);
String funcSpec = AVG.class.getName() + "()";
POUserFunc po = new POUserFunc(new OperatorKey("", r.nextLong()), -1,
null, new FuncSpec(funcSpec));
//************ Initial Calculations ******************
TupleFactory tf = TupleFactory.getInstance();
po.setAlgebraicFunction(INIT);
po.attachInput(tup1);
Tuple t = null;
Result res = po.getNextTuple();
Tuple outputInitial1 = (res.returnStatus == POStatus.STATUS_OK) ? (Tuple) res.result
: null;
Tuple outputInitial2 = (res.returnStatus == POStatus.STATUS_OK) ? (Tuple) res.result
: null;
System.out.println(outputInitial1 + " " + outputInitial2);
assertEquals(outputInitial1, outputInitial2);
Double sum = (Double) outputInitial1.get(0);
Long count = (Long) outputInitial1.get(1);
assertEquals(initialExpectedSum, sum);
assertEquals(initialExpectedCount, count);
//************ Intermediate Data and Calculations ******************
DataBag bag = BagFactory.getInstance().newDefaultBag();
bag.add(outputInitial1);
bag.add(outputInitial2);
Tuple outputInitial = tf.newTuple();
outputInitial.append(bag);
// Tuple outputIntermed = intermed.exec(outputInitial);
po = new POUserFunc(new OperatorKey("", r.nextLong()), -1, null,
new FuncSpec(funcSpec));
po.setAlgebraicFunction(INTERMED);
po.attachInput(outputInitial);
res = po.getNextTuple();
Tuple outputIntermed = (res.returnStatus == POStatus.STATUS_OK) ? (Tuple) res.result
: null;
sum = (Double) outputIntermed.get(0);
count = (Long) outputIntermed.get(1);
assertEquals(intermedExpectedSum, sum);
assertEquals(intermedExpectedCount, count);
System.out.println(outputIntermed);
//************ Final Calculations ******************
po = new POUserFunc(new OperatorKey("", r.nextLong()), -1, null,
new FuncSpec(funcSpec));
po.setAlgebraicFunction(FINAL);
po.attachInput(outputInitial);
res = po.getNextTuple();
Double output = (res.returnStatus == POStatus.STATUS_OK) ? (Double) res.result
: null;
// Double output = fin.exec(outputInitial);
assertEquals((Double)expectedAvg, output);
// System.out.println("output = " + output);