aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2025-08-17 23:36:54 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2025-08-17 23:36:54 +0300
commit7a811406dd16e057204bed1aa15cfe33d81ccb6b (patch)
tree67437eea5c2b922cf3847f4b24b4988b0a566d36 /tests
downloadcovsrv-7a811406dd16e057204bed1aa15cfe33d81ccb6b.tar.gz
covsrv-7a811406dd16e057204bed1aa15cfe33d81ccb6b.zip
initial commit
Diffstat (limited to 'tests')
-rw-r--r--tests/fail.c32
-rw-r--r--tests/pass.c39
-rw-r--r--tests/test.h10
3 files changed, 81 insertions, 0 deletions
diff --git a/tests/fail.c b/tests/fail.c
new file mode 100644
index 0000000..b1cd416
--- /dev/null
+++ b/tests/fail.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <assert.h>
+
+#include "test.h"
+
+/* this is a well behaved program that prints out a smiley face */
+
+int main()
+{
+#if defined(COVERAGE)
+ assert(!covsrv_init());
+ atexit(covsrv_destroy);
+#endif
+
+ /* alloc memory for the smiley */
+ char *p = mallocc(2 * sizeof(char));
+ if (!p) {
+ fprintf(stderr, "oops, malloc failed :(\n");
+ return -1;
+ }
+
+ p[0] = ':';
+ p[1] = ')';
+
+ /* oops, forgot the terminating null, let's realloc
+ * (but in a dangerous way) */
+ p = reallocc(p, 3 * sizeof(char));
+ p[2] = '\0';
+ printf("%s\n", p);
+
+ free(p);
+}
diff --git a/tests/pass.c b/tests/pass.c
new file mode 100644
index 0000000..bd5bfc3
--- /dev/null
+++ b/tests/pass.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <assert.h>
+
+#include "test.h"
+
+/* this is a well behaved program that prints out a smiley face */
+
+int main()
+{
+#if defined(COVERAGE)
+ assert(!covsrv_init());
+ atexit(covsrv_destroy);
+#endif
+
+ /* alloc memory for the smiley */
+ char *p = mallocc(2 * sizeof(char));
+ if (!p) {
+ fprintf(stderr, "oops, malloc failed :(\n");
+ return -1;
+ }
+
+ p[0] = ':';
+ p[1] = ')';
+
+ /* oops, forgot the terminating null, let's realloc */
+ char *new_p = reallocc(p, 3 * sizeof(char));
+ if (!new_p) {
+ fprintf(stderr, "oops, realloc failed :(\n");
+ free(p);
+ return -1;
+ }
+
+ p = new_p;
+
+ p[2] = '\0';
+ printf("%s\n", p);
+
+ free(p);
+}
diff --git a/tests/test.h b/tests/test.h
new file mode 100644
index 0000000..d7832d1
--- /dev/null
+++ b/tests/test.h
@@ -0,0 +1,10 @@
+#ifndef TEST_H
+#define TEST_H
+
+#include <stdlib.h>
+#include <covsrv/covsrv.h>
+
+#define mallocc(n) ({covsrv_die() ? NULL : malloc(n);})
+#define reallocc(p, n) ({covsrv_die() ? NULL : realloc(p, n);})
+
+#endif /* TEST_ALLOC_H */