}
//test null value in input
input.append(null);
Set<Integer> s = new HashSet<Integer>();
DataBag db = tb.exec(input);
for (Tuple t : db) {
s.add((Integer) t.get(0));
}
// finally check the bag had everything we put in the tuple.
assertEquals(101, s.size());
for (int i = 0; i < 100; ++i) {
assertTrue(s.contains(i));
}
assertTrue("null in tobag result", s.contains(null));
TOTUPLE tt = new TOTUPLE();
input = TupleFactory.getInstance().newTuple();
for (int i = 0; i < 100; ++i) {
input.append(i);
}
Tuple output = tt.exec(input);
assertTrue(!(input == output));
assertEquals(input, output);
TOP top = new TOP();
TupleFactory tupleFactory = TupleFactory.getInstance();
BagFactory bagFactory = DefaultBagFactory.getInstance();
Tuple inputTuple = tupleFactory.newTuple(3);
DataBag dBag = bagFactory.newDefaultBag();
// set N = 10 i.e retain top 10 tuples
inputTuple.set(0, 10);
// compare tuples by field number 1
inputTuple.set(1, 1);
// set the data bag containing the tuples
inputTuple.set(2, dBag);
// generate tuples of the form (group-1, 1), (group-2, 2) ...
for (long i = 0; i < 100; i++) {
Tuple nestedTuple = tupleFactory.newTuple(2);
nestedTuple.set(0, "group-" + i);
nestedTuple.set(1, i);
dBag.add(nestedTuple);
}
DataBag outBag = top.exec(inputTuple);
assertEquals(outBag.size(), 10L);
checkItemsGT(outBag, 1, 89);
// two initial results
Tuple init1 = (new TOP.Initial()).exec(inputTuple);
Tuple init2 = (new TOP.Initial()).exec(inputTuple);
// two intermediate results
DataBag intermedBag = bagFactory.newDefaultBag();
intermedBag.add(init1);
intermedBag.add(init2);
Tuple intermedInput = tupleFactory.newTuple(intermedBag);
Tuple intermedOutput1 = (new TOP.Intermed()).exec(intermedInput);
Tuple intermedOutput2 = (new TOP.Intermed()).exec(intermedInput);
checkItemsGT((DataBag)intermedOutput1.get(2), 1, 94);
// final result
DataBag finalInputBag = bagFactory.newDefaultBag();
finalInputBag.add(intermedOutput1);
finalInputBag.add(intermedOutput2);
Tuple finalInput = tupleFactory.newTuple(finalInputBag);
outBag = (new TOP.Final()).exec(finalInput);
assertEquals(outBag.size(), 10L);
checkItemsGT(outBag, 1, 96);
}