|
24 |
|
24 |
|
25 |
namespace ns3 { |
25 |
namespace ns3 { |
26 |
|
26 |
|
|
|
27 |
static ts_precision_t |
28 |
GetTsPrecision (void) |
29 |
{ |
30 |
return m_tsPrecision; |
31 |
} |
32 |
|
33 |
static void |
34 |
SetTsPrecision (ts_precision_t tsPrecision) |
35 |
{ |
36 |
m_tsPrecision = tsPrecision; |
37 |
m_tsPrecisionFactor = (int64_t)pow(10, m_tsPrecision); |
38 |
} |
39 |
|
27 |
TimeUnit<1>::TimeUnit(const std::string& s) |
40 |
TimeUnit<1>::TimeUnit(const std::string& s) |
28 |
{ |
41 |
{ |
29 |
std::string::size_type n = s.find_first_not_of("0123456789."); |
42 |
std::string::size_type n = s.find_first_not_of("0123456789."); |
Lines 33-110
TimeUnit<1>::TimeUnit(const std::string&
|
Link Here
|
---|
|
33 |
std::string trailer = s.substr(n, std::string::npos); |
46 |
std::string trailer = s.substr(n, std::string::npos); |
34 |
if (trailer == std::string("s")) |
47 |
if (trailer == std::string("s")) |
35 |
{ |
48 |
{ |
36 |
m_data = HighPrecision (r * 1000000000.0); |
49 |
m_data = HighPrecision (r * m_tsPrecisionFactor); |
37 |
return; |
50 |
return; |
38 |
} |
51 |
} |
39 |
if (trailer == std::string("ms")) |
52 |
if (trailer == std::string("ms")) |
40 |
{ |
53 |
{ |
41 |
m_data = HighPrecision ((int64_t)(r * 1000000), false); |
54 |
m_data = HighPrecision ((int64_t)(r * (m_tsPrecisionFactor/pow(10,3))), |
|
|
55 |
false); |
42 |
return; |
56 |
return; |
43 |
} |
57 |
} |
44 |
if (trailer == std::string("us")) |
58 |
if (trailer == std::string("us")) |
45 |
{ |
59 |
{ |
46 |
m_data = HighPrecision ((int64_t)(r * 1000), false); |
60 |
m_data = HighPrecision ((int64_t)(r * (m_tsPrecisionFactor/pow(10,6))), |
|
|
61 |
false); |
47 |
return; |
62 |
return; |
48 |
} |
63 |
} |
49 |
if (trailer == std::string("ns")) |
64 |
if (trailer == std::string("ns")) |
50 |
{ |
65 |
{ |
51 |
m_data = HighPrecision ((int64_t)r, false); |
66 |
m_data = HighPrecision ((int64_t)(r * (m_tsPrecisionFactor/pow(10,9))), |
|
|
67 |
false); |
68 |
return; |
69 |
} |
70 |
if (trailer == std::string("ps")) |
71 |
{ |
72 |
m_data = HighPrecision ((int64_t)(r * (m_tsPrecisionFactor/pow(10,12))), |
73 |
false); |
74 |
return; |
75 |
} |
76 |
if (trailer == std::string("fs")) |
77 |
{ |
78 |
m_data = HighPrecision ((int64_t)(r * (m_tsPrecisionFactor/pow(10,15))), |
79 |
false); |
52 |
return; |
80 |
return; |
53 |
} |
81 |
} |
54 |
NS_FATAL_ERROR("Can't Parse Time "<<s); |
82 |
NS_FATAL_ERROR("Can't Parse Time "<<s); |
55 |
} |
83 |
} |
56 |
//else |
84 |
//else |
57 |
//they didn't provide units, assume seconds |
85 |
//they didn't provide units, assume seconds |
58 |
m_data = HighPrecision (atof(s.c_str()) * 1000000000.0); |
86 |
m_data = HighPrecision (atof(s.c_str()) * m_tsPrecisionFactor); |
59 |
} |
87 |
} |
|
|
88 |
|
60 |
double |
89 |
double |
61 |
TimeUnit<1>::GetSeconds (void) const |
90 |
TimeUnit<1>::GetSeconds (void) const |
62 |
{ |
91 |
{ |
63 |
double ns = GetHighPrecision ().GetDouble (); |
92 |
double time_value = GetHighPrecision ().GetDouble (); |
64 |
return ns/1000000000.0; |
93 |
return time_value/m_tsPrecisionFactor; |
65 |
} |
94 |
} |
66 |
int32_t |
95 |
int64_t |
67 |
TimeUnit<1>::GetMilliSeconds (void) const |
96 |
TimeUnit<1>::GetMilliSeconds (void) const |
68 |
{ |
97 |
{ |
69 |
int64_t ns = GetHighPrecision ().GetInteger (); |
98 |
int64_t time_value = GetHighPrecision ().GetInteger (); |
70 |
ns /= 1000000; |
99 |
time_value = (int64_t)(time_value / (m_tsPrecisionFactor / pow(10,3))); |
71 |
return ns; |
100 |
return time_value; |
72 |
} |
101 |
} |
73 |
int64_t |
102 |
int64_t |
74 |
TimeUnit<1>::GetMicroSeconds (void) const |
103 |
TimeUnit<1>::GetMicroSeconds (void) const |
75 |
{ |
104 |
{ |
76 |
int64_t ns = GetHighPrecision ().GetInteger (); |
105 |
int64_t time_value = GetHighPrecision ().GetInteger (); |
77 |
return ns/1000; |
106 |
time_value = (int64_t)(time_value / (m_tsPrecisionFactor / pow(10,6))); |
|
|
107 |
return time_value; |
78 |
} |
108 |
} |
79 |
int64_t |
109 |
int64_t |
80 |
TimeUnit<1>::GetNanoSeconds (void) const |
110 |
TimeUnit<1>::GetNanoSeconds (void) const |
81 |
{ |
111 |
{ |
82 |
return GetHighPrecision ().GetInteger (); |
112 |
int64_t time_value = GetHighPrecision ().GetInteger (); |
83 |
} |
113 |
time_value = (int64_t)(time_value / (m_tsPrecisionFactor / pow(10,9))); |
|
|
114 |
return time_value; |
115 |
} |
116 |
int64_t |
117 |
TimeUnit<1>::GetPicoSeconds (void) const |
118 |
{ |
119 |
int64_t time_value = GetHighPrecision ().GetInteger (); |
120 |
time_value = (int64_t)(time_value / (m_tsPrecisionFactor / pow(10,12))); |
121 |
return time_value; |
122 |
} |
123 |
int64_t |
124 |
TimeUnit<1>::GetFemtoSeconds (void) const |
125 |
{ |
126 |
int64_t time_value = GetHighPrecision ().GetInteger (); |
127 |
time_value = (int64_t)(time_value / (m_tsPrecisionFactor / pow(10,15))); |
128 |
return time_value; |
129 |
} |
130 |
|
131 |
/** |
132 |
* This returns the value with the precision defined in m_tsPrecision |
133 |
*/ |
134 |
int64_t |
135 |
TimeUnit<1>::GetTimeStep (void) const |
136 |
{ |
137 |
int64_t time_value = GetHighPrecision ().GetInteger (); |
138 |
return time_value; |
139 |
} |
140 |
|
84 |
|
141 |
|
85 |
std::ostream& |
142 |
std::ostream& |
86 |
operator<< (std::ostream& os, Time const& time) |
143 |
operator<< (std::ostream& os, Time const& time) |
87 |
{ |
144 |
{ |
88 |
os << time.GetNanoSeconds () << "ns"; |
145 |
os << time.GetTimeStep () << "ts"; |
89 |
return os; |
146 |
return os; |
90 |
} |
147 |
} |
91 |
|
148 |
|
92 |
Time Seconds (double seconds) |
149 |
Time Seconds (double seconds) |
93 |
{ |
150 |
{ |
94 |
return Time (HighPrecision (seconds * 1000000000.0)); |
151 |
double d_sec = seconds * m_tsPrecisionFactor; |
95 |
} |
152 |
return Time (HighPrecision (d_sec)); |
96 |
Time MilliSeconds (uint32_t ms) |
153 |
// return Time (HighPrecision ((int64_t)d_sec, false)); |
97 |
{ |
154 |
} |
98 |
return Time (HighPrecision (ms * 1000000, false)); |
155 |
|
99 |
} |
156 |
Time MilliSeconds (uint64_t ms) |
|
|
157 |
{ |
158 |
double d_ms = ms * (m_tsPrecisionFactor/pow(10,3)); |
159 |
return Time (HighPrecision ((uint64_t)d_ms, false)); |
160 |
} |
161 |
|
100 |
Time MicroSeconds (uint64_t us) |
162 |
Time MicroSeconds (uint64_t us) |
101 |
{ |
163 |
{ |
102 |
return Time (HighPrecision (us * 1000, false)); |
164 |
double d_us = us * (m_tsPrecisionFactor/pow(10,6)); |
103 |
} |
165 |
return Time (HighPrecision ((uint64_t)d_us, false)); |
|
|
166 |
} |
167 |
|
104 |
Time NanoSeconds (uint64_t ns) |
168 |
Time NanoSeconds (uint64_t ns) |
105 |
{ |
169 |
{ |
106 |
return Time (HighPrecision (ns, false)); |
170 |
double d_ns = ns * (m_tsPrecisionFactor/pow(10,9)); |
107 |
} |
171 |
return Time (HighPrecision ((uint64_t)d_ns, false)); |
|
|
172 |
} |
173 |
Time PicoSeconds (uint64_t ps) |
174 |
{ |
175 |
double d_ps = ps * (m_tsPrecisionFactor/pow(10,12)); |
176 |
return Time (HighPrecision ((uint64_t)d_ps, false)); |
177 |
} |
178 |
Time FemtoSeconds (uint64_t fs) |
179 |
{ |
180 |
double d_fs = fs * (m_tsPrecisionFactor/pow(10,15)); |
181 |
return Time (HighPrecision ((uint64_t)d_fs, false)); |
182 |
} |
183 |
|
184 |
/** |
185 |
* The timestep value passed to this function must be of the precision of m_tsPrecision |
186 |
*/ |
187 |
Time TimeStep (uint64_t ts) |
188 |
{ |
189 |
return Time (HighPrecision (ts, false)); |
190 |
} |
191 |
|
108 |
Time Now (void) |
192 |
Time Now (void) |
109 |
{ |
193 |
{ |
110 |
return Time (Simulator::Now ()); |
194 |
return Time (Simulator::Now ()); |
|
136 |
TimeTests (); |
220 |
TimeTests (); |
137 |
virtual ~TimeTests (); |
221 |
virtual ~TimeTests (); |
138 |
virtual bool RunTests (void); |
222 |
virtual bool RunTests (void); |
|
|
223 |
|
224 |
/** |
225 |
* Verifies that a calculated time value is as expected using |
226 |
* doubles since GetSeconds() returns a double |
227 |
*/ |
228 |
void CheckTimeSec(std::string test_id, double actual, double expected, |
229 |
bool *flag, double precMultFactor = 1, |
230 |
bool verbose = false); |
231 |
|
232 |
/** |
233 |
* Verifies that a calculated time value is as expected. |
234 |
*/ |
235 |
void CheckTime(std::string test_id, int64_t actual, int64_t expected, |
236 |
bool *flag, double precMultFactor = 1, |
237 |
bool verbose = false); |
238 |
|
239 |
/** |
240 |
* Verifies the +, -, * and / operations for the TimeUnit<1> or Time class |
241 |
*/ |
242 |
void CheckOperations(Time t0, Time t1, bool *ok, bool verbose = false); |
243 |
|
244 |
/** |
245 |
* Verifies that the TimeUnit class stores values with the precision |
246 |
* set in the variable m_tsPrecision |
247 |
* Checks that overflow and underflow occur at expected numbers |
248 |
*/ |
249 |
void CheckPrecision(PrecisionType prec, uint64_t val, bool *ok, |
250 |
bool verbose = false); |
251 |
|
252 |
/** |
253 |
* Verifies that the conversion between units in the class |
254 |
* TimeUnit<1> or Time is done correctly. This is verified both when |
255 |
* setting and retrieving a Time value |
256 |
*/ |
257 |
void CheckConversions(uint64_t tval, bool *ok, bool verbose = false); |
258 |
|
259 |
/** |
260 |
* These are the old tests that used to be run |
261 |
*/ |
262 |
void CheckOld(bool *ok); |
139 |
}; |
263 |
}; |
140 |
|
264 |
|
141 |
TimeTests::TimeTests () |
265 |
TimeTests::TimeTests () |
Lines 143-163
TimeTests::TimeTests ()
|
Link Here
|
---|
|
143 |
{} |
267 |
{} |
144 |
TimeTests::~TimeTests () |
268 |
TimeTests::~TimeTests () |
145 |
{} |
269 |
{} |
|
|
270 |
|
146 |
bool TimeTests::RunTests (void) |
271 |
bool TimeTests::RunTests (void) |
147 |
{ |
272 |
{ |
148 |
bool ok = true; |
273 |
bool ok = true; |
149 |
|
274 |
|
|
|
275 |
Time t0, t1; |
276 |
|
277 |
CheckOld(&ok); |
278 |
|
279 |
t0 = MilliSeconds ((uint64_t)10.0); |
280 |
t1 = MilliSeconds ((uint64_t)11.0); |
281 |
|
282 |
CheckOperations(t0, t1, &ok); |
283 |
|
284 |
// t0 = Seconds ((uint64_t)10.0); |
285 |
// t1 = Seconds ((uint64_t)11.0); |
286 |
|
287 |
// CheckOperations(t0, t1, &ok); |
288 |
|
289 |
CheckConversions((uint64_t)5, &ok); |
290 |
CheckConversions((uint64_t)0, &ok); |
291 |
CheckConversions((uint64_t)783, &ok); |
292 |
CheckConversions((uint64_t)1132, &ok); |
293 |
CheckConversions((uint64_t)3341039, &ok); |
294 |
|
295 |
// Now vary the precision and check the conversions |
296 |
if (GetTsPrecision() != NS) { |
297 |
ok = false; |
298 |
} |
299 |
|
300 |
CheckPrecision(US, 7, &ok); |
301 |
|
302 |
CheckConversions((uint64_t)7, &ok); |
303 |
CheckConversions((uint64_t)546, &ok); |
304 |
CheckConversions((uint64_t)6231, &ok); |
305 |
CheckConversions((uint64_t)1234639, &ok); |
306 |
|
307 |
CheckPrecision(MS, 3, &ok); |
308 |
|
309 |
CheckConversions((uint64_t)3, &ok); |
310 |
CheckConversions((uint64_t)134, &ok); |
311 |
CheckConversions((uint64_t)2341, &ok); |
312 |
CheckConversions((uint64_t)8956239, &ok); |
313 |
|
314 |
CheckPrecision(PS, 21, &ok); |
315 |
|
316 |
CheckConversions((uint64_t)4, &ok); |
317 |
CheckConversions((uint64_t)342, &ok); |
318 |
CheckConversions((uint64_t)1327, &ok); |
319 |
CheckConversions((uint64_t)5439627, &ok); |
320 |
|
321 |
CheckPrecision(NS, 12, &ok); |
322 |
CheckConversions((uint64_t)12, &ok); |
323 |
|
324 |
CheckPrecision(SEC, 7, &ok); |
325 |
CheckConversions((uint64_t)7, &ok); |
326 |
|
327 |
CheckPrecision(FS, 5, &ok); |
328 |
CheckConversions((uint64_t)5, &ok); |
329 |
|
330 |
return ok; |
331 |
} |
332 |
|
333 |
void TimeTests::CheckOld (bool *ok) |
334 |
{ |
335 |
double dt0, dt1, dt2; |
336 |
int64_t it0, it1; |
337 |
|
150 |
Time t0 = Seconds (10.0); |
338 |
Time t0 = Seconds (10.0); |
151 |
//std::cout << "t0="<<t0.GetSeconds ()<<" (expected 10.0)"<<std::endl; |
339 |
CheckTimeSec("old 1", t0.GetSeconds(), 10.0, ok); |
|
|
340 |
|
152 |
Time t1 = Seconds (11.0); |
341 |
Time t1 = Seconds (11.0); |
153 |
//std::cout << "t1="<<t1.GetSeconds ()<<" (expected 11.0)"<<std::endl; |
342 |
CheckTimeSec("old 2", t1.GetSeconds(), 11.0, ok); |
|
|
343 |
|
154 |
t0 = Seconds (1.5); |
344 |
t0 = Seconds (1.5); |
155 |
//std::cout << "t0="<<t0.GetSeconds ()<<" (expected +1.5)"<<std::endl; |
345 |
CheckTimeSec("old 3", t0.GetSeconds(), 1.5, ok); |
|
|
346 |
|
156 |
t0 = Seconds (-1.5); |
347 |
t0 = Seconds (-1.5); |
157 |
//std::cout << "t0="<<t0.GetSeconds ()<<" (expected -1.5)"<<std::endl; |
348 |
CheckTimeSec("old 4", t0.GetSeconds(), -1.5, ok); |
158 |
|
349 |
|
159 |
t0 = Seconds (10.0); |
350 |
t0 = MilliSeconds ((uint64_t)10.0); |
160 |
t1 = Seconds (11.0); |
351 |
dt0 = t0.GetSeconds(); |
|
|
352 |
CheckTimeSec("old 5", dt0, 0.01, ok); |
353 |
|
354 |
t1 = MilliSeconds ((uint64_t)11.0); |
355 |
dt1 = t1.GetSeconds(); |
356 |
CheckTimeSec("old 6", dt1, 0.011, ok); |
161 |
|
357 |
|
162 |
Time t2, t3; |
358 |
Time t2, t3; |
163 |
|
359 |
|
Lines 166-209
bool TimeTests::RunTests (void)
|
Link Here
|
---|
|
166 |
{ |
362 |
{ |
167 |
ok = false; |
363 |
ok = false; |
168 |
} |
364 |
} |
|
|
365 |
dt2 = t2.GetSeconds(); |
366 |
CheckTimeSec("old 7", dt2, dt1-dt0, ok); |
367 |
|
169 |
t2 = t1 - t1; |
368 |
t2 = t1 - t1; |
170 |
if (!t2.IsZero ()) |
369 |
if (!t2.IsZero ()) |
171 |
{ |
370 |
{ |
172 |
ok = false; |
371 |
ok = false; |
173 |
} |
372 |
} |
|
|
373 |
dt2 = t2.GetSeconds(); |
374 |
CheckTimeSec("old 8", dt2, dt1-dt1, ok); |
375 |
|
174 |
t2 = t0 - t1; |
376 |
t2 = t0 - t1; |
175 |
if (!t2.IsStrictlyNegative ()) |
377 |
if (!t2.IsStrictlyNegative ()) |
176 |
{ |
378 |
{ |
177 |
ok = false; |
379 |
ok = false; |
178 |
} |
380 |
} |
179 |
|
381 |
dt2 = t2.GetSeconds(); |
180 |
t2 = t0 - t1; |
382 |
CheckTimeSec("old 9", dt2, dt0-dt1, ok); |
181 |
t3 = t2 * t0 / t1; |
383 |
|
182 |
t3 = t0 * t2 / t1; |
384 |
t1 = NanoSeconds(15); |
183 |
t3 = t0 * t1 / t2; |
385 |
it0 = t0.GetNanoSeconds(); |
184 |
t3 = t0 * (t1 / t2); |
386 |
it1 = t1.GetNanoSeconds(); |
185 |
t3 = (t0 * t1) / t2; |
387 |
TimeUnit<-2> tu4 = t0 / (t1 * t1 * t1); |
186 |
t3 = t0 / t1 * t2; |
388 |
CheckTime("old 10", tu4.GetHighPrecision().GetInteger(), it0 / (it1*it1*it1), |
187 |
t3 = (t0 / t1) * t2; |
389 |
ok, 1e9); |
188 |
TimeInvert ti0; |
|
|
189 |
ti0 = t0 / (t1 * t2); |
190 |
t3 = t0 * Scalar (10.0); |
191 |
t3 = Scalar (10.0) * t0; |
192 |
t3 = Scalar (10.0) * t0 / t2 * t1; |
193 |
t3 = (Scalar (10.0) * t0 ) / t2 * t1; |
194 |
Scalar s0 = t0 / t1; |
195 |
Scalar s1; |
196 |
s1 = t0 * t1 / (t2 * t0); |
197 |
TimeUnit<0> tu0; |
198 |
tu0 = s0; |
199 |
TimeUnit<1> tu1; |
200 |
tu1 = t0; |
201 |
TimeUnit<2> tu2; |
202 |
tu2 = t0 * t1; |
203 |
TimeUnit<3> tu3; |
204 |
tu3 = t0 * tu2; |
205 |
TimeUnit<-2> tu4; |
206 |
tu4 = t0 / tu3; |
207 |
|
390 |
|
208 |
Time tmp = MilliSeconds (0); |
391 |
Time tmp = MilliSeconds (0); |
209 |
if ((tmp != NanoSeconds (0)) || |
392 |
if ((tmp != NanoSeconds (0)) || |
Lines 215-233
bool TimeTests::RunTests (void)
|
Link Here
|
---|
|
215 |
|
398 |
|
216 |
Time t4; |
399 |
Time t4; |
217 |
t4 = Seconds (10.0) * Scalar (1.5); |
400 |
t4 = Seconds (10.0) * Scalar (1.5); |
218 |
//std::cout << "10.0s * 1.5 = " << t4.GetSeconds () << "s" << std::endl; |
401 |
CheckTimeSec("old 11", t4.GetSeconds(), 10, ok); |
|
|
402 |
|
219 |
Time t5; |
403 |
Time t5; |
220 |
t5 = NanoSeconds (10) * Scalar (1.5); |
404 |
t5 = NanoSeconds (10) * Scalar (1.5); |
221 |
//std::cout << "10ns * 1.5 = " << t5.GetNanoSeconds () << "ns" << |
405 |
CheckTime("old 12", t5.GetNanoSeconds(), 10, ok); |
222 |
//std::endl; |
406 |
|
|
|
407 |
t4 = Seconds (10.0) * Scalar (15) / Scalar (10); |
408 |
CheckTimeSec("old 13", t4.GetSeconds(), 15, ok); |
409 |
|
410 |
t5 = NanoSeconds (10) * Scalar (15) / Scalar (10); |
411 |
CheckTime("old 14", t5.GetNanoSeconds(), 15, ok); |
412 |
|
223 |
|
413 |
|
224 |
double foo = (t1 + t2).GetSeconds (); |
414 |
double foo = (t1 + t2).GetSeconds (); |
|
|
415 |
dt1 = t1.GetSeconds(); |
416 |
dt2 = t2.GetSeconds(); |
417 |
CheckTimeSec("old 15", foo, dt1+dt2, ok); |
418 |
|
225 |
foo += (t4 == t5)? 1 : 0; |
419 |
foo += (t4 == t5)? 1 : 0; |
|
|
420 |
CheckTimeSec("old 16", foo, dt1+dt2, ok); |
226 |
|
421 |
|
227 |
foo = (t1/t2).GetDouble (); |
422 |
foo = (t1/t2).GetDouble (); |
228 |
|
423 |
CheckTimeSec("old 17", foo, dt1/dt2, ok); |
229 |
return ok; |
424 |
} |
230 |
} |
425 |
|
|
|
426 |
|
427 |
void TimeTests::CheckOperations(Time t0, Time t1, bool *ok, bool verbose) |
428 |
{ |
429 |
|
430 |
if (verbose) |
431 |
std::cout << std::endl << "Check operations: " |
432 |
<< t0 << " " << t1 << std::endl; |
433 |
|
434 |
Time t2, t3; |
435 |
double it0, it1, it2, it3, itu2, itu3; |
436 |
int64_t iti0; |
437 |
|
438 |
it0 = t0.GetSeconds(); |
439 |
it1 = t1.GetSeconds(); |
440 |
|
441 |
t2 = t0 - t1; |
442 |
it2 = t2.GetSeconds(); |
443 |
CheckTimeSec("ops 1", it2, it0-it1, ok); |
444 |
|
445 |
t3 = t2 * t0 / t0; |
446 |
it3 = t3.GetSeconds(); |
447 |
CheckTimeSec("ops 2a", it3, it2*it0/it0, ok); |
448 |
|
449 |
t3 = t2 * t0 / t1; |
450 |
it3 = t3.GetSeconds(); |
451 |
CheckTimeSec("ops 2", it3, it2*it0/it1, ok); |
452 |
|
453 |
t3 = t0 * t2 / t1; |
454 |
it3 = t3.GetSeconds(); |
455 |
CheckTimeSec("ops 3", it3, it0*it2/it1, ok); |
456 |
|
457 |
t3 = t0 * t1 / t2; |
458 |
it3 = t3.GetSeconds(); |
459 |
CheckTimeSec("ops 4", it3, it0*it1/it2, ok); |
460 |
|
461 |
t3 = t0 * (t1 / t2); |
462 |
it3 = t3.GetSeconds(); |
463 |
CheckTimeSec("ops 5", it3, it0*(it1/it2), ok); |
464 |
|
465 |
t3 = (t0 * t1) / t2; |
466 |
it3 = t3.GetSeconds(); |
467 |
CheckTimeSec("ops 6", it3, (it0*it1)/it2, ok); |
468 |
|
469 |
t3 = t0 / t1 * t2; |
470 |
it3 = t3.GetSeconds(); |
471 |
CheckTimeSec("ops 7", it3, it0/it1*it2, ok); |
472 |
|
473 |
t3 = (t0 / t1) * t2; |
474 |
it3 = t3.GetSeconds(); |
475 |
CheckTimeSec("ops 8", it3, (it0/it1)*it2, ok); |
476 |
|
477 |
t3 = t0 * Scalar (10.0); |
478 |
it3 = t3.GetSeconds(); |
479 |
CheckTimeSec("ops 9", it3, it0*10, ok); |
480 |
|
481 |
t3 = Scalar (10.0) * t0; |
482 |
it3 = t3.GetSeconds(); |
483 |
CheckTimeSec("ops 10", it3, 10 * it0, ok); |
484 |
|
485 |
t3 = Scalar (10.0) * t0 / t2 * t1; |
486 |
it3 = t3.GetSeconds(); |
487 |
CheckTimeSec("ops 11", it3, 10 * it0 / it2 * it1, ok); |
488 |
|
489 |
t3 = (Scalar (10.0) * t0 ) / t2 * t1; |
490 |
it3 = t3.GetSeconds(); |
491 |
CheckTimeSec("ops 12", it3, (10 * it0) / it2 * it1, ok); |
492 |
|
493 |
TimeInvert ti0; |
494 |
ti0 = t0 / (t1 * t2); |
495 |
iti0 = ti0.GetHighPrecision().GetInteger(); |
496 |
// This check is not quite working yet. |
497 |
// CheckTime("ops 13", iti0, (int64_t)(it0/(it1*it2)), ok); |
498 |
|
499 |
Scalar s0 = t0 / t1; |
500 |
CheckTimeSec("ops 14", s0.GetDouble(), it0/it1, ok); |
501 |
|
502 |
Scalar s1; |
503 |
s1 = t0 * t1 / (t2 * t0); |
504 |
CheckTimeSec("ops 15", s1.GetDouble(), it0*it1/(it2*it0), ok); |
505 |
|
506 |
TimeUnit<0> tu0; |
507 |
tu0 = s0; |
508 |
CheckTimeSec("ops 16", tu0.GetDouble(), s0.GetDouble(), ok); |
509 |
|
510 |
TimeUnit<1> tu1; |
511 |
tu1 = t0; |
512 |
CheckTimeSec("ops 17", tu1.GetSeconds(), it0, ok); |
513 |
|
514 |
TimeUnit<2> tu2; |
515 |
tu2 = t0 * t1; |
516 |
CheckTimeSec("ops 18", tu2.GetHighPrecision().GetInteger()/(1e18), |
517 |
it0 * it1, ok); |
518 |
itu2 = tu2.GetHighPrecision().GetInteger()/(1e18); |
519 |
|
520 |
TimeUnit<3> tu3; |
521 |
tu3 = t0 / Scalar(10e6) * tu2; |
522 |
CheckTimeSec("ops 19", tu3.GetHighPrecision().GetInteger()/(1e27), |
523 |
it0 / 1000000 * itu2, ok); |
524 |
itu3 = tu3.GetHighPrecision().GetInteger()/(1e27); |
525 |
} |
526 |
|
527 |
void TimeTests::CheckConversions(uint64_t tval, bool *ok, bool verbose) { |
528 |
Time t_sec, t_ms, t_us, t_ns, t_ps, t_fs; |
529 |
|
530 |
if (verbose) |
531 |
std::cout << std::endl << "Check conversions: " << tval << std::endl; |
532 |
|
533 |
// First check the seconds |
534 |
t_sec = Seconds((double)tval); |
535 |
CheckTimeSec("conv sec sec", t_sec.GetSeconds(), (double)tval, ok); |
536 |
CheckTime("conv sec ms", t_sec.GetMilliSeconds(), (int64_t)(tval*1e3), ok, 1e3); |
537 |
CheckTime("conv sec us", t_sec.GetMicroSeconds(), (int64_t)(tval*1e6), ok, 1e6); |
538 |
CheckTime("conv sec ns", t_sec.GetNanoSeconds(), (int64_t)(tval*1e9), ok, 1e9); |
539 |
CheckTime("conv sec ps", t_sec.GetPicoSeconds(), |
540 |
(int64_t)(tval*1e12), ok, 1e12); |
541 |
CheckTime("conv sec fs", t_sec.GetFemtoSeconds(), |
542 |
(int64_t)(tval*1e15), ok, 1e15); |
543 |
|
544 |
// Then check the milliseconds |
545 |
t_ms = MilliSeconds(tval); |
546 |
CheckTimeSec("conv ms sec", t_ms.GetSeconds(), (double)tval/1e3, ok); |
547 |
CheckTime("conv ms ms", t_ms.GetMilliSeconds(), (int64_t)(tval), ok, 1e3); |
548 |
CheckTime("conv ms us", t_ms.GetMicroSeconds(), (int64_t)(tval*1e3), ok, 1e6); |
549 |
CheckTime("conv ms ns", t_ms.GetNanoSeconds(), (int64_t)(tval*1e6), ok, 1e9); |
550 |
CheckTime("conv ms ps", t_ms.GetPicoSeconds(), (int64_t)(tval*1e9), ok, 1e12); |
551 |
CheckTime("conv ms fs", t_ms.GetFemtoSeconds(), (int64_t)(tval*1e12), ok, 1e15); |
552 |
|
553 |
// Then check the microseconds |
554 |
t_us = MicroSeconds(tval); |
555 |
CheckTimeSec("conv us sec", t_us.GetSeconds(), (double)tval/1e6, ok); |
556 |
CheckTime("conv us ms", t_us.GetMilliSeconds(), (int64_t)(tval/1e3), ok, 1e3); |
557 |
CheckTime("conv us us", t_us.GetMicroSeconds(), (int64_t)(tval), ok, 1e6); |
558 |
CheckTime("conv us ns", t_us.GetNanoSeconds(), (int64_t)(tval*1e3), ok, 1e9); |
559 |
CheckTime("conv us ps", t_us.GetPicoSeconds(), (int64_t)(tval*1e6), ok, 1e12); |
560 |
CheckTime("conv us fs", t_us.GetFemtoSeconds(), (int64_t)(tval*1e9), ok, 1e15); |
561 |
|
562 |
// Then check the nanoseconds |
563 |
t_ns = NanoSeconds(tval); |
564 |
CheckTimeSec("conv ns sec", t_ns.GetSeconds(), (double)tval/1e9, ok); |
565 |
CheckTime("conv ns ms", t_ns.GetMilliSeconds(), (int64_t)(tval/1e6), ok, 1e3); |
566 |
CheckTime("conv ns us", t_ns.GetMicroSeconds(), (int64_t)(tval/1e3), ok, 1e6); |
567 |
CheckTime("conv ns ns", t_ns.GetNanoSeconds(), (int64_t)(tval), ok, 1e9); |
568 |
CheckTime("conv ns ps", t_ns.GetPicoSeconds(), (int64_t)(tval*1e3), ok, 1e12); |
569 |
CheckTime("conv ns fs", t_ns.GetFemtoSeconds(), (int64_t)(tval*1e6), ok, 1e15); |
570 |
|
571 |
// Then check the picoseconds |
572 |
t_ps = PicoSeconds(tval); |
573 |
CheckTimeSec("conv ps sec", t_ps.GetSeconds(), (double)tval/1e12, ok); |
574 |
CheckTime("conv ps ms", t_ps.GetMilliSeconds(), (int64_t)(tval/1e9), ok, 1e3); |
575 |
CheckTime("conv ps us", t_ps.GetMicroSeconds(), (int64_t)(tval/1e6), ok, 1e6); |
576 |
CheckTime("conv ps ns", t_ps.GetNanoSeconds(), (int64_t)(tval/1e3), ok, 1e9); |
577 |
CheckTime("conv ps ps", t_ps.GetPicoSeconds(), (int64_t)(tval), ok, 1e12); |
578 |
CheckTime("conv ps fs", t_ps.GetFemtoSeconds(), (int64_t)(tval*1e3), ok, 1e15); |
579 |
|
580 |
// Then check the femtoseconds |
581 |
t_fs = FemtoSeconds(tval); |
582 |
CheckTimeSec("conv fs sec", t_fs.GetSeconds(), (double)tval/1e15, ok); |
583 |
CheckTime("conv fs ms", t_fs.GetMilliSeconds(), (int64_t)(tval/1e12), ok, 1e3); |
584 |
CheckTime("conv fs us", t_fs.GetMicroSeconds(), (int64_t)(tval/1e9), ok, 1e6); |
585 |
CheckTime("conv fs ns", t_fs.GetNanoSeconds(), (int64_t)(tval/1e6), ok, 1e9); |
586 |
CheckTime("conv fs ps", t_fs.GetPicoSeconds(), (int64_t)(tval/1e3), ok, 1e12); |
587 |
CheckTime("conv fs fs", t_fs.GetFemtoSeconds(), (int64_t)(tval), ok, 1e15); |
588 |
|
589 |
|
590 |
} |
591 |
|
592 |
void TimeTests::CheckPrecision(PrecisionType prec, uint64_t val, bool *ok, |
593 |
bool verbose) { |
594 |
if (verbose) { |
595 |
std::cout << "check precision 10^-" << prec << std::endl; |
596 |
} |
597 |
|
598 |
SetTsPrecision(prec); |
599 |
if (GetTsPrecision() != prec) { |
600 |
ok = false; |
601 |
} |
602 |
|
603 |
/* These still need to be fixed. |
604 |
// The smallest value that can be stored is 1x10^(-prec) |
605 |
Time smallest = Seconds(pow(10,-prec)); |
606 |
CheckTimeSec("Prec small: ", smallest.GetSeconds(), pow(10,-prec), ok, 0.1, |
607 |
true); |
608 |
|
609 |
double d_ts = pow(10,-prec) - pow(10, -(prec+3)); |
610 |
Time too_small = Seconds(d_ts); |
611 |
CheckTimeSec("Prec too small: ", too_small.GetSeconds(), 0, ok, 0.1, true); |
612 |
|
613 |
double d_la = 0xFFFFFFFF*pow(10,-prec); |
614 |
Time largest = Seconds(d_la); |
615 |
CheckTimeSec("Prec large: ", largest.GetSeconds(), d_la, ok, 0.1, true); |
616 |
|
617 |
double d_tl = (0xFFFFFFFF*pow(10,-prec)) + 1; |
618 |
Time too_large = Seconds(d_tl); |
619 |
if ((largest.GetSeconds() + 1) == too_large.GetSeconds()) |
620 |
std::cout << "Overflow did not occur." << std::endl; |
621 |
|
622 |
NS_ASSERT(d_la+1 == d_tl); |
623 |
*/ |
624 |
} |
625 |
|
626 |
void TimeTests::CheckTimeSec (std::string test_id, double actual, |
627 |
double expected, bool *flag, double precMultFactor, |
628 |
bool verbose) |
629 |
{ |
630 |
double prec = pow(10,-ns3::m_tsPrecision) * precMultFactor; |
631 |
if ((actual < (expected-prec)) || (actual > (expected+prec))) { |
632 |
std::cout << "FAIL " << test_id |
633 |
<< " Expected:" << expected |
634 |
<< " Actual: " << actual |
635 |
<< " Precision: " << prec << std::endl; |
636 |
*flag = false; |
637 |
} else { |
638 |
if (verbose) { |
639 |
std::cout << "PASS " << test_id |
640 |
<< " Expected:" << expected |
641 |
<< " Actual: " << actual |
642 |
<< " Precision: " << prec << std::endl; |
643 |
} |
644 |
} |
645 |
} |
646 |
|
647 |
void TimeTests::CheckTime (std::string test_id, int64_t actual, |
648 |
int64_t expected, bool *flag, double precMultFactor, |
649 |
bool verbose) |
650 |
{ |
651 |
double prec = pow(10,-ns3::m_tsPrecision) * precMultFactor; |
652 |
if ((actual < (expected-prec)) || (actual > (expected+prec))) { |
653 |
std::cout << "FAIL " << test_id |
654 |
<< " Expected:" << expected |
655 |
<< " Actual: " << actual |
656 |
<< " Precision: " << prec << std::endl; |
657 |
*flag = false; |
658 |
} else { |
659 |
if (verbose) { |
660 |
std::cout << "PASS " << test_id |
661 |
<< " Expected:" << expected |
662 |
<< " Actual: " << actual |
663 |
<< " Precision: " << prec << std::endl; |
664 |
} |
665 |
} |
666 |
} |
667 |
|
231 |
|
668 |
|
232 |
static TimeTests g_time_tests; |
669 |
static TimeTests g_time_tests; |
233 |
|
670 |
|