[
https://issues.apache.org/jira/browse/METRON-1038?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16100496#comment-16100496
]
ASF GitHub Bot commented on METRON-1038:
----------------------------------------
Github user mattf-horton commented on a diff in the pull request:
https://github.com/apache/metron/pull/650#discussion_r129384644
--- Diff:
metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/MathFunctionsTest.java
---
@@ -38,10 +46,156 @@ public static Object run(String rule, Map<String,
Object> variables) {
@Test
public void testAbs() {
- Assert.assertEquals((Double)run("ABS(value)", ImmutableMap.of("value",
0)), 0, 1e-7);
- Assert.assertTrue(Double.isNaN((Double)run("ABS(value)",
ImmutableMap.of("value", Double.NaN))));
- Assert.assertEquals((Double)run("ABS(value)", ImmutableMap.of("value",
10.5)), 10.5, 1e-7);
- Assert.assertEquals((Double)run("ABS(value)", ImmutableMap.of("value",
-10.5)), 10.5, 1e-7);
+ assertValues("ABS",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 0d);
+ put(10.5d, 10.5d);
+ put(-10.5d, 10.5d);
+ }}
+ );
+ }
+
+ @Test
+ public void testSqrt() {
+ assertValues("SQRT",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 0d);
+ put(25d, 5d);
+ put(-10.5d, Double.NaN);
+ }}
+ );
+ }
+
+ @Test
+ public void testCeil() {
+ assertValues("CEIL",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 0d);
+ put(10.5d, 11d);
+ put(-10.5d, -10d);
+ }}
+ );
+ }
+
+ @Test
+ public void testFloor() {
+ assertValues("FLOOR",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 0d);
+ put(10.5d, 10d);
+ put(-10.5d, -11d);
+ }}
+ );
+ }
+
+ @Test
+ public void testSin() {
+ assertValues("SIN",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 0d);
+ put(Math.PI/6, 0.5);
+ put(Math.PI/4, Math.sqrt(2)/2.0);
+ put(Math.PI/3, Math.sqrt(3)/2.0);
+ put(Math.PI/2, 1d);
+ }}
+ );
+ }
+
+ @Test
+ public void testCos() {
+ assertValues("COS",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 1d);
+ put(Math.PI/6, Math.sqrt(3)/2.0);
+ put(Math.PI/4, Math.sqrt(2)/2.0);
+ put(Math.PI/3, 0.5d);
+ put(Math.PI/2, 0d);
+ }}
+ );
+ }
+
+ @Test
+ public void testTan() {
+ assertValues("TAN",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 0d);
+ put(Math.PI/6, Math.sqrt(3)/3.0);
+ put(Math.PI/4, 1d);
+ put(Math.PI/3, Math.sqrt(3));
+ put(Math.PI/2, Math.sin(Math.PI/2)/Math.cos(Math.PI/2));
+ }}
+ );
+ }
+
+ @Test
+ public void testExp() {
+ assertValues("EXP",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 1d);
+ put(0.5d, Math.sqrt(Math.E));
+ put(-0.5d, 1/Math.sqrt(Math.E));
+ put(1d, Math.E);
+ put(2d, Math.E*Math.E);
+ }}
+ );
+ }
+
+ @Test
+ public void testRound() {
+ assertValues("ROUND",
+ new HashMap<Double, Double>(baseExpectations) {{
+ put(0d, 0d);
+ put(0.5d, 1d);
+ put(0.4d, 0d);
+ put(-0.5d, 0d);
+ }}
+ );
+ }
+
+ @Test
+ public void testNaturalLog() {
+ testLog("LN", Math.E);
+ }
+
+ @Test
+ public void testLog2() {
+ testLog("LOG2", 2);
+ }
+
+ @Test
+ public void testLog10() {
+ testLog("LOG10", 10);
+ }
+
+ public void assertValues(String func, Map<Double, Double> expected) {
+ for(Map.Entry<Double, Double> test : expected.entrySet()) {
+ for(String expr : ImmutableList.of(func + "(value)"
+ ,func + "(" + test.getKey() + ")"
+ )
+ )
+ {
+ if (Double.isNaN(test.getValue())) {
+ Assert.assertTrue(expr + " != NaN, where value == " +
test.getKey(), Double.isNaN(toDouble(run(expr, ImmutableMap.of("value",
test.getKey())))));
+ } else {
+ Assert.assertEquals(expr + " != " + test.getValue() + " (where
value == " + test.getKey() + ")", test.getValue(), toDouble(run(expr,
ImmutableMap.of("value", test.getKey()))), EPSILON);
+ }
+ }
+ }
+ }
+
+ public Double toDouble(Object n) {
+ return ((Number)n).doubleValue();
+ }
+
+ public void testLog(String logExpr, double base) {
+ Map<Double, Double> expectedValues = new HashMap<Double,
Double>(baseExpectations) {{
+ put(base, 1d);
+ put(0d, Double.NEGATIVE_INFINITY);
+ }};
+ for(int i = 1;i <= 10;++i) {
+ expectedValues.put(Math.pow(base, i), (double)i);
+ }
+ assertValues(logExpr, expectedValues);
--- End diff --
These test look pretty good.
> Stellar should have a better collection of basic math operations
> ----------------------------------------------------------------
>
> Key: METRON-1038
> URL: https://issues.apache.org/jira/browse/METRON-1038
> Project: Metron
> Issue Type: Improvement
> Reporter: Casey Stella
>
> At the moment the math functions are woefully incomplete.
> We should add at least the ones difficult or impossible to reconstruct using
> existing stellar primitives/math functions:
> * log10
> * log2
> * ln
> * sqrt
> * ceil
> * floor
> * sin
> * cos
> * tan
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)