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);
429 :
TestCase (
"Input,output from,to strings")
435 std::stringstream ss (str);
439 bool pass = (str == ss.str ());
442 << (pass ?
"pass " :
"FAIL ")
443 <<
"\"" << str <<
"\"";
446 std::cout <<
", got " << ss.str ();
448 std::cout << std::endl;
454 std::cout << std::endl;
467 Time t (3.141592654e9);
470 <<
"example: raw: " << t
474 << std::fixed << std::setprecision (9)
475 <<
"example: in s: " << t.
As (Time::S)
479 << std::setprecision (6)
480 <<
"example: in ms: " << t.
As (Time::MS)
487 std::cout << std::endl;
virtual void DoRun(void)
Runs the Simple Time test case.
Simulation virtual time values and global simulation resolution.
TimeSimpleTestCase()
constructor for TimeSimpleTestCase.
int64_t Div(const Time &lhs, const Time &rhs)
Integer quotient from dividing two Times.
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
virtual void DoTeardown(void)
DoTeardown for TimeWithSignTestCase.
High precision numerical type, implementing Q64.64 fixed precision.
double GetSeconds(void) const
Get an approximation of the time stored in this instance 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.
TimeWithUnit As(const enum Unit unit) const
Attach a unit to a Time, to facilitate output in a specific unit.
time-tests Time with Sign test case
void TestMultiplicationByIntegerTypes()
Test multiplying a Time instance by various integer types.
Time PicoSeconds(uint64_t value)
Construct a Time in the indicated unit.
virtual void DoRun(void)
DoRun for TimeWithSignTestCase.
Time Years(double value)
Construct a Time in the indicated unit.
virtual void DoSetup(void)
setup function for TimeSimpleTestCase.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
virtual void DoSetup(void)
DoSetup for TimeWithSignTestCase.
#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.
void TestMultiplication(Time t, Time expected, T val, const std::string &msg)
Helper function to handle boilerplate code for multiplication tests.
Time Rem(const Time &lhs, const Time &rhs)
Remainder (modulus) from the quotient of two Times.
TimeTestSuite g_timeTestSuite
Member variable for time test suite.
double GetDouble(void) const
Get the raw time value, in the current resolution 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...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
TestCase * GetParent() const
Get the parent of this TestCsse.
Time Hours(double value)
Construct a Time in the indicated unit.
Time Minutes(double value)
Construct a Time in the indicated unit.
void TestDivisionByDecimalTypes()
Test dividing a Time instance by various decimal types.
void TestDivisionByIntegerTypes()
Test dividing a Time instance by various integer types.
std::string GetName(void) const
virtual void DoTeardown(void)
Does the tear down for TimeSimpleTestCase.
void TestMultiplicationByDecimalTypes()
Test multiplying a Time instance by various decimal types.
void TestDivision(Time t, Time expected, T val, const std::string &msg)
Helper function to handle boilerplate code for division tests.
Time Seconds(double value)
Construct a Time in the indicated unit.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time Days(double value)
Construct a Time in the indicated unit.
Time FemtoSeconds(uint64_t value)
Construct a Time in the indicated unit.
This test suite implements a Unit Test.
TimeWithSignTestCase()
constructor for TimeWithSignTestCase.
virtual void DoTimeOperations(void)
Tests the Time Operations.
time simple test case, Checks the basic operations on time
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.