diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile | 32 | ||||
-rw-r--r-- | tests/ipctest.c | 45 | ||||
-rw-r--r-- | tests/mqtest.c | 64 | ||||
-rw-r--r-- | tests/ncurses.c | 25 | ||||
-rw-r--r-- | tests/producer.c | 38 | ||||
-rw-r--r-- | tests/producer_consumer.c | 39 | ||||
-rw-r--r-- | tests/pthread.c | 50 | ||||
-rw-r--r-- | tests/semconfig.h | 9 | ||||
-rw-r--r-- | tests/semtest.c | 50 | ||||
-rw-r--r-- | tests/strsep.c | 49 |
10 files changed, 401 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..9350e8e --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,32 @@ +CFLAGS = -Wall -g +LDFLAGS = -lpthread -lrt -lncurses +CC = gcc +SOURCES = $(wildcard *.c) +BINARIES = $(patsubst %.c,%,$(SOURCES)) +DEPS = $(patsubst %.c,%.d,$(SOURCES)) + +all: $(BINARIES) + +%: %.c + $(CC) $(CFLAGS) $(LDFLAGS) $< -o $(patsubst %.c,%,$<) + +ipctest: ipctest.c ../src/ui_ipc.c + $(CC) $(CFLAGS) $(LDFLAGS) ipctest.c ../src/ui_ipc.c -o ipctest + +clean: + rm -f $(DEPS) + rm -f $(BINARIES) + +run: all + @echo "* running tests" + for test in $(patsubst %.c,%,$(SOURCES)); do \ + echo -n "* running $${test}"; \ + ./$${test} >/dev/null; \ + if [ $$? -ne 0 ]; then \ + echo " FAILED!"; \ + else \ + echo " OK!"; \ + fi; \ + done + +.PHONY: all install clean diff --git a/tests/ipctest.c b/tests/ipctest.c new file mode 100644 index 0000000..bd7c620 --- /dev/null +++ b/tests/ipctest.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <assert.h> + +#include "semconfig.h" +#include "../src/ui_ipc.h" + + + +int main(int argc, char **argv) { + int ret, c_status; + pid_t child; + + ret = ui_ipc_init(1); + + ret |= ui_ipc_sempost(SEM_BS); + ret |= ui_ipc_sempost(SEM_BS); + if ( (child = fork()) == 0 ) { + printf("child: wait (%d)\n", ui_ipc_getvalue(SEM_BS)); + ret |= ui_ipc_semtrywait(SEM_BS); + ret |= ui_ipc_semtrywait(SEM_BS); + ret |= ui_ipc_semwait(SEM_BS); + printf("child: done (%d)\n", ui_ipc_getvalue(SEM_BS)); + ret |= ui_ipc_sempost(SEM_BS); + exit( (ret == 0 ? 0 : ret) ); + } else if (child > 0) { + usleep(100000); + printf("parent: post (%d)\n", ui_ipc_getvalue(SEM_BS)); + ui_ipc_sempost(SEM_BS); + } else { + ret |= 1; + } + + wait(&c_status); + ret |= ui_ipc_semtrywait(SEM_BS); + ui_ipc_free(1); + exit( c_status | ret ); +} + diff --git a/tests/mqtest.c b/tests/mqtest.c new file mode 100644 index 0000000..f2808c8 --- /dev/null +++ b/tests/mqtest.c @@ -0,0 +1,64 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/wait.h> +#include <mqueue.h> + +#include <assert.h> + + +static mqd_t mq_test; +static mqd_t mq_recv; +static const size_t bufsiz = 256; + +int main(int argc, char **argv) +{ + int c_stat; + struct mq_attr m_attr; + char buf[bufsiz], recv[bufsiz]; + unsigned int prio; + ssize_t sz_recv; + + memset(buf, '\0', bufsiz); + memset(recv, '\0', bufsiz); + if (argc > 1) + strncpy(buf, argv[1], bufsiz-1); + + m_attr.mq_flags = 0; + m_attr.mq_msgsize = bufsiz; + m_attr.mq_maxmsg = 10; + m_attr.mq_curmsgs = 0; + + mq_unlink("/testmq"); + assert( (mq_test = mq_open( "/testmq", O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); + assert( mq_getattr(mq_test, &m_attr) == 0 ); + printf("flags..........: %ld\n" + "maxmsg.........: %ld\n" + "msgsize........: %ld\n" + "curmsg.........: %ld\n", + m_attr.mq_flags, m_attr.mq_maxmsg, m_attr.mq_msgsize, m_attr.mq_curmsgs); + + strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ"); + assert ( mq_send(mq_test, buf, bufsiz, 0) == 0 ); + assert ( (sz_recv = mq_receive(mq_test, recv, bufsiz, &prio)) > 0 ); + + printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf); + printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv); + + memset(recv, '\0', bufsiz); + if (fork() > 0) { + assert( (mq_recv = mq_open( "/testmq", O_RDONLY, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 ); + assert ( (sz_recv = mq_receive(mq_recv, recv, bufsiz, &prio)) > 0 ); + printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv); + return 0; + } + printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf); + assert ( mq_send(mq_test, buf, bufsiz, 0) == 0 ); + wait(&c_stat); + + return 0; +} + diff --git a/tests/ncurses.c b/tests/ncurses.c new file mode 100644 index 0000000..23ef28d --- /dev/null +++ b/tests/ncurses.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <stdlib.h> +#include <ncurses.h> +#include <assert.h> + + +static WINDOW *wnd; + +int +main(void) +{ + assert( (wnd = initscr()) != NULL ); + assert( start_color() == 0 ); + assert( init_pair(1, COLOR_BLACK, COLOR_WHITE) == 0 ); + assert( raw() == 0 ); + assert( keypad(wnd, TRUE) == 0 ); + assert( noecho() == 0 ); + assert( cbreak() == 0 ); + assert( printw("TESTEST") == 0 ); + assert( refresh() == 0 ); + assert( clear() == 0 ); + assert( delwin(wnd) == 0 ); + assert( endwin() == 0 ); + return 0; +} diff --git a/tests/producer.c b/tests/producer.c new file mode 100644 index 0000000..f1a568d --- /dev/null +++ b/tests/producer.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <assert.h> + +#include "semconfig.h" + +sem_t *mysem = NULL, *mycnt = NULL; +pid_t child; + + +int main(int argc, char **argv) { + int semval = 10; + + if (argc == 2) { + semval = atoi(argv[1]); + } + + if ( (mysem = sem_open(TESTSEM, O_CREAT, S_IRUSR | S_IWUSR, 0)) != NULL && + (mycnt = sem_open(CNTSEM, O_CREAT, S_IRUSR | S_IWUSR, semval)) != NULL ) { + while (semval-- >= 0) { + usleep(250); + printf("producer: +1"); + assert( sem_post(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + assert( sem_close(mysem) == 0 && sem_close(mycnt) == 0 ); + } else { + exit(1); + } + exit(0); +} + diff --git a/tests/producer_consumer.c b/tests/producer_consumer.c new file mode 100644 index 0000000..104004a --- /dev/null +++ b/tests/producer_consumer.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <assert.h> + +#include "semconfig.h" + +sem_t *mysem = NULL, *mycnt = NULL; +pid_t child; + + +int main(int argc, char **argv) { + int semval = 0; + + if ( (mysem = sem_open(TESTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL && + (mycnt = sem_open(CNTSEM, 0, S_IRUSR | S_IWUSR, 1)) != NULL ) { + assert( sem_getvalue(mycnt, &semval) == 0 ); + printf("factory producing %d items\n", semval); + + while ( semval-- >= 0 ) { + usleep(250); + LOG("consumer: -1"); + assert( sem_wait(mysem) == 0 ); + printf("remaining: %d\n", semval); + } + } else { + exit(1); + } + assert( sem_close(mysem) == 0 ); + assert( sem_unlink(TESTSEM) == 0 && sem_unlink(CNTSEM) == 0 ); + + exit(0); +} + diff --git a/tests/pthread.c b/tests/pthread.c new file mode 100644 index 0000000..eed53a6 --- /dev/null +++ b/tests/pthread.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <pthread.h> +#include <sys/time.h> +#include <errno.h> +#include <assert.h> + + +static pthread_mutex_t testMutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t testCond = PTHREAD_COND_INITIALIZER; + +static void +mywait(int time_secs) +{ + struct timespec now; + int rt; + + clock_gettime(CLOCK_REALTIME, &now); + now.tv_sec += time_secs; + + assert( pthread_mutex_lock(&testMutex) == 0 ); + rt = pthread_cond_timedwait(&testCond, &testMutex, &now); + assert( rt == ETIMEDOUT || rt == 0 ); + assert( pthread_mutex_unlock(&testMutex) == 0 ); + printf("Done.\n"); +} + +static void * +fun(void *arg) +{ + printf("Thread wait ..\n"); + mywait(1); + mywait(1); + return NULL; +} + +int +main(void) +{ + pthread_t thread; + void *ret; + + assert( pthread_create(&thread, NULL, fun, NULL) == 0 ); + usleep(50000); + assert( pthread_cond_signal(&testCond) == 0 ); + printf("Mainthread: join\n"); + assert( pthread_join(thread, &ret) == 0 ); + return 0; +} diff --git a/tests/semconfig.h b/tests/semconfig.h new file mode 100644 index 0000000..2878dbe --- /dev/null +++ b/tests/semconfig.h @@ -0,0 +1,9 @@ +#ifndef CONFIG_H +#define CONFIG_H 1 + +#define LOG(text) printf("%s\n", text); +#define CMD(cmd) LOG(cmd); cmd; +#define TESTSEM "/testsem" +#define CNTSEM "/testcnt" + +#endif diff --git a/tests/semtest.c b/tests/semtest.c new file mode 100644 index 0000000..fdecf2d --- /dev/null +++ b/tests/semtest.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <semaphore.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <assert.h> + +#include "semconfig.h" + + +sem_t *mysem = NULL; +pid_t child; + + +int main(int argc, char **argv) { + sem_unlink(TESTSEM); + if ( (mysem = sem_open(TESTSEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) != NULL ) { + if ( (child = fork()) == 0 ) { + /* child */ + usleep(250); + LOG("child: sempost"); + sem_post(mysem); + LOG("child: done"); + usleep(250); + exit(0); + } else if (child > 0) { + /* parent */ + LOG("parent: semwait"); + sem_wait(mysem); + LOG("parent: waitpid"); + waitpid(child, NULL, 0); + } else if (child < 0) { + perror("fork"); + } + } else { + sem_close(mysem); + exit(1); + } + + int sval; + assert ( sem_getvalue(mysem, &sval) == 0 ); + assert (sval == 0); + + sem_unlink(TESTSEM); + exit(0); +} + diff --git a/tests/strsep.c b/tests/strsep.c new file mode 100644 index 0000000..24564af --- /dev/null +++ b/tests/strsep.c @@ -0,0 +1,49 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + + +int main(int argc, char **argv) +{ + int i = 0; + char *tok, *p_tok; + static char **arr; + + int arrsiz = 1000; + static char *delim; + static char *str; + + if (argc == 1) { + printf("automatic test ..\n"); + delim = strdup(" "); + str = strdup("this is a simple string, which should be extracted to 12 strings"); + } else if (argc != 4) { + fprintf(stderr, "usage: %s [ARR_SIZ] [DELIM] [STRING]\n", argv[0]); + exit(1); + } else { + arrsiz = atoi(argv[1]); + delim = strdup(argv[2]); + str = strdup(argv[3]); + } + + arr = calloc(arrsiz, sizeof(char *)); + p_tok = str; + while ( (tok = strsep(&p_tok, delim)) != NULL ) { + arr[i] = tok; + i++; + } + + i = 0; + while ( arr[i] != NULL ) { + printf("ARRAY[%d]: %s\n", i, arr[i]); + i++; + } + + if (argc == 1) { + if (i == 12) { + return 0; + } else return -1; + } + return 0; +} |