Tizen 2.1 base
[platform/upstream/gcd.git] / kqueue-1.0.4 / test / test.c
1 /*
2  * Copyright (c) 2009 Mark Heily <mark@heily.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #ifdef __linux__
18 #include <execinfo.h>
19 #endif
20 #include <sys/types.h>
21 #include <limits.h>
22 #include <pthread.h>
23
24 #include "common.h"
25
26 static int __thread testnum = 1;
27 static int __thread error_flag = 1;
28 static char __thread * cur_test_id = NULL;
29
30 /* FIXME: not portable beyond linux */
31 static void
32 error_handler(int signum)
33 {
34 #ifdef __linux__
35         void *buf[32];
36
37     /* FIXME: the symbols aren't printing */
38         printf("***** ERROR: Program received signal %d *****\n", signum);
39         backtrace_symbols_fd(buf, sizeof(buf) / sizeof(void *), 2);
40 #else
41         printf("***** ERROR: Program received signal %d *****\n", signum);
42 #endif
43     exit(1);
44 }
45
46 static void
47 testing_atexit(void)
48 {
49     if (error_flag) {
50         printf(" *** TEST FAILED: %s\n", cur_test_id);
51         //TODO: print detailed log
52     } else {
53         printf("\n---\n"
54                 "+OK All %d tests completed.\n", testnum - 1);
55     }
56 }
57
58 void
59 test_begin(const char *func)
60 {
61     if (cur_test_id)
62         free(cur_test_id);
63     cur_test_id = strdup(func);
64
65     printf("%d: %s\n", testnum++, cur_test_id);
66     //TODO: redirect stdout/err to logfile
67 }
68
69 void
70 test_end(void)
71 {
72     free(cur_test_id);
73     cur_test_id = NULL;
74 }
75
76 void
77 testing_begin(void)
78 {
79     struct sigaction sa;
80
81     atexit(testing_atexit);
82
83     /* Install a signal handler for crashes and hangs */
84     memset(&sa, 0, sizeof(sa));
85     sa.sa_handler = error_handler;
86     sigemptyset(&sa.sa_mask);
87     sigaction(SIGSEGV, &sa, NULL);
88     sigaction(SIGABRT, &sa, NULL);
89     sigaction(SIGINT, &sa, NULL);
90 }
91
92 void
93 testing_end(void)
94 {
95     error_flag = 0;
96 }
97
98 /* Generate a unique ID */
99 int
100 testing_make_uid(void)
101 {
102     static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
103     static int id = 0;
104
105     pthread_mutex_lock(&mtx);
106     if (id == INT_MAX)
107         abort();
108     id++;
109     pthread_mutex_unlock(&mtx);
110
111     return (id);
112 }