Skip to content

Instantly share code, notes, and snippets.

@jasongorman
Created October 3, 2019 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasongorman/7aeddc85a926c3ac0f30e63f36523b90 to your computer and use it in GitHub Desktop.
Save jasongorman/7aeddc85a926c3ac0f30e63f36523b90 to your computer and use it in GitHub Desktop.
from maths import sqrt, factorial, floor, ceiling
run = 0
passed = 0
def main():
print('Running math tests...')
assert_equals('sqrt of 0.0', 0.0, sqrt(0.0))
assert_equals('sqrt of 1.0', 1.0, sqrt(1.0))
assert_equals('sqrt of 4.0', 2.0, sqrt(4.0))
assert_equals('sqrt of 6.25', 2.5, sqrt(6.25))
assert_raises("sqrt of -1.0", Exception, lambda: sqrt(-1.0))
assert_equals('factorial of 0', 0, factorial(0))
assert_equals('factorial of 1', 1, factorial(1))
assert_equals('factorial of 5', 120, factorial(5))
assert_raises('factorial of -1', Exception, lambda: factorial(-1))
assert_raises('factorial of 0.5', Exception, lambda: factorial(0.5))
assert_equals('floor of 0', 0, floor(0))
assert_equals('floor of 4.7', 4.0, floor(4.7))
assert_equals('floor of -4.7', -5.0, floor(-4.7))
assert_equals('ceiling of 2.3', 3.0, ceiling(2.3))
assert_equals('ceiling of 0', 0.0, ceiling(0.0))
assert_equals('ceiling of -2.3', -2.0, ceiling(-2.3))
print('Tests run: ', run)
print('Passed: ', passed, ', Failed: ', run - passed)
def assert_raises(test, exception, function):
global run
global passed
run += 1
try:
function.__call__()
print(test, ' failed - expected Exception to be raised')
except exception:
passed += 1
def assert_equals(test, expected, actual):
global run
global passed
run += 1
if expected == actual:
passed += 1
else:
print(test, ' failed - expected ', expected, ', actual ', actual)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment