xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/test-time.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: test-time.c,v 1.6 2016/01/08 21:35:41 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 #include "util-internal.h"
47 
48 int called = 0;
49 
50 #define NEVENT	20000
51 
52 struct event *ev[NEVENT];
53 
54 struct evutil_weakrand_state weakrand_state;
55 
56 static int
57 rand_int(int n)
58 {
59 	return evutil_weakrand_(&weakrand_state) % n;
60 }
61 
62 static void
63 time_cb(evutil_socket_t fd, short event, void *arg)
64 {
65 	struct timeval tv;
66 	int i, j;
67 
68 	called++;
69 
70 	if (called < 10*NEVENT) {
71 		for (i = 0; i < 10; i++) {
72 			j = rand_int(NEVENT);
73 			tv.tv_sec = 0;
74 			tv.tv_usec = rand_int(50000);
75 			if (tv.tv_usec % 2 || called < NEVENT)
76 				evtimer_add(ev[j], &tv);
77 			else
78 				evtimer_del(ev[j]);
79 		}
80 	}
81 }
82 
83 int
84 main(int argc, char **argv)
85 {
86 	struct timeval tv;
87 	int i;
88 #ifdef _WIN32
89 	WORD wVersionRequested;
90 	WSADATA wsaData;
91 
92 	wVersionRequested = MAKEWORD(2, 2);
93 
94 	(void) WSAStartup(wVersionRequested, &wsaData);
95 #endif
96 
97 	evutil_weakrand_seed_(&weakrand_state, 0);
98 
99 	/* Initalize the event library */
100 	event_init();
101 
102 	for (i = 0; i < NEVENT; i++) {
103 		ev[i] = malloc(sizeof(struct event));
104 		assert(ev[i] != NULL);
105 
106 		/* Initalize one event */
107 		evtimer_set(ev[i], time_cb, ev[i]);
108 		tv.tv_sec = 0;
109 		tv.tv_usec = rand_int(50000);
110 		evtimer_add(ev[i], &tv);
111 	}
112 
113 	event_dispatch();
114 
115 
116 	printf("%d, %d\n", called, NEVENT);
117 	return (called < NEVENT);
118 }
119 
120