ref: bd23b0292ee5add6989dec35cc75b7c7efcf394d
parent: bbe177e996faa075ea893b04f16cf341d6ea3678
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Oct 19 11:25:09 EDT 2018
[libc] Add test for malloc
--- /dev/null
+++ b/tests/libc/execute/0037-malloc.c
@@ -1,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;
+}
--- 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]