diff --git a/src/core/model/time.cc b/src/core/model/time.cc index 10b393428..6b30e8584 100644 --- a/src/core/model/time.cc +++ b/src/core/model/time.cc @@ -33,7 +33,7 @@ namespace ns3 { Time::Time (const std::string& s) { - std::string::size_type n = s.find_first_not_of ("0123456789."); + std::string::size_type n = s.find_first_not_of ("+-0123456789."); if (n != std::string::npos) { // Found non-numeric std::istringstream iss; diff --git a/src/core/test/time-test-suite.cc b/src/core/test/time-test-suite.cc index e31030c7d..09f2590f4 100644 --- a/src/core/test/time-test-suite.cc +++ b/src/core/test/time-test-suite.cc @@ -78,6 +78,61 @@ TimeSimpleTestCase::DoTeardown (void) Time::SetResolution (m_originalResolution); } +class TimesWithSignsTestCase : public TestCase +{ +public: + TimesWithSignsTestCase (); +private: + virtual void DoSetup (void); + virtual void DoRun (void); + virtual void DoTeardown (void); +}; + +TimesWithSignsTestCase::TimesWithSignsTestCase () + : TestCase ("Checks times that have plus or minus signs") +{ +} + +void +TimesWithSignsTestCase::DoSetup (void) +{ +} + +void +TimesWithSignsTestCase::DoRun (void) +{ + Time timePositive ("+1000.0"); + Time timePositiveWithUnits ("+1000.0ms"); + + Time timeNegative ("-1000.0"); + Time timeNegativeWithUnits ("-1000.0ms"); + + NS_TEST_ASSERT_MSG_EQ_TOL (timePositive.GetSeconds (), + +1000.0, + 1.0e-8, + "Positive time not parsed correctly."); + + NS_TEST_ASSERT_MSG_EQ_TOL (timePositiveWithUnits.GetSeconds (), + +1.0, + 1.0e-8, + "Positive time with units not parsed correctly."); + + NS_TEST_ASSERT_MSG_EQ_TOL (timeNegative.GetSeconds (), + -1000.0, + 1.0e-8, + "Negative time not parsed correctly."); + + NS_TEST_ASSERT_MSG_EQ_TOL (timeNegativeWithUnits.GetSeconds (), + -1.0, + 1.0e-8, + "Negative time with units not parsed correctly."); +} + +void +TimesWithSignsTestCase::DoTeardown (void) +{ +} + static class TimeTestSuite : public TestSuite { public: @@ -85,6 +140,7 @@ public: : TestSuite ("time", UNIT) { AddTestCase (new TimeSimpleTestCase (Time::US)); + AddTestCase (new TimesWithSignsTestCase ()); } } g_timeTestSuite;