xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/test-time.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: test-time.c,v 1.1.1.1 2013/12/27 23:31:29 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
5  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "event2/event-config.h"
30 
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #ifndef _WIN32
38 #include <unistd.h>
39 #include <sys/time.h>
40 #endif
41 #include <errno.h>
42 
43 #include "event2/event.h"
44 #include "event2/event_compat.h"
45 #include "event2/event_struct.h"
46 
47 int called = 0;
48 
49 #define NEVENT	20000
50 
51 struct event *ev[NEVENT];
52 
53 static int
54 rand_int(int n)
55 {
56 #ifdef _WIN32
57 	return (int)(rand() % n);
58 #else
59 	return (int)(random() % n);
60 #endif
61 }
62 
63 static void
64 time_cb(evutil_socket_t fd, short event, void *arg)
65 {
66 	struct timeval tv;
67 	int i, j;
68 
69 	called++;
70 
71 	if (called < 10*NEVENT) {
72 		for (i = 0; i < 10; i++) {
73 			j = rand_int(NEVENT);
74 			tv.tv_sec = 0;
75 			tv.tv_usec = rand_int(50000);
76 			if (tv.tv_usec % 2)
77 				evtimer_add(ev[j], &tv);
78 			else
79 				evtimer_del(ev[j]);
80 		}
81 	}
82 }
83 
84 int
85 main(int argc, char **argv)
86 {
87 	struct timeval tv;
88 	int i;
89 #ifdef _WIN32
90 	WORD wVersionRequested;
91 	WSADATA wsaData;
92 
93 	wVersionRequested = MAKEWORD(2, 2);
94 
95 	(void) WSAStartup(wVersionRequested, &wsaData);
96 #endif
97 
98 	/* Initalize the event library */
99 	event_init();
100 
101 	for (i = 0; i < NEVENT; i++) {
102 		ev[i] = malloc(sizeof(struct event));
103 
104 		/* Initalize one event */
105 		evtimer_set(ev[i], time_cb, ev[i]);
106 		tv.tv_sec = 0;
107 		tv.tv_usec = rand_int(50000);
108 		evtimer_add(ev[i], &tv);
109 	}
110 
111 	event_dispatch();
112 
113 	return (called < NEVENT);
114 }
115 
116