Skip to content
Snippets Groups Projects
Commit fc3d2911 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

ajout tests

parent 6f5fc7e3
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,7 @@ HTMLOPTIONS += -t html5
HTMLOPTIONS += -c css/tufte-css/tufte.css
HTMLOPTIONS += --self-contained
all: make.pdf base_3.pdf base_2.pdf base_1.pdf intro.pdf index.html
all: tests_assertions.pdf make.pdf base_3.pdf base_2.pdf base_1.pdf intro.pdf index.html
intro.pdf: intro.md metadata.yaml
pandoc $(PDFOPTIONS) -o $@ $^
......@@ -36,6 +36,9 @@ base_3.pdf: base_3.md metadata.yaml
make.pdf: make.md metadata.yaml
pandoc $(PDFOPTIONS) -o $@ $^
tests_assertions.pdf: tests_assertions.md metadata.yaml
pandoc $(PDFOPTIONS) -o $@ $^
index.html: index.md
pandoc -s $(OPTIONS) $(HTMLOPTIONS) -o $@ $<
......
% Tests unitaires et assertions
% Inspirés des slides de F. Glück
% 9 octobre 2019
# Tests unitaires
- Compilation `!=` bon fonctionnement!
- Toujours tester vos programmes.
- Tester les fonctionnalités une par une $\Rightarrow$ **tests unitaires**.
- Plus le code est modulaire, plus il est simple à tester.
- Plus le code est testé, moins il aura contiendra de bugs.
*Testing shows the presence, not the absence of bugs.* E. W. Dijkstra.
# Assertions (1/N)
```C
#include <assert.h>
void assert(int expression);
```
## Qu'est-ce donc?
- Macro permettant de tester une condition lors de l'exécution d'un programme:
- Si `expression == 0`{.C} (condition fausse), `assert()`{.C} affiche un message d'erreur sur `stderr`{.C} et termine l'exécution du programme.
- Sinon l'exécution se poursuit normalement.
## À quoi ça sert?
- Permet de réaliser ds tests unitaires.
- Permet de tester des conditions catastrophiques d'un programme.
- **Ne permet pas** de gérer les erreurs.
# Assertions (2/N)
\footnotesize
## Exemple
:::::::::::::: {.columns}
::: {.column width="52%"}
```C
#include <assert.h>
#include <stdlib.h>
void copy(int *src, int *dst, int n) {
// identique à assert(src != NULL)
assert(src); assert(dst);
assert(n >= 0);
for (int i = 0; i < n; ++i) {
dst[i] = src[i];
}
}
void fill(int *dst, int n, int val) {
assert(dst &&
"problem with allocated mem");
assert(n >= 0 &&
"n is the size of dst");
for (int i = 0; i < n; ++i) {
dst[i] = val;
}
}
```
:::
::: {.column width="48%"}
```C
int main(int argc, char **argv) {
int size = 10;
int *src = malloc(size *
sizeof(int));
fill(src, size, 0);
int *dst = malloc(size *
sizeof(int));
copy(src, dst, size);
return EXIT_SUCCESS;
}
```
:::
::::::::::::::
# Assertions (3/N)
## Cas typiques d'utilisation
- Vérification de la validité des pointeurs (typiquement `!= NULL`{.C}).
- Vérification du domaine des indices (dépassement de tableau).
## Bug vs erreur de *runtime*
- Les assertions sont là pour détecter les bugs (erreurs d'implémentation).
- Les assertions ne sont pas là pour gérer les problèmes externes au programme (allocation mémoire qui échoue, mauvais paramètre d'entrée passé par l'utilisateur, ...).
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment