Bug 1204 - Can't Parse Time +100000000.0ns
authorMitch Watrous <watrous@u.washington.edu>
Fri, 11 Nov 2011 14:36:05 -0800
changeset 7579 91a0ff9ee4eb
parent 7578 cf417e0145eb
child 7580 3ae0a64434f0
Bug 1204 - Can't Parse Time +100000000.0ns
src/core/model/time.cc
src/core/test/time-test-suite.cc
--- a/src/core/model/time.cc	Thu Nov 10 10:21:12 2011 +0100
+++ b/src/core/model/time.cc	Fri Nov 11 14:36:05 2011 -0800
@@ -33,7 +33,7 @@
 
 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;
--- a/src/core/test/time-test-suite.cc	Thu Nov 10 10:21:12 2011 +0100
+++ b/src/core/test/time-test-suite.cc	Fri Nov 11 14:36:05 2011 -0800
@@ -78,6 +78,61 @@
   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 @@
     : TestSuite ("time", UNIT)
   {
     AddTestCase (new TimeSimpleTestCase (Time::US));
+    AddTestCase (new TimesWithSignsTestCase ());
   }
 } g_timeTestSuite;