How to test the correctness of a function?

I

istillshine

When I want to test if a function foo in a .c file behaves as
expected, I usually call it in my main.c in the following way.



if (1) { /* test function foo */
z = foo(x, y); /* */
assert(z==1);
}


When I want to test other functions, I change if (1) to if (0).


Are there better (and simpler) ways to test correctness of a function?
 
I

Ian Collins

When I want to test if a function foo in a .c file behaves as
expected, I usually call it in my main.c in the following way.



if (1) { /* test function foo */
z = foo(x, y); /* */
assert(z==1);
}


When I want to test other functions, I change if (1) to if (0).


Are there better (and simpler) ways to test correctness of a function?

Write decent unit tests before you write the function. Write the
function to pass the tests, nothing more.
 
I

istillshine

Write decent unit tests before you write the function. Write the
function to pass the tests, nothing more.

Unit test. I heard of it. Is there any particular tool to do this?
Or I need write a file such as test_foo.c to test a function called
foo?
 
I

Ian Collins

Unit test. I heard of it. Is there any particular tool to do this?
Or I need write a file such as test_foo.c to test a function called
foo?

How can you test something without writing tests? There are C unit test
frameworks like CUnit.
 
U

user923005

How can you test something without writing tests?  There are C unit test
frameworks like CUnit.

As a P.S.:
If the total amount of input into the function is 4 bytes or less, you
can usually write an exhaustive test for every possible input.
At 6 bytes of input and above (booleans only count as 1 bit), you will
often have to tone it down to statistical tests + edge cases.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top