|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
istillshine@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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.com> wrote:
> 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? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
istillshine@gmail.com wrote:
> On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.com> wrote: > >> 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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Apr 11, 9:40pm, Ian Collins <ian-n...@hotmail.com> wrote:
> istillsh...@gmail.com wrote: > > On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.com> wrote: > > >> 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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
<istillshine@gmail.com> wrote in message
> On Apr 12, 12:18 am, Ian Collins <ian-n...@hotmail.com> wrote: > >> 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 |
|
![]() |
| Outils de la discussion | |
|
|