Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import sys
from os.path import dirname, abspath, join
root_path = abspath(join(dirname(__file__), "../../"))
sys.path.insert(0, root_path)
from pynq.parser import ExpressionParser
from pynq.expressions import BinaryExpression, ConstantExpression
parser = ExpressionParser()
operations_to_test = {
BinaryExpression.Add : (("1+2", "1", "2"), ("1 + 2", "1", "2")),
BinaryExpression.Subtract : (("1-2", "1", "2"), ("1 - 2", "1", "2")),
BinaryExpression.Multiply : (("1*2", "1", "2"), ("1 * 2", "1", "2")),
BinaryExpression.Divide : (("1/2", "1", "2"), ("1 / 2", "1", "2")),
BinaryExpression.Power : (("1**2", "1", "2"), ("1 ** 2", "1", "2")),
BinaryExpression.Modulo : (("1%2", "1", "2"), ("1 % 2", "1", "2")),
BinaryExpression.And : (("1 and 2", "1", "2"),),
BinaryExpression.Or : (("1 or 2", "1", "2"),),
BinaryExpression.Equal : (("1==2", "1", "2"),("1 == 2", "1", "2"),),
BinaryExpression.NotEqual : (("1!=2", "1", "2"),("1 != 2", "1", "2"),),
BinaryExpression.GreaterThan : (("1>2", "1", "2"),("1 > 2", "1", "2"),),
BinaryExpression.GreaterThanOrEqual : (("1>=2", "1", "2"),("1 >= 2", "1", "2"),),
BinaryExpression.LessThan : (("1<2", "1", "2"),("1 < 2", "1", "2"),),
BinaryExpression.LessThanOrEqual : (("1<=2", "1", "2"),("1 <= 2", "1", "2"),),
}
def test_for_null_for_binary_expressions():
def test_binary_expression_only_accepts_expressions_for_arguments(self):
a = 10
a_expr = ConstantExpression(10)
b = "20"
b_expr = ConstantExpression(20)
node_type = BinaryExpression.Add
self.assertRaisesEx(ValueError, BinaryExpression, node_type, a, b_expr, exc_pattern=re.compile("Lhs must be an expression \(an instance of a class that inherits from pynq.Expression\)"))
self.assertRaisesEx(ValueError, BinaryExpression, node_type, a_expr, b, exc_pattern=re.compile("Rhs must be an expression \(an instance of a class that inherits from pynq.Expression\)"))
self.assertRaisesEx(ValueError, BinaryExpression, None, a_expr, b_expr, exc_pattern=re.compile("The BinaryExpression node type is required"))
def test_nested_addition_expression(self):
a = ConstantExpression(10)
b = ConstantExpression(20)
c = ConstantExpression(30)
node_type = BinaryExpression.Add
expr = BinaryExpression(node_type, BinaryExpression(node_type, a, b), c)
self.assertEquals(expr.node_type, node_type)
self.failUnless(isinstance(expr.lhs, BinaryExpression), "The left-hand side of the binary expression should be a binary expression as well, but is %s" % expr.lhs.__class__)
self.assertEquals(expr.lhs.node_type, node_type)
self.assertEquals(expr.lhs.lhs, a)
self.assertEquals(expr.lhs.rhs, b)
self.assertEquals(expr.rhs, c)
def test_binary_expression_only_accepts_expressions_for_arguments(self):
a = 10
a_expr = ConstantExpression(10)
b = "20"
b_expr = ConstantExpression(20)
node_type = BinaryExpression.Add
self.assertRaisesEx(ValueError, BinaryExpression, node_type, a, b_expr, exc_pattern=re.compile("Lhs must be an expression \(an instance of a class that inherits from pynq.Expression\)"))
self.assertRaisesEx(ValueError, BinaryExpression, node_type, a_expr, b, exc_pattern=re.compile("Rhs must be an expression \(an instance of a class that inherits from pynq.Expression\)"))
self.assertRaisesEx(ValueError, BinaryExpression, None, a_expr, b_expr, exc_pattern=re.compile("The BinaryExpression node type is required"))
def led(self, left):
return BinaryExpression(BinaryExpression.Add, left, self.expression(self.lbp))