diff options
Diffstat (limited to 'test/std/utilities/tuple/tuple.tuple/tuple.elem')
9 files changed, 391 insertions, 0 deletions
diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const.fail.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const.fail.cpp new file mode 100644 index 0000000000000..490283e7abdd0 --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const.fail.cpp @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// 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> +// typename tuple_element<I, tuple<Types...> >::type const& +// get(const tuple<Types...>& t); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> +#include <string> +#include <cassert> + +int main() +{ + { + typedef std::tuple<double&, std::string, int> T; + double d = 1.5; + const T t(d, "high", 5); + assert(std::get<0>(t) == 1.5); + assert(std::get<1>(t) == "high"); + assert(std::get<2>(t) == 5); + std::get<0>(t) = 2.5; + assert(std::get<0>(t) == 2.5); + assert(std::get<1>(t) == "high"); + assert(std::get<2>(t) == 5); + assert(d == 2.5); + + std::get<1>(t) = "four"; + } +} diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const.pass.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const.pass.cpp new file mode 100644 index 0000000000000..e21768cb6f5fd --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_const.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. +// +//===----------------------------------------------------------------------===// + +// <tuple> + +// template <class... Types> class tuple; + +// template <size_t I, class... Types> +// typename tuple_element<I, tuple<Types...> >::type const& +// get(const tuple<Types...>& t); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> +#include <string> +#include <cassert> + +struct Empty {}; + +int main() +{ + { + typedef std::tuple<int> T; + const T t(3); + assert(std::get<0>(t) == 3); + } + { + typedef std::tuple<std::string, int> T; + const T t("high", 5); + assert(std::get<0>(t) == "high"); + assert(std::get<1>(t) == 5); + } +#if _LIBCPP_STD_VER > 11 + { + typedef std::tuple<double, int> T; + constexpr T t(2.718, 5); + static_assert(std::get<0>(t) == 2.718, ""); + static_assert(std::get<1>(t) == 5, ""); + } + { + typedef std::tuple<Empty> T; + constexpr T t{Empty()}; + constexpr Empty e = std::get<0>(t); + ((void)e); // Prevent unused warning + } +#endif + { + typedef std::tuple<double&, std::string, int> T; + double d = 1.5; + const T t(d, "high", 5); + assert(std::get<0>(t) == 1.5); + assert(std::get<1>(t) == "high"); + assert(std::get<2>(t) == 5); + std::get<0>(t) = 2.5; + assert(std::get<0>(t) == 2.5); + assert(std::get<1>(t) == "high"); + assert(std::get<2>(t) == 5); + assert(d == 2.5); + } +} diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_non_const.pass.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_non_const.pass.cpp new file mode 100644 index 0000000000000..1c2b17ad88325 --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_non_const.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// 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> +// typename tuple_element<I, tuple<Types...> >::type& +// get(tuple<Types...>& t); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> +#include <string> +#include <cassert> + +#if __cplusplus > 201103L + +struct Empty {}; + +struct S { + std::tuple<int, Empty> a; + int k; + Empty e; + constexpr S() : a{1,Empty{}}, k(std::get<0>(a)), e(std::get<1>(a)) {} + }; + +constexpr std::tuple<int, int> getP () { return { 3, 4 }; } +#endif + +int main() +{ + { + typedef std::tuple<int> T; + T t(3); + assert(std::get<0>(t) == 3); + std::get<0>(t) = 2; + assert(std::get<0>(t) == 2); + } + { + typedef std::tuple<std::string, int> T; + T t("high", 5); + assert(std::get<0>(t) == "high"); + assert(std::get<1>(t) == 5); + std::get<0>(t) = "four"; + std::get<1>(t) = 4; + assert(std::get<0>(t) == "four"); + assert(std::get<1>(t) == 4); + } + { + typedef std::tuple<double&, std::string, int> T; + double d = 1.5; + T t(d, "high", 5); + assert(std::get<0>(t) == 1.5); + assert(std::get<1>(t) == "high"); + assert(std::get<2>(t) == 5); + std::get<0>(t) = 2.5; + std::get<1>(t) = "four"; + std::get<2>(t) = 4; + assert(std::get<0>(t) == 2.5); + assert(std::get<1>(t) == "four"); + assert(std::get<2>(t) == 4); + assert(d == 2.5); + } +#if _LIBCPP_STD_VER > 11 + { // get on an rvalue tuple + static_assert ( std::get<0> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 0, "" ); + static_assert ( std::get<1> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 1, "" ); + static_assert ( std::get<2> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 2, "" ); + static_assert ( std::get<3> ( std::make_tuple ( 0.0f, 1, 2.0, 3L )) == 3, "" ); + static_assert(S().k == 1, ""); + static_assert(std::get<1>(getP()) == 4, ""); + } +#endif + +} diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_rv.pass.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_rv.pass.cpp new file mode 100644 index 0000000000000..6c456d4308269 --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/get_rv.pass.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// 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> +// typename tuple_element<I, tuple<Types...> >::type&& +// get(tuple<Types...>&& t); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> +#include <utility> +#include <memory> +#include <cassert> + +int main() +{ + { + typedef std::tuple<std::unique_ptr<int> > T; + T t(std::unique_ptr<int>(new int(3))); + std::unique_ptr<int> p = std::get<0>(std::move(t)); + assert(*p == 3); + } +} diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type.pass.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type.pass.cpp new file mode 100644 index 0000000000000..aa020dab47c7c --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +#include <tuple> +#include <string> +#include <complex> + +#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<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, "" ); + static_assert ( std::get<const int>(p5) == 2, "" ); + } + + { + const std::tuple<int, const int, double, double> p5 { 1, 2, 3.4, 5.6 }; + const int &i1 = std::get<int>(p5); + const int &i2 = std::get<const int>(p5); + assert ( i1 == 1 ); + assert ( i2 == 2 ); + } + + { + typedef std::unique_ptr<int> upint; + 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 + } + +#endif +} diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type1.fail.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type1.fail.cpp new file mode 100644 index 0000000000000..85c32ca6d4955 --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type1.fail.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +#include <tuple> +#include <string> +#include <complex> + +#include <cassert> + +int main() +{ +#if _LIBCPP_STD_VER > 11 + typedef std::complex<float> cf; + auto t1 = std::make_tuple<int, std::string> ( 42, "Hi" ); + assert (( std::get<cf>(t1) == cf {1,2} )); // no such type +#else +#error +#endif +} diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type2.fail.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type2.fail.cpp new file mode 100644 index 0000000000000..0a8d5829d02b1 --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type2.fail.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +#include <tuple> +#include <string> +#include <complex> + +#include <cassert> + +int main() +{ +#if _LIBCPP_STD_VER > 11 + typedef std::complex<float> cf; + auto t1 = std::make_tuple<int, int, std::string, cf> ( 42, 21, "Hi", { 1,2 } ); + assert ( std::get<int>(t1) == 42 ); // two ints here +#else +#error +#endif +} diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type3.fail.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type3.fail.cpp new file mode 100644 index 0000000000000..0a4550f387dc0 --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type3.fail.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +#include <tuple> +#include <string> +#include <complex> + +#include <cassert> + +int main() +{ +#if _LIBCPP_STD_VER > 11 + typedef std::complex<float> cf; + auto t1 = std::make_tuple<double, int, std::string, cf, int> ( 42, 21, "Hi", { 1,2 } ); + assert ( std::get<int>(t1) == 42 ); // two ints here (one at the end) +#else +#error +#endif +} diff --git a/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type4.fail.cpp b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type4.fail.cpp new file mode 100644 index 0000000000000..ffc715fe9a5bd --- /dev/null +++ b/test/std/utilities/tuple/tuple.tuple/tuple.elem/tuple.by.type4.fail.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +#include <tuple> +#include <string> +#include <memory> + +#include <cassert> + +int main() +{ +#if _LIBCPP_STD_VER > 11 + typedef std::unique_ptr<int> upint; + std::tuple<upint> t(upint(new int(4))); + upint p = std::get<upint>(t); +#else +#error +#endif +} |