#!/usr/bin/env python3 # -------------------------------------------------------------------------- def signedSquare(val): """Returns the square of a number, but if the number was negative the returned value is also negative Input: val A number to be squared Return: the signed square of the number """ sign = -1 if val < 0 else 1 square = val * val return(sign * square) # -------------------------------------------------------------------------- def runTestOneReturn(name, input, expected, actual): """Run a test on a subroutine that returns a single value. Input is: name - the name of the subroutine being tested input - the input to that subroutine (in quotes if more than one item) expected - the expected output from the subroutine actual - the actual output from the subroutine """ if(expected == actual): print("Success: %s(%s)" % (name, input)) else: print("Fail: %s(%s) - expected %s, obtained %s" % (name, input, expected, actual)) # -------------------------------------------------------------------------- def test_signedSquare(): """Run tests on the signedSquare() routine""" runTestOneReturn("signedSquare", 2, 4, signedSquare(2)) runTestOneReturn("signedSquare", 0, 0, signedSquare(0)) runTestOneReturn("signedSquare", -2, -4, signedSquare(-2)) # -------------------------------------------------------------------------- # MAIN PROGRAM: Run a set of tests if __name__ == "__main__": test_signedSquare()