Skip to content

Commit

Permalink
Added LIMIT operator and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ercpe committed Feb 2, 2016
1 parent 1a7a5a1 commit 4b481c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/rpn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ def __call__(self, tokens, pos):
return tokens


def operator_limit(value, low, high):
if low <= value <= high:
return value
return None


OPERATORS = {
# MATH
'+': RPNOperator(operator.add, 2),
Expand All @@ -37,6 +43,8 @@ def __call__(self, tokens, pos):
# COMPARE
'MIN': RPNOperator(min, 2),
'MAX': RPNOperator(max, 2),
# MINNAN, MAXNAN
'LIMIT': RPNOperator(operator_limit, 3),
}

class RPN(object):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ def test_max(self):

assert r.calc('1 2 MAX') == 2
assert r.calc('3 2 MAX') == 3

def test_limit(self):
r = RPN()

assert r.calc('1 1 10 LIMIT') == 1
assert r.calc('10 1 10 LIMIT') == 10
assert r.calc('0 1 10 LIMIT') is None
assert r.calc('-1 1 10 LIMIT') is None
assert r.calc('100 1 10 LIMIT') is None
9 changes: 9 additions & 0 deletions tests/test_complex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
from rpn import RPN


class TestComplex(object):

def test_complex(self):
r = RPN()
assert r.calc('3 4 2 * 1 5 - 2 3 ^ ^ / +') == 3.0001220703125

0 comments on commit 4b481c4

Please sign in to comment.