You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2013/04/16 08:16:45 UTC

svn commit: r1468308 - in /incubator/mesos/trunk/third_party/libprocess/third_party/stout: include/stout/duration.hpp tests/duration_tests.cpp

Author: bmahler
Date: Tue Apr 16 06:16:45 2013
New Revision: 1468308

URL: http://svn.apache.org/r1468308
Log:
Implemented Duration arithmetic operators +, -, +=, -=.

From: Jiang Yan Xu <ya...@jxu.me>
Review: https://reviews.apache.org/r/10422

Modified:
    incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp
    incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/duration_tests.cpp

Modified: incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp?rev=1468308&r1=1468307&r2=1468308&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp Tue Apr 16 06:16:45 2013
@@ -74,7 +74,17 @@ public:
   bool operator == (const Duration& d) const { return seconds == d.seconds; }
   bool operator != (const Duration& d) const { return seconds != d.seconds; }
 
-  // TODO(vinod): Overload arithmetic operators.
+  Duration& operator += (const Duration& that)
+  {
+    seconds += that.seconds;
+    return *this;
+  }
+
+  Duration& operator -= (const Duration& that)
+  {
+    seconds -= that.seconds;
+    return *this;
+  }
 
 protected:
   static const double NANOSECONDS =  0.000000001;
@@ -166,7 +176,7 @@ inline std::ostream& operator << (
   long precision = stream.precision();
 
   stream.setf(std::ios::fixed, std::ios::floatfield);
-  stream.precision(2);
+  stream.precision(std::numeric_limits<double>::digits10);
 
   if (duration < Microseconds(1)) {
     stream << duration.ns() << "ns";
@@ -194,4 +204,18 @@ inline std::ostream& operator << (
   return stream;
 }
 
+inline Duration operator + (const Duration& lhs, const Duration& rhs)
+{
+  Duration sum = lhs;
+  sum += rhs;
+  return sum;
+}
+
+inline Duration operator - (const Duration& lhs, const Duration& rhs)
+{
+  Duration diff = lhs;
+  diff -= rhs;
+  return diff;
+}
+
 #endif // __STOUT_DURATION_HPP__

Modified: incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/duration_tests.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/duration_tests.cpp?rev=1468308&r1=1468307&r2=1468308&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/duration_tests.cpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/duration_tests.cpp Tue Apr 16 06:16:45 2013
@@ -4,32 +4,53 @@
 
 #include <stout/duration.hpp>
 #include <stout/gtest.hpp>
+#include <stout/stringify.hpp>
 #include <stout/try.hpp>
 
 
-TEST(DurationTest, Test)
+TEST(DurationTest, Comparison)
 {
-  Try<Duration> _3hrs = Duration::parse("3hrs");
-  ASSERT_SOME(_3hrs);
+  EXPECT_EQ(Minutes(180), Hours(3));
+  EXPECT_EQ(Seconds(10800), Hours(3));
+  EXPECT_EQ(Milliseconds(10800000), Hours(3));
 
-  EXPECT_EQ(Hours(3.0), _3hrs.get());
-  EXPECT_EQ(Minutes(180.0), _3hrs.get());
-  EXPECT_EQ(Seconds(10800.0), _3hrs.get());
-  EXPECT_EQ(Milliseconds(10800000.0), _3hrs.get());
+  EXPECT_EQ(Milliseconds(1000), Seconds(1));
 
-  EXPECT_EQ(Milliseconds(1000.0), Seconds(1.0));
+  EXPECT_GT(Weeks(1), Days(6));
 
-  EXPECT_GT(Weeks(1.0), Days(6.0));
+  EXPECT_LT(Hours(23), Days(1));
 
-  EXPECT_LT(Hours(23.0), Days(1.0));
+  EXPECT_LE(Hours(24), Days(1));
+  EXPECT_GE(Hours(24), Days(1));
 
-  EXPECT_LE(Hours(24.0), Days(1.0));
-  EXPECT_GE(Hours(24.0), Days(1.0));
+  EXPECT_NE(Minutes(59), Hours(1));
+}
 
-  EXPECT_NE(Minutes(59.0), Hours(1.0));
+TEST(DurationTest, Parse)
+{
+  EXPECT_SOME_EQ(Hours(3), Duration::parse("3hrs"));
+  EXPECT_SOME_EQ(Hours(3.5), Duration::parse("3.5hrs"));
+}
+
+TEST(DurationTest, Arithmetic)
+{
+  Duration d = Seconds(11);
+  d += Seconds(9);
+  EXPECT_EQ(Seconds(20), d);
+
+  d = Seconds(11);
+  d -= Seconds(21);
+  EXPECT_EQ(Seconds(-10), d);
 
-  Try<Duration> _3_5hrs = Duration::parse("3.5hrs");
-  ASSERT_SOME(_3_5hrs);
+  EXPECT_EQ(Seconds(20), Seconds(11) + Seconds(9));
+  EXPECT_EQ(Seconds(-10), Seconds(11) - Seconds(21));
 
-  EXPECT_EQ(Hours(3.5), _3_5hrs.get());
+  EXPECT_EQ(Seconds(Days(11).secs() + 9), Days(11) + Seconds(9));
+}
+
+
+TEST(DurationTest, OutputFormat)
+{
+  EXPECT_EQ("3.141592653589793secs", stringify(Seconds(3.14159265358979323)));
+  EXPECT_EQ("3.140000000000000secs", stringify(Seconds(3.14)));
 }