1 /*
2 * Copyright (C) 2016 Jakub Kruszona-Zawadzki, Core Technology Sp. z o.o.
3 *
4 * This file is part of MooseFS.
5 *
6 * MooseFS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 2 (only).
9 *
10 * MooseFS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with MooseFS; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA
18 * or visit http://www.gnu.org/licenses/gpl-2.0.html
19 */
20
21 #include <sys/select.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "clocks.h"
29
30 #include "mfstest.h"
31
universal_usleep(uint64_t usec)32 void universal_usleep(uint64_t usec) {
33 struct timeval tv;
34 tv.tv_sec = usec/1000000;
35 tv.tv_usec = usec%1000000;
36 select(0, NULL, NULL, NULL, &tv);
37 }
38
main(void)39 int main(void) {
40 double st,en;
41 uint64_t stusec,enusec;
42 uint64_t stnsec,ennsec;
43
44 if (strcmp(monotonic_method(),"time")==0) {
45 printf("testing classic 'time(NULL)' clock doesn't make sense\n");
46 return 77;
47 }
48 mfstest_init();
49
50 mfstest_start(monotonic_clocks);
51 printf("used method: %s\n",monotonic_method());
52 st = monotonic_seconds();
53 stusec = monotonic_useconds();
54 stnsec = monotonic_nseconds();
55 universal_usleep(10000);
56 en = monotonic_seconds();
57 enusec = monotonic_useconds();
58 ennsec = monotonic_nseconds();
59 en -= st;
60 enusec -= stusec;
61 ennsec -= stnsec;
62 printf("second: %.6lf ; %"PRIu64" ; %"PRIu64"\n",en,enusec,ennsec);
63
64 mfstest_assert_double_ge(en,0.01);
65 mfstest_assert_uint64_ge(enusec,10000);
66 mfstest_assert_uint64_ge(ennsec,10000000);
67 mfstest_assert_double_lt(en,0.012);
68 mfstest_assert_uint64_lt(enusec,12000);
69 mfstest_assert_uint64_lt(ennsec,12000000);
70
71 mfstest_end();
72 mfstest_return();
73 }
74