-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aryan Roy <[email protected]>
- Loading branch information
1 parent
20965e1
commit 37702f2
Showing
3 changed files
with
155 additions
and
28 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
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,127 @@ | ||
from __future__ import annotations | ||
|
||
import formulate | ||
|
||
from formulate.toast import toast | ||
|
||
import ast | ||
|
||
|
||
def test_simple_add(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a+2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a+2.0)")) | ||
|
||
|
||
def test_simple_sub(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a-2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a-2.0)")) | ||
|
||
|
||
def test_simple_mul(): | ||
a = toast(formulate.numexpr.exp_to_ptree("f*2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(f*2.0)")) | ||
|
||
|
||
def test_simple_div(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a/2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a/2.0)")) | ||
|
||
|
||
def test_simple_lt(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a<2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a<2.0)")) | ||
|
||
|
||
def test_simple_lte(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a<=2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a<=2.0)")) | ||
|
||
|
||
def test_simple_gt(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a>2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a>2.0)")) | ||
|
||
|
||
def test_simple_gte(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a>=2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a>=2.0)")) | ||
|
||
|
||
def test_simple_eq(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a==2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a==2.0)")) | ||
|
||
|
||
def test_simple_neq(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a!=2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a!=2.0)")) | ||
|
||
|
||
def test_simple_bor(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a|b"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("a | b")) | ||
|
||
|
||
def test_simple_band(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a&c"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("a & c")) | ||
|
||
|
||
def test_simple_bxor(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a^2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a^2.0)")) | ||
|
||
def test_simple_pow(): | ||
a = toast(formulate.numexpr.exp_to_ptree("a**2.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(a**2.0)")) | ||
|
||
|
||
def test_simple_function(): | ||
a = toast(formulate.numexpr.exp_to_ptree("sqrt(4)"), nxp = True) | ||
out = a.to_root() | ||
assert out == "TMATH::Sqrt(4.0)" | ||
|
||
|
||
def test_simple_unary_pos(): | ||
a = toast(formulate.numexpr.exp_to_ptree("+5.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(+5.0)")) | ||
|
||
|
||
def test_simple_unary_neg(): | ||
a = toast(formulate.numexpr.exp_to_ptree("-5.0"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(-5.0)")) | ||
|
||
|
||
def test_simple_unary_binv(): | ||
a = toast(formulate.numexpr.exp_to_ptree("~bool"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("~bool")) | ||
|
||
|
||
|
||
def test_unary_binary_pos(): | ||
a = toast(formulate.numexpr.exp_to_ptree("2.0 - -6"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("(2.0-(-6.0))")) | ||
|
||
|
||
def test_complex_exp(): | ||
a = toast(formulate.numexpr.exp_to_ptree("(~a**b)*23/(var|45)"), nxp = True) | ||
out = a.to_root() | ||
assert ast.unparse(ast.parse(out)) == ast.unparse(ast.parse("((~(a**b))*(23.0/(var|45.0)))")) |