scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 85a3f664472f0e09c5384a62a48e4f7a8bcb2f47
parent 2ab8d22238ac1ea32123311d195d0eb0b7016c20
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 19 Oct 2018 15:25:09 +0100

[libc] Add test for malloc

Diffstat:
Atests/libc/execute/0037-malloc.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/libc/execute/libc-tests.lst | 1+
2 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/tests/libc/execute/0037-malloc.c b/tests/libc/execute/0037-malloc.c @@ -0,0 +1,68 @@ +#include <assert.h> +#include <stdlib.h> + +/* +output: + +end: +*/ + +int +check_test1(void **vec) +{ + int i, cnt; + + for (i = cnt = 0; i < 100; i++) { + if (vec[i]) + cnt++; + } + return cnt; +} + +void +test() +{ + int i,n, which, nalloc; + static void *p[100]; + + nalloc = 0; + for (i = 0; i < 5000; i++) { + which = rand() % 2; + if (which == 0 && nalloc < 100) { + for (n = rand()%100; p[n]; n = rand()%100) + ; + p[n] = malloc(rand() + 1); + nalloc++; + } else if (nalloc > 0) { + n = rand() % 100; + if (p[n]) + nalloc--; + free(p[n]); + p[n] = NULL; + } + assert(check_test1(p) == nalloc); + } + + for (i = 0; i < nalloc; i++) { + for (n = 0; n < 100 && !p[n]; n++) + ; + assert(n < 100); + free(p[n]); + p[n] = NULL; + } + assert(check_test1(p) == 0); + + for (i = 0; i < 100; i++) + assert(p[i] == NULL); + /* TODO: Add a verifier here */ +} + +int +main() +{ + puts("testing"); + test(); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst @@ -35,3 +35,4 @@ 0035-setlocale 0036-localeconv 0037-time +0038-malloc [TODO]