Github user mattf-horton commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/403#discussion_r95426043
  
    --- Diff: 
metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java
 ---
    @@ -32,124 +33,143 @@
     
     public class StringFunctionsTest {
     
    -    @Test
    -    public void testStringFunctions() throws Exception {
    -        final Map<String, String> variableMap = new HashMap<String, 
String>() {{
    -            put("foo", "casey");
    -            put("ip", "192.168.0.1");
    -            put("empty", "");
    -            put("spaced", "metron is great");
    -        }};
    -        Assert.assertTrue(runPredicate("true and TO_UPPER(foo) == 
'CASEY'", v -> variableMap.get(v)));
    -        Assert.assertTrue(runPredicate("foo in [ TO_LOWER('CASEY'), 
'david' ]", v -> variableMap.get(v)));
    -        Assert.assertTrue(runPredicate("TO_UPPER(foo) in [ 
TO_UPPER('casey'), 'david' ] and IN_SUBNET(ip, '192.168.0.0/24')", v -> 
variableMap.get(v)));
    -        Assert.assertFalse(runPredicate("TO_LOWER(foo) in [ 
TO_UPPER('casey'), 'david' ]", v -> variableMap.get(v)));
    +  @Test
    +  public void testStringFunctions() throws Exception {
    +    final Map<String, String> variableMap = new HashMap<String, String>() 
{{
    +      put("foo", "casey");
    +      put("ip", "192.168.0.1");
    +      put("empty", "");
    +      put("spaced", "metron is great");
    +    }};
    +    Assert.assertTrue(runPredicate("true and TO_UPPER(foo) == 'CASEY'", v 
-> variableMap.get(v)));
    +    Assert.assertTrue(runPredicate("foo in [ TO_LOWER('CASEY'), 'david' 
]", v -> variableMap.get(v)));
    +    Assert.assertTrue(runPredicate("TO_UPPER(foo) in [ TO_UPPER('casey'), 
'david' ] and IN_SUBNET(ip, '192.168.0.0/24')", v -> variableMap.get(v)));
    +    Assert.assertFalse(runPredicate("TO_LOWER(foo) in [ TO_UPPER('casey'), 
'david' ]", v -> variableMap.get(v)));
    +  }
    +
    +  @Test
    +  public void testStringFunctions_advanced() throws Exception {
    +    final Map<String, Object> variableMap = new HashMap<String, Object>() 
{{
    +      put("foo", "casey");
    +      put("bar", "bar.casey.grok");
    +      put("ip", "192.168.0.1");
    +      put("empty", "");
    +      put("spaced", "metron is great");
    +      put("myList", ImmutableList.of("casey", "apple", "orange"));
    +    }};
    +    Assert.assertTrue(runPredicate("foo in SPLIT(bar, '.')", v -> 
variableMap.get(v)));
    +    Assert.assertFalse(runPredicate("foo in SPLIT(ip, '.')", v -> 
variableMap.get(v)));
    +    Assert.assertTrue(runPredicate("foo in myList", v -> 
variableMap.get(v)));
    +    Assert.assertFalse(runPredicate("foo not in myList", v -> 
variableMap.get(v)));
    +  }
    +
    +  @Test
    +  public void testLeftRightFills() throws Exception{
    +    final Map<String, Object> variableMap = new HashMap<String, Object>() 
{{
    +      put("foo", null);
    +      put("bar", null);
    +      put("notInt","oh my");
    +    }};
    +
    +    //LEFT
    +    Object left = run("FILL_LEFT('123','X', 10)",new HashedMap());
    +    Assert.assertNotNull(left);
    +    Assert.assertEquals(10,((String)left).length());
    +    Assert.assertEquals("XXXXXXX123",(String)left);
    +
    +    //RIGHT
    +    Object right = run("FILL_RIGHT('123','X', 10)", new HashedMap());
    +    Assert.assertNotNull(right);
    +    Assert.assertEquals(10,((String)right).length());
    +    Assert.assertEquals("123XXXXXXX",(String)right);
    +
    +    //INPUT ALREADY LENGTH
    +    Object same = run("FILL_RIGHT('123','X', 3)", new HashedMap());
    +    Assert.assertEquals(3,((String)same).length());
    +    Assert.assertEquals("123",(String)same);
    +
    +    //INPUT BIGGER THAN LENGTH
    +    Object tooBig = run("FILL_RIGHT('1234567890','X', 3)", new 
HashedMap());
    +    Assert.assertEquals(10,((String)tooBig).length());
    +    Assert.assertEquals("1234567890",(String)tooBig);
    +
    +    //NULL VARIABLES
    +    boolean thrown = false;
    +    try{
    +      run("FILL_RIGHT('123',foo,bar)", variableMap);
    +    }catch(ParseException pe) {
    +      thrown = true;
    +      Assert.assertTrue(pe.getMessage().contains("are both required"));
         }
    -
    -    @Test
    -    public void testStringFunctions_advanced() throws Exception {
    -        final Map<String, Object> variableMap = new HashMap<String, 
Object>() {{
    -            put("foo", "casey");
    -            put("bar", "bar.casey.grok");
    -            put("ip", "192.168.0.1");
    -            put("empty", "");
    -            put("spaced", "metron is great");
    -            put("myList", ImmutableList.of("casey", "apple", "orange"));
    -        }};
    -        Assert.assertTrue(runPredicate("foo in SPLIT(bar, '.')", v -> 
variableMap.get(v)));
    -        Assert.assertFalse(runPredicate("foo in SPLIT(ip, '.')", v -> 
variableMap.get(v)));
    -        Assert.assertTrue(runPredicate("foo in myList", v -> 
variableMap.get(v)));
    -        Assert.assertFalse(runPredicate("foo not in myList", v -> 
variableMap.get(v)));
    +    Assert.assertTrue(thrown);
    +    thrown = false;
    +
    +    // NULL LENGTH
    +    try{
    +      run("FILL_RIGHT('123','X',bar)", variableMap);
    +    }catch(ParseException pe) {
    +      thrown = true;
    +      Assert.assertTrue(pe.getMessage().contains("are both required"));
         }
    -
    -    @Test
    -    public void testLeftRightFills() throws Exception{
    -        final Map<String, Object> variableMap = new HashMap<String, 
Object>() {{
    -            put("foo", null);
    -            put("bar", null);
    -            put("notInt","oh my");
    -        }};
    -
    -        //LEFT
    -        Object left = run("FILL_LEFT('123','X', 10)",new HashedMap());
    -        Assert.assertNotNull(left);
    -        Assert.assertEquals(10,((String)left).length());
    -        Assert.assertEquals("XXXXXXX123",(String)left);
    -
    -        //RIGHT
    -        Object right = run("FILL_RIGHT('123','X', 10)", new HashedMap());
    -        Assert.assertNotNull(right);
    -        Assert.assertEquals(10,((String)right).length());
    -        Assert.assertEquals("123XXXXXXX",(String)right);
    -
    -        //INPUT ALREADY LENGTH
    -        Object same = run("FILL_RIGHT('123','X', 3)", new HashedMap());
    -        Assert.assertEquals(3,((String)same).length());
    -        Assert.assertEquals("123",(String)same);
    -
    -        //INPUT BIGGER THAN LENGTH
    -        Object tooBig = run("FILL_RIGHT('1234567890','X', 3)", new 
HashedMap());
    -        Assert.assertEquals(10,((String)tooBig).length());
    -        Assert.assertEquals("1234567890",(String)tooBig);
    -
    -        //NULL VARIABLES
    -        boolean thrown = false;
    -        try{
    -            run("FILL_RIGHT('123',foo,bar)", variableMap);
    -        }catch(ParseException pe) {
    -            thrown = true;
    -            Assert.assertTrue(pe.getMessage().contains("are both 
required"));
    -        }
    -        Assert.assertTrue(thrown);
    -        thrown = false;
    -
    -        // NULL LENGTH
    -        try{
    -            run("FILL_RIGHT('123','X',bar)", variableMap);
    -        }catch(ParseException pe) {
    -            thrown = true;
    -            Assert.assertTrue(pe.getMessage().contains("are both 
required"));
    -        }
    -        Assert.assertTrue(thrown);
    -        thrown = false;
    -
    -        // NULL FILL
    -        try{
    -            run("FILL_RIGHT('123',foo, 7)", variableMap);
    -        }catch(ParseException pe) {
    -            thrown = true;
    -            Assert.assertTrue(pe.getMessage().contains("are both 
required"));
    -        }
    -        Assert.assertTrue(thrown);
    -        thrown = false;
    -
    -        // NON INTEGER LENGTH
    -        try {
    -            run("FILL_RIGHT('123','X', 'z' )", new HashedMap());
    -        }catch(ParseException pe){
    -            thrown = true;
    -            Assert.assertTrue(pe.getMessage().contains("not a valid 
Integer"));
    -        }
    -        Assert.assertTrue(thrown);
    -        thrown = false;
    -
    -        // EMPTY STRING PAD
    -        try {
    -            Object returnValue = run("FILL_RIGHT('123','', 10 )", new 
HashedMap());
    -        }catch(ParseException pe) {
    -            thrown = true;
    -            Assert.assertTrue(pe.getMessage().contains("cannot be an 
empty"));
    -        }
    -        Assert.assertTrue(thrown);
    -        thrown = false;
    -
    -        //MISSING LENGTH PARAMETER
    -        try {
    -            run("FILL_RIGHT('123',foo)", variableMap);
    -        }catch(ParseException pe){
    -            thrown = true;
    -            Assert.assertTrue(pe.getMessage().contains("expects three"));
    -        }
    -        Assert.assertTrue(thrown);
    +    Assert.assertTrue(thrown);
    +    thrown = false;
    +
    +    // NULL FILL
    +    try{
    +      run("FILL_RIGHT('123',foo, 7)", variableMap);
    +    }catch(ParseException pe) {
    +      thrown = true;
    +      Assert.assertTrue(pe.getMessage().contains("are both required"));
    +    }
    +    Assert.assertTrue(thrown);
    +    thrown = false;
    +
    +    // NON INTEGER LENGTH
    +    try {
    +      run("FILL_RIGHT('123','X', 'z' )", new HashedMap());
    +    }catch(ParseException pe){
    +      thrown = true;
    +      Assert.assertTrue(pe.getMessage().contains("not a valid Integer"));
    +    }
    +    Assert.assertTrue(thrown);
    +    thrown = false;
    +
    +    // EMPTY STRING PAD
    +    try {
    +      Object returnValue = run("FILL_RIGHT('123','', 10 )", new 
HashedMap());
    +    }catch(ParseException pe) {
    +      thrown = true;
    +      Assert.assertTrue(pe.getMessage().contains("cannot be an empty"));
    +    }
    +    Assert.assertTrue(thrown);
    +    thrown = false;
    +
    +    //MISSING LENGTH PARAMETER
    +    try {
    +      run("FILL_RIGHT('123',foo)", variableMap);
    +    }catch(ParseException pe){
    +      thrown = true;
    +      Assert.assertTrue(pe.getMessage().contains("expects three"));
         }
    +    Assert.assertTrue(thrown);
    +  }
    +
    +  @Test
    +  public void shannonEntropyTest() throws Exception {
    +    //test empty string
    +    Assert.assertEquals(0.0, (Double)run("STRING_ENTROPY('')", new 
HashMap<>()), 0.0);
    +    Assert.assertEquals(0.0, (Double)run("STRING_ENTROPY(foo)", 
ImmutableMap.of("foo", "")), 0.0);
    +
    +    /*
    +    Now consider the string aaaaaaaaaabbbbbccccc or 10 a's followed by 5 
b's and 5 c's.
    +    The probabilities of each character is as follows:
    +    p(a) = 1/2
    +    p(b) = 1/4
    +    p(c) = 1/4
    +    so the shannon entropy should be
    +      -p(a)*log_2(p(a)) - p(b)*log_2(p(b)) - p(c)*log_2(p(c)) =
    +      -0.5*-1 - 0.25*-2 - 0.25*-2 = 1.5
    +     */
    +    Assert.assertEquals(1.5, (Double)run("STRING_ENTROPY(foo)", 
ImmutableMap.of("foo", "aaaaaaaaaabbbbbccccc")), 0.0);
    --- End diff --
    
    It does seem that an empty string should have entropy exactly == 0.0 (line 
160).
    
    In line 173, I assume the argument is that a string with per-letter 
probabilities that are small exact powers of 2 will have its entropy calculated 
exactly.  I suspect that's true, but one would have to know the implementation 
to be sure.
    
    Otherwise one would have to use a delta of 4*Math.ulp(Double(1.5)) , I 
think.
    But it must pass as is, so it's good :-)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to