Search in sources :

Example 16 with CountAndLastTupleTestSink

use of org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink in project apex-malhar by apache.

the class MostFrequentKeyMapTest method testNodeProcessing.

/**
 * Test node logic emits correct results
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeProcessing() throws Exception {
    MostFrequentKeyMap<String, Integer> oper = new MostFrequentKeyMap<String, Integer>();
    CountAndLastTupleTestSink matchSink = new CountAndLastTupleTestSink();
    CountAndLastTupleTestSink listSink = new CountAndLastTupleTestSink();
    oper.most.setSink(matchSink);
    oper.list.setSink(listSink);
    oper.beginWindow(0);
    HashMap<String, Integer> amap = new HashMap<String, Integer>(1);
    HashMap<String, Integer> bmap = new HashMap<String, Integer>(1);
    HashMap<String, Integer> cmap = new HashMap<String, Integer>(1);
    int atot = 5;
    int btot = 7;
    int ctot = 6;
    amap.put("a", null);
    bmap.put("b", null);
    cmap.put("c", null);
    for (int i = 0; i < atot; i++) {
        oper.data.process(amap);
    }
    for (int i = 0; i < btot; i++) {
        oper.data.process(bmap);
    }
    for (int i = 0; i < ctot; i++) {
        oper.data.process(cmap);
    }
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    HashMap<String, Integer> tuple = (HashMap<String, Integer>) matchSink.tuple;
    Integer val = tuple.get("b");
    Assert.assertEquals("Count of b was ", btot, val.intValue());
    Assert.assertEquals("number emitted tuples", 1, listSink.count);
    ArrayList<HashMap<String, Integer>> list = (ArrayList<HashMap<String, Integer>>) listSink.tuple;
    val = list.get(0).get("b");
    Assert.assertEquals("Count of b was ", btot, val.intValue());
    matchSink.clear();
    listSink.clear();
    oper.beginWindow(0);
    atot = 5;
    btot = 4;
    ctot = 5;
    for (int i = 0; i < atot; i++) {
        oper.data.process(amap);
    }
    for (int i = 0; i < btot; i++) {
        oper.data.process(bmap);
    }
    for (int i = 0; i < ctot; i++) {
        oper.data.process(cmap);
    }
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, matchSink.count);
    Assert.assertEquals("number emitted tuples", 1, listSink.count);
    list = (ArrayList<HashMap<String, Integer>>) listSink.tuple;
    int acount = 0;
    int ccount = 0;
    for (HashMap<String, Integer> h : list) {
        val = h.get("a");
        if (val == null) {
            ccount = h.get("c");
        } else {
            acount = val;
        }
    }
    Assert.assertEquals("Count of a was ", atot, acount);
    Assert.assertEquals("Count of c was ", ctot, ccount);
    HashMap<String, Integer> mtuple = (HashMap<String, Integer>) matchSink.tuple;
    val = mtuple.get("a");
    if (val == null) {
        val = mtuple.get("c");
    }
    Assert.assertEquals("Count of least frequent key was ", ctot, val.intValue());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Test(org.junit.Test)

Example 17 with CountAndLastTupleTestSink

use of org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink in project apex-malhar by apache.

the class CompareExceptMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testNodeProcessingSchema(CompareExceptMap oper) {
    CountAndLastTupleTestSink compareSink = new CountAndLastTupleTestSink();
    CountAndLastTupleTestSink exceptSink = new CountAndLastTupleTestSink();
    oper.compare.setSink(compareSink);
    oper.except.setSink(exceptSink);
    oper.setKey("a");
    oper.setValue(3.0);
    oper.setTypeEQ();
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    input.put("a", 2);
    input.put("b", 20);
    input.put("c", 1000);
    oper.data.process(input);
    input.clear();
    input.put("a", 3);
    input.put("b", 21);
    input.put("c", 30);
    oper.data.process(input);
    oper.endWindow();
    // One for each key
    Assert.assertEquals("number emitted tuples", 1, exceptSink.count);
    for (Map.Entry<String, Number> e : ((HashMap<String, Number>) exceptSink.tuple).entrySet()) {
        if (e.getKey().equals("a")) {
            Assert.assertEquals("emitted value for 'a' was ", new Double(2), e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("b")) {
            Assert.assertEquals("emitted tuple for 'b' was ", new Double(20), e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("c")) {
            Assert.assertEquals("emitted tuple for 'c' was ", new Double(1000), e.getValue().doubleValue(), 0);
        }
    }
    Assert.assertEquals("number emitted tuples", 1, compareSink.count);
    for (Map.Entry<String, Number> e : ((HashMap<String, Number>) compareSink.tuple).entrySet()) {
        if (e.getKey().equals("a")) {
            Assert.assertEquals("emitted value for 'a' was ", new Double(3), e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("b")) {
            Assert.assertEquals("emitted tuple for 'b' was ", new Double(21), e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("c")) {
            Assert.assertEquals("emitted tuple for 'c' was ", new Double(30), e.getValue().doubleValue(), 0);
        }
    }
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with CountAndLastTupleTestSink

use of org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink in project apex-malhar by apache.

the class MarginMapTest method testNodeProcessingSchema.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void testNodeProcessingSchema(MarginMap oper) {
    CountAndLastTupleTestSink marginSink = new CountAndLastTupleTestSink();
    oper.margin.setSink(marginSink);
    oper.beginWindow(0);
    HashMap<String, Number> input = new HashMap<String, Number>();
    input.put("a", 2);
    input.put("b", 20);
    input.put("c", 1000);
    oper.numerator.process(input);
    input.clear();
    input.put("a", 2);
    input.put("b", 40);
    input.put("c", 500);
    oper.denominator.process(input);
    oper.endWindow();
    // One for each key
    Assert.assertEquals("number emitted tuples", 1, marginSink.count);
    HashMap<String, Number> output = (HashMap<String, Number>) marginSink.tuple;
    for (Map.Entry<String, Number> e : output.entrySet()) {
        LOG.debug(String.format("Key, value is %s,%f", e.getKey(), e.getValue().doubleValue()));
        if (e.getKey().equals("a")) {
            Assert.assertEquals("emitted value for 'a' was ", 0d, e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("b")) {
            Assert.assertEquals("emitted tuple for 'b' was ", 0.5, e.getValue().doubleValue(), 0);
        } else if (e.getKey().equals("c")) {
            Assert.assertEquals("emitted tuple for 'c' was ", -1.0, e.getValue().doubleValue(), 0);
        } else {
            LOG.debug(String.format("key was %s", e.getKey()));
        }
    }
}
Also used : HashMap(java.util.HashMap) CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Map(java.util.Map) HashMap(java.util.HashMap)

Example 19 with CountAndLastTupleTestSink

use of org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink in project apex-malhar by apache.

the class MinKeyValTest method testSchemaNodeProcessing.

/**
 * Test operator logic emits correct results for each schema.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testSchemaNodeProcessing(MinKeyVal oper, String type) {
    CountAndLastTupleTestSink minSink = new CountAndLastTupleTestSink();
    oper.min.setSink(minSink);
    oper.beginWindow(0);
    int numtuples = 10000;
    if (type.equals("integer")) {
        for (int i = numtuples; i > 0; i--) {
            oper.data.process(new KeyValPair("a", new Integer(i)));
        }
    } else if (type.equals("double")) {
        for (int i = numtuples; i > 0; i--) {
            oper.data.process(new KeyValPair("a", (double) i));
        }
    } else if (type.equals("long")) {
        for (int i = numtuples; i > 0; i--) {
            oper.data.process(new KeyValPair("a", (long) i));
        }
    } else if (type.equals("short")) {
        for (short j = 1000; j > 0; j--) {
            // cannot cross 64K
            oper.data.process(new KeyValPair("a", j));
        }
    } else if (type.equals("float")) {
        for (int i = numtuples; i > 0; i--) {
            oper.data.process(new KeyValPair("a", (float) i));
        }
    }
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, minSink.count);
    Number val = ((KeyValPair<String, Number>) minSink.tuple).getValue().intValue();
    if (type.equals("short")) {
        Assert.assertEquals("emitted min value was ", 1, val);
    } else {
        Assert.assertEquals("emitted min value was ", 1, val);
    }
}
Also used : CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair)

Example 20 with CountAndLastTupleTestSink

use of org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink in project apex-malhar by apache.

the class MinTest method testNodeSchemaProcessing.

/**
 * Test oper logic emits correct results
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeSchemaProcessing() {
    Min<Double> oper = new Min<Double>();
    CountAndLastTupleTestSink minSink = new CountAndLastTupleTestSink();
    oper.min.setSink(minSink);
    // 
    oper.beginWindow(0);
    Double a = 2.0;
    Double b = 20.0;
    Double c = 1000.0;
    oper.data.process(a);
    oper.data.process(b);
    oper.data.process(c);
    a = 1.0;
    oper.data.process(a);
    a = 10.0;
    oper.data.process(a);
    b = 5.0;
    oper.data.process(b);
    b = 12.0;
    oper.data.process(b);
    c = 22.0;
    oper.data.process(c);
    c = 14.0;
    oper.data.process(c);
    a = 46.0;
    oper.data.process(a);
    b = 2.0;
    oper.data.process(b);
    a = 23.0;
    oper.data.process(a);
    // 
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, minSink.count);
    Assert.assertEquals("emitted high value was ", 1.0, minSink.tuple);
}
Also used : CountAndLastTupleTestSink(org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink) Test(org.junit.Test)

Aggregations

CountAndLastTupleTestSink (org.apache.apex.malhar.lib.testbench.CountAndLastTupleTestSink)20 HashMap (java.util.HashMap)16 Map (java.util.Map)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)2 MatchMap (org.apache.apex.malhar.lib.algo.MatchMap)1