22 #include "ns3/int64x64.h"
23 #include "ns3/nstime.h"
51 void DoSetup()
override;
56 void DoRun()
override;
61 virtual void DoTimeOperations();
66 void DoTeardown()
override;
79 void TestMultiplication(
Time t,
Time expected, T val,
const std::string& msg);
84 void TestMultiplicationByIntegerTypes();
89 void TestMultiplicationByDecimalTypes();
101 template <
typename T>
102 void TestDivision(
Time t,
Time expected, T val,
const std::string& msg);
107 void TestDivisionByIntegerTypes();
112 void TestDivisionByDecimalTypes();
116 :
TestCase(
"Sanity check of common time operations")
129 constexpr
long long oneSec = 1000000000;
135 std::cout <<
"Testing Time Subtraction \n";
140 std::cout <<
"Testing Time Multiplication \n";
146 std::cout <<
"Testing Time Division \n";
151 std::cout <<
"Testing modulo division \n";
153 t1 =
Time(101LL * oneSec);
166 "is 10 really 10 ?");
173 "is 10 really 10 ?");
181 "is 10 really 10 ?");
184 TimeStep(1).GetSeconds(),
188 TimeStep(1).GetSeconds(),
189 "is 10 really 10 ?");
199 "is 1ns really 1ns ?");
201 "is 1ps really 1ps ?");
203 "is 1fs really 1fs ?");
208 Time::SetResolution(Time::PS);
218 template <
typename T>
223 using TestEntry = std::tuple<Time, std::string>;
224 std::array<TestEntry, 2> TESTS{std::make_tuple(t * val,
"Test Time * value: "),
225 std::make_tuple(val * t,
"Test Time * value: ")};
227 for (
auto test : TESTS)
229 std::string errMsg = std::get<1>(test) + msg;
247 static_cast<unsigned char>(scale),
248 "Multiplication by unsigned char");
249 TestMultiplication(t, expected,
static_cast<short>(scale),
"Multiplication by short");
252 static_cast<unsigned short>(scale),
253 "Multiplication by unsigned short");
257 static_cast<unsigned int>(scale),
258 "Multiplication by unsigned int");
262 static_cast<unsigned long>(scale),
263 "Multiplication by unsigned long");
264 TestMultiplication(t, expected,
static_cast<long long>(scale),
"Multiplication by long long");
267 static_cast<unsigned long long>(scale),
268 "Multiplication by unsigned long long");
269 TestMultiplication(t, expected,
static_cast<std::size_t
>(scale),
"Multiplication by size_t");
285 TestMultiplication(t, expected,
static_cast<double>(scale),
"Multiplication by double");
288 template <
typename T>
292 Time result = t / val;
306 TestDivision(t, expected,
static_cast<char>(scale),
"Division by char");
307 TestDivision(t, expected,
static_cast<unsigned char>(scale),
"Division by unsigned char");
308 TestDivision(t, expected,
static_cast<short>(scale),
"Division by short");
309 TestDivision(t, expected,
static_cast<unsigned short>(scale),
"Division by unsigned short");
310 TestDivision(t, expected,
static_cast<int>(scale),
"Division by int");
311 TestDivision(t, expected,
static_cast<unsigned int>(scale),
"Division by unsigned int");
312 TestDivision(t, expected,
static_cast<long>(scale),
"Division by long");
313 TestDivision(t, expected,
static_cast<unsigned long>(scale),
"Division by unsigned long");
314 TestDivision(t, expected,
static_cast<long long>(scale),
"Division by long long");
317 static_cast<unsigned long long>(scale),
318 "Division by unsigned long long");
319 TestDivision(t, expected,
static_cast<std::size_t
>(scale),
"Division by size_t");
322 TestDivision(t, expected, scale64,
"Division by int64x64_t");
335 TestDivision(t, expected,
static_cast<double>(scale),
"Division by double");
359 void DoRun()
override;
368 :
TestCase(
"Checks times that have plus or minus signs")
380 Time timePositive(
"+1000.0");
381 Time timePositiveWithUnits(
"+1000.0ms");
383 Time timeNegative(
"-1000.0");
384 Time timeNegativeWithUnits(
"-1000.0ms");
389 "Positive time not parsed correctly.");
394 "Positive time with units not parsed correctly.");
399 "Negative time not parsed correctly.");
404 "Negative time with units not parsed correctly.");
428 void DoRun()
override;
433 void Check(
const std::string& str);
440 void CheckAs(
const Time t,
const std::string expect);
444 :
TestCase(
"Input,output from,to strings")
451 std::stringstream ss(str);
455 bool pass = (str == ss.str());
457 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << (pass ?
"pass " :
"FAIL ") <<
"\""
461 std::cout <<
", got " << ss.str();
463 std::cout << std::endl;
470 std::stringstream ss;
471 ss << std::fixed << std::setprecision(6) << t.
As();
474 bool pass = (str == expect);
476 std::cout <<
GetParent()->
GetName() <<
" InputOutput:As: " << (pass ?
"pass " :
"FAIL ") <<
"\""
480 std::cout <<
", got " << str;
482 std::cout << std::endl;
489 std::cout << std::endl;
501 Time t(3.141592654e9);
504 <<
"example: raw: " << t << std::endl;
506 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << std::fixed << std::setprecision(9)
507 <<
"example: in s: " << t.
As(Time::S) << std::endl;
509 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << std::setprecision(6)
510 <<
"example: in ms: " << t.
As(Time::MS) << std::endl;
516 <<
"example: auto scale: \n";
517 CheckAs(t * 1e-9,
"+3.000000ns");
518 CheckAs(t * 1e-8,
"+31.000000ns");
519 CheckAs(t * 1e-7,
"+314.000000ns");
520 CheckAs(t * 1e-6,
"+3.142000us");
521 CheckAs(t * 1e-5,
"+31.416000us");
522 CheckAs(t * 1e-4,
"+314.159000us");
523 CheckAs(t * 1e-3,
"+3.141593ms");
524 CheckAs(t * 1e-2,
"+31.415927ms");
525 CheckAs(t * 1e-1,
"+314.159265ms");
526 CheckAs(t * 1e-0,
"+3.141593s");
527 CheckAs(t * 1e+1,
"+31.415927s");
528 CheckAs(t * 1e+2,
"+5.235988min");
529 CheckAs(t * 1e+3,
"+52.359878min");
530 CheckAs(t * 1e+4,
"+8.726646h");
531 CheckAs(t * 1e+5,
"+3.636103d");
532 CheckAs(t * 1e+6,
"+36.361026d");
533 CheckAs(t * 1e+7,
"+363.610261d");
534 CheckAs(t * 1e+8,
"+9.961925y");
time simple test case, Checks the basic operations on time
void DoTeardown() override
Does the tear down for TimeSimpleTestCase.
virtual void DoTimeOperations()
Tests the Time Operations.
void DoSetup() override
setup function for TimeSimpleTestCase.
void TestMultiplicationByDecimalTypes()
Test multiplying a Time instance by various decimal types.
void TestMultiplicationByIntegerTypes()
Test multiplying a Time instance by various integer types.
TimeSimpleTestCase()
constructor for TimeSimpleTestCase.
void DoRun() override
Runs the Simple Time test case.
void TestDivisionByIntegerTypes()
Test dividing a Time instance by various integer types.
void TestDivision(Time t, Time expected, T val, const std::string &msg)
Helper function to handle boilerplate code for division tests.
void TestDivisionByDecimalTypes()
Test dividing a Time instance by various decimal types.
void TestMultiplication(Time t, Time expected, T val, const std::string &msg)
Helper function to handle boilerplate code for multiplication tests.
time-tests Time with Sign test case
void DoTeardown() override
DoTeardown for TimeWithSignTestCase.
void DoSetup() override
DoSetup for TimeWithSignTestCase.
void DoRun() override
DoRun for TimeWithSignTestCase.
TimeWithSignTestCase()
constructor for TimeWithSignTestCase.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
TestCase * GetParent() const
Get the parent of this TestCase.
std::string GetName() const
@ UNIT
This test suite implements a Unit Test.
Simulation virtual time values and global simulation resolution.
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
int64_t GetInteger() const
Get the raw time value, in the current resolution unit.
double GetDouble() const
Get the raw time value, in the current resolution unit.
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
High precision numerical type, implementing Q64.64 fixed precision.
TimeTestSuite g_timeTestSuite
Member variable for time test suite.
int64_t Div(const Length &numerator, const Length &denominator, Length *remainder)
Calculate how many times numerator can be split into denominator sized pieces.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time Seconds(double value)
Construct a Time in the indicated unit.
Time Days(double value)
Construct a Time in the indicated unit.
Time Hours(double value)
Construct a Time in the indicated unit.
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time Minutes(double value)
Construct a Time in the indicated unit.
Time Years(double value)
Construct a Time in the indicated unit.
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Time Rem(const Time &lhs, const Time &rhs)
Remainder (modulus) from the quotient of two Times.