|
A Discrete-Event Network Simulator
|
API
|
Go to the documentation of this file.
30 #include "ns3/nstime.h"
31 #include "ns3/int64x64.h"
51 virtual void DoSetup (
void);
56 virtual void DoRun (
void);
61 virtual void DoTimeOperations (
void);
66 virtual void DoTeardown (
void);
79 void TestMultiplication (
Time t,
Time expected, T val,
const std::string& msg);
84 void TestMultiplicationByIntegerTypes ();
89 void TestMultiplicationByDecimalTypes ();
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")
126 constexpr
long long oneSec = 1000000000;
132 std::cout <<
"Testing Time Subtraction \n";
137 std::cout <<
"Testing Time Multiplication \n";
143 std::cout <<
"Testing Time Division \n";
148 std::cout <<
"Testing modulo division \n";
150 t1 =
Time (101LL * oneSec);
163 "is 10 really 10 ?");
167 "is 10 really 10 ?");
171 "is 10 really 10 ?");
175 "is 10 really 10 ?");
179 "is 10 really 10 ?");
181 "is 1ms really 1ms ?");
183 "is 1us really 1us ?");
191 "is 1ns really 1ns ?");
193 "is 1ps really 1ps ?");
195 "is 1fs really 1fs ?");
200 Time::SetResolution (Time::PS);
203 "change resolution to PS");
213 const std::string& msg)
216 using TestEntry = std::tuple<Time, std::string>;
217 std::array<TestEntry, 2> TESTS{
218 std::make_tuple (t * val,
"Test Time * value: "),
219 std::make_tuple (val * t,
"Test Time * value: ")
222 for (
auto test: TESTS )
224 std::string errMsg = std::get<1> (test) + msg;
239 TestMultiplication (t, expected,
static_cast<char> (scale),
"Multiplication by char");
241 "Multiplication by unsigned char");
242 TestMultiplication (t, expected,
static_cast<short> (scale),
"Multiplication by short");
244 "Multiplication by unsigned short");
247 "Multiplication by unsigned int");
248 TestMultiplication (t, expected,
static_cast<long> (scale),
"Multiplication by long");
250 "Multiplication by unsigned long");
252 "Multiplication by long long");
254 "Multiplication by unsigned long long");
256 "Multiplication by size_t");
273 "Multiplication by double");
280 Time result = t / val;
294 TestDivision (t, expected,
static_cast<char> (scale),
"Division by char");
295 TestDivision (t, expected,
static_cast<unsigned char> (scale),
296 "Division by unsigned char");
297 TestDivision (t, expected,
static_cast<short> (scale),
"Division by short");
298 TestDivision (t, expected,
static_cast<unsigned short> (scale),
299 "Division by unsigned short");
300 TestDivision (t, expected,
static_cast<int> (scale),
"Division by int");
301 TestDivision (t, expected,
static_cast<unsigned int> (scale),
302 "Division by unsigned int");
303 TestDivision (t, expected,
static_cast<long> (scale),
"Division by long");
304 TestDivision (t, expected,
static_cast<unsigned long> (scale),
305 "Division by unsigned long");
306 TestDivision (t, expected,
static_cast<long long> (scale),
307 "Division by long long");
308 TestDivision (t, expected,
static_cast<unsigned long long> (scale),
309 "Division by unsigned long long");
310 TestDivision (t, expected,
static_cast<std::size_t
> (scale),
311 "Division by size_t");
314 TestDivision (t, expected, scale64,
"Division by int64x64_t");
328 "Division by double");
353 virtual void DoRun (
void);
362 :
TestCase (
"Checks times that have plus or minus signs")
372 Time timePositive (
"+1000.0");
373 Time timePositiveWithUnits (
"+1000.0ms");
375 Time timeNegative (
"-1000.0");
376 Time timeNegativeWithUnits (
"-1000.0ms");
381 "Positive time not parsed correctly.");
386 "Positive time with units not parsed correctly.");
391 "Negative time not parsed correctly.");
396 "Negative time with units not parsed correctly.");
420 virtual void DoRun (
void);
425 void Check (
const std::string & str);
432 void CheckAs (
const Time t,
const std::string expect);
436 :
TestCase (
"Input,output from,to strings")
442 std::stringstream ss (str);
446 bool pass = (str == ss.str ());
449 << (pass ?
"pass " :
"FAIL ")
450 <<
"\"" << str <<
"\"";
453 std::cout <<
", got " << ss.str ();
455 std::cout << std::endl;
462 std::stringstream ss;
463 ss << std::fixed << std::setprecision (6) << t.
As ();
466 bool pass = (str == expect);
469 << (pass ?
"pass " :
"FAIL ")
470 <<
"\"" << expect <<
"\"";
473 std::cout <<
", got " << str;
475 std::cout << std::endl;
483 std::cout << std::endl;
496 Time t (3.141592654e9);
499 <<
"example: raw: " << t
503 << std::fixed << std::setprecision (9)
504 <<
"example: in s: " << t.
As (Time::S)
508 << std::setprecision (6)
509 <<
"example: in ms: " << t.
As (Time::MS)
517 <<
"example: auto scale: \n";
518 CheckAs (t * 1e-9,
"+3.000000ns");
519 CheckAs (t * 1e-8,
"+31.000000ns");
520 CheckAs (t * 1e-7,
"+314.000000ns");
521 CheckAs (t * 1e-6,
"+3.142000us");
522 CheckAs (t * 1e-5,
"+31.416000us");
523 CheckAs (t * 1e-4,
"+314.159000us");
524 CheckAs (t * 1e-3,
"+3.141593ms");
525 CheckAs (t * 1e-2,
"+31.415927ms");
526 CheckAs (t * 1e-1,
"+314.159265ms");
527 CheckAs (t * 1e-0,
"+3.141593s");
528 CheckAs (t * 1e+1,
"+31.415927s");
529 CheckAs (t * 1e+2,
"+5.235988min");
530 CheckAs (t * 1e+3,
"+52.359878min");
531 CheckAs (t * 1e+4,
"+8.726646h");
532 CheckAs (t * 1e+5,
"+3.636103d");
533 CheckAs (t * 1e+6,
"+36.361026d");
534 CheckAs (t * 1e+7,
"+363.610261d");
535 CheckAs (t * 1e+8,
"+9.961925y");
virtual void DoRun(void)
DoRun for TimeWithSignTestCase.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
void TestDivision(Time t, Time expected, T val, const std::string &msg)
Helper function to handle boilerplate code for division tests.
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time Years(double value)
Construct a Time in the indicated unit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
time-tests Time with Sign test case
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
std::string GetName(void) const
TimeWithSignTestCase()
constructor for TimeWithSignTestCase.
TestCase * GetParent() const
Get the parent of this TestCsse.
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
High precision numerical type, implementing Q64.64 fixed precision.
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
void TestDivisionByDecimalTypes()
Test dividing a Time instance by various decimal types.
void TestDivisionByIntegerTypes()
Test dividing a Time instance by various integer types.
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
void TestMultiplication(Time t, Time expected, T val, const std::string &msg)
Helper function to handle boilerplate code for multiplication tests.
void TestMultiplicationByIntegerTypes()
Test multiplying a Time instance by various integer types.
virtual void DoTeardown(void)
DoTeardown for TimeWithSignTestCase.
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
#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 MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
void TestMultiplicationByDecimalTypes()
Test multiplying a Time instance by various decimal types.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Simulation virtual time values and global simulation resolution.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Time Days(double value)
Construct a Time in the indicated unit.
Time Minutes(double value)
Construct a Time in the indicated unit.
virtual void DoTeardown(void)
Does the tear down for TimeSimpleTestCase.
Time Rem(const Time &lhs, const Time &rhs)
Remainder (modulus) from the quotient of two Times.
virtual void DoRun(void)
Runs the Simple Time test case.
#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.
TimeTestSuite g_timeTestSuite
Member variable for time test suite.
@ UNIT
This test suite implements a Unit Test.
Time Seconds(double value)
Construct a Time in the indicated unit.
time simple test case, Checks the basic operations on time
virtual void DoSetup(void)
setup function for TimeSimpleTestCase.
TimeSimpleTestCase()
constructor for TimeSimpleTestCase.
int64_t Div(const Length &numerator, const Length &denominator, Length *remainder)
This function provides a string parsing method that does not rely on istream, which has been found to...
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
virtual void DoTimeOperations(void)
Tests the Time Operations.
double GetDouble(void) const
Get the raw time value, in the current resolution unit.
Time Hours(double value)
Construct a Time in the indicated unit.
virtual void DoSetup(void)
DoSetup for TimeWithSignTestCase.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.