Skip to content

Commit

Permalink
increment and decrement function added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Lehmann committed Apr 20, 2020
1 parent ee13d6d commit c159afd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
24 changes: 24 additions & 0 deletions engine/eval/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,27 @@ def __repr__(self) -> str:
def calculate(self, value: int) -> int:
"""Calculate the integer square root of given value."""
return int(math.sqrt(value))


class Increment(Function):
"""Calculate +1 of variable value."""

def __repr__(self) -> str:
"""String representation of the increment function."""
return "Increment(%s)" % super().__repr__()

def calculate(self, value: int) -> int:
"""Calculate the increment of given value."""
return value + 1


class Decrement(Function):
"""Calculate -1 of variable value."""

def __repr__(self) -> str:
"""String representation of the decrement function."""
return "Decrement(%s)" % super().__repr__()

def calculate(self, value: int) -> int:
"""Calculate the decrement of given value."""
return value - 1
14 changes: 13 additions & 1 deletion tests/test_engine_eval_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from hamcrest import assert_that, equal_to, calling, raises

from engine.eval.variable import Variable
from engine.eval.functions import Square, SumOfDigits, SquareRoot
from engine.eval.functions import Square, SumOfDigits, SquareRoot, Increment, Decrement


class BadVariable(Variable):
Expand Down Expand Up @@ -46,3 +46,15 @@ def test_integer_square_root(self):
fun = SquareRoot(Variable(1025))
assert_that(fun.get(), equal_to(32))
assert_that(str(fun), "SquareRoot(Variable(1025))")

def test_increment(self):
"""Testing increment function."""
fun = Increment(Variable(10))
assert_that(fun.get(), equal_to(11))
assert_that(str(fun), "Increment(Variable(10))")

def test_decrement(self):
"""Testing decrement function."""
fun = Decrement(Variable(10))
assert_that(fun.get(), equal_to(9))
assert_that(str(fun), "Decrement(Variable(10))")

0 comments on commit c159afd

Please sign in to comment.