11#include "ns3/int64x64.h"
12#include "ns3/nstime.h"
14#include "ns3/time-span.h"
47 void DoRun()
override;
107 :
TestCase(
"Sanity check of common time operations")
120 constexpr long long oneSec = 1000000000;
126 std::cout <<
"Testing Time Subtraction \n";
131 std::cout <<
"Testing Time Multiplication \n";
137 std::cout <<
"Testing Time Division \n";
142 std::cout <<
"Testing modulo division \n";
144 t1 =
Time(101LL * oneSec);
157 "is 10 really 10 ?");
164 "is 10 really 10 ?");
172 "is 10 really 10 ?");
180 "is 10 really 10 ?");
188 ns.GetNanoSeconds ();
190 "is 1ns really 1ns ?");
192 "is 1ps really 1ps ?");
194 "is 1fs really 1fs ?");
214 using TestEntry = std::tuple<Time, std::string>;
215 std::array<TestEntry, 2> TESTS{std::make_tuple(t * val,
"Test Time * value: "),
216 std::make_tuple(val * t,
"Test Time * value: ")};
218 for (
auto test : TESTS)
220 std::string errMsg = std::get<1>(
test) + msg;
238 static_cast<unsigned char>(scale),
239 "Multiplication by unsigned char");
240 TestMultiplication(t, expected,
static_cast<short>(scale),
"Multiplication by short");
243 static_cast<unsigned short>(scale),
244 "Multiplication by unsigned short");
248 static_cast<unsigned int>(scale),
249 "Multiplication by unsigned int");
253 static_cast<unsigned long>(scale),
254 "Multiplication by unsigned long");
255 TestMultiplication(t, expected,
static_cast<long long>(scale),
"Multiplication by long long");
258 static_cast<unsigned long long>(scale),
259 "Multiplication by unsigned long long");
260 TestMultiplication(t, expected,
static_cast<std::size_t
>(scale),
"Multiplication by size_t");
276 TestMultiplication(t, expected,
static_cast<double>(scale),
"Multiplication by double");
297 TestDivision(t, expected,
static_cast<char>(scale),
"Division by char");
298 TestDivision(t, expected,
static_cast<unsigned char>(scale),
"Division by unsigned char");
299 TestDivision(t, expected,
static_cast<short>(scale),
"Division by short");
300 TestDivision(t, expected,
static_cast<unsigned short>(scale),
"Division by unsigned short");
301 TestDivision(t, expected,
static_cast<int>(scale),
"Division by int");
302 TestDivision(t, expected,
static_cast<unsigned int>(scale),
"Division by unsigned int");
303 TestDivision(t, expected,
static_cast<long>(scale),
"Division by long");
304 TestDivision(t, expected,
static_cast<unsigned long>(scale),
"Division by unsigned long");
305 TestDivision(t, expected,
static_cast<long long>(scale),
"Division by long long");
308 static_cast<unsigned long long>(scale),
309 "Division by unsigned long long");
310 TestDivision(t, expected,
static_cast<std::size_t
>(scale),
"Division by size_t");
313 TestDivision(t, expected, scale64,
"Division by int64x64_t");
326 TestDivision(t, expected,
static_cast<double>(scale),
"Division by double");
350 void DoRun()
override;
359 :
TestCase(
"Checks times that have plus or minus signs")
371 Time timePositive(
"+1000.0");
372 Time timePositiveWithUnits(
"+1000.0ms");
374 Time timeNegative(
"-1000.0");
375 Time timeNegativeWithUnits(
"-1000.0ms");
380 "Positive time not parsed correctly.");
385 "Positive time with units not parsed correctly.");
390 "Negative time not parsed correctly.");
395 "Negative time with units not parsed correctly.");
419 void DoRun()
override;
424 void Check(
const std::string& str);
431 void CheckAs(
const Time t,
const std::string expect);
435 :
TestCase(
"Input,output from,to strings")
442 std::stringstream ss(str);
446 bool pass = (str == ss.str());
448 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << (pass ?
"pass " :
"FAIL ") <<
"\""
452 std::cout <<
", got " << ss.str();
454 std::cout << std::endl;
461 std::stringstream ss;
462 ss << std::fixed << std::setprecision(6) << t.
As();
465 bool pass = (str == expect);
467 std::cout <<
GetParent()->
GetName() <<
" InputOutput:As: " << (pass ?
"pass " :
"FAIL ") <<
"\""
471 std::cout <<
", got " << str;
473 std::cout << std::endl;
480 std::cout << std::endl;
492 Time t(3.141592654e9);
495 <<
"example: raw: " << t << std::endl;
497 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << std::fixed << std::setprecision(9)
498 <<
"example: in s: " << t.
As(
Time::S) << std::endl;
500 std::cout <<
GetParent()->
GetName() <<
" InputOutput: " << std::setprecision(6)
501 <<
"example: in ms: " << t.
As(
Time::MS) << std::endl;
507 <<
"example: auto scale: \n";
508 CheckAs(t * 1e-9,
"+3.000000ns");
509 CheckAs(t * 1e-8,
"+31.000000ns");
510 CheckAs(t * 1e-7,
"+314.000000ns");
511 CheckAs(t * 1e-6,
"+3.142000us");
512 CheckAs(t * 1e-5,
"+31.416000us");
513 CheckAs(t * 1e-4,
"+314.159000us");
514 CheckAs(t * 1e-3,
"+3.141593ms");
515 CheckAs(t * 1e-2,
"+31.415927ms");
516 CheckAs(t * 1e-1,
"+314.159265ms");
517 CheckAs(t * 1e-0,
"+3.141593s");
518 CheckAs(t * 1e+1,
"+31.415927s");
519 CheckAs(t * 1e+2,
"+5.235988min");
520 CheckAs(t * 1e+3,
"+52.359878min");
521 CheckAs(t * 1e+4,
"+8.726646h");
522 CheckAs(t * 1e+5,
"+3.636103d");
523 CheckAs(t * 1e+6,
"+36.361026d");
524 CheckAs(t * 1e+7,
"+363.610261d");
525 CheckAs(t * 1e+8,
"+9.961925y");
527 std::stringstream timeSpanStream{
" { 50ms , 100ms } "};
529 timeSpanStream >> timeSpan;
533 std::stringstream timeSpanOutput;
537 TimeSpan timeSpanString(
" { 50ms , 100ms } ");
540 "Unexpected string TimeSpan begin time");
543 "Unexpected string TimeSpan end time");
548 "Different TimeSpan objects compare equal");
575 void DoRun()
override;
579 :
TestCase(
"Integer unit conversion overflow wraps without undefined behavior")
592 "test precondition: resolution must be coarser than fs");
598 const uint64_t totalFemtos = 10000000000000000000ULL;
602 static_cast<int64_t
>(totalFemtos),
603 "femtosecond overflow of a positive Time must wrap modulo 2^64");
608 -
static_cast<int64_t
>(totalFemtos),
609 "femtosecond overflow of a negative Time must wrap modulo 2^64");
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.
Checks that integer unit conversions whose result does not fit in int64_t wrap around in a well-defin...
void DoRun() override
DoRun for TimeToIntegerOverflowTestCase.
TimeToIntegerOverflowTestCase()
Constructor for TimeToIntegerOverflowTestCase.
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.
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
std::string GetName() const
@ UNIT
This test suite implements a Unit Test.
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
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.
static Unit GetResolution()
Time TimeStep(uint64_t ts)
Scheduler interface.
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.
Class representing a period of time defined via a begin time and an end time.
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_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort 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