PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > How to test the correctness of a function?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to test the correctness of a function?

Réponse
 
LinkBack Outils de la discussion
Vieux 12/04/2008, 05h14   #1
istillshine@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?
  Réponse avec citation
Vieux 12/04/2008, 05h18   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test the correctness of a function?

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.
  Réponse avec citation
Vieux 12/04/2008, 05h34   #3
istillshine@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test the correctness of a function?

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?
  Réponse avec citation
Vieux 12/04/2008, 05h40   #4
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test the correctness of a function?

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.
  Réponse avec citation
Vieux 12/04/2008, 05h56   #5
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test the correctness of a function?

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.
  Réponse avec citation
Vieux 12/04/2008, 11h02   #6
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to test the correctness of a function?

<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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11002 seconds with 14 queries