]> arthur.barton.de Git - netatalk.git/blob - libevent/test/test-time.c
Add libevent
[netatalk.git] / libevent / test / test-time.c
1 /*
2  * Compile with:
3  * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
4  */
5 #include "event2/event-config.h"
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #ifndef WIN32
14 #include <unistd.h>
15 #include <sys/time.h>
16 #endif
17 #include <errno.h>
18
19 #include "event2/event.h"
20 #include "event2/event_compat.h"
21 #include "event2/event_struct.h"
22
23 int called = 0;
24
25 #define NEVENT  20000
26
27 struct event *ev[NEVENT];
28
29 static int
30 rand_int(int n)
31 {
32 #ifdef WIN32
33         return (int)(rand() % n);
34 #else
35         return (int)(random() % n);
36 #endif
37 }
38
39 static void
40 time_cb(evutil_socket_t fd, short event, void *arg)
41 {
42         struct timeval tv;
43         int i, j;
44
45         called++;
46
47         if (called < 10*NEVENT) {
48                 for (i = 0; i < 10; i++) {
49                         j = rand_int(NEVENT);
50                         tv.tv_sec = 0;
51                         tv.tv_usec = rand_int(50000);
52                         if (tv.tv_usec % 2)
53                                 evtimer_add(ev[j], &tv);
54                         else
55                                 evtimer_del(ev[j]);
56                 }
57         }
58 }
59
60 int
61 main(int argc, char **argv)
62 {
63         struct timeval tv;
64         int i;
65 #ifdef WIN32
66         WORD wVersionRequested;
67         WSADATA wsaData;
68         int     err;
69
70         wVersionRequested = MAKEWORD(2, 2);
71
72         err = WSAStartup(wVersionRequested, &wsaData);
73 #endif
74
75         /* Initalize the event library */
76         event_init();
77
78         for (i = 0; i < NEVENT; i++) {
79                 ev[i] = malloc(sizeof(struct event));
80
81                 /* Initalize one event */
82                 evtimer_set(ev[i], time_cb, ev[i]);
83                 tv.tv_sec = 0;
84                 tv.tv_usec = rand_int(50000);
85                 evtimer_add(ev[i], &tv);
86         }
87
88         event_dispatch();
89
90         return (called < NEVENT);
91 }
92