root/trunk/whisperlib/common/base/test/timer_test.cc

Revision 7, 2.9 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31
32 #include <stdlib.h>
33
34 #include "common/base/types.h"
35 #include "common/base/log.h"
36 #include "common/base/timer.h"
37 #include "common/base/system.h"
38
39 int main(int argc, char* argv[]) {
40   common::Init(argc, argv);
41
42   struct timespec tc = timer::TimespecFromMsec(0LL);
43   CHECK_EQ(tc.tv_sec, 0);
44   CHECK_EQ(tc.tv_nsec, 0);
45
46   struct timespec ts_i1 = timer::TimespecAbsoluteMsec(0LL);
47   struct timespec ts_i2 = timer::TimespecAbsoluteMsec(100LL);
48   CHECK_EQ(timer::TimespecToMsec(ts_i1) + 100, timer::TimespecToMsec(ts_i2));
49
50
51   const int64 ms = timer::TicksMsec();
52   const int64 us = timer::TicksUsec();
53   const int64 ns = timer::TicksNsec();
54
55   CHECK_LE(ms * 1000, us);
56   CHECK_LE(us * 1000, ns);
57   CHECK_GE((ms + 1) * 1000, us);
58   CHECK_GE((us + 1000) * 1000, ns);
59
60   timer::SleepMsec(100);
61   const int64 ms2 = timer::TicksMsec();
62   CHECK_GE(ms2, ms + 100);
63   CHECK_LT(ms2, ms + 120);
64
65   timer::SleepMsec(1100);
66   const int64 ms3 = timer::TicksMsec();
67   CHECK_GE(ms3, ms + 1200);
68   CHECK_LT(ms3, ms + 1220);
69
70   struct timespec ts1 = timer::TimespecFromMsec(ms);
71   struct timespec ts2 = timer::TimespecFromMsec(ms2);
72   struct timespec ts3 = timer::TimespecFromMsec(ms3);
73   CHECK_EQ(ms, timer::TimespecToMsec(ts1));
74   CHECK_EQ(ms2, timer::TimespecToMsec(ts2));
75   CHECK_EQ(ms3, timer::TimespecToMsec(ts3));
76
77   LOG_INFO << "PASS";
78   common::Exit(0);
79 }
Note: See TracBrowser for help on using the browser.