You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2013/12/11 02:19:53 UTC

git commit: Added min/max functions to stout which take two Options.

Updated Branches:
  refs/heads/master c4a88cb12 -> 473dd4fb3


Added min/max functions to stout which take two Options.

From: Jie Yu <yu...@gmail.com>
Review: https://reviews.apache.org/r/16158


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/473dd4fb
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/473dd4fb
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/473dd4fb

Branch: refs/heads/master
Commit: 473dd4fb3af51fb19c42828bcbba5ca7b2f1d54c
Parents: c4a88cb
Author: Benjamin Hindman <be...@gmail.com>
Authored: Tue Dec 10 17:19:22 2013 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Tue Dec 10 17:19:22 2013 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/3rdparty/Makefile.am        |  1 +
 3rdparty/libprocess/3rdparty/stout/Makefile.am  |  1 +
 .../3rdparty/stout/include/stout/option.hpp     | 32 ++++++++++++
 .../3rdparty/stout/tests/option_tests.cpp       | 52 ++++++++++++++++++++
 4 files changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/473dd4fb/3rdparty/libprocess/3rdparty/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/Makefile.am b/3rdparty/libprocess/3rdparty/Makefile.am
index f9d1aed..068b17b 100644
--- a/3rdparty/libprocess/3rdparty/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/Makefile.am
@@ -128,6 +128,7 @@ stout_tests_SOURCES =				\
   $(STOUT)/tests/main.cpp			\
   $(STOUT)/tests/multimap_tests.cpp		\
   $(STOUT)/tests/none_tests.cpp			\
+  $(STOUT)/tests/option_tests.cpp		\
   $(STOUT)/tests/os_tests.cpp			\
   $(STOUT)/tests/protobuf_tests.cpp		\
   $(STOUT)/tests/protobuf_tests.pb.cc		\

http://git-wip-us.apache.org/repos/asf/mesos/blob/473dd4fb/3rdparty/libprocess/3rdparty/stout/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am
index 3e22851..9e7a4a8 100644
--- a/3rdparty/libprocess/3rdparty/stout/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am
@@ -74,6 +74,7 @@ EXTRA_DIST =					\
   tests/main.cpp				\
   tests/multimap_tests.cpp			\
   tests/none_tests.cpp				\
+  tests/option_tests.cpp			\
   tests/os_tests.cpp				\
   tests/os/sendfile_tests.cpp			\
   tests/os/signals_tests.cpp			\

http://git-wip-us.apache.org/repos/asf/mesos/blob/473dd4fb/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
index 42b75a5..f93bc15 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
@@ -3,6 +3,8 @@
 
 #include <assert.h>
 
+#include <algorithm>
+
 #include <stout/result.hpp>
 
 template <typename T>
@@ -93,4 +95,34 @@ private:
   T* t;
 };
 
+
+template <typename T>
+Option<T> min(const Option<T>& left, const Option<T>& right)
+{
+  if (left.isSome() && right.isSome()) {
+    return std::min(left.get(), right.get());
+  } else if (left.isSome()) {
+    return left.get();
+  } else if (right.isSome()) {
+    return right.get();
+  } else {
+    return Option<T>::none();
+  }
+}
+
+
+template <typename T>
+Option<T> max(const Option<T>& left, const Option<T>& right)
+{
+  if (left.isSome() && right.isSome()) {
+    return std::max(left.get(), right.get());
+  } else if (left.isSome()) {
+    return left.get();
+  } else if (right.isSome()) {
+    return right.get();
+  } else {
+    return Option<T>::none();
+  }
+}
+
 #endif // __STOUT_OPTION_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/473dd4fb/3rdparty/libprocess/3rdparty/stout/tests/option_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/option_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/option_tests.cpp
new file mode 100644
index 0000000..be56d1f
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/tests/option_tests.cpp
@@ -0,0 +1,52 @@
+#include <gmock/gmock.h>
+
+#include <stout/gtest.hpp>
+#include <stout/none.hpp>
+#include <stout/option.hpp>
+
+TEST(OptionTest, Min)
+{
+  Option<int> none1 = None();
+  Option<int> none2 = None();
+  Option<int> value1 = 10;
+  Option<int> value2 = 20;
+
+  Option<int> result = min(none1, none2);
+  ASSERT_NONE(result);
+
+  result = min(none1, value1);
+  ASSERT_SOME(result);
+  EXPECT_EQ(10, result.get());
+
+  result = min(value2, none2);
+  ASSERT_SOME(result);
+  EXPECT_EQ(20, result.get());
+
+  result = min(value1, value2);
+  ASSERT_SOME(result);
+  EXPECT_EQ(10, result.get());
+}
+
+
+TEST(OptionTest, Max)
+{
+  Option<int> none1 = None();
+  Option<int> none2 = None();
+  Option<int> value1 = 10;
+  Option<int> value2 = 20;
+
+  Option<int> result = max(none1, none2);
+  ASSERT_NONE(result);
+
+  result = max(none1, value1);
+  ASSERT_SOME(result);
+  EXPECT_EQ(10, result.get());
+
+  result = max(value2, none2);
+  ASSERT_SOME(result);
+  EXPECT_EQ(20, result.get());
+
+  result = max(value1, value2);
+  ASSERT_SOME(result);
+  EXPECT_EQ(20, result.get());
+}