commit 7f5ab67ddd9c17c709bd7c491ea4a922d144c387
parent 1482e00fea74962b337138777c54021dc066943b
Auteur: Selve <selve@asteride.xyz>
Date: Wed, 24 Jan 2024 17:35:42 -0500
ajout d'assertions aux fonctions de tampon.c
Diffstat:
M | tampon.c | | | 32 | ++++++++++++++++++++++++++++++++ |
1 file changed, 32 insertions(+), 0 deletions(-)
diff --git a/tampon.c b/tampon.c
@@ -1,18 +1,34 @@
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "tampon.h"
+/* invariants de la structure tampon */
+#define INVARIANTS_TP(t) \
+ (assert((t) != NULL), \
+ assert((t)->tp != NULL), \
+ assert((t)->pc != NULL), \
+ assert((t)->tll >= 1), \
+ assert((t)->pc - (t)->tp >= 0), \
+ assert((size_t) ((t)->pc - (t)->tp) <= (t)->tll), \
+ assert((t)->tll * TP_FACT_AGR > (t)->tll))
+
static int tp_plein(char, struct tampon *);
/* initialiser un tampon tp de taille tll */
int
tp_init(struct tampon *tp, size_t tll)
{
+ assert(tll >= 1);
+ assert(tp != NULL);
+
if ((tp->pc = tp->tp = realloc(tp->tp, tll)) == NULL)
return -1;
tp->tll = tll;
+ INVARIANTS_TP(tp);
+
return 0;
}
@@ -20,10 +36,14 @@ tp_init(struct tampon *tp, size_t tll)
int
tp_ecr(char c, struct tampon *tp)
{
+ INVARIANTS_TP(tp);
+
if ((size_t) (tp->pc - tp->tp) == tp->tll) /* le tampon est-il plein? */
return tp_plein(c, tp);
*tp->pc++ = c;
+ INVARIANTS_TP(tp);
+
return (unsigned char) c;
}
@@ -33,6 +53,8 @@ tp_plein(char c, struct tampon *tp)
{
size_t diff;
+ INVARIANTS_TP(tp);
+
diff = tp->pc - tp->tp;
if ((tp->tp = realloc(tp->tp, tp->tll * TP_FACT_AGR)) == NULL)
return -1;
@@ -40,6 +62,8 @@ tp_plein(char c, struct tampon *tp)
tp->pc = tp->tp + diff; /* tp->tp pourrait avoir changé */
*tp->pc++ = c;
+ INVARIANTS_TP(tp);
+
return (unsigned char) c;
}
@@ -50,6 +74,8 @@ tp_aff(struct tampon *tp)
char *pc;
int nc; /* nombre de caractères dans le tampons */
+ INVARIANTS_TP(tp);
+
pc = tp->tp;
nc = tp->pc - tp->tp;
while (nc-- > 0) {
@@ -60,11 +86,17 @@ tp_aff(struct tampon *tp)
}
putc(*pc++, stdout);
}
+
+ INVARIANTS_TP(tp);
}
/* effacer le contenu du tampon t */
void
tp_eff(struct tampon *tp)
{
+ INVARIANTS_TP(tp);
+
tp->pc = tp->tp;
+
+ INVARIANTS_TP(tp);
}