summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/utilities/optional/optional.specalg
diff options
context:
space:
mode:
authorEric Fiselier <eric@efcs.ca>2016-10-12 06:45:11 +0000
committerEric Fiselier <eric@efcs.ca>2016-10-12 06:45:11 +0000
commit6ee4001cc957acac39181b68193e1414c3163698 (patch)
tree6f4a8a05e89e454e08b4385f522b0d84b9e64d7a /libcxx/test/std/utilities/optional/optional.specalg
parent49a278fb6ddd123d68ddbe7cedbedff92cd433c1 (diff)
downloadbcm5719-llvm-6ee4001cc957acac39181b68193e1414c3163698.tar.gz
bcm5719-llvm-6ee4001cc957acac39181b68193e1414c3163698.zip
Add <optional> header.
This patch is largely thanks to Casey Carter @ Microsoft. He did the initial work of porting our experimental implementation and tests over to namespace std. llvm-svn: 283977
Diffstat (limited to 'libcxx/test/std/utilities/optional/optional.specalg')
-rw-r--r--libcxx/test/std/utilities/optional/optional.specalg/make_optional.pass.cpp51
-rw-r--r--libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit.pass.cpp45
-rw-r--r--libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp53
-rw-r--r--libcxx/test/std/utilities/optional/optional.specalg/swap.pass.cpp303
4 files changed, 452 insertions, 0 deletions
diff --git a/libcxx/test/std/utilities/optional/optional.specalg/make_optional.pass.cpp b/libcxx/test/std/utilities/optional/optional.specalg/make_optional.pass.cpp
new file mode 100644
index 00000000000..3fbf19f8ee1
--- /dev/null
+++ b/libcxx/test/std/utilities/optional/optional.specalg/make_optional.pass.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+// <optional>
+
+// template <class T>
+// constexpr optional<decay_t<T>> make_optional(T&& v);
+
+#include <optional>
+#include <string>
+#include <memory>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main()
+{
+ using std::optional;
+ using std::make_optional;
+ {
+ int arr[10]; ((void)arr);
+ ASSERT_SAME_TYPE(decltype(make_optional(arr)), optional<int*>);
+ }
+ {
+ constexpr auto opt = make_optional(2);
+ ASSERT_SAME_TYPE(decltype(opt), const optional<int>);
+ static_assert(opt.value() == 2);
+ }
+ {
+ optional<int> opt = make_optional(2);
+ assert(*opt == 2);
+ }
+ {
+ std::string s("123");
+ optional<std::string> opt = make_optional(s);
+ assert(*opt == s);
+ }
+ {
+ std::unique_ptr<int> s(new int(3));
+ optional<std::unique_ptr<int>> opt = make_optional(std::move(s));
+ assert(**opt == 3);
+ assert(s == nullptr);
+ }
+}
diff --git a/libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit.pass.cpp b/libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit.pass.cpp
new file mode 100644
index 00000000000..bdfeefbcc1d
--- /dev/null
+++ b/libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit.pass.cpp
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+// <optional>
+
+// template <class T, class... Args>
+// constexpr optional<T> make_optional(Args&&... args);
+
+#include <optional>
+#include <string>
+#include <memory>
+#include <cassert>
+
+int main()
+{
+ using std::optional;
+ using std::make_optional;
+
+ {
+ constexpr auto opt = make_optional<int>('a');
+ static_assert(*opt == int('a'), "");
+ }
+ {
+ std::string s("123");
+ auto opt = make_optional<std::string>(s);
+ assert(*opt == s);
+ }
+ {
+ std::unique_ptr<int> s(new int(3));
+ auto opt = make_optional<std::unique_ptr<int>>(std::move(s));
+ assert(**opt == 3);
+ assert(s == nullptr);
+ }
+ {
+ auto opt = make_optional<std::string>(4, 'X');
+ assert(*opt == "XXXX");
+ }
+}
diff --git a/libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp b/libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp
new file mode 100644
index 00000000000..f17f5820d20
--- /dev/null
+++ b/libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+// <optional>
+
+// template <class T, class U, class... Args>
+// constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
+
+#include <optional>
+#include <string>
+#include <memory>
+#include <cassert>
+
+#include "test_macros.h"
+
+struct TestT {
+ int x;
+ int size;
+ constexpr TestT(std::initializer_list<int> il) : x(*il.begin()), size(il.size()) {}
+ constexpr TestT(std::initializer_list<int> il, const int*)
+ : x(*il.begin()), size(il.size()) {}
+};
+
+int main()
+{
+ using std::make_optional;
+ {
+ constexpr auto opt = make_optional<TestT>({42, 2, 3});
+ ASSERT_SAME_TYPE(decltype(opt), const std::optional<TestT>);
+ static_assert(opt->x == 42, "");
+ static_assert(opt->size == 3, "");
+ }
+ {
+ constexpr auto opt = make_optional<TestT>({42, 2, 3}, nullptr);
+ static_assert(opt->x == 42, "");
+ static_assert(opt->size == 3, "");
+ }
+ {
+ auto opt = make_optional<std::string>({'1', '2', '3'});
+ assert(*opt == "123");
+ }
+ {
+ auto opt = make_optional<std::string>({'a', 'b', 'c'}, std::allocator<char>{});
+ assert(*opt == "abc");
+ }
+}
diff --git a/libcxx/test/std/utilities/optional/optional.specalg/swap.pass.cpp b/libcxx/test/std/utilities/optional/optional.specalg/swap.pass.cpp
new file mode 100644
index 00000000000..f364d7dab79
--- /dev/null
+++ b/libcxx/test/std/utilities/optional/optional.specalg/swap.pass.cpp
@@ -0,0 +1,303 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+// <optional>
+
+// template <class T> void swap(optional<T>& x, optional<T>& y)
+// noexcept(noexcept(x.swap(y)));
+
+#include <optional>
+#include <type_traits>
+#include <cassert>
+
+#include "test_macros.h"
+
+using std::optional;
+
+class X
+{
+ int i_;
+public:
+ static unsigned dtor_called;
+ X(int i) : i_(i) {}
+ X(X&& x) = default;
+ X& operator=(X&&) = default;
+ ~X() {++dtor_called;}
+
+ friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
+};
+
+unsigned X::dtor_called = 0;
+
+class Y
+{
+ int i_;
+public:
+ static unsigned dtor_called;
+ Y(int i) : i_(i) {}
+ Y(Y&&) = default;
+ ~Y() {++dtor_called;}
+
+ friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
+ friend void swap(Y& x, Y& y) {std::swap(x.i_, y.i_);}
+};
+
+unsigned Y::dtor_called = 0;
+
+class Z
+{
+ int i_;
+public:
+ Z(int i) : i_(i) {}
+ Z(Z&&) { TEST_THROW(7);}
+
+ friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
+ friend void swap(Z& x, Z& y) { TEST_THROW(6);}
+};
+
+int main()
+{
+ {
+ optional<int> opt1;
+ optional<int> opt2;
+ static_assert(noexcept(swap(opt1, opt2)) == true, "");
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ swap(opt1, opt2);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ }
+ {
+ optional<int> opt1(1);
+ optional<int> opt2;
+ static_assert(noexcept(swap(opt1, opt2)) == true, "");
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == false);
+ swap(opt1, opt2);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 1);
+ }
+ {
+ optional<int> opt1;
+ optional<int> opt2(2);
+ static_assert(noexcept(swap(opt1, opt2)) == true, "");
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ swap(opt1, opt2);
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 2);
+ assert(static_cast<bool>(opt2) == false);
+ }
+ {
+ optional<int> opt1(1);
+ optional<int> opt2(2);
+ static_assert(noexcept(swap(opt1, opt2)) == true, "");
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ swap(opt1, opt2);
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 2);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 1);
+ }
+ {
+ optional<X> opt1;
+ optional<X> opt2;
+ static_assert(noexcept(swap(opt1, opt2)) == true, "");
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ swap(opt1, opt2);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ assert(X::dtor_called == 0);
+ }
+ {
+ optional<X> opt1(1);
+ optional<X> opt2;
+ static_assert(noexcept(swap(opt1, opt2)) == true, "");
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == false);
+ X::dtor_called = 0;
+ swap(opt1, opt2);
+ assert(X::dtor_called == 1);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 1);
+ }
+ {
+ optional<X> opt1;
+ optional<X> opt2(2);
+ static_assert(noexcept(swap(opt1, opt2)) == true, "");
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ X::dtor_called = 0;
+ swap(opt1, opt2);
+ assert(X::dtor_called == 1);
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 2);
+ assert(static_cast<bool>(opt2) == false);
+ }
+ {
+ optional<X> opt1(1);
+ optional<X> opt2(2);
+ static_assert(noexcept(swap(opt1, opt2)) == true, "");
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ X::dtor_called = 0;
+ swap(opt1, opt2);
+ assert(X::dtor_called == 1); // from inside std::swap
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 2);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 1);
+ }
+ {
+ optional<Y> opt1;
+ optional<Y> opt2;
+ static_assert(noexcept(swap(opt1, opt2)) == false, "");
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ swap(opt1, opt2);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ assert(Y::dtor_called == 0);
+ }
+ {
+ optional<Y> opt1(1);
+ optional<Y> opt2;
+ static_assert(noexcept(swap(opt1, opt2)) == false, "");
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == false);
+ Y::dtor_called = 0;
+ swap(opt1, opt2);
+ assert(Y::dtor_called == 1);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 1);
+ }
+ {
+ optional<Y> opt1;
+ optional<Y> opt2(2);
+ static_assert(noexcept(swap(opt1, opt2)) == false, "");
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ Y::dtor_called = 0;
+ swap(opt1, opt2);
+ assert(Y::dtor_called == 1);
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 2);
+ assert(static_cast<bool>(opt2) == false);
+ }
+ {
+ optional<Y> opt1(1);
+ optional<Y> opt2(2);
+ static_assert(noexcept(swap(opt1, opt2)) == false, "");
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ Y::dtor_called = 0;
+ swap(opt1, opt2);
+ assert(Y::dtor_called == 0);
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 2);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 1);
+ }
+ {
+ optional<Z> opt1;
+ optional<Z> opt2;
+ static_assert(noexcept(swap(opt1, opt2)) == false, "");
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ swap(opt1, opt2);
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == false);
+ }
+#ifndef TEST_HAS_NO_EXCEPTIONS
+ {
+ optional<Z> opt1;
+ opt1.emplace(1);
+ optional<Z> opt2;
+ static_assert(noexcept(swap(opt1, opt2)) == false, "");
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == false);
+ try
+ {
+ swap(opt1, opt2);
+ assert(false);
+ }
+ catch (int i)
+ {
+ assert(i == 7);
+ }
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == false);
+ }
+ {
+ optional<Z> opt1;
+ optional<Z> opt2;
+ opt2.emplace(2);
+ static_assert(noexcept(swap(opt1, opt2)) == false, "");
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ try
+ {
+ swap(opt1, opt2);
+ assert(false);
+ }
+ catch (int i)
+ {
+ assert(i == 7);
+ }
+ assert(static_cast<bool>(opt1) == false);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ }
+ {
+ optional<Z> opt1;
+ opt1.emplace(1);
+ optional<Z> opt2;
+ opt2.emplace(2);
+ static_assert(noexcept(swap(opt1, opt2)) == false, "");
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ try
+ {
+ swap(opt1, opt2);
+ assert(false);
+ }
+ catch (int i)
+ {
+ assert(i == 6);
+ }
+ assert(static_cast<bool>(opt1) == true);
+ assert(*opt1 == 1);
+ assert(static_cast<bool>(opt2) == true);
+ assert(*opt2 == 2);
+ }
+#endif // TEST_HAS_NO_EXCEPTIONS
+}
OpenPOWER on IntegriCloud