use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.
the class SPLOperatorsTest method testOpParams.
/**
* Test we can invoke an SPL operator with various parameter types.
*/
private void testOpParams(String testName, OpParamAdder opParamAdder) throws Exception {
Topology topology = new Topology(testName);
opParamAdder.init(topology, getConfig());
// getConfig().put(ContextProperties.KEEP_ARTIFACTS, true);
StreamSchema schema = Type.Factory.getStreamSchema("tuple<" + "rstring r" + ", ustring u" + ", boolean b" + ", int8 i8, int16 i16, int32 i32, int64 i64" + ", uint8 ui8, uint16 ui16, uint32 ui32, uint64 ui64" + ", float32 f32, float64 f64" + " >");
Random rand = new Random();
String r = "test X\tY\"Lit\nerals\\nX\\tY " + rand.nextInt();
opParamAdder.put("r", r);
String u = "test X\tY\"Lit\nerals\\nX\\tY " + rand.nextInt();
opParamAdder.put("u", SPL.createValue(u, MetaType.USTRING));
boolean b = rand.nextBoolean();
opParamAdder.put("b", b);
byte i8 = (byte) rand.nextInt();
short i16 = (short) rand.nextInt();
int i32 = rand.nextInt();
long i64 = rand.nextLong();
opParamAdder.put("i8", i8);
opParamAdder.put("i16", i16);
opParamAdder.put("i32", i32);
opParamAdder.put("i64", i64);
// 255 => -1
byte ui8 = (byte) 0xFF;
// 65534 => -2
short ui16 = (short) 0xFFFE;
// 4294967293 => -3
int ui32 = 0xFFFFFFFD;
// 18446744073709551612 => -4
long ui64 = 0xFFFFFFFFFFFFFFFCL;
opParamAdder.put("ui8", SPL.createValue(ui8, MetaType.UINT8));
opParamAdder.put("ui16", SPL.createValue(ui16, MetaType.UINT16));
opParamAdder.put("ui32", SPL.createValue(ui32, MetaType.UINT32));
opParamAdder.put("ui64", SPL.createValue(ui64, MetaType.UINT64));
float f32 = rand.nextFloat();
double f64 = rand.nextDouble();
opParamAdder.put("f32", f32);
opParamAdder.put("f64", f64);
SPL.addToolkit(topology, new File(getTestRoot(), "spl/testtk"));
SPLStream paramTuple = SPL.invokeSource(topology, "testgen::TypeLiteralTester", opParamAdder.getParams(), schema);
// paramTuple.print();
// paramTuple.filter(new AllowAll<Tuple>());
Tester tester = topology.getTester();
Condition<Long> expectedCount = tester.tupleCount(paramTuple, 1);
MostRecent<Tuple> mr = tester.splHandler(paramTuple, new MostRecent<Tuple>());
// getConfig().put(ContextProperties.KEEP_ARTIFACTS, true);
complete(tester, expectedCount, 10, TimeUnit.SECONDS);
assertTrue(expectedCount.toString(), expectedCount.valid());
Tuple tuple = mr.getMostRecentTuple();
// System.out.println("tuple: " + tuple);
assertEquals(r, tuple.getString("r"));
assertEquals(u, tuple.getString("u"));
assertEquals(i8, tuple.getByte("i8"));
assertEquals(i16, tuple.getShort("i16"));
assertEquals(i32, tuple.getInt("i32"));
assertEquals(i64, tuple.getLong("i64"));
assertEquals(ui8, tuple.getByte("ui8"));
assertEquals(ui16, tuple.getShort("ui16"));
assertEquals(ui32, tuple.getInt("ui32"));
assertEquals(ui64, tuple.getLong("ui64"));
assertEquals(f32, tuple.getFloat("f32"), 0.001);
assertEquals(f64, tuple.getDouble("f64"), 0.001);
}
use of com.ibm.streams.operator.Tuple in project streamsx.kafka by IBMStreams.
the class KafkaSPLStreamsUtils method stringContentsUnordered.
public static Condition<List<String>> stringContentsUnordered(Tester tester, SPLStream stream, String... values) {
final List<String> sortedValues = Arrays.asList(values);
Collections.sort(sortedValues);
final StreamCollector<LinkedList<Tuple>, Tuple> tuples = StreamCollector.newLinkedListCollector();
tester.splHandler(stream, tuples);
return new Condition<List<String>>() {
@Override
public List<String> getResult() {
List<String> strings = new ArrayList<>(tuples.getTupleCount());
synchronized (tuples.getTuples()) {
for (Tuple tuple : tuples.getTuples()) {
strings.add(tuple.getString(0));
}
}
return strings;
}
@Override
public boolean valid() {
List<String> strings = getResult();
if (strings.size() != sortedValues.size())
return false;
Collections.sort(strings);
return sortedValues.equals(strings);
}
@Override
public String toString() {
return "Received Tuples: " + getResult();
}
};
}
Aggregations