diff options
author | Mike Frysinger <vapier@gentoo.org> | 2005-09-07 22:18:47 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2005-09-07 22:18:47 +0000 |
commit | 21c491215171a282ddf0e8fd198b5221df49adc8 (patch) | |
tree | 914e20ab2bedd109e5301524aef0c25762127c69 /test | |
parent | 71646527ef6c1d635d379c7dcf82ce5dbc14d44b (diff) |
clean up code and make it easy to extend
Diffstat (limited to 'test')
-rw-r--r-- | test/mmap/mmap.c | 62 |
1 files changed, 54 insertions, 8 deletions
diff --git a/test/mmap/mmap.c b/test/mmap/mmap.c index 3649461ac..8b29737a0 100644 --- a/test/mmap/mmap.c +++ b/test/mmap/mmap.c @@ -5,23 +5,69 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <errno.h> #include <unistd.h> #include <sys/mman.h> +#define SIZEOF_ARRAY(type) (sizeof(type)/sizeof(*type)) + +struct mmap_test { + void *ret; + int err; + struct { + void *start; + size_t length; + int prot; + int flags; + int fd; + off_t offset; + } args; +}; + +struct mmap_test tests[] = { + [0] { + .err = 0, + .args.start = NULL, + .args.length = 4096, + .args.prot = PROT_READ|PROT_WRITE, + .args.flags = MAP_PRIVATE|MAP_ANONYMOUS, + .args.fd = 0, + .args.offset = 0 + }, +}; + +#define err(fmt, args...) \ + do { \ + fprintf(stderr, fmt "\n" , ## args); \ + exit(1); \ + } while (0) +#define errp(fmt, args...) err(fmt ": %s" , ## args , strerror(errno)) int main(int argc, char **argv) { - void *ptr; + int i; + struct mmap_test *t; + for (i=0; i<SIZEOF_ARRAY(tests); ++i) { + t = tests + i; - ptr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); + errno = 0; + t->ret = mmap(t->args.start, t->args.length, t->args.prot, + t->args.flags, t->args.fd, t->args.offset); - if(ptr==MAP_FAILED){ - perror("mmap"); - exit(1); + if (t->err) { + if (t->ret != MAP_FAILED) + err("mmap test %i should have failed, but gave us %p", i, t->ret); + else if (t->err != errno) + errp("mmap test %i failed, but gave us wrong errno (got %i instead of %i)", i, errno, t->err); + } else { + if (t->ret == MAP_FAILED) + errp("mmap test %i failed", i); + else if (munmap(t->ret, t->args.length) != 0) + errp("munmap test %i failed", i); + } } - printf("mmap returned %p\n",ptr); + exit(0); } - |