diff options
Diffstat (limited to 'lib/libc_r/TEST')
25 files changed, 700 insertions, 826 deletions
diff --git a/lib/libc_r/TEST/Makefile b/lib/libc_r/TEST/Makefile index 5b47a0121d8..9b95619ccdf 100644 --- a/lib/libc_r/TEST/Makefile +++ b/lib/libc_r/TEST/Makefile @@ -10,32 +10,39 @@ LIBC_R?= /usr/lib/libc_r.a -CFLAGS += -pthread +CPPFLAGS += -pthread +LDFLAGS += -pthread CFLAGS += -ggdb -Wall # -Werror CFLAGS += -DSRCDIR='"${.CURDIR}"' LDLIBS += -lm DPADD += ${LIBC_R} +MKDEP = -p # This list used to include test_select, but that test doesn't terminate. TESTS = test_create test_pthread_join test_switch test_sleep test_readdir \ - test_fork test_execve test_preemption test_preemption_float \ + test_fork test_execve test_preemption \ test_sock_1 test_sock_2 test_stdio_1 test_pthread_mutex \ test_pthread_cond_timedwait test_netdb test_pw test_cwd \ test_sock_2a +SRCS = ${TESTS:=.c} +CLEANFILES += ${TESTS} +SKIP_TESTS += test_sock_2a + # This list used to include p_bench_semaphore, but the semaphore support isn't # defined for all targets (or used for any). + BENCHMARKS = p_bench_read p_bench_mutex p_bench_yield \ p_bench_getpid p_bench_pthread_create -CLEANFILES += ${TESTS} ${BENCHMARKS} +SRCS += ${BENCHMARKS:=.c} +CLEANFILES += ${BENCHMARKS} -SKIP_TESTS = test_sock_2a all : tests # benchmarks tests : ${TESTS} - @faillist= ; ulimit -t 600; \ + @faillist= ; ulimit -t 5; \ for i in ${.ALLSRC} ; do \ case " ${SKIP_TESTS} " in \ *" $$i "*) \ @@ -51,11 +58,12 @@ tests : ${TESTS} esac; \ done; \ if test -n "$$faillist"; then \ - echo; echo "*** FAILED TESTS:$$faillist"; exit 1; \ + echo; echo "*** tests that failed:$$faillist"; exit 1; \ else \ - exit 0; \ + echo; echo "All tests passed OK."; exit 0; \ fi benchmarks: ${BENCHMARKS} .include <bsd.prog.mk> +.include <bsd.dep.mk> diff --git a/lib/libc_r/TEST/p_bench_read.c b/lib/libc_r/TEST/p_bench_read.c index ab504a88fc5..8c9f6a03e48 100644 --- a/lib/libc_r/TEST/p_bench_read.c +++ b/lib/libc_r/TEST/p_bench_read.c @@ -34,8 +34,10 @@ main(int argc, char **argv) struct timeval starttime, endtime; char *infile = "/dev/null"; int count = 1000000; + double elapsed; int debug = 0; int size = 1; + int ignore = 1000; int fd; int i; @@ -79,16 +81,19 @@ main(int argc, char **argv) return 1; } - if (gettimeofday(&starttime, NULL)) { - perror ("gettimeofday"); - return 1; - } + printf("read: "); + fflush(stdout); - for (i = 0; i < count; i++) { + for (i = 0; i < count + ignore; i++) { if (read(fd, word_ptr, size) < OK) { printf("Error: read\n"); exit(0); } + if (i == ignore) + if (gettimeofday(&starttime, NULL)) { + perror ("gettimeofday"); + return 1; + } } if (gettimeofday(&endtime, NULL)) { @@ -96,9 +101,9 @@ main(int argc, char **argv) return 1; } - printf("%d reads of %s took %ld usecs.\n", count, infile, - (endtime.tv_sec - starttime.tv_sec) * 1000000 + - (endtime.tv_usec - starttime.tv_usec)); + elapsed = (double)((endtime.tv_sec - starttime.tv_sec) * 1000000 + + (endtime.tv_usec - starttime.tv_usec)) / 1000000; + printf("%f sec/read\n", elapsed / count); return 0; } diff --git a/lib/libc_r/TEST/test.h b/lib/libc_r/TEST/test.h index 625be5793a7..d667e56794b 100644 --- a/lib/libc_r/TEST/test.h +++ b/lib/libc_r/TEST/test.h @@ -2,52 +2,99 @@ #ifndef _h_test_ #define _h_test_ +#include <stdio.h> #include <signal.h> +#include <errno.h> +#include <string.h> +#include <stdarg.h> -extern int _thread_sys_write __P((int, const char*, size_t)); +int _thread_sys_write __P((int, const char*, size_t)); +__dead void _thread_sys__exit __P((int)) __attribute__((noreturn)); +void _thread_dump_info __P((void)); -static __dead void __panic __P((const char *, int, const char*)) - __attribute__((noreturn)); +static __dead void __vpanic __P((const char *, const char *, const char *, + int, const char *, va_list)) __attribute__((noreturn)); +static __dead void __panic __P((const char *, const char *, const char *, + int, const char *, ...)) __attribute__((noreturn)); -static void __panic(const char *filenm, int lineno, const char*panicstr) { - extern __dead void _thread_sys__exit __P((int)) - __attribute__((noreturn)); - extern size_t strlen __P((const char*)); - extern int sprintf __P((char *, const char *, ...)); +static void +__vpanic(type, errstr, filenm, lineno, fmt, ap) + const char *type; + const char *errstr; + const char *filenm; + int lineno; + const char *fmt; + va_list ap; +{ char buf[1024]; - _thread_sys_write(2, panicstr, strlen(panicstr)); - sprintf(buf, "%s:%d\n", filenm, lineno); + /* "<type> at <filenm>:<lineno>: <fmt ap...>:<errstr>" */ + snprintf(buf, sizeof buf, "%s at %s:%d\n", type, filenm, lineno); + _thread_sys_write(2, buf, strlen(buf)); + vsnprintf(buf, sizeof buf, fmt, ap); + if (errstr != NULL) { + strlcat(buf, ": ", sizeof buf); + strlcat(buf, errstr, sizeof buf); + } + strlcat(buf, "\n", sizeof buf); _thread_sys_write(2, buf, strlen(buf)); - kill(0, SIGINFO); + + _thread_dump_info(); _thread_sys__exit(1); while(1); } -#include <stdarg.h> -#include <string.h> - -static __dead void __die __P((int err, const char *, int, const char *, ...)); -static void __die(int err, const char *file, int line, const char *fmt, ...) - +static void +__panic(type, errstr, filenm, lineno, fmt) + const char *type; + const char *errstr; + const char *filenm; + int lineno; + const char *fmt; { - char buf[1024]; va_list ap; + va_start(ap, fmt); - vsnprintf(buf, sizeof buf, fmt, ap); + __vpanic(type, errstr, filenm, lineno, fmt, ap); va_end(ap); - strlcat(buf, ": ", sizeof buf); - strlcat(buf, strerror(err), sizeof buf); - _thread_sys_write(2, buf, strlen(buf)); - __panic(file, line, "\ndied at: "); - __die(0,0,0,0); } -#define DIE(e, m, args...) __die(e, __FILE__, __LINE__, m , ## args) +#define DIE(e, m, args...) \ + __panic("died", strerror(e), __FILE__, __LINE__, m , ## args) + +#define PANIC(m, args...) \ + __panic("panic", NULL, __FILE__, __LINE__, m, ## args) + +#define ASSERT(x) do { \ + if (!(x)) \ + __panic("assert failed", NULL, __FILE__, __LINE__, "%s", #x); \ +} while(0) + +#define ASSERTe(x,rhs) do { \ + int _x; \ + _x = (x); \ + if (!(_x rhs)) \ + __panic("assert failed", strerror(_x), __FILE__, __LINE__, \ + "%s %s", #x, #rhs); \ +} while(0) + +#define _CHECK(x, rhs, efn) do { \ + int _x; \ + _x = (int)(x); \ + if (!(_x rhs)) \ + __panic("check failed", efn, __FILE__, __LINE__, \ + "failed check %s %s", #x, #rhs); \ +} while(0) + +#define CHECKr(x) _CHECK(x, == 0, strerror(_x)) +#define CHECKe(x) _CHECK(x, != -1, strerror(errno)) +#define CHECKn(x) _CHECK(x, != 0, strerror(errno)) +#define CHECKhn(x) _CHECK(x, != 0, hstrerror(h_errno)) +#define CHECKen(x) _CHECK(x, != 0, strerror(errno)) -#define PANIC() __panic(__FILE__, __LINE__, "\npanic at ") +#define SUCCEED exit(0) -#define OK (0) -#define NOTOK (-1) +#define OK (0) +#define NOTOK (-1) #endif _h_test_ diff --git a/lib/libc_r/TEST/test_create.c b/lib/libc_r/TEST/test_create.c index 73c95140b15..fbf99549c4f 100644 --- a/lib/libc_r/TEST/test_create.c +++ b/lib/libc_r/TEST/test_create.c @@ -18,7 +18,7 @@ void* new_thread(void* arg) printf("New thread was passed arg address %p\n", arg); printf("New thread stack at %p\n", &i); return(NULL); - PANIC(); + PANIC("return"); } int @@ -26,13 +26,10 @@ main() { pthread_t thread; int i; - int ret; printf("Original thread stack at %p\n", &i); - if ((ret = pthread_create(&thread, NULL, new_thread, - (void *)0xdeadbeef))) - DIE(ret, "pthread_create"); - if ((ret = pthread_join(thread, NULL))) - DIE(ret, "pthread_join"); - return(0); + CHECKr(pthread_create(&thread, NULL, new_thread, + (void *)0xdeadbeef)); + CHECKr(pthread_join(thread, NULL)); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_cwd.c b/lib/libc_r/TEST/test_cwd.c index 5bb6401d699..9f19d88ab53 100644 --- a/lib/libc_r/TEST/test_cwd.c +++ b/lib/libc_r/TEST/test_cwd.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <sys/param.h> #include <unistd.h> +#include "test.h" int main(int argc, char **argv) @@ -9,5 +10,5 @@ main(int argc, char **argv) printf("getcwd => %s\n", getcwd(wd, MAXPATHLEN)); printf("getwd => %s\n", getwd(wd)); - exit(0); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_execve.c b/lib/libc_r/TEST/test_execve.c index 81d8a27eade..2e37d6dfb68 100644 --- a/lib/libc_r/TEST/test_execve.c +++ b/lib/libc_r/TEST/test_execve.c @@ -12,13 +12,12 @@ #include <unistd.h> #include <fcntl.h> #include <stdio.h> -#include <err.h> #include "test.h" extern char **environ; char *argv[] = { "/bin/echo", - "This message should be displayed after the execve system call", + "This line should appear after the execve", NULL }; @@ -34,23 +33,17 @@ main() if (isatty(1)) { char *ttynm; - ttynm = ttyname(1); + CHECKn(ttynm = ttyname(1)); printf("tty is %s\n", ttynm); - if ((fd = open(ttynm, O_RDWR)) < OK) - err(1, "%s", ttynm); + CHECKe(fd = open(ttynm, O_RDWR)); } else - errx(1, "stdout not a tty"); + PANIC("stdout not a tty"); - printf("This output is necessary to set the stdout fd to NONBLOCKING\n"); + CHECKn(printf("This output is necessary to set the stdout fd to NONBLOCKING\n")); /* do a dup2 */ - dup2(fd, 1); - write(1, should_succeed, (size_t)strlen(should_succeed)); -#if i_understood_this - _thread_sys_write(1, should_fail, strlen(should_fail)); -#endif - if (execve(argv[0], argv, environ) < OK) - err(1, "execve"); - - PANIC(); + CHECKe(dup2(fd, 1)); + CHECKe(write(1, should_succeed, (size_t)strlen(should_succeed))); + CHECKe(execve(argv[0], argv, environ)); + DIE(errno, "execve %s", argv[0]); } diff --git a/lib/libc_r/TEST/test_fcntl.c b/lib/libc_r/TEST/test_fcntl.c index 60bc77ce464..bfe38cb16ec 100644 --- a/lib/libc_r/TEST/test_fcntl.c +++ b/lib/libc_r/TEST/test_fcntl.c @@ -1,32 +1,27 @@ #include <stdio.h> #include <fcntl.h> +#include "test.h" main() { int flags, child; - if ((flags = fcntl(0, F_GETFL)) < 0) { - perror("fcntl 1st GETFL"); - } - printf ("flags = %x\n", flags); + CHECKe(flags = fcntl(0, F_GETFL)); + printf("flags = %x\n", flags); - switch(child = fork()) { - case -1: - printf("error during fork\n"); - break; + CHECKe(child = fork()); + switch(child) { case 0: /* child */ - execlp("test_create", "test_create", NULL); - break; + CHECKe(execlp("test_create", "test_create", NULL)); + /* NOTREACHED */ default: /* parent */ - wait(NULL); + CHECKe(wait(NULL)); break; } while(1){ - if ((flags = fcntl(0, F_GETFL)) < 0) { - perror("fcntl parent GETFL"); - } - printf ("parent %d flags = %x\n", child, flags); - sleep(1); + CHECKe(flags = fcntl(0, F_GETFL)); + printf ("parent %d flags = %x\n", child, flags); + sleep(1); } } diff --git a/lib/libc_r/TEST/test_fork.c b/lib/libc_r/TEST/test_fork.c index 8d44b38c345..efaafb0a7ba 100644 --- a/lib/libc_r/TEST/test_fork.c +++ b/lib/libc_r/TEST/test_fork.c @@ -8,37 +8,71 @@ */ #include <pthread.h> +#include <pthread_np.h> #include <stdio.h> #include <fcntl.h> #include <sys/types.h> #include <unistd.h> +#include <signal.h> +#include <sys/wait.h> #include "test.h" +void * +sleeper(void *arg) +{ + pthread_set_name_np(pthread_self(), "slpr"); + printf("sleeper\n"); + sleep(10); + PANIC("sleeper timed out"); +} + +static void +sigchld(sig) + int sig; +{ + int status; + + ASSERT(sig == SIGCHLD); + CHECKe(wait(&status)); + ASSERT(WIFEXITED(status)); + ASSERT(WEXITSTATUS(status) == 0); + SUCCEED; +} + int main() { int flags; pid_t pid; - extern int _thread_sys_fcntl __P((int, int, ...)); + pthread_t sleeper_thread; - if (((flags = _thread_sys_fcntl(1, F_GETFL, NULL)) >= OK) && - (flags & (O_NONBLOCK | O_NDELAY))) { - _thread_sys_fcntl(1, F_SETFL, flags & ~(O_NONBLOCK | O_NDELAY)); - } - printf("parent process %d\n", getpid()); - - switch(pid = fork()) { - case OK: - exit(OK); - case NOTOK: - printf("fork() FAILED\n"); - exit(2); - default: - printf("child process %d\n", pid); - break; + CHECKe(flags = fcntl(STDOUT_FILENO, F_GETFL)); + if ((flags & (O_NONBLOCK | O_NDELAY))) { + CHECKe(fcntl(STDOUT_FILENO, F_SETFL, + flags & ~(O_NONBLOCK | O_NDELAY))); } - printf("test_fork PASSED\n"); + CHECKr(pthread_create(&sleeper_thread, NULL, sleeper, NULL)); + sleep(1); + + CHECKe(signal(SIGCHLD, sigchld)); - return 0; + printf("forking\n"); + + CHECKe(pid = fork()); + switch(pid) { + case 0: + sleep(1); + printf("child process %d\n", getpid()); + _thread_dump_info(); + printf("\n"); + _exit(0); + PANIC("_exit"); + default: + printf("parent process %d [child %d]\n", getpid(), pid); + _thread_dump_info(); + printf("\n"); + CHECKe(pause()); + PANIC("pause"); + } } diff --git a/lib/libc_r/TEST/test_netdb.c b/lib/libc_r/TEST/test_netdb.c index 3c075231461..b4853030a5f 100644 --- a/lib/libc_r/TEST/test_netdb.c +++ b/lib/libc_r/TEST/test_netdb.c @@ -17,102 +17,51 @@ #include <unistd.h> #include "test.h" -struct servent * getservbyname_r __P((const char *, const char *, struct servent *, char *, int)); -struct hostent * gethostbyname_r __P((const char *, struct hostent *, char *, int, int *)); - -int debug = 0; - -static int test_serv() +static void test_serv() { struct servent *serv; char answer[1024]; - if ((serv = getservbyname("telnet", "tcp")) != NULL) - printf("getservbyname -> port %d\n", ntohs(serv->s_port)); - else - printf("getservbyname -> NULL (bad)\n"); + CHECKhn(serv = getservbyname("telnet", "tcp")); + printf("getservbyname -> port %d\n", ntohs(serv->s_port)); - if ((serv = getservbyname_r("telnet", "tcp", serv, answer, 1024))!=NULL) - printf("getservbyname_r -> port %d\n", ntohs(serv->s_port)); - else - printf("getservbyname_r -> NULL (bad)\n"); - return(OK); + CHECKhn(serv = getservbyname_r("telnet", "tcp", serv, answer, 1024)); + printf("getservbyname_r -> port %d\n", ntohs(serv->s_port)); } -static int test_host() +static void test_host() { struct hostent *host; struct in_addr addr; char answer[1024]; int error; - if ((host = gethostbyname("maze.mit.edu")) != NULL) { - memcpy(&addr, host->h_addr, sizeof(addr)); - printf("gethostbyname -> %s\n", inet_ntoa(addr)); - } else { - printf("gethostbyname -> NULL (bad)\n"); - host = (struct hostent *)answer; - } + CHECKhn(host = gethostbyname("localhost")); + memcpy(&addr, host->h_addr, sizeof(addr)); + printf("gethostbyname -> %s\n", inet_ntoa(addr)); - if ((host = gethostbyname_r("maze.mit.edu", host, answer, 1024, &error)) + if ((host = gethostbyname_r("localhost", host, answer, 1024, &error)) != NULL) { memcpy(&addr, host->h_addr, sizeof(addr)); printf("gethostbyname_r -> %s\n", inet_ntoa(addr)); } else { - printf("gethostbyname_r -> NULL (bad)\n"); + DIE(error, "gethostbyname_r"); } - return(OK); } -static int test_localhost() +static void test_localhost() { - struct hostent *host; - - if ((host = gethostbyname("127.0.0.1")) != NULL) { - return(OK); - } - return(NOTOK); -} + struct hostent *host; -/* ========================================================================== - * usage(); - */ -void usage(void) -{ - printf("test_netdb [-d?]\n"); - errno = 0; + CHECKhn(host = gethostbyname("127.0.0.1")); } int main(int argc, char **argv) { + test_serv(); + test_localhost(); + test_host(); - /* Getopt variables. */ - extern int optind, opterr; - extern char *optarg; - char ch; - - while ((ch = getopt(argc, argv, "d?")) != (char)EOF) { - switch (ch) { - case 'd': - debug++; - break; - case '?': - usage(); - return(OK); - default: - usage(); - return(NOTOK); - } - } - - printf("test_netdb START\n"); - - if (test_serv() || test_localhost() || test_host()) { - printf("test_netdb FAILED\n"); - exit(1); - } - - printf("test_netdb PASSED\n"); - exit(0); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_pause.c b/lib/libc_r/TEST/test_pause.c index 46c5080e43e..ab71982d7d6 100644 --- a/lib/libc_r/TEST/test_pause.c +++ b/lib/libc_r/TEST/test_pause.c @@ -1,8 +1,11 @@ #include <stdio.h> #include <signal.h> +#include <string.h> +#include "test.h" foo(int sig) { + printf("%s\n", strsignal(sig)); return; } @@ -10,10 +13,9 @@ main() { sigset_t all; - signal (1, foo); - sigfillset(&all); - sigprocmask(SIG_BLOCK, &all, NULL); - printf("Begin pause\n"); - pause(); - printf("Done pause\n"); + CHECKe(signal(1, foo)); + CHECKe(sigfillset(&all)); + CHECKe(sigprocmask(SIG_BLOCK, &all, NULL)); + CHECKe(pause()); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_preemption.c b/lib/libc_r/TEST/test_preemption.c index 9acac1e7986..54ca1b9117e 100644 --- a/lib/libc_r/TEST/test_preemption.c +++ b/lib/libc_r/TEST/test_preemption.c @@ -15,29 +15,24 @@ void* new_thread(void * new_buf) { int i; - printf("new_thread\n"); + printf("yielding:"); for (i = 0; i < 10; i++) { - printf("yielding "); + printf(" %d", i); fflush(stdout); pthread_yield(); } - printf("yielded 10 times ok\n"); - exit(0); + printf("\n"); + SUCCEED; } int main() { pthread_t thread; - int ret; - printf("test_preemption START\n"); + CHECKr(pthread_create(&thread, NULL, new_thread, NULL)); - if ((ret = pthread_create(&thread, NULL, new_thread, NULL))) - DIE(ret, "pthread_create"); - - while(1); - /* NOTREACHED */ - - return (1); + while(1) + ; + PANIC("while"); } diff --git a/lib/libc_r/TEST/test_preemption_float.c b/lib/libc_r/TEST/test_preemption_float.c index 621297001a5..48246f577ed 100644 --- a/lib/libc_r/TEST/test_preemption_float.c +++ b/lib/libc_r/TEST/test_preemption_float.c @@ -7,6 +7,7 @@ #include <pthread.h> #include <math.h> #include <stdio.h> +#include "test.h" int limit = 2; int float_passed = 0; @@ -58,39 +59,56 @@ void *trig_loop (void *x) { pthread_exit(&float_passed); } +int +floatloop(pthread_attr_t *attrp) +{ + pthread_t thread[2]; + int *x, *y; + + CHECKr(pthread_create (&thread[0], attrp, trig_loop, 0)); + CHECKr(pthread_create (&thread[1], attrp, log_loop, 0)); + CHECKr(pthread_join(thread[0], (void **) &x)); + CHECKr(pthread_join(thread[1], (void **) &y)); + + /* Return 0 for success */ + return ((*y == float_failed)?2:0) | + ((*x == float_failed)?1:0); +} + #define N 10 -int main () { - pthread_t thread[2]; - pthread_attr_t attr; - int *x, *y; - - pthread_attr_init(&attr); - /* pthread_attr_setfloatstate(&attr, PTHREAD_NOFLOAT); */ - - while(limit < 100000) { - pthread_create (&thread[0], &attr, trig_loop, 0); - pthread_create (&thread[1], &attr, log_loop, 0); - pthread_join(thread[0], (void **) &x); - pthread_join(thread[1], (void **) &y); - if ((*x == float_failed) || (*y == float_failed)) { - limit *= 4; - break; +int +main() +{ + pthread_attr_t attr; + int i; + + /* Try with float point state not preserved */ + + CHECKr(pthread_attr_init(&attr)); + CHECKr(pthread_attr_setfloatstate(&attr, PTHREAD_NOFLOAT)); + + for(limit = 2; limit < 100000; limit *=4) + if (floatloop(&attr) != 0) + break; + + if (limit >= 100000) { + printf("results are INDETERMINATE\n"); + SUCCEED; /* XXX */ } - limit *= 4; - } - if ((*x == float_passed) && (*y == float_passed)) { - printf("test_preemption_float INDETERMINATE\n"); - return(0); - } - pthread_create (&thread[0], NULL, trig_loop, 0); - pthread_create (&thread[1], NULL, log_loop, 0); - pthread_join(thread[0], (void **) &x); - pthread_join(thread[1], (void **) &y); - - if ((*x == float_failed) || (*y == float_failed)) { - printf("test_preemption_float FAILED\n"); - return(1); - } - printf("test_preemption_float PASSED\n"); - return(0); + + limit *= 4; /* just to make sure */ + + printf("using limit = %d\n", limit); + + for (i = 0; i < 32; i++) { + /* Try the failure mode one more time. */ + if (floatloop(&attr) == 0) { + printf("%d ", i); + fflush(stdout); + } + /* Now see if saving float state will get rid of failure. */ + ASSERT(floatloop(NULL) == 0); + } + + SUCCEED; } diff --git a/lib/libc_r/TEST/test_pthread_cond_timedwait.c b/lib/libc_r/TEST/test_pthread_cond_timedwait.c index 9942f3aa26e..95cf2a8d466 100644 --- a/lib/libc_r/TEST/test_pthread_cond_timedwait.c +++ b/lib/libc_r/TEST/test_pthread_cond_timedwait.c @@ -11,28 +11,25 @@ #include <stdio.h> #include <errno.h> #include <unistd.h> - -#ifndef ETIME -#define ETIME ETIMEDOUT -#endif +#include "test.h" pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; void* thread_1(void * new_buf) { - pthread_mutex_lock(&mutex); - pthread_cond_signal(&cond); - pthread_mutex_unlock(&mutex); + CHECKr(pthread_mutex_lock(&mutex)); + CHECKr(pthread_cond_signal(&cond)); + CHECKr(pthread_mutex_unlock(&mutex)); pthread_exit(NULL); } void* thread_2(void * new_buf) { sleep(1); - pthread_mutex_lock(&mutex); - pthread_cond_signal(&cond); - pthread_mutex_unlock(&mutex); + CHECKr(pthread_mutex_lock(&mutex)); + CHECKr(pthread_cond_signal(&cond)); + CHECKr(pthread_mutex_unlock(&mutex)); pthread_exit(NULL); } @@ -42,52 +39,41 @@ main() struct timespec abstime = { 0, 0 }; struct timeval curtime; pthread_t thread; + int ret; printf("pthread_cond_timedwait START\n"); - pthread_mutex_lock(&mutex); - gettimeofday(&curtime, NULL); + CHECKr(pthread_mutex_lock(&mutex)); + CHECKe(gettimeofday(&curtime, NULL)); abstime.tv_sec = curtime.tv_sec + 5; /* Test a condition timeout */ - if (pthread_cond_timedwait(&cond, &mutex, &abstime) != ETIME) { - printf("pthread_cond_timedwait failed to timeout\n"); - printf("pthread_cond_timedwait FAILED\n"); - pthread_mutex_unlock(&mutex); - exit(1); + switch((ret = pthread_cond_timedwait(&cond, &mutex, &abstime))) { + case 0: + PANIC("pthread_cond_timedwait #0 failed to timeout"); + /* NOTREACHED */ + case ETIMEDOUT: + /* expected behaviour */ + printf("Got first timeout ok\n"); /* Added by monty */ + break; + default: + DIE(ret, "pthread_cond_timedwait"); + /* NOTREACHED */ } - printf("Got first timeout ok\n"); /* Added by monty */ + /* Test a normal condition signal */ - if (pthread_create(&thread, NULL, thread_1, NULL)) { - printf("pthread_create failed\n"); - exit(2); - } + CHECKr(pthread_create(&thread, NULL, thread_1, NULL)); abstime.tv_sec = curtime.tv_sec + 10; - if (pthread_cond_timedwait(&cond, &mutex, &abstime)) { - printf("pthread_cond_timedwait #1 timedout\n"); - printf("pthread_cond_timedwait FAILED\n"); - pthread_mutex_unlock(&mutex); - exit(1); - } + CHECKr(pthread_cond_timedwait(&cond, &mutex, &abstime)); /* Test a normal condition signal after a sleep */ - if (pthread_create(&thread, NULL, thread_2, NULL)) { - printf("pthread_create failed\n"); - exit(2); - } + CHECKr(pthread_create(&thread, NULL, thread_2, NULL)); pthread_yield(); abstime.tv_sec = curtime.tv_sec + 10; - if (pthread_cond_timedwait(&cond, &mutex, &abstime)) { - printf("pthread_cond_timedwait #2 timedout\n"); - printf("pthread_cond_timedwait FAILED\n"); - pthread_mutex_unlock(&mutex); - exit(1); - } + CHECKr(pthread_cond_timedwait(&cond, &mutex, &abstime)); - printf("pthread_cond_timedwait PASSED\n"); - pthread_mutex_unlock(&mutex); - exit(0); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_pthread_join.c b/lib/libc_r/TEST/test_pthread_join.c index 0d5f29a916c..a4331e0d8db 100644 --- a/lib/libc_r/TEST/test_pthread_join.c +++ b/lib/libc_r/TEST/test_pthread_join.c @@ -8,6 +8,7 @@ */ #include <pthread.h> +#include <unistd.h> #include <stdio.h> #include "test.h" @@ -17,9 +18,10 @@ void* new_thread_1(void * new_buf) int i; sprintf((char *)new_buf, "New thread %%d stack at %p\n", &i); - pthread_yield(); + pthread_yield(); /* (ensure parent can wait on live thread) */ + sleep(1); return(new_buf); - PANIC(); + PANIC("return"); } /* This thread doesn't yield so the creator has a dead thread to wait on */ @@ -29,7 +31,7 @@ void* new_thread_2(void * new_buf) sprintf((char *)new_buf, "New thread %%d stack at %p\n", &i); return(new_buf); - PANIC(); + PANIC("return"); } int @@ -40,38 +42,23 @@ main() int debug = 1; int i = 0; - printf("Original thread stack at %p\n", &i); - if (pthread_create(&thread, NULL, new_thread_1, (void *)buf) == 0) { - if (pthread_join(thread, (void **)(&status)) == 0) { - if (debug) { printf(status, ++i); } - } else { - printf("ERROR: Joining with new thread #1.\n"); - printf("FAILED: test_pthread_join\n"); - exit(1); - } - } else { - printf("ERROR: Creating new thread #1\n"); - printf("FAILED: test_pthread_join\n"); - exit(2); - } + if (debug) + printf("Original thread stack at %p\n", &i); + CHECKr(pthread_create(&thread, NULL, new_thread_1, (void *)buf)); + CHECKr(pthread_join(thread, (void **)(&status))); + if (debug) + printf(status, ++i); /* Now have the created thread finishing before the join. */ - if (pthread_create(&thread, NULL, new_thread_2, (void *)buf) == 0){ - pthread_yield(); - if (pthread_join(thread, (void **)(&status)) == 0) { - if (debug) { printf(status, ++i); } - } else { - printf("ERROR: Joining with new thread #2.\n"); - printf("FAILED: test_pthread_join\n"); - exit(1); - } - } else { - printf("ERROR: Creating new thread #2\n"); - printf("FAILED: test_pthread_join\n"); - exit(2); - } - printf("test_pthread_join PASSED\n"); - return(0); + CHECKr(pthread_create(&thread, NULL, new_thread_2, (void *)buf)); + pthread_yield(); + sleep(1); /* (ensure thread is dead) */ + CHECKr(pthread_join(thread, (void **)(&status))); + + if (debug) + printf(status, ++i); + + SUCCEED; } diff --git a/lib/libc_r/TEST/test_pthread_mutex.c b/lib/libc_r/TEST/test_pthread_mutex.c index 251ba90f75f..00d5616d0d4 100644 --- a/lib/libc_r/TEST/test_pthread_mutex.c +++ b/lib/libc_r/TEST/test_pthread_mutex.c @@ -7,219 +7,128 @@ * -Started coding this file. */ -#include <errno.h> #include <pthread.h> +#include <pthread_np.h> #include <stdio.h> #include "test.h" - int contention_variable; -void * thread_contention(void * arg) +void * +thread_contention(arg) + void *arg; { - pthread_mutex_t * mutex = arg; - - if (pthread_mutex_lock(mutex)) { - printf("pthread_mutex_lock() ERROR\n"); - pthread_exit(NULL); - } + pthread_mutex_t *mutex = arg; - if (contention_variable != 1) { - printf("contention_variable != 1 ERROR\n"); - pthread_exit(NULL); - } + CHECKr(pthread_mutex_lock(mutex)); + ASSERT(contention_variable == 1); contention_variable = 2; - - if (pthread_mutex_unlock(mutex)) { - printf("pthread_mutex_unlock() ERROR\n"); - pthread_exit(NULL); - } + CHECKr(pthread_mutex_unlock(mutex)); pthread_exit(NULL); } -int test_contention_lock(pthread_mutex_t * mutex) +void +test_contention_lock(mutex) + pthread_mutex_t *mutex; { pthread_t thread; - printf("test_contention_lock()\n"); - - if (pthread_mutex_lock(mutex)) { - printf("pthread_mutex_lock() ERROR\n"); - return(NOTOK); - } + printf(" test_contention_lock()\n"); + CHECKr(pthread_mutex_lock(mutex)); contention_variable = 0; - - if (pthread_create(&thread, NULL, thread_contention, mutex)) { - printf("pthread_create() FAILED\n"); - exit(2); - } - + CHECKr(pthread_create(&thread, NULL, thread_contention, mutex)); + pthread_set_name_np(thread, "cntntn"); pthread_yield(); - contention_variable = 1; - - if (pthread_mutex_unlock(mutex)) { - printf("pthread_mutex_unlock() ERROR\n"); - return(NOTOK); - } - - if (pthread_mutex_lock(mutex)) { - printf("pthread_mutex_lock() ERROR\n"); - return(NOTOK); - } - - if (contention_variable != 2) { - printf("contention_variable != 2 ERROR\n"); - return(NOTOK); - } - - if (pthread_mutex_unlock(mutex)) { - printf("pthread_mutex_unlock() ERROR\n"); - return(NOTOK); - } - - return(OK); + CHECKr(pthread_mutex_unlock(mutex)); + CHECKr(pthread_mutex_lock(mutex)); + ASSERT(contention_variable == 2); + CHECKr(pthread_mutex_unlock(mutex)); } -int test_nocontention_lock(pthread_mutex_t * mutex) +void +test_nocontention_lock(mutex) + pthread_mutex_t *mutex; { - printf("test_nocontention_lock()\n"); - if (pthread_mutex_lock(mutex)) { - printf("pthread_mutex_lock() ERROR\n"); - return(NOTOK); - } - if (pthread_mutex_unlock(mutex)) { - printf("pthread_mutex_unlock() ERROR\n"); - return(NOTOK); - } - return(OK); + printf(" test_nocontention_lock()\n"); + CHECKr(pthread_mutex_lock(mutex)); + CHECKr(pthread_mutex_unlock(mutex)); } -int test_debug_double_lock(pthread_mutex_t * mutex) +void +test_debug_double_lock(mutex) + pthread_mutex_t *mutex; { - int ret; - - printf("test_debug_double_lock()\n"); - if (pthread_mutex_lock(mutex)) { - printf("pthread_mutex_lock() ERROR\n"); - return(NOTOK); - } - if ((ret = pthread_mutex_lock(mutex)) != EDEADLK) { - DIE(ret, "test_debug_double_lock: pthread_mutex_lock"); - printf("double lock error not detected ERROR\n"); - return(NOTOK); - } - if (pthread_mutex_unlock(mutex)) { - printf("pthread_mutex_unlock() ERROR\n"); - return(NOTOK); - } - return(OK); + printf(" test_debug_double_lock()\n"); + CHECKr(pthread_mutex_lock(mutex)); + ASSERTe(pthread_mutex_lock(mutex), == EDEADLK); + CHECKr(pthread_mutex_unlock(mutex)); } -int test_debug_double_unlock(pthread_mutex_t * mutex) +void +test_debug_double_unlock(mutex) + pthread_mutex_t *mutex; { - int ret; - - printf("test_debug_double_unlock()\n"); - if (pthread_mutex_lock(mutex)) { - printf("pthread_mutex_lock() ERROR\n"); - return(NOTOK); - } - if (pthread_mutex_unlock(mutex)) { - printf("pthread_mutex_unlock() ERROR\n"); - return(NOTOK); - } - if ((ret = pthread_mutex_unlock(mutex)) != EPERM) { - DIE(ret, "test_debug_double_unlock: mutex_unlock2"); - printf("double unlock error not detected ERROR\n"); - return(NOTOK); - } - return(OK); + printf(" test_debug_double_unlock()\n"); + CHECKr(pthread_mutex_lock(mutex)); + CHECKr(pthread_mutex_unlock(mutex)); + ASSERTe(pthread_mutex_unlock(mutex), == EPERM); } -int test_nocontention_trylock(pthread_mutex_t * mutex) +void +test_nocontention_trylock(mutex) + pthread_mutex_t *mutex; { - printf("test_nocontention_trylock()\n"); - if (pthread_mutex_trylock(mutex)) { - printf("pthread_mutex_trylock() ERROR\n"); - return(NOTOK); - } - if (pthread_mutex_unlock(mutex)) { - printf("pthread_mutex_unlock() ERROR\n"); - return(NOTOK); - } - return(OK); + printf(" test_nocontention_trylock()\n"); + CHECKr(pthread_mutex_trylock(mutex)); + CHECKr(pthread_mutex_unlock(mutex)); } -int test_mutex_static(void) +void +test_mutex_static() { pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER; printf("test_mutex_static()\n"); - if (test_nocontention_lock(&mutex_static) || - test_contention_lock(&mutex_static)) { - return(NOTOK); - } - return(OK); + test_nocontention_lock(&mutex_static); + test_contention_lock(&mutex_static); } -int test_mutex_fast(void) +void +test_mutex_fast(void) { pthread_mutex_t mutex_fast; printf("test_mutex_fast()\n"); - if (pthread_mutex_init(&mutex_fast, NULL)) { - printf("pthread_mutex_init() ERROR\n"); - return(NOTOK); - } - if (test_nocontention_lock(&mutex_fast) || - test_contention_lock(&mutex_fast)) { - return(NOTOK); - } - if (pthread_mutex_destroy(&mutex_fast)) { - printf("pthread_mutex_destroy() ERROR\n"); - return(NOTOK); - } - return(OK); + CHECKr(pthread_mutex_init(&mutex_fast, NULL)); + test_nocontention_lock(&mutex_fast); + test_contention_lock(&mutex_fast); + CHECKr(pthread_mutex_destroy(&mutex_fast)); } -int test_mutex_debug() +void +test_mutex_debug() { pthread_mutexattr_t mutex_debug_attr; pthread_mutex_t mutex_debug; printf("test_mutex_debug()\n"); - pthread_mutexattr_init(&mutex_debug_attr); - pthread_mutexattr_settype(&mutex_debug_attr, PTHREAD_MUTEX_ERRORCHECK); - if (pthread_mutex_init(&mutex_debug, &mutex_debug_attr)) { - printf("pthread_mutex_init() ERROR\n"); - return(NOTOK); - } - if (test_nocontention_lock(&mutex_debug) || - test_contention_lock(&mutex_debug) || - test_debug_double_lock(&mutex_debug) || - test_debug_double_unlock(&mutex_debug)) { - return(NOTOK); - } - if (pthread_mutex_destroy(&mutex_debug)) { - printf("pthread_mutex_destroy() ERROR\n"); - return(NOTOK); - } - return(OK); + CHECKr(pthread_mutexattr_init(&mutex_debug_attr)); + CHECKr(pthread_mutexattr_settype(&mutex_debug_attr, + PTHREAD_MUTEX_ERRORCHECK)); + CHECKr(pthread_mutex_init(&mutex_debug, &mutex_debug_attr)); + test_nocontention_lock(&mutex_debug); + test_contention_lock(&mutex_debug); + test_debug_double_lock(&mutex_debug); + test_debug_double_unlock(&mutex_debug); + CHECKr(pthread_mutex_destroy(&mutex_debug)); } int main() { - - printf("test_pthread_mutex START\n"); - - if (test_mutex_static() || test_mutex_fast() || test_mutex_debug()) { - printf("test_pthread_mutex FAILED\n"); - exit(1); - } - - printf("test_pthread_mutex PASSED\n"); - exit(0); + test_mutex_static(); + test_mutex_fast(); + test_mutex_debug(); + SUCCEED; } - diff --git a/lib/libc_r/TEST/test_pw.c b/lib/libc_r/TEST/test_pw.c index d7740a109eb..b99e7cc37fd 100644 --- a/lib/libc_r/TEST/test_pw.c +++ b/lib/libc_r/TEST/test_pw.c @@ -2,20 +2,18 @@ #include <sys/types.h> #include <unistd.h> #include <pwd.h> +#include "test.h" int main() { - struct passwd *pw; + struct passwd *pw; - pw = getpwuid(getuid()); - if (!pw) { - printf("getpwuid(%d) died!\n", getuid()); - exit(1); - } - printf("getpwuid(%d) => %p\n", getuid(), pw); - printf(" you are: %s\n uid: %d\n gid: %d\n class: %s\n gecos: %s\n dir: %s\n shell: %s\n", - pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_class, pw->pw_gecos, pw->pw_dir, - pw->pw_shell); - exit(0); + CHECKen(pw = getpwuid(getuid())); + printf("getpwuid(%d) => %p\n", getuid(), pw); + printf(" name: %s\n uid: %d\n gid: %d\n" + "class: %s\ngecos: %s\n dir: %s\nshell: %s\n", + pw->pw_name, pw->pw_uid, pw->pw_gid, + pw->pw_class, pw->pw_gecos, pw->pw_dir, pw->pw_shell); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_readdir.c b/lib/libc_r/TEST/test_readdir.c index 62167fa3100..39ca3e4201d 100644 --- a/lib/libc_r/TEST/test_readdir.c +++ b/lib/libc_r/TEST/test_readdir.c @@ -11,6 +11,7 @@ #include <sys/types.h> #include <dirent.h> #include <stdio.h> +#include "test.h" int main() @@ -19,23 +20,12 @@ main() DIR * dot_dir; int found = 0; - if ((dot_dir = opendir(".")) != NULL) { - while ((file = readdir(dot_dir)) != NULL) { - if (strcmp("test_readdir", file->d_name) == 0) { - found = 1; - } - } - closedir(dot_dir); - if (found) { - printf("test_readdir PASSED\n"); - exit(0); - } else { - printf("Couldn't find file test_readdir ERROR\n"); - } - } else { - printf("opendir() ERROR\n"); - } - printf("test_readdir FAILED\n"); - exit(1); + CHECKen(dot_dir = opendir(".")); + while ((file = readdir(dot_dir)) != NULL) + if (strcmp("test_readdir", file->d_name) == 0) + found = 1; + CHECKe(closedir(dot_dir)); + ASSERT(found); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_select.c b/lib/libc_r/TEST/test_select.c index 0401d77a666..fedd8295318 100644 --- a/lib/libc_r/TEST/test_select.c +++ b/lib/libc_r/TEST/test_select.c @@ -1,32 +1,28 @@ #include <pthread.h> +#include <pthread_np.h> #include <stdio.h> -#ifndef ultrix #include <sys/fcntl.h> -#else /* ultrix */ -#include <fcntl.h> -#endif /* !ultrix */ #include <sys/types.h> #include <sys/time.h> -#ifdef hpux -#include <sys/file.h> -#endif /* hpux */ #include <errno.h> +#include <unistd.h> +#include "test.h" #define NLOOPS 1000 int ntouts = 0; void * -bg_routine(void *arg) +bg_routine(arg) + void *arg; { - write(1,"bg routine running\n",19); + char dot = '.'; + pthread_set_name_np(pthread_self(), "bg"); + write(STDOUT_FILENO,"bg routine running\n",19); /*pthread_dump_state();*/ while (1) { int n; - char dot; - - dot = '.'; pthread_yield(); - write(1,&dot,1); + write(STDOUT_FILENO, &dot, sizeof dot); pthread_yield(); n = NLOOPS; while (n-- > 0) @@ -35,81 +31,68 @@ bg_routine(void *arg) } void * -fg_routine(void *arg) +fg_routine(arg) + void *arg; { - int flags, stat, nonblock_flag; - static struct timeval tout = { 0, 500000 }; + int flags; + /* static struct timeval tout = { 0, 500000 }; */ + int n; + fd_set r; + + pthread_set_name_np(pthread_self(), "fg"); + + flags = fcntl(STDIN_FILENO, F_GETFL); + CHECKr(fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK)); -#if 0 -#if defined(hpux) || defined(__alpha) - nonblock_flag = O_NONBLOCK; -#else - nonblock_flag = FNDELAY; -#endif - printf("fg_routine running\n"); - flags = fcntl(0, F_GETFL, 0); - printf("stdin flags b4 anything = %x\n", flags); - stat = fcntl(0, F_SETFL, flags | nonblock_flag); - if (stat < 0) { - printf("fcntl(%x) => %d\n", nonblock_flag, errno); - printf("could not set nonblocking i/o on stdin [oldf %x, stat %d]\n", - flags, stat); - exit(1); - } - printf("stdin flags = 0x%x after turning on %x\n", flags, nonblock_flag); -#endif while (1) { - int n; - fd_set r; + int maxfd; FD_ZERO(&r); - FD_SET(0,&r); + FD_SET(STDIN_FILENO,&r); + maxfd = STDIN_FILENO; + + errno = 0; printf("select>"); - n = select(1, &r, (fd_set*)0, (fd_set*)0, (struct timeval *)0); - if (n < 0) { - perror ("select"); - exit(1); - } else if (n > 0) { + CHECKe(n = select(maxfd+1, &r, (fd_set*)0, (fd_set*)0, + (struct timeval *)0)); + + if (n > 0) { int nb; char buf[128]; printf("=> select returned: %d\n", n); - while ((nb = read(0, buf, sizeof(buf)-1)) >= 0) { + while ((nb = read(STDIN_FILENO, buf, sizeof(buf)-1)) >= 0) { buf[nb] = '\0'; printf("read %d: |%s|\n", nb, buf); } - printf("=> out of read loop: %d / %d\n", nb, errno); - if (nb < 0) { - if (errno != EWOULDBLOCK && errno != EAGAIN) { - perror ("read"); - exit(1); - } - } + printf("=> out of read loop: len = %d / %s\n", nb, strerror(errno)); + if (nb < 0) + ASSERTe(errno, == EWOULDBLOCK || errno == EAGAIN); } else ntouts++; } } -main(int argc, char **argv) +int +main(argc, argv) + int argc; + char **argv; { - pthread_t bg_thread, fg_thread; - int junk; + pthread_t bg_thread, fg_thread; + int junk; - pthread_init(); - setbuf(stdout,NULL); - setbuf(stderr,NULL); - if (argc > 1) { - if (pthread_create(&bg_thread, NULL, bg_routine, 0) < 0) { - printf("error: could not create bg thread\n"); - exit(1); - } - } - if (pthread_create(&fg_thread, NULL, fg_routine, 0) < 0) { - printf("error: could not create fg thread\n"); - exit(1); - } - printf("threads forked: bg=%lx fg=%lx\n", bg_thread, fg_thread); - /*pthread_dump_state();*/ - printf("initial thread %lx joining fg...\n", pthread_self()); - pthread_join(fg_thread, (void **)&junk); + setbuf(stdout,NULL); + setbuf(stderr,NULL); + + CHECKr(pthread_create(&bg_thread, NULL, bg_routine, 0)); + CHECKr(pthread_create(&fg_thread, NULL, fg_routine, 0)); + + printf("threads forked: bg=%p fg=%p\n", bg_thread, fg_thread); + /*pthread_dump_state();*/ + /* + printf("initial thread %p joining fg...\n", pthread_self()); + CHECKr(pthread_join(fg_thread, (void **)&junk)); + */ + sleep(20); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_setjmp.c b/lib/libc_r/TEST/test_setjmp.c index ea24ecd63bc..31610fd8387 100644 --- a/lib/libc_r/TEST/test_setjmp.c +++ b/lib/libc_r/TEST/test_setjmp.c @@ -1,13 +1,18 @@ #include <setjmp.h> +#include "test.h" + +int reached; main() { -jmp_buf foo; + jmp_buf foo; -if (setjmp(foo)) { - exit(0); -} -printf("Hi mom\n"); -longjmp(foo, 1); -printf("Should never reach here\n"); + reached = 0; + if (setjmp(foo)) { + ASSERT(reached); + SUCCEED; + } + reached = 1; + longjmp(foo, 1); + PANIC("longjmp"); } diff --git a/lib/libc_r/TEST/test_sleep.c b/lib/libc_r/TEST/test_sleep.c index 47b476219f7..784a203f741 100644 --- a/lib/libc_r/TEST/test_sleep.c +++ b/lib/libc_r/TEST/test_sleep.c @@ -10,6 +10,7 @@ #include <pthread.h> #include <stdio.h> #include <unistd.h> +#include "test.h" const char buf[] = "abcdefghijklimnopqrstuvwxyz"; int fd = 1; @@ -36,14 +37,12 @@ main() sleep(3); printf("Done sleeping\n"); - for(i = 0; i < count; i++) { - if (pthread_create(&thread[i], NULL, new_thread, (void *) i)) { - printf("error creating new thread %ld\n", i); - } - } + for(i = 0; i < count; i++) + CHECKr(pthread_create(&thread[i], NULL, new_thread, + (void *) i)); for (i = 0; i < count; i++) - pthread_join(thread[i], NULL); + CHECKr(pthread_join(thread[i], NULL)); - return(0); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_sock_1.c b/lib/libc_r/TEST/test_sock_1.c index 87eec5a8aae..77f5182db74 100644 --- a/lib/libc_r/TEST/test_sock_1.c +++ b/lib/libc_r/TEST/test_sock_1.c @@ -17,143 +17,124 @@ #include "test.h" #include <sched.h> #include <string.h> +#include <stdlib.h> struct sockaddr_in a_sout; int success = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_attr_t attr; -#define MESSAGE5 "This should be message #5" -#define MESSAGE6 "This should be message #6" +static int counter = 0; -void * sock_connect(void* arg) +void * +sock_connect(arg) + void *arg; { char buf[1024]; - int fd, tmp; - int ret; + int fd; /* Ensure sock_read runs first */ - if ((ret = pthread_mutex_lock(&mutex))) - DIE(ret, "sock_connect:pthread_mutex_lock()"); + CHECKr(pthread_mutex_lock(&mutex)); a_sout.sin_addr.s_addr = htonl(0x7f000001); /* loopback */ + CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - DIE(errno, "sock_connect:socket()"); + ASSERT(++counter == 2); - printf("This should be message #2\n"); - if (connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout)) < 0) - DIE(errno, "sock_connect:connect()"); + /* connect to the socket */ + CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout))); + CHECKe(close(fd)); - close(fd); - - if ((ret = pthread_mutex_unlock(&mutex))) - DIE(ret, "sock_connect:pthread_mutex_lock()"); + CHECKr(pthread_mutex_unlock(&mutex)); - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - DIE(ret, "sock_connect:socket()"); - - printf("This should be message #3\n"); - - if (connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout)) < 0) - DIE(errno, "sock_connect:connect()"); + CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); + ASSERT(++counter == 3); + CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout))); /* Ensure sock_read runs again */ pthread_yield(); - pthread_yield(); - pthread_yield(); - pthread_yield(); - if ((ret = pthread_mutex_lock(&mutex))) - DIE(ret, "sock_connect:pthread_mutex_lock()"); + sleep(1); - if ((tmp = read(fd, buf, 1024)) <= 0) - DIE(errno, "sock_connect:read() == %d", tmp); + CHECKr(pthread_mutex_lock(&mutex)); + CHECKe(read(fd, buf, 1024)); - write(fd, MESSAGE6, sizeof(MESSAGE6)); - printf("%s\n", buf); - close(fd); - success++; + write(fd, "6", 1); - if ((ret = pthread_mutex_unlock(&mutex))) - DIE(ret, "sock_connect:pthread_mutex_unlock()"); + ASSERT(++counter == atoi(buf)); + CHECKe(close(fd)); + success++; + CHECKr(pthread_mutex_unlock(&mutex)); return(NULL); } -extern struct fd_table_entry ** fd_table; -void * sock_write(void* arg) +void * +sock_write(arg) + void *arg; { int fd = *(int *)arg; - write(fd, MESSAGE5, sizeof(MESSAGE5)); + CHECKe(write(fd, "5", 1)); return(NULL); } -void * sock_accept(void* arg) +void * +sock_accept(arg) + void *arg; { pthread_t thread; struct sockaddr a_sin; - int a_sin_size, a_fd, fd, tmp; + int a_sin_size, a_fd, fd; short port; char buf[1024]; - int ret; port = 3276; a_sout.sin_family = AF_INET; a_sout.sin_port = htons(port); a_sout.sin_addr.s_addr = INADDR_ANY; - if ((a_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - DIE(errno, "sock_accept:socket()"); + CHECKe(a_fd = socket(AF_INET, SOCK_STREAM, 0)); - while (bind(a_fd, (struct sockaddr *) &a_sout, sizeof(a_sout)) < 0) { + while (1) { + if(0 == bind(a_fd, (struct sockaddr *) &a_sout, sizeof(a_sout))) + break; if (errno == EADDRINUSE) { a_sout.sin_port = htons((++port)); continue; } - DIE(errno, "sock_accept:bind()"); + DIE(errno, "bind"); } + CHECKe(listen(a_fd, 2)); - if (listen(a_fd, 2)) - DIE(errno, "sock_accept:listen()"); - - a_sin_size = sizeof(a_sin); - printf("This should be message #1\n"); + ASSERT(++counter == 1); - if ((ret = pthread_create(&thread, &attr, sock_connect, - (void *)0xdeadbeaf))) - DIE(ret, "sock_accept:pthread_create(sock_connect)"); + CHECKr(pthread_create(&thread, &attr, sock_connect, + (void *)0xdeadbeaf)); - if ((fd = accept(a_fd, &a_sin, &a_sin_size)) < 0) - DIE(errno, "sock_accept:accept()"); - - if ((ret = pthread_mutex_lock(&mutex))) - DIE(ret, "sock_accept:pthread_mutex_lock()"); + a_sin_size = sizeof(a_sin); + CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size)); + CHECKr(pthread_mutex_lock(&mutex)); + CHECKe(close(fd)); - close(fd); + ASSERT(++counter == 4); a_sin_size = sizeof(a_sin); - printf("This should be message #4\n"); - if ((fd = accept(a_fd, &a_sin, &a_sin_size)) < 0) - DIE(errno, "sock_accept:accept()"); - - if ((ret = pthread_mutex_unlock(&mutex))) - DIE(ret, "sock_accept:pthread_mutex_unlock()"); + CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size)); + CHECKr(pthread_mutex_unlock(&mutex)); /* Setup a write thread */ - if ((ret = pthread_create(&thread, &attr, sock_write, &fd))) - DIE(ret, "sock_accept:pthread_create(sock_write)"); - if ((tmp = read(fd, buf, 1024)) <= 0) - DIE(errno, "sock_accept:read() == %d\n", tmp); + CHECKr(pthread_create(&thread, &attr, sock_write, &fd)); + CHECKe(read(fd, buf, 1024)); + + ASSERT(++counter == atoi(buf)); - printf("%s\n", buf); - close(fd); + CHECKe(close(fd)); - if ((ret = pthread_mutex_lock(&mutex))) - DIE(ret, "sock_accept:pthread_mutex_lock()"); + CHECKr(pthread_mutex_lock(&mutex)); success++; - if ((ret = pthread_mutex_unlock(&mutex))) - DIE(ret, "sock_accept:pthread_mutex_unlock()"); + CHECKr(pthread_mutex_unlock(&mutex)); + + CHECKr(pthread_join(thread, NULL)); return(NULL); } @@ -161,23 +142,19 @@ int main() { pthread_t thread; - int ret; setbuf(stdout, NULL); setbuf(stderr, NULL); - if ((ret = pthread_attr_init(&attr))) - DIE(ret, "main:pthread_attr_init()"); + CHECKr(pthread_attr_init(&attr)); #if 0 - if ((ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO))) - DIE(ret, "main:pthread_attr_setschedpolicy()"); + CHECKr(pthread_attr_setschedpolicy(&attr, SCHED_FIFO)); #endif - if ((ret = pthread_create(&thread, &attr, sock_accept, - (void *)0xdeadbeaf))) - DIE(ret, "main:pthread_create(sock_accept)"); - - printf("initial thread %p going to sleep\n", pthread_self()); - sleep(2); - printf("done sleeping. success = %d\n", success); - exit(success == 2?0:1); + CHECKr(pthread_create(&thread, &attr, sock_accept, + (void *)0xdeadbeaf)); + + CHECKr(pthread_join(thread, NULL)); + + ASSERT(success == 2); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_sock_2.c b/lib/libc_r/TEST/test_sock_2.c index a81fd0a3f69..59c240e6ac6 100644 --- a/lib/libc_r/TEST/test_sock_2.c +++ b/lib/libc_r/TEST/test_sock_2.c @@ -8,9 +8,11 @@ */ #include <pthread.h> +#include <pthread_np.h> #include <errno.h> #include <stdio.h> #include <sys/types.h> +#include <sys/wait.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> @@ -22,62 +24,116 @@ struct sockaddr_in a_sout; #define MESSAGE5 "This should be message #5" #define MESSAGE6 "This should be message #6" -void * sock_write(void* arg) +void * +sock_write(arg) + void *arg; { int fd = *(int *)arg; - write(fd, MESSAGE5, sizeof(MESSAGE5)); + pthread_set_name_np(pthread_self(), "writer"); + CHECKe(write(fd, MESSAGE5, sizeof(MESSAGE5))); return(NULL); } -void * sock_accept(void* arg) +static pthread_mutex_t waiter_mutex = PTHREAD_MUTEX_INITIALIZER; + +void* +waiter(sig) { - pthread_t thread; + int status; + pid_t pid; + + pthread_set_name_np(pthread_self(), "waiter"); + CHECKr(pthread_mutex_lock(&waiter_mutex)); + printf("waiting for child\n"); + CHECKe(pid = wait(&status)); + ASSERT(WIFEXITED(status)); + ASSERT(WEXITSTATUS(status) == 0); + printf("child exited\n"); + CHECKr(pthread_mutex_unlock(&waiter_mutex)); + return (NULL); +} + +void * +sock_accept(arg) + void *arg; +{ + pthread_t thread, wthread; struct sockaddr a_sin; - int a_sin_size, a_fd, fd, tmp; - short port; + int a_sin_size, a_fd, fd; + u_int16_t port; char buf[1024]; + pid_t pid; port = 3276; a_sout.sin_family = AF_INET; a_sout.sin_port = htons(port); a_sout.sin_addr.s_addr = INADDR_ANY; - if ((a_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - DIE(errno, "sock_accept:socket()"); + CHECKe(a_fd = socket(AF_INET, SOCK_STREAM, 0)); - while (bind(a_fd, (struct sockaddr *) &a_sout, sizeof(a_sout)) < 0) { + while(1) { + if (bind(a_fd, (struct sockaddr *)&a_sout, sizeof(a_sout))==0) + break; if (errno == EADDRINUSE) { a_sout.sin_port = htons((++port)); continue; } - DIE(errno, "sock_accept:bind()"); + DIE(errno, "bind"); } - if (listen(a_fd, 2)) - DIE(errno, "sock_accept:listen()"); + printf("listening on port %d\n", port); + + CHECKe(listen(a_fd, 2)); + printf("%d: This should be message #1\n", getpid()); + + CHECKr(pthread_mutex_init(&waiter_mutex, NULL)); + CHECKr(pthread_mutex_lock(&waiter_mutex)); + CHECKr(pthread_create(&wthread, NULL, waiter, NULL)); + + CHECKe(pid = fork()); + switch(pid) { + case 0: + execl("test_sock_2a", "test_sock_2a", "fork okay", NULL); + DIE(errno, "execl"); + default: + break; + } + CHECKr(pthread_mutex_unlock(&waiter_mutex)); + pthread_yield(); + a_sin_size = sizeof(a_sin); - printf("This should be message #1\n"); - if ((fd = accept(a_fd, &a_sin, &a_sin_size)) < 0) - DIE(errno, "Error: sock_accept:accept()"); - close(fd); + CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size)); + CHECKe(close(fd)); + sleep(1); + printf("%d: This should be message #4\n", getpid()); + a_sin_size = sizeof(a_sin); memset(&a_sin, 0, sizeof(a_sin)); - printf("This should be message #4\n"); - if ((fd = accept(a_fd, &a_sin, &a_sin_size)) < 0) - DIE(errno, "sock_accept:accept()"); + CHECKe(fd = accept(a_fd, &a_sin, &a_sin_size)); /* Setup a write thread */ - if (pthread_create(&thread, NULL, sock_write, &fd)) - DIE(errno, "sock_accept:pthread_create(sock_write)"); - if ((tmp = read(fd, buf, 1024)) <= 0) - DIE(errno, "Error: sock_accept:read() == %d", tmp); - printf("%s\n", buf); - close(fd); + CHECKr(pthread_create(&thread, NULL, sock_write, &fd)); + CHECKe(read(fd, buf, 1024)); + + printf("%d: %s\n", getpid(), buf); /* message 6 */ + + CHECKe(close(fd)); + + if (pthread_mutex_trylock(&waiter_mutex) == EBUSY) { + sleep(2); + if (pthread_mutex_trylock(&waiter_mutex) == EBUSY) { + /* forcibly kill child */ + CHECKe(kill(pid, SIGKILL)); + PANIC("child %d took too long to exit", pid); + } + } + CHECKr(pthread_join(wthread, NULL)); + return(NULL); } @@ -85,28 +141,14 @@ int main() { pthread_t thread; - int ret; - - switch(fork()) { - case -1: - DIE(errno, "main:fork()"); - - case 0: - execl("test_sock_2a", "test_sock_2a", "fork okay", NULL); - DIE(errno, "execl"); - default: - break; - } setbuf(stdout, NULL); setbuf(stderr, NULL); - if ((ret = pthread_create(&thread, NULL, sock_accept, - (void *)0xdeadbeaf))) - DIE(ret, "main:pthread_create(sock_accept)"); + CHECKr(pthread_create(&thread, NULL, sock_accept, + (void *)0xdeadbeaf)); - if ((ret = pthread_join(thread, NULL))) - DIE(ret, "main:pthread_join()"); + CHECKr(pthread_join(thread, NULL)); - return (0); + SUCCEED; } diff --git a/lib/libc_r/TEST/test_sock_2a.c b/lib/libc_r/TEST/test_sock_2a.c index 649df1ae4e1..833100492a7 100644 --- a/lib/libc_r/TEST/test_sock_2a.c +++ b/lib/libc_r/TEST/test_sock_2a.c @@ -23,10 +23,11 @@ struct sockaddr_in a_sout; #define MESSAGE6 "This should be message #6" void * -sock_connect(void* arg) +sock_connect(arg) + void *arg; { char buf[1024]; - int fd, tmp; + int fd; short port; port = 3276; @@ -34,52 +35,50 @@ sock_connect(void* arg) a_sout.sin_port = htons(port); a_sout.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* loopback */ - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - DIE(errno, "sock_connect:socket()"); + CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); - printf("This should be message #2\n"); - if (connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout)) < 0) - DIE(errno, "sock_connect:connect()"); - close(fd); + printf("%d: This should be message #2\n", getpid()); + + CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout))); + CHECKe(close(fd)); - if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - DIE(errno, "sock_connect:socket()"); + CHECKe(fd = socket(AF_INET, SOCK_STREAM, 0)); - printf("This should be message #3\n"); + printf("%d: This should be message #3\n", getpid()); - if (connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout)) < 0) - DIE(errno, "sock_connect:connect()"); + CHECKe(connect(fd, (struct sockaddr *) &a_sout, sizeof(a_sout))); /* Ensure sock_read runs again */ - if ((tmp = read(fd, buf, 1024)) <= 0) - DIE(errno, "sock_connect:read() == %d\n", tmp); - write(fd, MESSAGE6, sizeof(MESSAGE6)); - printf("%s\n", buf); - close(fd); + CHECKe(read(fd, buf, 1024)); + CHECKe(write(fd, MESSAGE6, sizeof(MESSAGE6))); + + printf("%d: %s\n", getpid(), buf); + CHECKe(close(fd)); return (NULL); } int -main(int argc, char **argv) +main(argc, argv) + int argc; + char **argv; { pthread_t thread; - int ret; if (argv[1] && (!strcmp(argv[1], "fork okay"))) { sleep(1); setbuf(stdout, NULL); setbuf(stderr, NULL); - if ((ret = pthread_create(&thread, NULL, sock_connect, - (void *)0xdeadbeaf))) - DIE(ret, "main:pthread_create(sock_connect)"); - pthread_join(thread, NULL); - exit(0); + CHECKr(pthread_create(&thread, NULL, sock_connect, + (void *)0xdeadbeaf)); + CHECKr(pthread_join(thread, NULL)); + SUCCEED; } else { - printf("test_sock_2a needs to be execed from test_sock_2.\n"); - printf("It is not a stand alone test.\n"); - exit(1); + fprintf(stderr, "test_sock_2a needs to be exec'ed from " + "test_sock_2.\n"); + fprintf(stderr, "It is not a stand alone test.\n"); + PANIC("usage"); } } diff --git a/lib/libc_r/TEST/test_stdio_1.c b/lib/libc_r/TEST/test_stdio_1.c index 72a79630641..fe146b25b45 100644 --- a/lib/libc_r/TEST/test_stdio_1.c +++ b/lib/libc_r/TEST/test_stdio_1.c @@ -11,113 +11,67 @@ char * dir_name = SRCDIR; char * fullname; /* Test fopen()/ftell()/getc() */ -int test_1(void) +void +test_1() { struct stat statbuf; - FILE * fp; + FILE * fp; int i; - if (stat(fullname, &statbuf) < OK) { - printf("ERROR: Couldn't stat %s\n", fullname); - return(NOTOK); - } + CHECKe(stat(fullname, &statbuf)); - if ((fp = fopen(fullname, "r")) == NULL) { - printf("ERROR: Couldn't open %s\n", fullname); - return(NOTOK); - } + CHECKn((fp = fopen(fullname, "r"))); /* Get the entire file */ - while ((i = getc(fp)) != EOF); - - if (ftell(fp) != statbuf.st_size) { - printf("ERROR: ftell() and stat() don't agree."); - return(NOTOK); - } - - if (fclose(fp) < OK) { - printf("ERROR: fclose() failed."); - return(NOTOK); - } - return(OK); + while ((i = getc(fp)) != EOF) + ; + + ASSERT(ftell(fp) == statbuf.st_size); + + CHECKe(fclose(fp)); } /* Test fopen()/fclose() */ -int test_2(void) +void +test_2() { FILE *fp1, *fp2; - if ((fp1 = fopen(fullname, "r")) == NULL) { - printf("ERROR: Couldn't fopen %s\n", fullname); - return(NOTOK); - } - - if (fclose(fp1) < OK) { - printf("ERROR: fclose() failed."); - return(NOTOK); - } - - if ((fp2 = fopen(fullname, "r")) == NULL) { - printf("ERROR: Couldn't fopen %s\n", fullname); - return(NOTOK); - } - - if (fclose(fp2) < OK) { - printf("ERROR: fclose() failed."); - return(NOTOK); - } + CHECKn(fp1 = fopen(fullname, "r")); + CHECKe(fclose(fp1)); - if (fp1 != fp2) { - printf("ERROR: FILE table leak.\n"); - return(NOTOK); - } + CHECKn(fp2 = fopen(fullname, "r")); + CHECKe(fclose(fp2)); - return(OK); + ASSERT(fp1 == fp2); } /* Test sscanf()/sprintf() */ -int test_3(void) +void +test_3(void) { - char * str = "10 4.53"; + char * str = "10 4.53"; char buf[64]; - double d; - int i; + double d; + int i; - if (sscanf(str, "%d %lf", &i, &d) != 2) { - printf("ERROR: sscanf didn't parse input string correctly\n"); - return(NOTOK); - } + ASSERT(sscanf(str, "%d %lf", &i, &d) == 2); /* Should have a check */ sprintf(buf, "%d %2.2f", i, d); - - if (strcmp(buf, str)) { - printf("ERROR: sscanf()/sprintf() didn't parse unparse correctly\n"); - return(NOTOK); - } - return(OK); + ASSERT(strcmp(buf, str) == 0); } int main() { - printf("test_stdio_1 START\n"); - - if ((fullname = malloc (strlen (dir_name) + strlen (base_name) + 2)) != NULL) { - sprintf (fullname, "%s/%s", dir_name, base_name); - } else { - perror ("malloc"); - exit(1); - } + CHECKn(fullname = malloc (strlen (dir_name) + strlen (base_name) + 2)); + sprintf (fullname, "%s/%s", dir_name, base_name); - if (test_1() || test_2() || test_3()) { - printf("test_stdio_1 FAILED\n"); - exit(1); - } + test_1(); + test_2(); + test_3(); - printf("test_stdio_1 PASSED\n"); - exit(0); + SUCCEED; } - - diff --git a/lib/libc_r/TEST/test_switch.c b/lib/libc_r/TEST/test_switch.c index 992b1e6c5a1..5d637bda041 100644 --- a/lib/libc_r/TEST/test_switch.c +++ b/lib/libc_r/TEST/test_switch.c @@ -1,4 +1,4 @@ -/* ==== test_switch.c ============================================================ +/* ==== test_switch.c ======================================================== * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu * * Description : Test context switch functionality. @@ -28,18 +28,21 @@ void usage(void) errno = 0; } -void* new_thread(void* arg) +void * +new_thread(arg) + void *arg; { while(1) { - write (fd, (char *) arg, 1); + CHECKe(write (fd, (char *) arg, 1)); x[(char *)arg - buf] = 1; } - fprintf(stderr, "Compiler error\n"); - exit(1); + PANIC("while"); } int -main(int argc, char **argv) +main(argc, argv) + int argc; + char **argv; { pthread_t thread; int count = 2; @@ -74,18 +77,16 @@ main(int argc, char **argv) return(NOTOK); } - for (i = 0; i < count; i++) { - if (pthread_create(&thread, NULL, new_thread, (void*)(buf+i))) { - fprintf (stderr, "error creating new thread %ld\n", i); - exit (1); - } - } + /* create the threads */ + for (i = 0; i < count; i++) + CHECKr(pthread_create(&thread, NULL, new_thread, + (void*)(buf+i))); + + /* give all threads a chance to run */ sleep (2); + for (i = 0; i < count; i++) - if (x[i] == 0) { - fprintf (stderr, "thread %ld never ran\n", i); - return 1; - } - printf ("\n%s PASSED\n", argv[0]); - return 0; + ASSERT(x[i]); /* make sure each thread ran */ + + SUCCEED; } |