diff options
author | "Steven J. Hill" <sjhill@realitydiluted.com> | 2005-07-23 14:38:30 +0000 |
---|---|---|
committer | "Steven J. Hill" <sjhill@realitydiluted.com> | 2005-07-23 14:38:30 +0000 |
commit | 6fa52ec791e030081b7102f50222a13af7fa0d49 (patch) | |
tree | 6e27183ba1e77a9b68f1fb44f9418e0a4b45f497 /test/malloc/malloc.c | |
parent | 6a200ab5331e4d93c99adf22372d66633a296d4c (diff) | |
download | uClibc-alpine-6fa52ec791e030081b7102f50222a13af7fa0d49.tar.bz2 uClibc-alpine-6fa52ec791e030081b7102f50222a13af7fa0d49.tar.xz |
Merge from trunk.
Diffstat (limited to 'test/malloc/malloc.c')
-rw-r--r-- | test/malloc/malloc.c | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/test/malloc/malloc.c b/test/malloc/malloc.c index 8e93cf205..d63856845 100644 --- a/test/malloc/malloc.c +++ b/test/malloc/malloc.c @@ -1,6 +1,7 @@ #include <unistd.h> #include <stdlib.h> +#include <string.h> #include <stdio.h> #define N_PTRS 1000 @@ -10,61 +11,71 @@ #define random_size() (random()%MAX_SIZE) #define random_ptr() (random()%N_PTRS) -void test1(void); -void test2(void); +int test1(void); +int test2(void); -int main(int argc,char *argv[]) +int main(int argc, char *argv[]) { - test1(); - test2(); - return 0; + return test1() + test2(); } -void test1(void) +int test1(void) { void **ptrs; int i,j; int size; + int ret = 0; srandom(0x19730929); ptrs = malloc(N_PTRS*sizeof(void *)); - for(i=0;i<N_PTRS;i++){ - ptrs[i]=malloc(random_size()); + for(i=0; i<N_PTRS; i++){ + if ((ptrs[i] = malloc(random_size())) == NULL) { + printf("malloc random failed! %i\n", i); + ++ret; + } } - for(i=0;i<N_ALLOCS;i++){ - j=random_ptr(); + for(i=0; i<N_ALLOCS; i++){ + j = random_ptr(); free(ptrs[j]); - size=random_size(); - ptrs[j]=malloc(size); - if(!ptrs[j]){ - printf("malloc failed! %d\n",i); + size = random_size(); + ptrs[j] = malloc(size); + if (!ptrs[j]) { + printf("malloc failed! %d\n", i); + ++ret; } memset(ptrs[j],0,size); } - for(i=0;i<N_PTRS;i++){ + for(i=0; i<N_PTRS; i++){ free(ptrs[i]); } + + return ret; } -void test2(void) +int test2(void) { void *ptr = NULL; + int ret = 0; ptr = realloc(ptr,100); - if(!ptr){ + if (!ptr) { printf("couldn't realloc() a NULL pointer\n"); - }else{ + ++ret; + } else { free(ptr); } ptr = malloc(100); ptr = realloc(ptr, 0); - if(ptr){ + if (ptr) { printf("realloc(,0) failed\n"); + ++ret; free(ptr); } + + return ret; } |