Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
bool fun( int a, int b)
{
  if(a>100)&&(b<1000)
  return a-b;
  if(a<100 && b>1000)
  return b-a;
  if(a<100 && b<1000)
  return a+b;
}


How can I test this code?
How should I write test cases?
Posted
Updated 4-Nov-10 0:48am
v2
Comments
CPallini 4-Nov-10 6:58am    
bool is NOT a C keyword.
Aescleal 4-Nov-10 7:27am    
bool is a typedef of _Bool in C99 though - he might be the one person using C99 on the planet. Or just compiling as C++ on most compilers :-)

Hi,

This code is not even going to compile because you have missing brackets. First you need code that compiles and runs without errors. Then think about what happens if a == 100? There is no return for this situation, try to cover all values of a and b.

To test this code you need to call it with many possible values. Write some test code and ask a question with your example code, then people may be able to offer suggestions to improve it. I suppose what I am saying is the function is pretty simple so just have a go yourself.

Ali :)
 
Share this answer
 
Comments
Aescleal 4-Nov-10 9:12am    
Oops, sorry, added a superfluous "as" in front of many when I read your answer... Sorry about that! I'll edit my answer a bit.
In addition to what Alison said about actually having code that will compile...

The important thing is that you test around the boundaries where interesting things are happening - in this case where the conditionals are changing. So in your case look at values of a around 100 and values of b around 1000. In addition there's a couple of cases where a = b might be interesting.

How you write the tests depends on how confident a programmer you are. If you're just starting out in C keep the tests simple:

assert( fun( 50, 50 ) );


where assert is a macro that checks whether a statement is true. It's defined in assert.h - you also need to have DEBUG defined to use it.

If you're a bit more confident then you could try a table driven approach. Define a structure with a value of a, a value of b and the expected value of feeding those values into fun:

struct fun_test
{
    int a;
    int b;
    bool expected_result;
};

Each of these structures defines one test:

struct fun_test test1 = { 50, 50, false };


you can then write a helper to process the test:

void process_fun_test( struct fun_test_definition *to_test )
{
    assert( fun( to_test->a, to_test->b ) == to_test->expected_result );
}


The last thing you do is create an array with all the tests you want to carry out in it and then iterate through the lot. This is, however, getting more towards a general unit testing framework and is probably overkill for your needs.

Cheers,

Ash

PS:If you're interested in a different philosophy of unit testing look up Test Driven Development online. With TDD you write tests before you write the code and the tests act as the specification of the code.

PPS: If this is homework for a course make sure you pull the correct buzzwords from your textbook when describing your solution.

PPPS: No idea if this code even works, I haven't checked it on a compiler.

PPPPS: Line removed that showed I didn't read Alison's comment - and to add insult to injury I was spelling her name incorrectly as well. Time for a lie down methinks.
 
Share this answer
 
v3
Comments
LittleYellowBird 4-Nov-10 7:59am    
Hi Ash, I said 'many values' not 'as many as possible' ... I just meant 'some' 'a few' etc. I totally agree with you, and your comments about testing around the boundaries are spot on. Your answer is much more comprehensive than mine and you give some great advice (just had to defend myself a little). Good answer. :)
LittleYellowBird 4-Nov-10 10:09am    
Hi Ash, thanks for changing your answer *thumbs up* :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900