Tuesday, June 19, 2012

Homework 10: testing

1. Assume you need to test a function named max . The function max receives two int arguments and returns the larger. Write the definition of driver function  testmax whose job it is to determine whether max is correct. So testmax returns true if  max is correct and returns false otherwise. Instructor's notes: testmax does not take any arguments, only max does. So, the header of testmax will look like this:


bool testmax () { return max ( 5 , 4 ) == 5 && max ( 4 , 5 ) == 5 && max ( 4 , 4 ) == 4; }

2. Assume you need to test a function named inOrder . The function inOrder receives three int arguments and returns true if and only if the arguments are in non-decreasing order: that is, the second argument is not less than the first and the third is not less than the second. Write the definition of driver function  testInOrder whose job it is to determine whether inOrder is correct. So testInOrder returns true if  inOrder is correct and returns false otherwise.
. For the purposes of this exercise, assume inOrder is an expensive function call, so call it as few times as possible!

bool testInOrder () { return inOrder ( 1 , 2 , 3 ) && inOrder ( 1 , 1 , 3 ) && inOrder ( 1 , 2 , 2 ) && inOrder ( 2 , 2 , 2 ) && !inOrder ( 5 , 2 , 3 ) && !inOrder ( 3 , 4 , 1 ) && !inOrder ( 2 , 1 , 3 ) && !inOrder ( 3 , 2 , 1 ) && !inOrder ( 3 , 1 , 1 ) && !inOrder ( 1 , 3 , 1 ) && !inOrder ( 3 , 3 , 1 ) && !inOrder ( 3 , 1 , 3 ); }

No comments:

Post a Comment