473,327 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to test the correctness of a function?

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?
Jun 27 '08 #1
5 7436
is*********@gmail.com wrote:
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.

--
Ian Collins.
Jun 27 '08 #2
On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.comwrote:
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?
Jun 27 '08 #3
is*********@gmail.com wrote:
On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.comwrote:
>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?
How can you test something without writing tests? There are C unit test
frameworks like CUnit.

--
Ian Collins.
Jun 27 '08 #4
On Apr 11, 9:40*pm, Ian Collins <ian-n...@hotmail.comwrote:
istillsh...@gmail.com wrote:
On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.comwrote:
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?

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.
Jun 27 '08 #5
<is*********@gmail.comwrote in message
On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.comwrote:
>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?
Generally you want the test code in a different file to the function code.

If the function has no dependencies and performs no IO then tests are
usually quite straightforwards. You need to write cases so that each
boundary is checked - try for zero, one, a normal number, maximum, and
maximum plus one. Each line of code should be executed at least once.

If the function has dependencies then the process is much more difficult. It
might depend on substantial portions of the program, so the test harness
becomes effectively the whole program. Input might be so difficult to set up
that the unit test becomes extremely difficult to devise. If the function
performs IO then of course you need the harware as well as part of your test
harness.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: lallous | last post by:
Usually you can check for a defined variable as: #ifdef DEF1 // do stuff here #endif Can I test if a function is defined then do things, example: int myFnc() { // blah blah }
7
by: Pietro | last post by:
Hi at all, I am looking for a mean to test if a function work with a certain Browser or not. I'ld like to make a funcrion that return true if the browse is compatible with a certain funcrion or...
4
by: Agnes | last post by:
I create an object named Number , with the function numtowords. i input '100' as parameter, It will return "one hundred". However, any simple way to test this object ???? I only know the old way ...
22
by: Daniel Rucareanu | last post by:
I have the following script: function Test(){} Test.F = function(){} Test.F.FF = function(){} Test.F.FF.FFF = function(){} Test.F.FF.FFF.FFFF = function(){} //var alias = function(){}; var...
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
2
gauravgmbhr
by: gauravgmbhr | last post by:
How to Test (Using Any Testing Tools Like PHPUnit)Those Function that interact with the database SERVER and returns Some Data and the data that comes From the Function is always Volatile I.e each...
4
by: PierpaoloScappatura | last post by:
I need to test private function
3
by: robin1983 | last post by:
Hi guys, I want to know is there any software with that i can test my mail function at any time in my local system. Actually, I m using Wamp and some time I need to test mail() function. As i m...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.