-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from markus-s24/artur
Power and truncated division operators
- Loading branch information
Showing
8 changed files
with
328 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/hubspot/jinjava/el/ext/PowerOfOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.hubspot.jinjava.el.ext; | ||
|
||
import de.odysseus.el.misc.TypeConverter; | ||
import de.odysseus.el.tree.impl.Parser.ExtensionHandler; | ||
import de.odysseus.el.tree.impl.Parser.ExtensionPoint; | ||
import de.odysseus.el.tree.impl.Scanner; | ||
import de.odysseus.el.tree.impl.ast.AstBinary; | ||
import de.odysseus.el.tree.impl.ast.AstBinary.SimpleOperator; | ||
import de.odysseus.el.tree.impl.ast.AstNode; | ||
|
||
public class PowerOfOperator extends SimpleOperator { | ||
|
||
@Override | ||
protected Object apply(TypeConverter converter, Object a, Object b) { | ||
boolean aInt = a instanceof Integer || a instanceof Long; | ||
boolean bInt = b instanceof Integer || b instanceof Long; | ||
boolean aNum = aInt || a instanceof Double || a instanceof Float; | ||
boolean bNum = bInt || b instanceof Double || b instanceof Float; | ||
|
||
if (aInt && bInt) { | ||
Long d = converter.convert(a, Long.class); | ||
Long e = converter.convert(b, Long.class); | ||
return (long)Math.pow(d, e); | ||
} | ||
if (aNum && bNum) { | ||
Double d = converter.convert(a, Double.class); | ||
Double e = converter.convert(b, Double.class); | ||
return Math.pow(d, e); | ||
} | ||
throw new IllegalArgumentException("Unsupported operand type(s) for **: " | ||
+ "'" + a.getClass().getSimpleName() + "' and " | ||
+ "'" + b.getClass().getSimpleName() + "'"); | ||
} | ||
|
||
public static final Scanner.ExtensionToken TOKEN = new Scanner.ExtensionToken("**"); | ||
public static final PowerOfOperator OP = new PowerOfOperator(); | ||
|
||
public static final ExtensionHandler HANDLER = new ExtensionHandler(ExtensionPoint.MUL) { | ||
@Override | ||
public AstNode createAstNode(AstNode... children) { | ||
return new AstBinary(children[0], children[1], OP); | ||
} | ||
}; | ||
|
||
} | ||
|
||
|
48 changes: 48 additions & 0 deletions
48
src/main/java/com/hubspot/jinjava/el/ext/TruncDivOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.hubspot.jinjava.el.ext; | ||
|
||
import de.odysseus.el.misc.TypeConverter; | ||
import de.odysseus.el.tree.impl.Parser.ExtensionHandler; | ||
import de.odysseus.el.tree.impl.Parser.ExtensionPoint; | ||
import de.odysseus.el.tree.impl.Scanner; | ||
import de.odysseus.el.tree.impl.ast.AstBinary; | ||
import de.odysseus.el.tree.impl.ast.AstBinary.SimpleOperator; | ||
import de.odysseus.el.tree.impl.ast.AstNode; | ||
|
||
public class TruncDivOperator extends SimpleOperator { | ||
|
||
@Override | ||
protected Object apply(TypeConverter converter, Object a, Object b) { | ||
|
||
boolean aInt = a instanceof Integer || a instanceof Long; | ||
boolean bInt = b instanceof Integer || b instanceof Long; | ||
boolean aNum = aInt || a instanceof Double || a instanceof Float; | ||
boolean bNum = bInt || b instanceof Double || b instanceof Float; | ||
|
||
if (aInt && bInt) { | ||
Long d = converter.convert(a, Long.class); | ||
Long e = converter.convert(b, Long.class); | ||
return Math.floorDiv(d, e); | ||
} | ||
if (aNum && bNum) { | ||
Double d = converter.convert(a, Double.class); | ||
Double e = converter.convert(b, Double.class); | ||
return Math.floor(d/e); | ||
} | ||
throw new IllegalArgumentException("Unsupported operand type(s) for //: " | ||
+ "'" + a.getClass().getSimpleName() + "' and " | ||
+ "'" + b.getClass().getSimpleName() + "'"); | ||
} | ||
|
||
public static final Scanner.ExtensionToken TOKEN = new Scanner.ExtensionToken("//"); | ||
public static final TruncDivOperator OP = new TruncDivOperator(); | ||
|
||
public static final ExtensionHandler HANDLER = new ExtensionHandler(ExtensionPoint.MUL) { | ||
@Override | ||
public AstNode createAstNode(AstNode... children) { | ||
return new AstBinary(children[0], children[1], OP); | ||
} | ||
}; | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.hubspot.jinjava.el.ext; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.hubspot.jinjava.Jinjava; | ||
import com.hubspot.jinjava.interpret.FatalTemplateErrorsException; | ||
|
||
public class PowerOfTest { | ||
|
||
@Before | ||
public void setUp() { | ||
jinja = new Jinjava(); | ||
} | ||
|
||
@Test | ||
public void testPowerOfInteger() { | ||
|
||
Map<String, Object> context = Maps.newHashMap(); | ||
context.put("base", 2); | ||
context.put("exponent", 8); | ||
|
||
String template = "{% set x = base ** exponent %}{{x}}"; | ||
String rendered = jinja.render(template, context); | ||
assertEquals("256", rendered); | ||
} | ||
|
||
@Test | ||
public void testPowerOfFractional() { | ||
|
||
Map<String, Object> context = Maps.newHashMap(); | ||
context.put("base", 2); | ||
context.put("exponent", 8.0); | ||
|
||
String template = "{% set x = base ** exponent %}{{x}}"; | ||
String rendered = jinja.render(template, context); | ||
assertEquals("256.0", rendered); | ||
} | ||
|
||
@Test | ||
public void test04PowerOfStringFails() { | ||
|
||
Map<String, Object> context = Maps.newHashMap(); | ||
context.put("base", "2"); | ||
context.put("exponent", "8"); | ||
|
||
String template = "{% set x = base ** exponent %}{{x}}"; | ||
try { | ||
jinja.render(template, context); | ||
} catch (FatalTemplateErrorsException e) { | ||
String msg = e.getMessage(); | ||
assertTrue(msg.contains("Unsupported operand type(s)")); | ||
} | ||
} | ||
|
||
private Jinjava jinja; | ||
} |
72 changes: 72 additions & 0 deletions
72
src/test/java/com/hubspot/jinjava/el/ext/TruncDivTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.hubspot.jinjava.el.ext; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.hubspot.jinjava.Jinjava; | ||
import com.hubspot.jinjava.interpret.FatalTemplateErrorsException; | ||
|
||
public class TruncDivTest { | ||
|
||
@Before | ||
public void setUp() { | ||
jinja = new Jinjava(); | ||
} | ||
|
||
/** | ||
* Test the truncated division operator "//" with integer values | ||
*/ | ||
@Test | ||
public void testTruncDivInteger() { | ||
Map<String, Object> context = Maps.newHashMap(); | ||
context.put("dividend", 5); | ||
context.put("divisor", 2); | ||
|
||
String template = "{% set x = dividend // divisor %}{{x}}"; | ||
String rendered = jinja.render(template, context); | ||
assertEquals("2", rendered); | ||
} | ||
|
||
/** | ||
* Test the truncated division operator "//" with fractional values | ||
*/ | ||
@Test | ||
public void testTruncDivFractional() { | ||
|
||
Map<String, Object> context = Maps.newHashMap(); | ||
context.put("dividend", 5.0); | ||
context.put("divisor", 2); | ||
|
||
String template = "{% set x = dividend // divisor %}{{x}}"; | ||
String rendered = jinja.render(template, context); | ||
assertEquals("2.0", rendered); | ||
} | ||
|
||
/** | ||
* Test the truncated division operator "//" with strings | ||
*/ | ||
@Test | ||
public void testTruncDivStringFails() { | ||
|
||
Map<String, Object> context = Maps.newHashMap(); | ||
context.put("dividend", "5"); | ||
context.put("divisor", "2"); | ||
|
||
String template = "{% set x = dividend // divisor %}{{x}}"; | ||
try { | ||
jinja.render(template, context); | ||
} catch (FatalTemplateErrorsException e) { | ||
String msg = e.getMessage(); | ||
assertTrue(msg.contains("Unsupported operand type(s)")); | ||
} | ||
} | ||
|
||
|
||
private Jinjava jinja; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters