1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/third_party/libevent/test/test-time.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 1.4 +/* 1.5 + * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu> 1.6 + * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 3. The name of the author may not be used to endorse or promote products 1.17 + * derived from this software without specific prior written permission. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1.20 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1.21 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1.22 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1.23 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1.24 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.25 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.26 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.28 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.29 + */ 1.30 +#include "event2/event-config.h" 1.31 + 1.32 +#include <sys/types.h> 1.33 +#include <sys/stat.h> 1.34 +#include <fcntl.h> 1.35 +#include <stdlib.h> 1.36 +#include <stdio.h> 1.37 +#include <string.h> 1.38 +#ifndef WIN32 1.39 +#include <unistd.h> 1.40 +#include <sys/time.h> 1.41 +#endif 1.42 +#include <errno.h> 1.43 + 1.44 +#include "event2/event.h" 1.45 +#include "event2/event_compat.h" 1.46 +#include "event2/event_struct.h" 1.47 + 1.48 +int called = 0; 1.49 + 1.50 +#define NEVENT 20000 1.51 + 1.52 +struct event *ev[NEVENT]; 1.53 + 1.54 +static int 1.55 +rand_int(int n) 1.56 +{ 1.57 +#ifdef WIN32 1.58 + return (int)(rand() % n); 1.59 +#else 1.60 + return (int)(random() % n); 1.61 +#endif 1.62 +} 1.63 + 1.64 +static void 1.65 +time_cb(evutil_socket_t fd, short event, void *arg) 1.66 +{ 1.67 + struct timeval tv; 1.68 + int i, j; 1.69 + 1.70 + called++; 1.71 + 1.72 + if (called < 10*NEVENT) { 1.73 + for (i = 0; i < 10; i++) { 1.74 + j = rand_int(NEVENT); 1.75 + tv.tv_sec = 0; 1.76 + tv.tv_usec = rand_int(50000); 1.77 + if (tv.tv_usec % 2) 1.78 + evtimer_add(ev[j], &tv); 1.79 + else 1.80 + evtimer_del(ev[j]); 1.81 + } 1.82 + } 1.83 +} 1.84 + 1.85 +int 1.86 +main(int argc, char **argv) 1.87 +{ 1.88 + struct timeval tv; 1.89 + int i; 1.90 +#ifdef WIN32 1.91 + WORD wVersionRequested; 1.92 + WSADATA wsaData; 1.93 + 1.94 + wVersionRequested = MAKEWORD(2, 2); 1.95 + 1.96 + (void) WSAStartup(wVersionRequested, &wsaData); 1.97 +#endif 1.98 + 1.99 + /* Initalize the event library */ 1.100 + event_init(); 1.101 + 1.102 + for (i = 0; i < NEVENT; i++) { 1.103 + ev[i] = malloc(sizeof(struct event)); 1.104 + 1.105 + /* Initalize one event */ 1.106 + evtimer_set(ev[i], time_cb, ev[i]); 1.107 + tv.tv_sec = 0; 1.108 + tv.tv_usec = rand_int(50000); 1.109 + evtimer_add(ev[i], &tv); 1.110 + } 1.111 + 1.112 + event_dispatch(); 1.113 + 1.114 + return (called < NEVENT); 1.115 +} 1.116 +