Question:
C++ test harness help?
Derrick
2011-12-04 18:00:05 UTC
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!

my code:

bool testInOrder(int a, int b, int c)
{
inOrder(a, b, c);
if(bool inOrder == true)
{if(a <= b && b <= c){return true;}}
else
return false;
}

wbat a i doing wrong?
Three answers:
oops
2011-12-04 18:45:23 UTC
What are you doing wrong? You're not being thorough enough. Also, your test function doesn't need to accept any arguments. It can and should generate them itself. Putting a limitation like that (calling the function to be tested as few times as possible) on a test is ridiculous. This is test code, not end user code. Our priority here is correctness, not efficiency. Afterall, what's the purpose of a test if you're not thorough? You might as well just write this:



    bool testInOrder()

    {

        // no calls, this is as few as you can get

        return true;

    }



I would absolutely not call the function fewer times than shown below.



    bool testInOrder()

    {

        if (inOrder(-1,0,1) == false) return false;

        if (inOrder(1,1,1) == false) return false;

        if (inOrder(1,2,1) == true) return false;

        if (inOrder(2,1,2) == true) return false;



        // all tests passed

        return true;

    }
D Bügger™
2011-12-04 18:13:02 UTC
replace with following code(this code satisfies all the conditions i.e.

i).testInOrder verifies the result of inOrder.Since when if(a <= b && b <= c) is false it

skips the call to inOrder cause inOrder is certain to be false.When if(a <= b && b <= c) is true

the value returned is same as returned by inOrder to the testInOrder method i.e. true.



ii)."assume inOrder is an EXPENSIVE function call, so call it as FEW times as possible!":-the code doen't call inOrder at all if if(a <= b && b <= c) is false

).:-

the code:-

(it will work for sure only if you call it properly (resubmit the whole program if any error occurs.):-



bool testInOrder(int a, int b, int c)

{

if(a <= b && b <= c)

{

return inOrder(a, b, c); //inorder here return true /false

}

else

{

return false;

}





your code:-

bool testInOrder(int a, int b, int c)

{

inOrder(a, b, c); <-calls inOrder twice,first here then below

if(bool inOrder == true) <-on calling inOrder it wil return true * also / /bool keyword not required

{



}

else

return false;

}





*

i
Kenneth
2011-12-04 18:07:49 UTC
bool testInOrder(int a, int b, int c)

{

return inOrder(a, b, c);

}



inOrder() is already going to return true or false


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...