11#include "ns3/int64x64.h"
12#include "ns3/nstime.h"
45 void DoRun()
override;
105 :
TestCase(
"Sanity check of common time operations")
118 constexpr long long oneSec = 1000000000;
124 std::cout <<
"Testing Time Subtraction \n";
129 std::cout <<
"Testing Time Multiplication \n";
135 std::cout <<
"Testing Time Division \n";
140 std::cout <<
"Testing modulo division \n";
142 t1 =
Time(101LL * oneSec);
155 "is 10 really 10 ?");
162 "is 10 really 10 ?");
170 "is 10 really 10 ?");
173 TimeStep(1).GetSeconds(),
177 TimeStep(1).GetSeconds(),
178 "is 10 really 10 ?");
186 ns.GetNanoSeconds ();
188 "is 1ns really 1ns ?");
190 "is 1ps really 1ps ?");
192 "is 1fs really 1fs ?");
212 using TestEntry = std::tuple<Time, std::string>;
213 std::array<TestEntry, 2> TESTS{std::make_tuple(t * val,
"Test Time * value: "),
214 std::make_tuple(val * t,
"Test Time * value: ")};
216 for (
auto test : TESTS)
218 std::string errMsg = std::get<1>(
test) + msg;
236 static_cast<unsigned char>(scale),
237 "Multiplication by unsigned char");
238 TestMultiplication(t, expected,
static_cast<short>(scale),
"Multiplication by short");
241 static_cast<unsigned short>(scale),
242 "Multiplication by unsigned short");
246 static_cast<unsigned int>(scale),
247 "Multiplication by unsigned int");
251 static_cast<unsigned long>(scale),
252 "Multiplication by unsigned long");
253 TestMultiplication(t, expected,
static_cast<long long>(scale),
"Multiplication by long long");
256 static_cast<unsigned long long>(scale),
257 "Multiplication by unsigned long long");
258 TestMultiplication(t, expected,
static_cast<std::size_t
>(scale),
"Multiplication by size_t");
274 TestMultiplication(t, expected,
static_cast<double>(scale),
"Multiplication by double");
281 Time result = t / val;
295 TestDivision(t, expected,
static_cast<char>(scale),
"Division by char");
296 TestDivision(t, expected,
static_cast<unsigned char>(scale),
"Division by unsigned char");
297 TestDivision(t, expected,
static_cast<short>(scale),
"Division by short");
298 TestDivision(t, expected,
static_cast<unsigned short>(scale),
"Division by unsigned short");
299 TestDivision(t, expected,
static_cast<int>(scale),
"Division by int");
300 TestDivision(t, expected,
static_cast<unsigned int>(scale),
"Division by unsigned int");
301 TestDivision(t, expected,
static_cast<long>(scale),
"Division by long");
302 TestDivision(t, expected,
static_cast<unsigned long>(scale),
"Division by unsigned long");
303 TestDivision(t, expected,
static_cast<long long>(scale),
"Division by long long");
306 static_cast<unsigned long long>(scale),
307 "Division by unsigned long long");
308 TestDivision(t, expected,
static_cast<std::size_t
>(scale),
"Division by size_t");
311 TestDivision(t, expected, scale64,
"Division by int64x64_t");
324 TestDivision(t, expected,
static_cast<double>(scale),
"Division by double");
348 void DoRun()
override;
357 :
TestCase(
"Checks times that have plus or minus signs")
369 Time timePositive(
"+1000.0");
370 Time timePositiveWithUnits(
"+1000.0ms");
372 Time timeNegative(
"-1000.0");
373 Time timeNegativeWithUnits(
"-1000.0ms");
378 "Positive time not parsed correctly.");
383 "Positive time with units not parsed correctly.");
388 "Negative time not parsed correctly.");
393 "Negative time with units not parsed correctly.");
417 void DoRun()
override;
422 void Check(
const std::string& str);
429 void CheckAs(
const Time t,
const std::string expect);
433 :
TestCase(
"Input,output from,to strings")
440 std::stringstream ss(str);
444 bool pass = (str == ss.str());
446 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << (pass ?
"pass " :
"FAIL ") <<
"\""
450 std::cout <<
", got " << ss.str();
452 std::cout << std::endl;
459 std::stringstream ss;
460 ss << std::fixed << std::setprecision(6) << t.
As();
463 bool pass = (str == expect);
465 std::cout <<
GetParent()->
GetName() <<
" InputOutput:As: " << (pass ?
"pass " :
"FAIL ") <<
"\""
469 std::cout <<
", got " << str;
471 std::cout << std::endl;
478 std::cout << std::endl;
490 Time t(3.141592654e9);
493 <<
"example: raw: " << t << std::endl;
495 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << std::fixed << std::setprecision(9)
496 <<
"example: in s: " << t.
As(
Time::S) << std::endl;
498 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << std::setprecision(6)
499 <<
"example: in ms: " << t.
As(
Time::MS) << std::endl;
505 <<
"example: auto scale: \n";
506 CheckAs(t * 1e-9,
"+3.000000ns");
507 CheckAs(t * 1e-8,
"+31.000000ns");
508 CheckAs(t * 1e-7,
"+314.000000ns");
509 CheckAs(t * 1e-6,
"+3.142000us");
510 CheckAs(t * 1e-5,
"+31.416000us");
511 CheckAs(t * 1e-4,
"+314.159000us");
512 CheckAs(t * 1e-3,
"+3.141593ms");
513 CheckAs(t * 1e-2,
"+31.415927ms");
514 CheckAs(t * 1e-1,
"+314.159265ms");
515 CheckAs(t * 1e-0,
"+3.141593s");
516 CheckAs(t * 1e+1,
"+31.415927s");
517 CheckAs(t * 1e+2,
"+5.235988min");
518 CheckAs(t * 1e+3,
"+52.359878min");
519 CheckAs(t * 1e+4,
"+8.726646h");
520 CheckAs(t * 1e+5,
"+3.636103d");
521 CheckAs(t * 1e+6,
"+36.361026d");
522 CheckAs(t * 1e+7,
"+363.610261d");
523 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, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
TestCase * GetParent() const
Get the parent of this TestCase.
std::string GetName() const
static constexpr auto UNIT
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.
static void SetResolution(Unit resolution)
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.
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.
-ns3 Test suite for the ns3 wrapper script