Implémentation de RipplingMOO (entralecement + auto-for).
[GPU.git] / WCudaMSE / API_CppTest / SRC / core / time.cpp
1 // ---
2 //
3 // $Id: time.cpp,v 1.4 2008/07/15 20:33:31 hartwork Exp $
4 //
5 // CppTest - A C++ Unit Testing Framework
6 // Copyright (c) 2003 Niklas Lundell
7 //
8 // ---
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA 02111-1307, USA.
24 //
25 // ---
26
27 #if (defined(__WIN32__) || defined(WIN32))
28 # include "winconfig.h"
29 #else
30 # include "config.h"
31 #endif
32
33 #include "missing.h"
34 #include "cpptest-time.h"
35
36 #ifdef HAVE_GETTIMEOFDAY
37 #ifdef HAVE_SYS_TIME_H
38 #include <sys/time.h>
39 #else
40 #include <time.h>
41 #endif
42 #endif
43
44 using namespace std;
45
46 namespace Test
47 {
48 namespace
49 {
50 const unsigned int UsecPerSec = 1000000;
51
52 } // anonymous namespace
53
54 /// Constructs a time object with zeroed time.
55 ///
56 Time::Time()
57 : _sec(0),
58 _usec(0)
59 {}
60
61 /// Constructs a time object.
62 ///
63 /// \param sec Seconds.
64 /// \param usec Micro-seconds.
65 ///
66 Time::Time(unsigned int sec, unsigned int usec)
67 : _sec(sec),
68 _usec(usec)
69 {}
70
71 /// \return Seconds.
72 ///
73 unsigned int
74 Time::seconds() const
75 {
76 return _sec;
77 }
78
79 /// \return Micro-seconds.
80 ///
81 unsigned int
82 Time::microseconds() const
83 {
84 return _usec;
85 }
86
87 /// \return The current time.
88 ///
89 Time
90 Time::current()
91 {
92 struct timeval tv;
93 gettimeofday(&tv, 0);
94 return Time(tv.tv_sec, tv.tv_usec);
95 }
96
97 /// \relates Time
98 ///
99 /// Computes the time elapsed between two time values.
100 ///
101 /// \param t1 Left-hand time, should be greater than \a t2.
102 /// \param t2 Right-hand time, should be less than \a t1.
103 ///
104 /// \return Computed time value.
105 ///
106 Time
107 operator-(const Time& t1, const Time& t2)
108 {
109 if (t2._sec > t1._sec || (t2._sec == t1._sec && t2._usec > t1._usec))
110 return Time();
111
112 unsigned int sec = t1._sec - t2._sec;
113 unsigned int usec;
114
115 if (t2._usec > t1._usec)
116 {
117 --sec;
118 usec = UsecPerSec - (t2._usec - t1._usec);
119 }
120 else
121 usec = t1._usec - t2._usec;
122
123 return Time(sec, usec);
124 }
125
126 /// \relates Time
127 ///
128 /// Adds two time values.
129 ///
130 /// \param t1 Left-hand time.
131 /// \param t2 Right-hand time.
132 ///
133 /// \return Computed time value.
134 ///
135 Time
136 operator+(const Time& t1, const Time& t2)
137 {
138 unsigned int sec = t1._sec + t2._sec;
139 unsigned int usec = t1._usec + t2._usec;
140
141 if (usec > UsecPerSec)
142 {
143 ++sec;
144 usec -= UsecPerSec;
145 }
146 return Time(sec, usec);
147 }
148
149 /// \relates Time
150 ///
151 /// Outputs a time to an output stream.
152 ///
153 /// \param os Output stream to write to.
154 /// \param t %Time to output.
155 ///
156 /// \return A reference to the given output stream.
157 ///
158 ostream&
159 operator<<(ostream& os, const Time& t)
160 {
161 int old_fill(os.fill());
162 int old_width(os.width());
163
164 os << t.seconds() << '.';
165 os.fill('0');
166 os.width(6);
167 os << t.microseconds();
168
169 os.fill(old_fill);
170 os.width(old_width);
171
172 return os;
173 }
174
175 } // namespace Test
176