@Test
public void testCast() throws ExecException {
POCast cast = new POCast(newOperatorKey(), -1);
POProject proj = new POProject(newOperatorKey(), -1, 0);
proj.setResultType(DataType.CHARARRAY);
List<PhysicalOperator> inputs = new ArrayList<PhysicalOperator>();
inputs.add(proj);
cast.setInputs(inputs);
// cast to double
String[] items = { "12.0", "-13.2", "0.1f", "1.3e2", "zjf",
MaxDouble.toString(), MinDouble.toString() };
Double[] doubleExpected = { 12.0, -13.2, 0.1, 1.3e2, null, MaxDouble,
MinDouble };
for (int i = 0; i < items.length; ++i) {
Tuple tuple = TupleFactory.getInstance().newTuple(1);
tuple.set(0, items[i]);
proj.attachInput(tuple);
Double actual = (Double) cast.getNext(dummyDouble).result;
if (doubleExpected[i] != null) {
assertEquals(doubleExpected[i], actual, 1e-6);
} else {
assertNull(actual);
}
}
// cast to float
items = new String[] { "12.0", "-13.2", "0.1f", "1.3e2",
MaxFloat.toString(), MinFloat.toString(), "zjf" };
Float[] floatExpected = { 12.0f, -13.2f, 0.1f, 1.3e2f, MaxFloat,
MinFloat, null };
for (int i = 0; i < items.length; ++i) {
Tuple tuple = TupleFactory.getInstance().newTuple(1);
tuple.set(0, items[i]);
proj.attachInput(tuple);
Float actual = (Float) cast.getNext(dummyFloat).result;
if (floatExpected[i] != null) {
assertEquals(floatExpected[i], actual, 1e-6);
} else {
assertNull(actual);
}
}
// cast to long
items = new String[] { "1", "-1", "12.2", "12.8", MaxLong.toString(),
MinLong.toString(), "df1.2" };
Long[] longExpected = { 1L, -1L, 12L, 12L, MaxLong, MinLong, null };
for (int i = 0; i < items.length; ++i) {
Tuple tuple = TupleFactory.getInstance().newTuple(1);
tuple.set(0, items[i]);
proj.attachInput(tuple);
Long actual = (Long) cast.getNext(dummyLong).result;
if (longExpected[i] != null) {
assertEquals(longExpected[i], actual);
} else {
assertNull(actual);
}
}
// cast to int
items = new String[] { "1", "-1", "12.2", "12.8",
MaxInteger.toString(), MinInteger.toString(), "ff4332" };
Integer[] intExpected = { 1, -1, 12, 12, MaxInteger, MinInteger, null };
for (int i = 0; i < items.length; ++i) {
Tuple tuple = TupleFactory.getInstance().newTuple(1);
tuple.set(0, items[i]);
proj.attachInput(tuple);
Integer actual = (Integer) cast.getNext(dummyInteger).result;
if (intExpected[i] != null) {
assertEquals(intExpected[i], actual);
} else {
assertNull(actual);