diff options
Diffstat (limited to 'libcxx/test/std/utilities')
5 files changed, 264 insertions, 11 deletions
diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const_rv.fail.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const_rv.fail.cpp new file mode 100644 index 00000000000..58df2df7679 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const_rv.fail.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <tuple> + +// template <class... Types> class tuple; + +// template <size_t I, class... Types> +// const typename tuple_element<I, tuple<Types...> >::type&& +// get(const tuple<Types...>&& t); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> + +template <class T> void cref(T const&) {}; +template <class T> void cref(T const&&) = delete; + +std::tuple<int> const tup4() { return std::make_tuple(4); } + +int main() +{ + // LWG2485: tuple should not open a hole in the type system, get() should + // imitate [expr.ref]'s rules for accessing data members + { + cref(std::get<0>(tup4())); // expected-error {{call to deleted function 'cref'}} + } +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const_rv.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const_rv.pass.cpp new file mode 100644 index 00000000000..720a9064015 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const_rv.pass.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <tuple> + +// template <class... Types> class tuple; + +// template <size_t I, class... Types> +// const typename tuple_element<I, tuple<Types...> >::type&& +// get(const tuple<Types...>&& t); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> +#include <utility> +#include <string> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +int main() +{ + { + typedef std::tuple<int> T; + const T t(3); + static_assert(std::is_same<const int&&, decltype(std::get<0>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<0>(std::move(t))), ""); + const int&& i = std::get<0>(std::move(t)); + assert(i == 3); + } + + { + typedef std::tuple<std::string, int> T; + const T t("high", 5); + static_assert(std::is_same<const std::string&&, decltype(std::get<0>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<0>(std::move(t))), ""); + static_assert(std::is_same<const int&&, decltype(std::get<1>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<1>(std::move(t))), ""); + const std::string&& s = std::get<0>(std::move(t)); + const int&& i = std::get<1>(std::move(t)); + assert(s == "high"); + assert(i == 5); + } + + { + int x = 42; + int const y = 43; + std::tuple<int&, int const&> const p(x, y); + static_assert(std::is_same<int&, decltype(std::get<0>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<0>(std::move(p))), ""); + static_assert(std::is_same<int const&, decltype(std::get<1>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<1>(std::move(p))), ""); + } + + { + int x = 42; + int const y = 43; + std::tuple<int&&, int const&&> const p(std::move(x), std::move(y)); + static_assert(std::is_same<int&&, decltype(std::get<0>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<0>(std::move(p))), ""); + static_assert(std::is_same<int const&&, decltype(std::get<1>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<1>(std::move(p))), ""); + } + +#if TEST_STD_VER > 11 + { + typedef std::tuple<double, int> T; + constexpr const T t(2.718, 5); + static_assert(std::get<0>(std::move(t)) == 2.718, ""); + static_assert(std::get<1>(std::move(t)) == 5, ""); + } +#endif +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type.pass.cpp index aa020dab47c..7bbd0016678 100644 --- a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type.pass.cpp +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type.pass.cpp @@ -10,30 +10,31 @@ // UNSUPPORTED: c++98, c++03, c++11 #include <tuple> +#include <utility> #include <string> #include <complex> +#include <type_traits> #include <cassert> int main() { -#if _LIBCPP_STD_VER > 11 typedef std::complex<float> cf; { auto t1 = std::tuple<int, std::string, cf> { 42, "Hi", { 1,2 }}; - assert ( std::get<int>(t1) == 42 ); // find at the beginning + assert ( std::get<int>(t1) == 42 ); // find at the beginning assert ( std::get<std::string>(t1) == "Hi" ); // find in the middle assert ( std::get<cf>(t1).real() == 1 ); // find at the end assert ( std::get<cf>(t1).imag() == 2 ); } - + { auto t2 = std::tuple<int, std::string, int, cf> { 42, "Hi", 23, { 1,2 }}; // get<int> would fail! assert ( std::get<std::string>(t2) == "Hi" ); assert (( std::get<cf>(t2) == cf{ 1,2 } )); } - + { constexpr std::tuple<int, const int, double, double> p5 { 1, 2, 3.4, 5.6 }; static_assert ( std::get<int>(p5) == 1, "" ); @@ -53,8 +54,40 @@ int main() std::tuple<upint> t(upint(new int(4))); upint p = std::get<upint>(std::move(t)); // get rvalue assert(*p == 4); - assert(std::get<0>(t) == nullptr); // has been moved from + assert(std::get<upint>(t) == nullptr); // has been moved from + } + + { + typedef std::unique_ptr<int> upint; + const std::tuple<upint> t(upint(new int(4))); + const upint&& p = std::get<upint>(std::move(t)); // get const rvalue + assert(*p == 4); + assert(std::get<upint>(t) != nullptr); } -#endif + { + int x = 42; + int tuple y = 43; + std::tuple<int&, int const&> const t(x, y); + static_assert(std::is_same<int&, decltype(std::get<int&>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<int&>(std::move(t))), ""); + static_assert(std::is_same<int const&, decltype(std::get<int const&>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<int const&>(std::move(t))), ""); + } + + { + int x = 42; + int tuple y = 43; + std::tuple<int&&, int const&&> const t(std::move(x), std::move(y)); + static_assert(std::is_same<int&&, decltype(std::get<int&&>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<int&&>(std::move(t))), ""); + static_assert(std::is_same<int const&&, decltype(std::get<int const&&>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<int const&&>(std::move(t))), ""); + } + + { + constexpr const std::tuple<int, const int, double, double> t { 1, 2, 3.4, 5.6 }; + static_assert(std::get<int>(std::move(t)) == 1, ""); + static_assert(std::get<const int>(std::move(t)) == 2, ""); + } } diff --git a/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_const_rv.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_const_rv.pass.cpp new file mode 100644 index 00000000000..edd2f3d0752 --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pair.astuple/get_const_rv.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <utility> + +// template <class T1, class T2> struct pair + +// template<size_t I, class T1, class T2> +// const typename tuple_element<I, std::pair<T1, T2> >::type&& +// get(const pair<T1, T2>&&); + +// UNSUPPORTED: c++98, c++03 + +#include <utility> +#include <memory> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +int main() +{ + { + typedef std::pair<std::unique_ptr<int>, short> P; + const P p(std::unique_ptr<int>(new int(3)), 4); + static_assert(std::is_same<const std::unique_ptr<int>&&, decltype(std::get<0>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<0>(std::move(p))), ""); + const std::unique_ptr<int>&& ptr = std::get<0>(std::move(p)); + assert(*ptr == 3); + } + + { + int x = 42; + int const y = 43; + std::pair<int&, int const&> const p(x, y); + static_assert(std::is_same<int&, decltype(std::get<0>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<0>(std::move(p))), ""); + static_assert(std::is_same<int const&, decltype(std::get<1>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<1>(std::move(p))), ""); + } + + { + int x = 42; + int const y = 43; + std::pair<int&&, int const&&> const p(std::move(x), std::move(y)); + static_assert(std::is_same<int&&, decltype(std::get<0>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<0>(std::move(p))), ""); + static_assert(std::is_same<int const&&, decltype(std::get<1>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<1>(std::move(p))), ""); + } + +#if TEST_STD_VER > 11 + { + typedef std::pair<int, short> P; + constexpr const P p1(3, 4); + static_assert(std::get<0>(std::move(p1)) == 3, ""); + static_assert(std::get<1>(std::move(p1)) == 4, ""); + } +#endif +} diff --git a/libcxx/test/std/utilities/utility/pairs/pair.astuple/pairs.by.type.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pair.astuple/pairs.by.type.pass.cpp index 176d58330d1..efcc2cedc6e 100644 --- a/libcxx/test/std/utilities/utility/pairs/pair.astuple/pairs.by.type.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pair.astuple/pairs.by.type.pass.cpp @@ -7,15 +7,17 @@ // //===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11 + #include <utility> #include <string> +#include <type_traits> #include <complex> #include <cassert> int main() { -#if _LIBCPP_STD_VER > 11 typedef std::complex<float> cf; { auto t1 = std::make_pair<int, cf> ( 42, { 1,2 } ); @@ -23,7 +25,7 @@ int main() assert ( std::get<cf>(t1).real() == 1 ); assert ( std::get<cf>(t1).imag() == 2 ); } - + { const std::pair<int, const int> p1 { 1, 2 }; const int &i1 = std::get<int>(p1); @@ -35,10 +37,48 @@ int main() { typedef std::unique_ptr<int> upint; std::pair<upint, int> t(upint(new int(4)), 42); - upint p = std::get<0>(std::move(t)); // get rvalue + upint p = std::get<upint>(std::move(t)); // get rvalue + assert(*p == 4); + assert(std::get<upint>(t) == nullptr); // has been moved from + } + + { + typedef std::unique_ptr<int> upint; + const std::pair<upint, int> t(upint(new int(4)), 42); + static_assert(std::is_same<const upint&&, decltype(std::get<upint>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<upint>(std::move(t))), ""); + static_assert(std::is_same<const int&&, decltype(std::get<int>(std::move(t)))>::value, ""); + static_assert(noexcept(std::get<int>(std::move(t))), ""); + auto&& p = std::get<upint>(std::move(t)); // get const rvalue + auto&& i = std::get<int>(std::move(t)); // get const rvalue assert(*p == 4); - assert(std::get<0>(t) == nullptr); // has been moved from + assert(i == 42); + assert(std::get<upint>(t) != nullptr); } -#endif + { + int x = 42; + int const y = 43; + std::pair<int&, int const&> const p(x, y); + static_assert(std::is_same<int&, decltype(std::get<int&>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<int&>(std::move(p))), ""); + static_assert(std::is_same<int const&, decltype(std::get<int const&>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<int const&>(std::move(p))), ""); + } + + { + int x = 42; + int const y = 43; + std::pair<int&&, int const&&> const p(std::move(x), std::move(y)); + static_assert(std::is_same<int&&, decltype(std::get<int&&>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<int&&>(std::move(p))), ""); + static_assert(std::is_same<int const&&, decltype(std::get<int const&&>(std::move(p)))>::value, ""); + static_assert(noexcept(std::get<int const&&>(std::move(p))), ""); + } + + { + constexpr const std::pair<int, const int> p { 1, 2 }; + static_assert(std::get<int>(std::move(p)) == 1, ""); + static_assert(std::get<const int>(std::move(p)) == 2, ""); + } } |