diff options
Diffstat (limited to 'libstdc++-v3/testsuite')
73 files changed, 4922 insertions, 63 deletions
diff --git a/libstdc++-v3/testsuite/20_util/any/assign/1.cc b/libstdc++-v3/testsuite/20_util/any/assign/1.cc new file mode 100644 index 00000000000..582a92d2f07 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/assign/1.cc @@ -0,0 +1,60 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <testsuite_hooks.h> + +using std::any; + +void test01() +{ + any x; + any y; + y = x; + VERIFY( x.empty() ); + VERIFY( y.empty() ); + + y = std::move(x); + VERIFY( x.empty() ); + VERIFY( y.empty() ); +} + +void test02() +{ + any x(1); + any y; + y = x; + VERIFY( !x.empty() ); + VERIFY( !y.empty() ); + + x = std::move(y); + VERIFY( !x.empty() ); + VERIFY( y.empty() ); + + x = y; + VERIFY( x.empty() ); + VERIFY( y.empty() ); +} + +int main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/assign/2.cc b/libstdc++-v3/testsuite/20_util/any/assign/2.cc new file mode 100644 index 00000000000..b333e5df796 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/assign/2.cc @@ -0,0 +1,51 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <testsuite_hooks.h> + +using std::any; +using std::any_cast; + +struct X +{ + bool moved = false; + bool moved_from = false; + X() = default; + X(const X&) = default; + X(X&& x) : moved(true) { x.moved_from = true; } +}; + +void test01() +{ + X x; + any a1; + a1 = x; + VERIFY(x.moved_from == false); + any a2; + a2 = std::move(x); + VERIFY(x.moved_from == true); + VERIFY(any_cast<X&>(a2).moved == true ); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/assign/self.cc b/libstdc++-v3/testsuite/20_util/any/assign/self.cc new file mode 100644 index 00000000000..e773efad11c --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/assign/self.cc @@ -0,0 +1,93 @@ +// Copyright (C) 2015-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++17" } + +#include <any> +#include <set> +#include <testsuite_hooks.h> + +std::set<const void*> live_objects; + +struct A { + A() { live_objects.insert(this); } + ~A() { live_objects.erase(this); } + A(const A& a) { VERIFY(live_objects.count(&a)); live_objects.insert(this); } +}; + +void +test01() +{ + using std::any; + + any a; + a = a; + VERIFY( a.empty() ); + + a = A{}; + a = a; + VERIFY( !a.empty() ); + + a.clear(); + VERIFY( live_objects.empty() ); +} + +void +test02() +{ + using std::any; + + struct X { + any a; + }; + + X x; + std::swap(x, x); // results in "self-move-assignment" of X::a + VERIFY( x.a.empty() ); + + x.a = A{}; + std::swap(x, x); // results in "self-move-assignment" of X::a + VERIFY( !x.a.empty() ); + + x.a.clear(); + VERIFY( live_objects.empty() ); +} + +void +test03() +{ + using std::any; + + any a; + a.swap(a); + VERIFY( a.empty() ); + + a = A{}; + a.swap(a); + VERIFY( !a.empty() ); + + a.clear(); + VERIFY( live_objects.empty() ); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/cons/1.cc b/libstdc++-v3/testsuite/20_util/any/cons/1.cc new file mode 100644 index 00000000000..d66320413d4 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/cons/1.cc @@ -0,0 +1,58 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <testsuite_hooks.h> + +using std::any; + +void test01() +{ + any x; + VERIFY( x.empty() ); + + any y(x); + VERIFY( x.empty() ); + VERIFY( y.empty() ); + + any z(std::move(y)); + VERIFY( y.empty() ); + VERIFY( z.empty() ); +} + +void test02() +{ + any x(1); + VERIFY( !x.empty() ); + + any y(x); + VERIFY( !x.empty() ); + VERIFY( !y.empty() ); + + any z(std::move(y)); + VERIFY( y.empty() ); + VERIFY( !z.empty() ); +} + +int main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/cons/2.cc b/libstdc++-v3/testsuite/20_util/any/cons/2.cc new file mode 100644 index 00000000000..613fa626d06 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/cons/2.cc @@ -0,0 +1,49 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <testsuite_hooks.h> + +using std::any; +using std::any_cast; + +struct X +{ + bool moved = false; + bool moved_from = false; + X() = default; + X(const X&) = default; + X(X&& x) : moved(true) { x.moved_from = true; } +}; + +void test01() +{ + X x; + any a1(x); + VERIFY(x.moved_from == false); + any a2(std::move(x)); + VERIFY(x.moved_from == true); + VERIFY(any_cast<X&>(a2).moved == true ); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/debug/irreflexive_neg.cc b/libstdc++-v3/testsuite/20_util/any/cons/aligned.cc index 26e7f87eb78..9b23d8ac0f4 100644 --- a/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/debug/irreflexive_neg.cc +++ b/libstdc++-v3/testsuite/20_util/any/cons/aligned.cc @@ -5,66 +5,48 @@ // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. -// + // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -// + // You should have received a copy of the GNU General Public License along // with this library; see the file COPYING3. If not see // <http://www.gnu.org/licenses/>. -// -// { dg-options "-std=gnu++11" } -// { dg-require-debug-mode "" } -// { dg-do run { xfail *-*-* } } - -#include <algorithm> -#include <testsuite_hooks.h> -struct A -{ - A(int i) : _i(i) - { } +// { dg-options "-std=gnu++17" } - int _i; -}; +#include <any> +#include <cstdint> +#include <testsuite_hooks.h> -bool -operator<(A a, int i) -{ return a._i < i; } +// Alignment requiremnts of this type prevent it being stored in 'any' +struct alignas(2 * alignof(void*)) X { }; bool -operator<(int i, A a) -{ return i < a._i; } - -void test01() +stored_internally(void* obj, const std::any& a) { - bool test __attribute__((unused)) = true; - - A as[] { 0, 1, 2, 3 }; - int is[] { 0, 1, 2, 3 }; - VERIFY( !std::lexicographical_compare(as, as + 4, is, is + 4) ); + std::uintptr_t a_addr = reinterpret_cast<std::uintptr_t>(&a); + std::uintptr_t a_end = a_addr + sizeof(a); + std::uintptr_t obj_addr = reinterpret_cast<std::uintptr_t>(obj); + return (a_addr <= obj_addr) && (obj_addr < a_end); } -bool -bad_lower(int lhs, int rhs) +void +test01() { - if (lhs == 0) - return true; - - return lhs < rhs; -} + std::any a = X{}; + X& x = std::any_cast<X&>(a); + VERIFY( !stored_internally(&x, a) ); -void test02() -{ - int is[] { 0, 1, 2, 3 }; - std::lexicographical_compare(is, is + 4, is, is + 4, bad_lower); + a = 'X'; + char& c = std::any_cast<char&>(a); + VERIFY( stored_internally(&c, a) ); } -int main() +int +main() { test01(); - test02(); - return 0; } diff --git a/libstdc++-v3/testsuite/20_util/any/cons/nontrivial.cc b/libstdc++-v3/testsuite/20_util/any/cons/nontrivial.cc new file mode 100644 index 00000000000..bb46452c619 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/cons/nontrivial.cc @@ -0,0 +1,75 @@ +// Copyright (C) 2015-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++17" } + +#include <any> +#include <testsuite_hooks.h> + +struct LocationAware +{ + LocationAware() { } + ~LocationAware() { VERIFY(self == this); } + LocationAware(const LocationAware&) { } + LocationAware& operator=(const LocationAware&) { return *this; } + LocationAware(LocationAware&&) noexcept { } + LocationAware& operator=(LocationAware&&) noexcept { return *this; } + + void* const self = this; +}; +static_assert(std::is_nothrow_move_constructible<LocationAware>::value, ""); +static_assert(!std::is_trivially_copyable<LocationAware>::value, ""); + +using std::any; + +void +test01() +{ + + LocationAware l; + any a = l; +} + +void +test02() +{ + LocationAware l; + any a = l; + any b = a; + { + any tmp = std::move(a); + a = std::move(b); + b = std::move(tmp); + } +} + +void +test03() +{ + LocationAware l; + any a = l; + any b = a; + swap(a, b); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc b/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc new file mode 100644 index 00000000000..96f9419e070 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc @@ -0,0 +1,114 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <string> +#include <cstring> +#include <testsuite_hooks.h> + +using std::any; +using std::any_cast; + +void test01() +{ + using std::string; + using std::strcmp; + + // taken from example in N3804 proposal + + any x(5); // x holds int + VERIFY(any_cast<int>(x) == 5); // cast to value + any_cast<int&>(x) = 10; // cast to reference + VERIFY(any_cast<int>(x) == 10); + + x = "Meow"; // x holds const char* + VERIFY(strcmp(any_cast<const char*>(x), "Meow") == 0); + any_cast<const char*&>(x) = "Harry"; + VERIFY(strcmp(any_cast<const char*>(x), "Harry") == 0); + + x = string("Meow"); // x holds string + string s, s2("Jane"); + s = move(any_cast<string&>(x)); // move from any + VERIFY(s == "Meow"); + any_cast<string&>(x) = move(s2); // move to any + VERIFY(any_cast<const string&>(x) == "Jane"); + + string cat("Meow"); + const any y(cat); // const y holds string + VERIFY(any_cast<const string&>(y) == cat); +} + +void test02() +{ + using std::bad_any_cast; + any x(1); + auto p = any_cast<double>(&x); + VERIFY(p == nullptr); + + x = 1.0; + p = any_cast<double>(&x); + VERIFY(p != nullptr); + + x = any(); + p = any_cast<double>(&x); + VERIFY(p == nullptr); + + try { + any_cast<double>(x); + VERIFY(false); + } catch (const bad_any_cast&) { + } +} + +static int move_count = 0; + +void test03() +{ + struct MoveEnabled + { + MoveEnabled(MoveEnabled&&) + { + ++move_count; + } + MoveEnabled() = default; + MoveEnabled(const MoveEnabled&) = default; + }; + MoveEnabled m; + MoveEnabled m2 = any_cast<MoveEnabled>(any(m)); + VERIFY(move_count == 1); + MoveEnabled&& m3 = any_cast<MoveEnabled&&>(any(m)); + VERIFY(move_count == 1); + struct MoveDeleted + { + MoveDeleted(MoveDeleted&&) = delete; + MoveDeleted() = default; + MoveDeleted(const MoveDeleted&) = default; + }; + MoveDeleted md; + MoveDeleted&& md2 = any_cast<MoveDeleted>(any(std::move(md))); + MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md))); +} + +int main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc b/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc new file mode 100644 index 00000000000..43c6c6b4a26 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc @@ -0,0 +1,30 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> + +void test01() +{ + using std::any; + using std::any_cast; + + const any y(1); + any_cast<int&>(y); // { dg-error "qualifiers" "" { target { *-*-* } } 357 } +} diff --git a/libstdc++-v3/testsuite/20_util/any/misc/any_cast_no_rtti.cc b/libstdc++-v3/testsuite/20_util/any/misc/any_cast_no_rtti.cc new file mode 100644 index 00000000000..fa16f2371be --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/misc/any_cast_no_rtti.cc @@ -0,0 +1,54 @@ +// { dg-options "-std=gnu++17 -fno-rtti" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <string> +#include <cstring> +#include <testsuite_hooks.h> + +using std::any; +using std::any_cast; + +void test01() +{ + using std::bad_any_cast; + any x(1); + auto p = any_cast<double>(&x); + VERIFY(p == nullptr); + + x = 1.0; + p = any_cast<double>(&x); + VERIFY(p != nullptr); + + x = any(); + p = any_cast<double>(&x); + VERIFY(p == nullptr); + + try { + any_cast<double>(x); + VERIFY(false); + } catch (const bad_any_cast&) { + } +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/misc/swap.cc b/libstdc++-v3/testsuite/20_util/any/misc/swap.cc new file mode 100644 index 00000000000..0b3e1eb26de --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/misc/swap.cc @@ -0,0 +1,38 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <testsuite_hooks.h> + +using std::any; + +void test01() +{ + any x(1); + any y; + swap(x, y); + VERIFY( x.empty() ); + VERIFY( !y.empty() ); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/modifiers/1.cc b/libstdc++-v3/testsuite/20_util/any/modifiers/1.cc new file mode 100644 index 00000000000..36b9c247843 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/modifiers/1.cc @@ -0,0 +1,44 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <testsuite_hooks.h> + +using std::any; + +void test01() +{ + any x(1); + any y; + x.swap(y); + VERIFY( x.empty() ); + VERIFY( !y.empty() ); + x.swap(y); + VERIFY( !x.empty() ); + VERIFY( y.empty() ); + + x.clear(); + VERIFY( x.empty() ); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/observers/type.cc b/libstdc++-v3/testsuite/20_util/any/observers/type.cc new file mode 100644 index 00000000000..af4dc30c766 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/observers/type.cc @@ -0,0 +1,39 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <testsuite_hooks.h> + +using std::any; + +void test01() +{ + any x; + VERIFY( x.type() == typeid(void) ); + x = 1; + VERIFY( x.type() == typeid(int) ); + x = any(); + VERIFY( x.type() == typeid(void) ); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/any/typedefs.cc b/libstdc++-v3/testsuite/20_util/any/typedefs.cc new file mode 100644 index 00000000000..11a57924b25 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/typedefs.cc @@ -0,0 +1,30 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <any> +#include <type_traits> +#include <typeinfo> + +using check1_t = std::any; +using check2_t = std::bad_any_cast; + +static_assert(std::is_base_of<std::bad_cast, check2_t>::value, + "bad_any_cast must derive from bad_cast"); + diff --git a/libstdc++-v3/testsuite/20_util/optional/assignment/1.cc b/libstdc++-v3/testsuite/20_util/optional/assignment/1.cc new file mode 100644 index 00000000000..25c62418b44 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/assignment/1.cc @@ -0,0 +1,195 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct exception {}; + +int counter = 0; + +struct mixin_counter +{ + mixin_counter() { ++counter; } + mixin_counter(mixin_counter const&) { ++counter; } + ~mixin_counter() { --counter; } +}; + +struct value_type : private mixin_counter +{ + enum state_type + { + zero, + moved_from, + throwing_construction, + throwing_copy, + throwing_copy_assignment, + throwing_move, + throwing_move_assignment, + threw, + }; + + value_type() = default; + + explicit value_type(state_type state_) + : state(state_) + { + throw_if(throwing_construction); + } + + value_type(value_type const& other) + : state(other.state) + { + throw_if(throwing_copy); + } + + value_type& + operator=(value_type const& other) + { + state = other.state; + throw_if(throwing_copy_assignment); + return *this; + } + + value_type(value_type&& other) + : state(other.state) + { + other.state = moved_from; + throw_if(throwing_move); + } + + value_type& + operator=(value_type&& other) + { + state = other.state; + other.state = moved_from; + throw_if(throwing_move_assignment); + return *this; + } + + void throw_if(state_type match) + { + if(state == match) + { + state = threw; + throw exception {}; + } + } + + state_type state = zero; +}; + +int main() +{ + using O = std::optional<value_type>; + using S = value_type::state_type; + auto const make = [](S s = S::zero) { return O { std::in_place, s }; }; + + enum outcome_type { nothrow, caught, bad_catch }; + + // Check copy/move assignment for disengaged optional + + // From disengaged optional + { + O o; + VERIFY( !o ); + O p; + o = p; + VERIFY( !o ); + VERIFY( !p ); + } + + { + O o; + VERIFY( !o ); + O p; + o = std::move(p); + VERIFY( !o ); + VERIFY( !p ); + } + + { + O o; + VERIFY( !o ); + o = {}; + VERIFY( !o ); + } + + // From engaged optional + { + O o; + VERIFY( !o ); + O p = make(S::throwing_copy_assignment); + o = p; + VERIFY( o && o->state == S::throwing_copy_assignment ); + VERIFY( p && p->state == S::throwing_copy_assignment ); + } + + { + O o; + VERIFY( !o ); + O p = make(S::throwing_move_assignment); + o = std::move(p); + VERIFY( o && o->state == S::throwing_move_assignment ); + VERIFY( p && p->state == S::moved_from ); + } + + { + outcome_type outcome {}; + O o; + VERIFY( !o ); + O p = make(S::throwing_copy); + + try + { + o = p; + } + catch(exception const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( outcome == caught ); + VERIFY( !o ); + VERIFY( p && p->state == S::throwing_copy ); + } + + { + outcome_type outcome {}; + O o; + VERIFY( !o ); + O p = make(S::throwing_move); + + try + { + o = std::move(p); + } + catch(exception const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( outcome == caught ); + VERIFY( !o ); + VERIFY( p && p->state == S::moved_from ); + } + + VERIFY( counter == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/assignment/2.cc b/libstdc++-v3/testsuite/20_util/optional/assignment/2.cc new file mode 100644 index 00000000000..d9d6ffab51f --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/assignment/2.cc @@ -0,0 +1,193 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct exception {}; + +int counter = 0; + +struct mixin_counter +{ + mixin_counter() { ++counter; } + mixin_counter(mixin_counter const&) { ++counter; } + ~mixin_counter() { --counter; } +}; + +struct value_type : private mixin_counter +{ + enum state_type + { + zero, + moved_from, + throwing_construction, + throwing_copy, + throwing_copy_assignment, + throwing_move, + throwing_move_assignment, + threw, + }; + + value_type() = default; + + explicit value_type(state_type state_) + : state(state_) + { + throw_if(throwing_construction); + } + + value_type(value_type const& other) + : state(other.state) + { + throw_if(throwing_copy); + } + + value_type& + operator=(value_type const& other) + { + state = other.state; + throw_if(throwing_copy_assignment); + return *this; + } + + value_type(value_type&& other) + : state(other.state) + { + other.state = moved_from; + throw_if(throwing_move); + } + + value_type& + operator=(value_type&& other) + { + state = other.state; + other.state = moved_from; + throw_if(throwing_move_assignment); + return *this; + } + + void throw_if(state_type match) + { + if(state == match) + { + state = threw; + throw exception {}; + } + } + + state_type state = zero; +}; + +int main() +{ + using O = std::optional<value_type>; + using S = value_type::state_type; + auto const make = [](S s = S::zero) { return O { std::in_place, s }; }; + + enum outcome_type { nothrow, caught, bad_catch }; + + // Check copy/move assignment for engaged optional + + // From disengaged optional + { + O o = make(S::zero); + VERIFY( o ); + O p; + o = p; + VERIFY( !o ); + VERIFY( !p ); + } + + { + O o = make(S::zero); + VERIFY( o ); + O p; + o = std::move(p); + VERIFY( !o ); + VERIFY( !p ); + } + + { + O o = make(S::zero); + VERIFY( o ); + o = {}; + VERIFY( !o ); + } + + // From engaged optional + { + O o = make(S::zero); + VERIFY( o ); + O p = make(S::throwing_copy); + o = p; + VERIFY( o && o->state == S::throwing_copy); + VERIFY( p && p->state == S::throwing_copy); + } + + { + O o = make(S::zero); + VERIFY( o ); + O p = make(S::throwing_move); + o = std::move(p); + VERIFY( o && o->state == S::throwing_move); + VERIFY( p && p->state == S::moved_from); + } + + { + outcome_type outcome {}; + O o = make(S::zero); + VERIFY( o ); + O p = make(S::throwing_copy_assignment); + + try + { + o = p; + } + catch(exception const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( o && o->state == S::threw); + VERIFY( p && p->state == S::throwing_copy_assignment); + } + + { + outcome_type outcome {}; + O o = make(S::zero); + VERIFY( o ); + O p = make(S::throwing_move_assignment); + + try + { + o = std::move(p); + } + catch(exception const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( o && o->state == S::threw); + VERIFY( p && p->state == S::moved_from); + } + + VERIFY( counter == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/assignment/3.cc b/libstdc++-v3/testsuite/20_util/optional/assignment/3.cc new file mode 100644 index 00000000000..ce038b37cac --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/assignment/3.cc @@ -0,0 +1,158 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct exception {}; + +int counter = 0; + +struct mixin_counter +{ + mixin_counter() { ++counter; } + mixin_counter(mixin_counter const&) { ++counter; } + ~mixin_counter() { --counter; } +}; + +struct value_type : private mixin_counter +{ + enum state_type + { + zero, + moved_from, + throwing_construction, + throwing_copy, + throwing_copy_assignment, + throwing_move, + throwing_move_assignment, + threw, + }; + + value_type() = default; + + explicit value_type(state_type state_) + : state(state_) + { + throw_if(throwing_construction); + } + + value_type(value_type const& other) + : state(other.state) + { + throw_if(throwing_copy); + } + + value_type& + operator=(value_type const& other) + { + state = other.state; + throw_if(throwing_copy_assignment); + return *this; + } + + value_type(value_type&& other) + : state(other.state) + { + other.state = moved_from; + throw_if(throwing_move); + } + + value_type& + operator=(value_type&& other) + { + state = other.state; + other.state = moved_from; + throw_if(throwing_move_assignment); + return *this; + } + + void throw_if(state_type match) + { + if(state == match) + { + state = threw; + throw exception {}; + } + } + + state_type state = zero; +}; + +int main() +{ + using O = std::optional<value_type>; + using S = value_type::state_type; + auto const make = [](S s = S::zero) { return value_type { s }; }; + + enum outcome_type { nothrow, caught, bad_catch }; + + // Check value assignment for disengaged optional + + { + O o; + value_type v = make(S::throwing_copy_assignment); + o = v; + VERIFY( o && o->state == S::throwing_copy_assignment ); + } + + { + O o; + value_type v = make(S::throwing_move_assignment); + o = std::move(v); + VERIFY( o && o->state == S::throwing_move_assignment ); + } + + { + outcome_type outcome {}; + O o; + value_type v = make(S::throwing_copy); + + try + { + o = v; + } + catch(exception const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( !o ); + } + + { + outcome_type outcome {}; + O o; + value_type v = make(S::throwing_move); + + try + { + o = std::move(v); + } + catch(exception const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( !o ); + } + + VERIFY( counter == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/assignment/4.cc b/libstdc++-v3/testsuite/20_util/optional/assignment/4.cc new file mode 100644 index 00000000000..7b972381831 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/assignment/4.cc @@ -0,0 +1,158 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct exception {}; + +int counter = 0; + +struct mixin_counter +{ + mixin_counter() { ++counter; } + mixin_counter(mixin_counter const&) { ++counter; } + ~mixin_counter() { --counter; } +}; + +struct value_type : private mixin_counter +{ + enum state_type + { + zero, + moved_from, + throwing_construction, + throwing_copy, + throwing_copy_assignment, + throwing_move, + throwing_move_assignment, + threw, + }; + + value_type() = default; + + explicit value_type(state_type state_) + : state(state_) + { + throw_if(throwing_construction); + } + + value_type(value_type const& other) + : state(other.state) + { + throw_if(throwing_copy); + } + + value_type& + operator=(value_type const& other) + { + state = other.state; + throw_if(throwing_copy_assignment); + return *this; + } + + value_type(value_type&& other) + : state(other.state) + { + other.state = moved_from; + throw_if(throwing_move); + } + + value_type& + operator=(value_type&& other) + { + state = other.state; + other.state = moved_from; + throw_if(throwing_move_assignment); + return *this; + } + + void throw_if(state_type match) + { + if(state == match) + { + state = threw; + throw exception {}; + } + } + + state_type state = zero; +}; + +int main() +{ + using O = std::optional<value_type>; + using S = value_type::state_type; + auto const make = [](S s = S::zero) { return value_type { s }; }; + + enum outcome_type { nothrow, caught, bad_catch }; + + // Check value assignment for engaged optional + + { + O o = make(); + value_type v = make(S::throwing_copy); + o = v; + VERIFY( o && o->state == S::throwing_copy); + } + + { + O o = make(); + value_type v = make(S::throwing_move); + o = std::move(v); + VERIFY( o && o->state == S::throwing_move); + } + + { + outcome_type outcome {}; + O o = make(); + value_type v = make(S::throwing_copy_assignment); + + try + { + o = v; + } + catch(exception const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( o && o->state == S::threw ); + } + + { + outcome_type outcome {}; + O o = make(); + value_type v = make(S::throwing_move_assignment); + + try + { + o = std::move(v); + } + catch(exception const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( o && o->state == S::threw ); + } + + VERIFY( counter == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/assignment/5.cc b/libstdc++-v3/testsuite/20_util/optional/assignment/5.cc new file mode 100644 index 00000000000..e450a461399 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/assignment/5.cc @@ -0,0 +1,66 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int counter = 0; + +struct mixin_counter +{ + mixin_counter() { ++counter; } + mixin_counter(mixin_counter const&) { ++counter; } + ~mixin_counter() { --counter; } +}; + +struct value_type : private mixin_counter { }; + +int main() +{ + using O = std::optional<value_type>; + + // Check std::nullopt_t and 'default' (= {}) assignment + + { + O o; + o = std::nullopt; + VERIFY( !o ); + } + + { + O o { std::in_place }; + o = std::nullopt; + VERIFY( !o ); + } + + { + O o; + o = {}; + VERIFY( !o ); + } + + { + O o { std::in_place }; + o = {}; + VERIFY( !o ); + } + + VERIFY( counter == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/assignment/6.cc b/libstdc++-v3/testsuite/20_util/optional/assignment/6.cc new file mode 100644 index 00000000000..78e975bbe32 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/assignment/6.cc @@ -0,0 +1,83 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int counter = 0; + +struct mixin_counter +{ + mixin_counter() { ++counter; } + mixin_counter(mixin_counter const&) { ++counter; } + ~mixin_counter() { --counter; } +}; + +struct value_type : private mixin_counter +{ + value_type() = default; + value_type(int) : state(1) { } + value_type(std::initializer_list<char>, const char*) : state(2) { } + int state = 0; +}; + +int main() +{ + using O = std::optional<value_type>; + + // Check emplace + + { + O o; + o.emplace(); + VERIFY( o && o->state == 0 ); + } + { + O o { std::in_place, 0 }; + o.emplace(); + VERIFY( o && o->state == 0 ); + } + + { + O o; + o.emplace(0); + VERIFY( o && o->state == 1 ); + } + { + O o { std::in_place }; + o.emplace(0); + VERIFY( o && o->state == 1 ); + } + + { + O o; + o.emplace({ 'a' }, ""); + VERIFY( o && o->state == 2 ); + } + { + O o { std::in_place }; + o.emplace({ 'a' }, ""); + VERIFY( o && o->state == 2 ); + } + + static_assert( !std::is_constructible<O, std::initializer_list<int>, int>(), "" ); + + VERIFY( counter == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/cons/copy.cc b/libstdc++-v3/testsuite/20_util/optional/cons/copy.cc new file mode 100644 index 00000000000..58f91768df4 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/cons/copy.cc @@ -0,0 +1,126 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct tracker +{ + tracker(int value) : value(value) { ++count; } + ~tracker() { --count; } + + tracker(tracker const& other) : value(other.value) { ++count; } + tracker(tracker&& other) : value(other.value) + { + other.value = -1; + ++count; + } + + tracker& operator=(tracker const&) = default; + tracker& operator=(tracker&&) = default; + + int value; + + static int count; +}; + +int tracker::count = 0; + +struct exception { }; + +struct throwing_copy +{ + throwing_copy() = default; + throwing_copy(throwing_copy const&) { throw exception {}; } +}; + +int main() +{ + // [20.5.4.1] Constructors + + { + std::optional<long> o; + auto copy = o; + VERIFY( !copy ); + VERIFY( !o ); + } + + { + const long val = 0x1234ABCD; + std::optional<long> o { std::in_place, val}; + auto copy = o; + VERIFY( copy ); + VERIFY( *copy == val ); + VERIFY( o && o == val ); + } + + { + std::optional<tracker> o; + auto copy = o; + VERIFY( !copy ); + VERIFY( tracker::count == 0 ); + VERIFY( !o ); + } + + { + std::optional<tracker> o { std::in_place, 333 }; + auto copy = o; + VERIFY( copy ); + VERIFY( copy->value == 333 ); + VERIFY( tracker::count == 2 ); + VERIFY( o && o->value == 333 ); + } + + enum outcome { nothrow, caught, bad_catch }; + + { + outcome result = nothrow; + std::optional<throwing_copy> o; + + try + { + auto copy = o; + } + catch(exception const&) + { result = caught; } + catch(...) + { result = bad_catch; } + + VERIFY( result == nothrow ); + } + + { + outcome result = nothrow; + std::optional<throwing_copy> o { std::in_place }; + + try + { + auto copy = o; + } + catch(exception const&) + { result = caught; } + catch(...) + { result = bad_catch; } + + VERIFY( result == caught ); + } + + VERIFY( tracker::count == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/cons/default.cc b/libstdc++-v3/testsuite/20_util/optional/cons/default.cc new file mode 100644 index 00000000000..ebde86b807d --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/cons/default.cc @@ -0,0 +1,60 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct tracker +{ + tracker() { ++count; } + ~tracker() { --count; } + + tracker(tracker const&) { ++count; } + tracker(tracker&&) { ++count; } + + tracker& operator=(tracker const&) = default; + tracker& operator=(tracker&&) = default; + + static int count; +}; + +int tracker::count = 0; + +int main() +{ + // [20.5.4.1] Constructors + + { + std::optional<tracker> o; + VERIFY( !o ); + } + + { + std::optional<tracker> o {}; + VERIFY( !o ); + } + + { + std::optional<tracker> o = {}; + VERIFY( !o ); + } + + VERIFY( tracker::count == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/cons/move.cc b/libstdc++-v3/testsuite/20_util/optional/cons/move.cc new file mode 100644 index 00000000000..54fd8c958e5 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/cons/move.cc @@ -0,0 +1,126 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct tracker +{ + tracker(int value) : value(value) { ++count; } + ~tracker() { --count; } + + tracker(tracker const& other) : value(other.value) { ++count; } + tracker(tracker&& other) : value(other.value) + { + other.value = -1; + ++count; + } + + tracker& operator=(tracker const&) = default; + tracker& operator=(tracker&&) = default; + + int value; + + static int count; +}; + +int tracker::count = 0; + +struct exception { }; + +struct throwing_move +{ + throwing_move() = default; + throwing_move(throwing_move const&) { throw exception {}; } +}; + +int main() +{ + // [20.5.4.1] Constructors + + { + std::optional<long> o; + auto moved_to = std::move(o); + VERIFY( !moved_to ); + VERIFY( !o ); + } + + { + const long val = 0x1234ABCD; + std::optional<long> o { std::in_place, val}; + auto moved_to = std::move(o); + VERIFY( moved_to ); + VERIFY( *moved_to == val ); + VERIFY( o && *o == val ); + } + + { + std::optional<tracker> o; + auto moved_to = std::move(o); + VERIFY( !moved_to ); + VERIFY( tracker::count == 0 ); + VERIFY( !o ); + } + + { + std::optional<tracker> o { std::in_place, 333 }; + auto moved_to = std::move(o); + VERIFY( moved_to ); + VERIFY( moved_to->value == 333 ); + VERIFY( tracker::count == 2 ); + VERIFY( o && o->value == -1 ); + } + + enum outcome { nothrow, caught, bad_catch }; + + { + outcome result = nothrow; + std::optional<throwing_move> o; + + try + { + auto moved_to = std::move(o); + } + catch(exception const&) + { result = caught; } + catch(...) + { result = bad_catch; } + + VERIFY( result == nothrow ); + } + + { + outcome result = nothrow; + std::optional<throwing_move> o { std::in_place }; + + try + { + auto moved_to = std::move(o); + } + catch(exception const&) + { result = caught; } + catch(...) + { result = bad_catch; } + + VERIFY( result == caught ); + } + + VERIFY( tracker::count == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/cons/value.cc b/libstdc++-v3/testsuite/20_util/optional/cons/value.cc new file mode 100644 index 00000000000..13a6e9ca4dd --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/cons/value.cc @@ -0,0 +1,258 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <vector> +#include <string> + +struct tracker +{ + tracker(int value) : value(value) { ++count; } + ~tracker() { --count; } + + tracker(tracker const& other) : value(other.value) { ++count; } + tracker(tracker&& other) : value(other.value) + { + other.value = -1; + ++count; + } + + tracker& operator=(tracker const&) = default; + tracker& operator=(tracker&&) = default; + + int value; + + static int count; +}; + +int tracker::count = 0; + +struct exception { }; + +struct throwing_construction +{ + explicit throwing_construction(bool propagate) : propagate(propagate) { } + + throwing_construction(throwing_construction const& other) + : propagate(other.propagate) + { + if(propagate) + throw exception {}; + } + + bool propagate; +}; + +int main() +{ + // [20.5.4.1] Constructors + + { + auto i = 0x1234ABCD; + std::optional<long> o { i }; + VERIFY( o ); + VERIFY( *o == 0x1234ABCD ); + VERIFY( i == 0x1234ABCD ); + } + + { + auto i = 0x1234ABCD; + std::optional<long> o = i; + VERIFY( o ); + VERIFY( *o == 0x1234ABCD ); + VERIFY( i == 0x1234ABCD ); + } + + { + auto i = 0x1234ABCD; + std::optional<long> o = { i }; + VERIFY( o ); + VERIFY( *o == 0x1234ABCD ); + VERIFY( i == 0x1234ABCD ); + } + + { + auto i = 0x1234ABCD; + std::optional<long> o { std::move(i) }; + VERIFY( o ); + VERIFY( *o == 0x1234ABCD ); + VERIFY( i == 0x1234ABCD ); + } + + { + auto i = 0x1234ABCD; + std::optional<long> o = std::move(i); + VERIFY( o ); + VERIFY( *o == 0x1234ABCD ); + VERIFY( i == 0x1234ABCD ); + } + + { + auto i = 0x1234ABCD; + std::optional<long> o = { std::move(i) }; + VERIFY( o ); + VERIFY( *o == 0x1234ABCD ); + VERIFY( i == 0x1234ABCD ); + } + + { + std::vector<int> v = { 0, 1, 2, 3, 4, 5 }; + std::optional<std::vector<int>> o { v }; + VERIFY( !v.empty() ); + VERIFY( o->size() == 6 ); + } + + { + std::vector<int> v = { 0, 1, 2, 3, 4, 5 }; + std::optional<std::vector<int>> o = v; + VERIFY( !v.empty() ); + VERIFY( o->size() == 6 ); + } + + { + std::vector<int> v = { 0, 1, 2, 3, 4, 5 }; + std::optional<std::vector<int>> o { v }; + VERIFY( !v.empty() ); + VERIFY( o->size() == 6 ); + } + + { + std::vector<int> v = { 0, 1, 2, 3, 4, 5 }; + std::optional<std::vector<int>> o { std::move(v) }; + VERIFY( v.empty() ); + VERIFY( o->size() == 6 ); + } + + { + std::vector<int> v = { 0, 1, 2, 3, 4, 5 }; + std::optional<std::vector<int>> o = std::move(v); + VERIFY( v.empty() ); + VERIFY( o->size() == 6 ); + } + + { + std::vector<int> v = { 0, 1, 2, 3, 4, 5 }; + std::optional<std::vector<int>> o { std::move(v) }; + VERIFY( v.empty() ); + VERIFY( o->size() == 6 ); + } + + { + tracker t { 333 }; + std::optional<tracker> o = t; + VERIFY( o->value == 333 ); + VERIFY( tracker::count == 2 ); + VERIFY( t.value == 333 ); + } + + { + tracker t { 333 }; + std::optional<tracker> o = std::move(t); + VERIFY( o->value == 333 ); + VERIFY( tracker::count == 2 ); + VERIFY( t.value == -1 ); + } + + enum outcome { nothrow, caught, bad_catch }; + + { + outcome result = nothrow; + throwing_construction t { false }; + + try + { + std::optional<throwing_construction> o { t }; + } + catch(exception const&) + { result = caught; } + catch(...) + { result = bad_catch; } + + VERIFY( result == nothrow ); + } + + { + outcome result = nothrow; + throwing_construction t { true }; + + try + { + std::optional<throwing_construction> o { t }; + } + catch(exception const&) + { result = caught; } + catch(...) + { result = bad_catch; } + + VERIFY( result == caught ); + } + + { + outcome result = nothrow; + throwing_construction t { false }; + + try + { + std::optional<throwing_construction> o { std::move(t) }; + } + catch(exception const&) + { result = caught; } + catch(...) + { result = bad_catch; } + + VERIFY( result == nothrow ); + } + + { + outcome result = nothrow; + throwing_construction t { true }; + + try + { + std::optional<throwing_construction> o { std::move(t) }; + } + catch(exception const&) + { result = caught; } + catch(...) + { result = bad_catch; } + + VERIFY( result == caught ); + } + + { + std::optional<std::string> os = "foo"; + struct X + { + explicit X(int) {} + X& operator=(int) {return *this;} + }; + std::optional<X> ox{42}; + std::optional<int> oi{42}; + std::optional<X> ox2{oi}; + std::optional<std::string> os2; + os2 = "foo"; + std::optional<X> ox3; + ox3 = 42; + std::optional<X> ox4; + ox4 = oi; + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/cons/value_neg.cc b/libstdc++-v3/testsuite/20_util/optional/cons/value_neg.cc new file mode 100644 index 00000000000..4bf5dcc4ec2 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/cons/value_neg.cc @@ -0,0 +1,39 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <string> +#include <memory> + +int main() +{ + { + struct X + { + explicit X(int) {} + }; + std::optional<X> ox{42}; + std::optional<X> ox2 = 42; // { dg-error "conversion" } + std::optional<std::unique_ptr<int>> oup{new int}; + std::optional<std::unique_ptr<int>> oup2 = new int; // { dg-error "conversion" } + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/cons/default.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/cons/default.cc new file mode 100644 index 00000000000..ae523dfbc16 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/cons/default.cc @@ -0,0 +1,42 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int main() +{ + // [20.5.4.1] Constructors + + { + constexpr std::optional<int> o; + static_assert( !o, "" ); + } + + { + constexpr std::optional<int> o {}; + static_assert( !o, "" ); + } + + { + constexpr std::optional<int> o = {}; + static_assert( !o, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/cons/value.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/cons/value.cc new file mode 100644 index 00000000000..de7744d3dc8 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/cons/value.cc @@ -0,0 +1,69 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int main() +{ + // [20.5.4.1] Constructors + + { + constexpr long i = 0x1234ABCD; + constexpr std::optional<long> o { i }; + static_assert( o, "" ); + static_assert( *o == 0x1234ABCD, "" ); + } + + { + constexpr long i = 0x1234ABCD; + constexpr std::optional<long> o = i; + static_assert( o, "" ); + static_assert( *o == 0x1234ABCD, "" ); + } + + { + constexpr long i = 0x1234ABCD; + constexpr std::optional<long> o = { i }; + static_assert( o, "" ); + static_assert( *o == 0x1234ABCD, "" ); + } + + { + constexpr long i = 0x1234ABCD; + constexpr std::optional<long> o { std::move(i) }; + static_assert( o, "" ); + static_assert( *o == 0x1234ABCD, "" ); + } + + { + constexpr long i = 0x1234ABCD; + constexpr std::optional<long> o = std::move(i); + static_assert( o, "" ); + static_assert( *o == 0x1234ABCD, "" ); + } + + { + constexpr long i = 0x1234ABCD; + constexpr std::optional<long> o = { std::move(i) }; + static_assert( o, "" ); + static_assert( *o == 0x1234ABCD, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/in_place.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/in_place.cc new file mode 100644 index 00000000000..cd75275aaff --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/in_place.cc @@ -0,0 +1,43 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int main() +{ + // [20.5.5] In-place construction + static_assert( std::is_same<decltype(std::in_place), const std::in_place_t>(), "" ); + static_assert( std::is_empty<std::in_place_t>(), "" ); + + { + constexpr std::optional<int> o { std::in_place }; + static_assert( o, "" ); + static_assert( *o == int {}, "" ); + + static_assert( !std::is_convertible<std::in_place_t, std::optional<int>>(), "" ); + } + + { + constexpr std::optional<int> o { std::in_place, 42 }; + static_assert( o, "" ); + static_assert( *o == 42, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/make_optional.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/make_optional.cc new file mode 100644 index 00000000000..44ee654a07b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/make_optional.cc @@ -0,0 +1,30 @@ +// { dg-options "-std=gnu++17" } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int main() +{ + constexpr int i = 42; + constexpr auto o = std::make_optional(i); + static_assert( std::is_same<decltype(o), const std::optional<int>>(), "" ); + static_assert( o && *o == 42, "" ); + static_assert( &*o != &i, "" ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/nullopt.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/nullopt.cc new file mode 100644 index 00000000000..61fddefa5a4 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/nullopt.cc @@ -0,0 +1,46 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int main() +{ + // [20.5.6] Disengaged state indicator + static_assert( std::is_same<decltype(std::nullopt), const std::nullopt_t>(), "" ); + static_assert( std::is_empty<std::nullopt_t>(), "" ); + static_assert( std::is_literal_type<std::nullopt_t>(), "" ); + static_assert( !std::is_default_constructible<std::nullopt_t>(), "" ); + + { + constexpr std::optional<int> o = std::nullopt; + static_assert( !o, "" ); + } + + { + constexpr std::optional<int> o = { std::nullopt }; + static_assert( !o, "" ); + } + + { + constexpr std::optional<int> o { std::nullopt }; + static_assert( !o, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/1.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/1.cc new file mode 100644 index 00000000000..6c06f089ec2 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/1.cc @@ -0,0 +1,32 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> + +struct value_type +{ + int i; +}; + +int main() +{ + constexpr std::optional<value_type> o { value_type { 51 } }; + static_assert( (*o).i == 51, "" ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/2.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/2.cc new file mode 100644 index 00000000000..11363af32cb --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/2.cc @@ -0,0 +1,35 @@ +// { dg-options "-std=gnu++17" } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> + +struct value_type +{ + int i; + + void* operator&() { return nullptr; } // N.B. non-const +}; + +int main() +{ + constexpr std::optional<value_type> o { value_type { 51 } }; + static_assert( o->i == 51, "" ); + static_assert( o->i == (*o).i, "" ); + static_assert( &o->i == &(*o).i, "" ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/3.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/3.cc new file mode 100644 index 00000000000..118fea92087 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/3.cc @@ -0,0 +1,33 @@ +// { dg-options "-std=gnu++17" } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> + +struct value_type +{ + int i; +}; + +int main() +{ + constexpr std::optional<value_type> o { value_type { 51 } }; + static_assert( o.value().i == 51, "" ); + static_assert( o.value().i == (*o).i, "" ); + static_assert( &o.value().i == &(*o).i, "" ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/4.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/4.cc new file mode 100644 index 00000000000..3e6436c6db1 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/4.cc @@ -0,0 +1,34 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> + +struct value_type +{ + int i; +}; + +int main() +{ + constexpr std::optional<value_type> o { value_type { 51 } }; + constexpr value_type fallback { 3 }; + static_assert( o.value_or(fallback).i == 51, "" ); + static_assert( o.value_or(fallback).i == (*o).i, "" ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/5.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/5.cc new file mode 100644 index 00000000000..15ac84b25ee --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/observers/5.cc @@ -0,0 +1,39 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> + +struct value_type +{ + int i; +}; + +int main() +{ + { + constexpr std::optional<value_type> o = std::nullopt; + static_assert( !o, "" ); + } + + { + constexpr std::optional<value_type> o { value_type { 51 } }; + static_assert( o, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/1.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/1.cc new file mode 100644 index 00000000000..c948e170125 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/1.cc @@ -0,0 +1,99 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +namespace ns +{ + struct value_type + { + int i; + const char* s; + }; + + constexpr bool + strcmp(const char* lhs, const char* rhs) + { + return *lhs == *rhs && (!*lhs || strcmp(lhs + 1, rhs + 1)); + } + + constexpr bool + strrel(const char* lhs, const char* rhs) + { + return (*rhs && (!*lhs || (*lhs < *rhs))) + || ((*lhs && *rhs && !(*rhs < *lhs)) && strrel(lhs + 1, rhs + 1)); + } + + constexpr bool + operator==(value_type const& lhs, value_type const& rhs) + { return (lhs.i == rhs.i) && strcmp(lhs.s, rhs.s); } + + constexpr bool + operator!=(value_type const& lhs, value_type const& rhs) + { return !(lhs == rhs); } + + constexpr bool + operator<(value_type const& lhs, value_type const& rhs) + { return (lhs.i < rhs.i) || (!(rhs.i < lhs.i) && strrel(lhs.s, rhs.s)); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + + { + constexpr O o, p; + static_assert( o == p, "" ); + static_assert( !(o != p), "" ); + } + + { + constexpr O o { value_type { 42, "forty-two" } }, p; + static_assert( !(o == p), "" ); + static_assert( o != p, "" ); + } + + { + constexpr O o, p { value_type { 42, "forty-two" } }; + static_assert( !(o == p), "" ); + static_assert( o != p, "" ); + } + + { + constexpr O o { value_type { 11, "eleventy" } }, p { value_type { 42, "forty-two" } }; + static_assert( !(o == p), "" ); + static_assert( o != p, "" ); + } + + { + constexpr O o { value_type { 42, "forty-two" } }, p { value_type { 11, "eleventy" } }; + static_assert( !(o == p), "" ); + static_assert( o != p, "" ); + } + + { + constexpr O o { value_type { 42, "forty-two" } }, p { value_type { 42, "forty-two" } }; + static_assert( o == p, "" ); + static_assert( !(o != p), "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/2.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/2.cc new file mode 100644 index 00000000000..9aa9273255b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/2.cc @@ -0,0 +1,111 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +namespace ns +{ + struct value_type + { + int i; + const char* s; + }; + + constexpr bool + strcmp(const char* lhs, const char* rhs) + { + return *lhs == *rhs && (!*lhs || strcmp(lhs + 1, rhs + 1)); + } + + constexpr bool + strrel(const char* lhs, const char* rhs) + { + return (*rhs && (!*lhs || (*lhs < *rhs))) + || ((*lhs && *rhs && !(*rhs < *lhs)) && strrel(lhs + 1, rhs + 1)); + } + + constexpr bool + operator==(value_type const& lhs, value_type const& rhs) + { return (lhs.i == rhs.i) && strcmp(lhs.s, rhs.s); } + + constexpr bool + operator!=(value_type const& lhs, value_type const& rhs) + { return !(lhs == rhs); } + + constexpr bool + operator<(value_type const& lhs, value_type const& rhs) + { return (lhs.i < rhs.i) || (!(rhs.i < lhs.i) && strrel(lhs.s, rhs.s)); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + + { + constexpr O o, p; + static_assert( !(o < p), "" ); + static_assert( !(o > p), "" ); + static_assert( o <= p, "" ); + static_assert( o >= p, "" ); + } + + { + constexpr O o { value_type { 42, "forty-two" } }, p; + static_assert( !(o < p), "" ); + static_assert( o > p, "" ); + static_assert( !(o <= p), "" ); + static_assert( o >= p, "" ); + } + + { + constexpr O o, p { value_type { 42, "forty-two" } }; + static_assert( o < p, "" ); + static_assert( !(o > p), "" ); + static_assert( o <= p, "" ); + static_assert( !(o >= p), "" ); + } + + { + constexpr O o { value_type { 11, "eleventy" } }, p { value_type { 42, "forty-two" } }; + static_assert( o < p, "" ); + static_assert( !(o > p), "" ); + static_assert( o <= p, "" ); + static_assert( !(o >= p), "" ); + } + + { + constexpr O o { value_type { 42, "forty-two" } }, p { value_type { 11, "eleventy" } }; + static_assert( !(o < p), "" ); + static_assert( o > p, "" ); + static_assert( !(o <= p), "" ); + static_assert( o >= p, "" ); + } + + { + constexpr O o { value_type { 42, "forty-two" } }, p { value_type { 42, "forty-two" } }; + static_assert( !(o < p), "" ); + static_assert( !(o > p), "" ); + static_assert( o <= p, "" ); + static_assert( o >= p, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/3.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/3.cc new file mode 100644 index 00000000000..eb34f796399 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/3.cc @@ -0,0 +1,89 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +namespace ns +{ + struct value_type + { + int i; + const char* s; + }; + + constexpr bool + strcmp(const char* lhs, const char* rhs) + { + return *lhs == *rhs && (!*lhs || strcmp(lhs + 1, rhs + 1)); + } + + constexpr bool + strrel(const char* lhs, const char* rhs) + { + return (*rhs && (!*lhs || (*lhs < *rhs))) + || ((*lhs && *rhs && !(*rhs < *lhs)) && strrel(lhs + 1, rhs + 1)); + } + + constexpr bool + operator==(value_type const& lhs, value_type const& rhs) + { return (lhs.i == rhs.i) && strcmp(lhs.s, rhs.s); } + + constexpr bool + operator!=(value_type const& lhs, value_type const& rhs) + { return !(lhs == rhs); } + + constexpr bool + operator<(value_type const& lhs, value_type const& rhs) + { return (lhs.i < rhs.i) || (!(rhs.i < lhs.i) && strrel(lhs.s, rhs.s)); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + + constexpr value_type reference { 42, "forty-two" }; + + { + constexpr O o; + static_assert( !(o == reference), "" ); + static_assert( !(reference == o), "" ); + static_assert( o != reference, "" ); + static_assert( reference != o, "" ); + } + + { + constexpr O o { value_type { 11, "eleventy" } }; + static_assert( !(o == reference), "" ); + static_assert( !(reference == o), "" ); + static_assert( o != reference, "" ); + static_assert( reference != o, "" ); + } + + { + constexpr O o { value_type { 42, "forty-two" } }; + static_assert( o == reference, "" ); + static_assert( reference == o, "" ); + static_assert( !(o != reference), "" ); + static_assert( !(reference != o), "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/4.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/4.cc new file mode 100644 index 00000000000..15130d4efbd --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/4.cc @@ -0,0 +1,101 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +namespace ns +{ + struct value_type + { + int i; + const char* s; + }; + + constexpr bool + strcmp(const char* lhs, const char* rhs) + { + return *lhs == *rhs && (!*lhs || strcmp(lhs + 1, rhs + 1)); + } + + constexpr bool + strrel(const char* lhs, const char* rhs) + { + return (*rhs && (!*lhs || (*lhs < *rhs))) + || ((*lhs && *rhs && !(*rhs < *lhs)) && strrel(lhs + 1, rhs + 1)); + } + + constexpr bool + operator==(value_type const& lhs, value_type const& rhs) + { return (lhs.i == rhs.i) && strcmp(lhs.s, rhs.s); } + + constexpr bool + operator!=(value_type const& lhs, value_type const& rhs) + { return !(lhs == rhs); } + + constexpr bool + operator<(value_type const& lhs, value_type const& rhs) + { return (lhs.i < rhs.i) || (!(rhs.i < lhs.i) && strrel(lhs.s, rhs.s)); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + + constexpr value_type reference { 42, "forty-two" }; + + { + constexpr O o; + static_assert( o < reference, "" ); + static_assert( !(reference < o), "" ); + static_assert( !(o > reference), "" ); + static_assert( reference > o, "" ); + static_assert( o <= reference, "" ); + static_assert( !(reference <= o), "" ); + static_assert( !(o >= reference), "" ); + static_assert( reference >= o, "" ); + } + + { + constexpr O o { value_type { 11, "eleventy" } }; + static_assert( o < reference, "" ); + static_assert( !(reference < o), "" ); + static_assert( !(o > reference), "" ); + static_assert( reference > o, "" ); + static_assert( o <= reference, "" ); + static_assert( !(reference <= o), "" ); + static_assert( !(o >= reference), "" ); + static_assert( reference >= o, "" ); + } + + { + constexpr O o { value_type { 42, "forty-two" } }; + static_assert( !(o < reference), "" ); + static_assert( !(reference < o), "" ); + static_assert( !(o > reference), "" ); + static_assert( !(reference > o), "" ); + static_assert( o <= reference, "" ); + static_assert( reference <= o, "" ); + static_assert( o >= reference, "" ); + static_assert( reference >= o, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/5.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/5.cc new file mode 100644 index 00000000000..99a208208bc --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/5.cc @@ -0,0 +1,80 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +namespace ns +{ + struct value_type + { + int i; + const char* s; + }; + + constexpr bool + strcmp(const char* lhs, const char* rhs) + { + return *lhs == *rhs && (!*lhs || strcmp(lhs + 1, rhs + 1)); + } + + constexpr bool + strrel(const char* lhs, const char* rhs) + { + return (*rhs && (!*lhs || (*lhs < *rhs))) + || ((*lhs && *rhs && !(*rhs < *lhs)) && strrel(lhs + 1, rhs + 1)); + } + + constexpr bool + operator==(value_type const& lhs, value_type const& rhs) + { return (lhs.i == rhs.i) && strcmp(lhs.s, rhs.s); } + + constexpr bool + operator!=(value_type const& lhs, value_type const& rhs) + { return !(lhs == rhs); } + + constexpr bool + operator<(value_type const& lhs, value_type const& rhs) + { return (lhs.i < rhs.i) || (!(rhs.i < lhs.i) && strrel(lhs.s, rhs.s)); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + using std::nullopt; + + { + constexpr O o; + static_assert( o == nullopt, "" ); + static_assert( nullopt == o, "" ); + static_assert( !(o != nullopt), "" ); + static_assert( !(nullopt != o), "" ); + } + + { + constexpr O o { std::in_place }; + static_assert( !(o == nullopt), "" ); + static_assert( !(nullopt == o), "" ); + static_assert( o != nullopt, "" ); + static_assert( nullopt != o, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/6.cc b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/6.cc new file mode 100644 index 00000000000..7337287852b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/constexpr/relops/6.cc @@ -0,0 +1,88 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +namespace ns +{ + struct value_type + { + int i; + const char* s; + }; + + constexpr bool + strcmp(const char* lhs, const char* rhs) + { + return *lhs == *rhs && (!*lhs || strcmp(lhs + 1, rhs + 1)); + } + + constexpr bool + strrel(const char* lhs, const char* rhs) + { + return (*rhs && (!*lhs || (*lhs < *rhs))) + || ((*lhs && *rhs && !(*rhs < *lhs)) && strrel(lhs + 1, rhs + 1)); + } + + constexpr bool + operator==(value_type const& lhs, value_type const& rhs) + { return (lhs.i == rhs.i) && strcmp(lhs.s, rhs.s); } + + constexpr bool + operator!=(value_type const& lhs, value_type const& rhs) + { return !(lhs == rhs); } + + constexpr bool + operator<(value_type const& lhs, value_type const& rhs) + { return (lhs.i < rhs.i) || (!(rhs.i < lhs.i) && strrel(lhs.s, rhs.s)); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + using std::nullopt; + + { + constexpr O o; + static_assert( !(o < nullopt), "" ); + static_assert( !(nullopt < o), "" ); + static_assert( !(o > nullopt), "" ); + static_assert( !(nullopt > o), "" ); + static_assert( o <= nullopt, "" ); + static_assert( nullopt <= o, "" ); + static_assert( o >= nullopt, "" ); + static_assert( nullopt >= o, "" ); + } + + { + constexpr O o { std::in_place }; + static_assert( !(o < nullopt), "" ); + static_assert( nullopt < o, "" ); + static_assert( o > nullopt, "" ); + static_assert( !(nullopt > o), "" ); + static_assert( !(o <= nullopt), "" ); + static_assert( nullopt <= o, "" ); + static_assert( o >= nullopt, "" ); + static_assert( !(nullopt >= o), "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/in_place.cc b/libstdc++-v3/testsuite/20_util/optional/in_place.cc new file mode 100644 index 00000000000..ef8c744f1dc --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/in_place.cc @@ -0,0 +1,66 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <vector> + +int main() +{ + // [20.5.5] In-place construction + static_assert( std::is_same<decltype(std::in_place), const std::in_place_t>(), "" ); + static_assert( std::is_empty<std::in_place_t>(), "" ); + + { + std::optional<int> o { std::in_place }; + VERIFY( o ); + VERIFY( *o == int() ); + + static_assert( !std::is_convertible<std::in_place_t, std::optional<int>>(), "" ); + } + + { + std::optional<int> o { std::in_place, 42 }; + VERIFY( o ); + VERIFY( *o == 42 ); + } + + { + std::optional<std::vector<int>> o { std::in_place, 18, 4 }; + VERIFY( o ); + VERIFY( o->size() == 18 ); + VERIFY( (*o)[17] == 4 ); + } + + { + std::optional<std::vector<int>> o { std::in_place, { 18, 4 } }; + VERIFY( o ); + VERIFY( o->size() == 2 ); + VERIFY( (*o)[0] == 18 ); + } + + { + std::optional<std::vector<int>> o { std::in_place, { 18, 4 }, std::allocator<int> {} }; + VERIFY( o ); + VERIFY( o->size() == 2 ); + VERIFY( (*o)[0] == 18 ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/make_optional.cc b/libstdc++-v3/testsuite/20_util/optional/make_optional.cc new file mode 100644 index 00000000000..6f245c716ad --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/make_optional.cc @@ -0,0 +1,31 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int main() +{ + const int i = 42; + auto o = std::make_optional(i); + static_assert( std::is_same<decltype(o), std::optional<int>>(), "" ); + VERIFY( o && *o == 42 ); + VERIFY( &*o != &i ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/nullopt.cc b/libstdc++-v3/testsuite/20_util/optional/nullopt.cc new file mode 100644 index 00000000000..f9ee748b9c2 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/nullopt.cc @@ -0,0 +1,46 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +int main() +{ + // [20.5.6] Disengaged state indicator + static_assert( std::is_same<decltype(std::nullopt), const std::nullopt_t>(), "" ); + static_assert( std::is_empty<std::nullopt_t>(), "" ); + static_assert( std::is_literal_type<std::nullopt_t>(), "" ); + static_assert( !std::is_default_constructible<std::nullopt_t>(), "" ); + + { + std::optional<int> o = std::nullopt; + VERIFY( !o ); + } + + { + std::optional<int> o = { std::nullopt }; + VERIFY( !o ); + } + + { + std::optional<int> o { std::nullopt }; + VERIFY( !o ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/observers/1.cc b/libstdc++-v3/testsuite/20_util/optional/observers/1.cc new file mode 100644 index 00000000000..ba10bce7119 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/observers/1.cc @@ -0,0 +1,33 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct value_type +{ + int i; +}; + +int main() +{ + std::optional<value_type> o { value_type { 51 } }; + VERIFY( (*o).i == 51 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/observers/2.cc b/libstdc++-v3/testsuite/20_util/optional/observers/2.cc new file mode 100644 index 00000000000..d3799b9214a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/observers/2.cc @@ -0,0 +1,37 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct value_type +{ + int i; +}; + +void* operator&(const value_type&) = delete; + +int main() +{ + std::optional<value_type> o { value_type { 51 } }; + VERIFY( o->i == 51 ); + VERIFY( o->i == (*o).i ); + VERIFY( &o->i == &(*o).i ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/observers/3.cc b/libstdc++-v3/testsuite/20_util/optional/observers/3.cc new file mode 100644 index 00000000000..9ad5d01ae42 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/observers/3.cc @@ -0,0 +1,58 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct value_type +{ + int i; +}; + +int main() +{ + { + std::optional<value_type> o { value_type { 51 } }; + VERIFY( o.value().i == 51 ); + VERIFY( o.value().i == (*o).i ); + VERIFY( &o.value().i == &(*o).i ); + } + + { + enum outcome_type { nothrow, caught, bad_catch }; + + outcome_type outcome {}; + std::optional<value_type> o = std::nullopt; + bool called = false; + auto const eat = [&called](int) { called = true; }; + + try + { + eat(o.value().i); + } + catch(std::bad_optional_access const&) + { outcome = caught; } + catch(...) + { outcome = bad_catch; } + + VERIFY( outcome == caught ); + VERIFY( !called ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/observers/4.cc b/libstdc++-v3/testsuite/20_util/optional/observers/4.cc new file mode 100644 index 00000000000..9a305674fb0 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/observers/4.cc @@ -0,0 +1,35 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct value_type +{ + int i; +}; + +int main() +{ + std::optional<value_type> o { value_type { 51 } }; + value_type fallback { 3 }; + VERIFY( o.value_or(fallback).i == 51 ); + VERIFY( o.value_or(fallback).i == (*o).i ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/observers/5.cc b/libstdc++-v3/testsuite/20_util/optional/observers/5.cc new file mode 100644 index 00000000000..386d81de2e0 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/observers/5.cc @@ -0,0 +1,40 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct value_type +{ + int i; +}; + +int main() +{ + { + std::optional<value_type> o = std::nullopt; + VERIFY( !o ); + } + + { + std::optional<value_type> o { value_type { 51 } }; + VERIFY( o ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/1.cc b/libstdc++-v3/testsuite/20_util/optional/relops/1.cc new file mode 100644 index 00000000000..62770329eff --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/relops/1.cc @@ -0,0 +1,85 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <tuple> +#include <string> + +namespace ns +{ + struct value_type + { + int i; + std::string s; + }; + + bool + operator==(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); } + + bool + operator<(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + + { + O o, p; + VERIFY( o == p ); + VERIFY( !(o != p) ); + } + + { + O o { value_type { 42, "forty-two" } }, p; + VERIFY( !(o == p) ); + VERIFY( o != p ); + } + + { + O o, p { value_type { 42, "forty-two" } }; + VERIFY( !(o == p) ); + VERIFY( o != p ); + } + + { + O o { value_type { 11, "eleventy" } }, p { value_type { 42, "forty-two" } }; + VERIFY( !(o == p) ); + VERIFY( o != p ); + } + + { + O o { value_type { 42, "forty-two" } }, p { value_type { 11, "eleventy" } }; + VERIFY( !(o == p) ); + VERIFY( o != p ); + } + + { + O o { value_type { 42, "forty-two" } }, p { value_type { 42, "forty-two" } }; + VERIFY( o == p ); + VERIFY( !(o != p) ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/2.cc b/libstdc++-v3/testsuite/20_util/optional/relops/2.cc new file mode 100644 index 00000000000..65071c04bce --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/relops/2.cc @@ -0,0 +1,97 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <tuple> +#include <string> + +namespace ns +{ + struct value_type + { + int i; + std::string s; + }; + + bool + operator==(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); } + + bool + operator<(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + + { + O o, p; + VERIFY( !(o < p) ); + VERIFY( !(o > p) ); + VERIFY( o <= p ); + VERIFY( o >= p ); + } + + { + O o { value_type { 42, "forty-two" } }, p; + VERIFY( !(o < p) ); + VERIFY( o > p ); + VERIFY( !(o <= p) ); + VERIFY( o >= p ); + } + + { + O o, p { value_type { 42, "forty-two" } }; + VERIFY( o < p ); + VERIFY( !(o > p) ); + VERIFY( o <= p ); + VERIFY( !(o >= p) ); + } + + { + O o { value_type { 11, "eleventy" } }, p { value_type { 42, "forty-two" } }; + VERIFY( o < p ); + VERIFY( !(o > p) ); + VERIFY( o <= p ); + VERIFY( !(o >= p) ); + } + + { + O o { value_type { 42, "forty-two" } }, p { value_type { 11, "eleventy" } }; + VERIFY( !(o < p) ); + VERIFY( o > p ); + VERIFY( !(o <= p) ); + VERIFY( o >= p ); + } + + { + O o { value_type { 42, "forty-two" } }, p { value_type { 42, "forty-two" } }; + VERIFY( !(o < p) ); + VERIFY( !(o > p) ); + VERIFY( o <= p ); + VERIFY( o >= p ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/3.cc b/libstdc++-v3/testsuite/20_util/optional/relops/3.cc new file mode 100644 index 00000000000..2fd9e8bc1e7 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/relops/3.cc @@ -0,0 +1,75 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <tuple> +#include <string> + +namespace ns +{ + struct value_type + { + int i; + std::string s; + }; + + bool + operator==(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); } + + bool + operator<(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + + value_type const reference { 42, "forty-two" }; + + { + O o; + VERIFY( !(o == reference) ); + VERIFY( !(reference == o) ); + VERIFY( o != reference ); + VERIFY( reference != o ); + } + + { + O o { value_type { 11, "eleventy" } }; + VERIFY( !(o == reference) ); + VERIFY( !(reference == o) ); + VERIFY( o != reference ); + VERIFY( reference != o ); + } + + { + O o { value_type { 42, "forty-two" } }; + VERIFY( o == reference ); + VERIFY( reference == o ); + VERIFY( !(o != reference) ); + VERIFY( !(reference != o) ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/4.cc b/libstdc++-v3/testsuite/20_util/optional/relops/4.cc new file mode 100644 index 00000000000..363e633a40c --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/relops/4.cc @@ -0,0 +1,87 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <tuple> +#include <string> + +namespace ns +{ + struct value_type + { + int i; + std::string s; + }; + + bool + operator==(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); } + + bool + operator<(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + + value_type const reference { 42, "forty-two" }; + + { + O o; + VERIFY( o < reference ); + VERIFY( !(reference < o) ); + VERIFY( !(o > reference) ); + VERIFY( reference > o ); + VERIFY( o <= reference ); + VERIFY( !(reference <= o) ); + VERIFY( !(o >= reference) ); + VERIFY( reference >= o ); + } + + { + O o { value_type { 11, "eleventy" } }; + VERIFY( o < reference ); + VERIFY( !(reference < o) ); + VERIFY( !(o > reference) ); + VERIFY( reference > o ); + VERIFY( o <= reference ); + VERIFY( !(reference <= o) ); + VERIFY( !(o >= reference) ); + VERIFY( reference >= o ); + } + + { + O o { value_type { 42, "forty-two" } }; + VERIFY( !(o < reference) ); + VERIFY( !(reference < o) ); + VERIFY( !(o > reference) ); + VERIFY( !(reference > o) ); + VERIFY( o <= reference ); + VERIFY( reference <= o ); + VERIFY( o >= reference ); + VERIFY( reference >= o ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/5.cc b/libstdc++-v3/testsuite/20_util/optional/relops/5.cc new file mode 100644 index 00000000000..e79e0dbc278 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/relops/5.cc @@ -0,0 +1,66 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <tuple> +#include <string> + +namespace ns +{ + struct value_type + { + int i; + std::string s; + }; + + bool + operator==(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); } + + bool + operator<(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + using std::nullopt; + + { + O o; + VERIFY( o == nullopt ); + VERIFY( nullopt == o ); + VERIFY( !(o != nullopt) ); + VERIFY( !(nullopt != o) ); + } + + { + O o { std::in_place }; + VERIFY( !(o == nullopt) ); + VERIFY( !(nullopt == o) ); + VERIFY( o != nullopt ); + VERIFY( nullopt != o ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/6.cc b/libstdc++-v3/testsuite/20_util/optional/relops/6.cc new file mode 100644 index 00000000000..4ead5d1acda --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/relops/6.cc @@ -0,0 +1,74 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <tuple> +#include <string> + +namespace ns +{ + struct value_type + { + int i; + std::string s; + }; + + bool + operator==(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); } + + bool + operator<(value_type const& lhs, value_type const& rhs) + { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); } + +} // namespace ns + +int main() +{ + using ns::value_type; + using O = std::optional<value_type>; + using std::nullopt; + + { + O o; + VERIFY( !(o < nullopt) ); + VERIFY( !(nullopt < o) ); + VERIFY( !(o > nullopt) ); + VERIFY( !(nullopt > o) ); + VERIFY( o <= nullopt ); + VERIFY( nullopt <= o ); + VERIFY( o >= nullopt ); + VERIFY( nullopt >= o ); + } + + { + O o { std::in_place }; + VERIFY( !(o < nullopt) ); + VERIFY( nullopt < o ); + VERIFY( o > nullopt ); + VERIFY( !(nullopt > o) ); + VERIFY( !(o <= nullopt) ); + VERIFY( nullopt <= o ); + VERIFY( o >= nullopt ); + VERIFY( !(nullopt >= o) ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/requirements.cc b/libstdc++-v3/testsuite/20_util/optional/requirements.cc new file mode 100644 index 00000000000..aab572f5795 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/requirements.cc @@ -0,0 +1,259 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +#include <tuple> + +using std::bad_optional_access; +static_assert( std::is_default_constructible<bad_optional_access>::value, "" ); + +struct trivially_destructible +{ + trivially_destructible() = delete; + trivially_destructible(trivially_destructible const&) = delete; + trivially_destructible& operator=(trivially_destructible const&) = delete; + trivially_destructible(trivially_destructible&&) = delete; + trivially_destructible& operator=(trivially_destructible&&) = delete; + ~trivially_destructible() noexcept = default; +}; + +static_assert( std::is_trivially_destructible<trivially_destructible>(), "" ); + +struct no_default_constructor +{ + no_default_constructor() = delete; +}; + +struct no_copy_constructor +{ + no_copy_constructor() = default; + no_copy_constructor(no_copy_constructor const&) = delete; + no_copy_constructor& operator=(no_copy_constructor const&) = default; + no_copy_constructor(no_copy_constructor&&) = default; + no_copy_constructor& operator=(no_copy_constructor&&) = default; +}; + +struct no_copy_assignment +{ + no_copy_assignment() = default; + no_copy_assignment(no_copy_assignment const&) = default; + no_copy_assignment(no_copy_assignment&&) = default; + no_copy_assignment& operator=(no_copy_assignment&&) = default; +}; + +struct no_move_constructor +{ + no_move_constructor() = default; + no_move_constructor(no_move_constructor const&) = default; + no_move_constructor& operator=(no_move_constructor const&) = default; + no_move_constructor(no_move_constructor&&) = delete; + no_move_constructor& operator=(no_move_constructor&&) = default; +}; + +struct no_move_assignment +{ + no_move_assignment() = default; + no_move_assignment(no_move_assignment const&) = default; + no_move_assignment& operator=(no_move_assignment const&) = default; + no_move_assignment(no_move_assignment&&) = default; + no_move_assignment& operator=(no_move_assignment&&) = delete; +}; + +struct no_copy : no_copy_constructor, no_copy_assignment { }; +struct no_move : no_move_constructor, no_move_assignment { }; + +// Laxest possible model of a value type for optional +struct only_destructible +{ + only_destructible(only_destructible&&) = delete; +}; + +int main() +{ + { + static_assert( std::is_trivially_destructible<std::optional<trivially_destructible>>(), "" ); + } + + { + using T = no_default_constructor; + using O = std::optional<T>; + static_assert( std::is_same<O::value_type, T>(), "" ); + static_assert( std::is_default_constructible<O>(), "" ); + { O o; } + static_assert( std::is_copy_constructible<O>(), "" ); + { O o; auto copy = o; } + static_assert( std::is_copy_assignable<O>(), "" ); + { O o, p; p = o; } + static_assert( std::is_move_constructible<O>(), "" ); + { O o; auto moved_to = std::move(o); } + static_assert( std::is_move_assignable<O>(), "" ); + { O o, p; p = std::move(o); } + } + + { + using T = no_copy_constructor; + using O = std::optional<T>; + static_assert( std::is_same<O::value_type, T>(), "" ); + static_assert( std::is_default_constructible<O>(), "" ); + { O o; } + static_assert( !std::is_copy_constructible<O>(), "" ); + static_assert( !std::is_copy_assignable<O>(), "" ); + static_assert( std::is_move_constructible<O>(), "" ); + { O o; auto moved_to = std::move(o); } + static_assert( std::is_move_assignable<O>(), "" ); + { O o, p; p = std::move(o); } + } + + { + using T = no_copy_assignment; + using O = std::optional<T>; + static_assert( std::is_default_constructible<O>(), "" ); + { O o; } + static_assert( std::is_copy_constructible<O>(), "" ); + { O o; auto copy = o; } + static_assert( !std::is_copy_assignable<O>(), "" ); + static_assert( std::is_move_constructible<O>(), "" ); + { O o; auto moved_to = std::move(o); } + static_assert( std::is_move_assignable<O>(), "" ); + { O o, p; p = std::move(o); } + } + + { + using T = no_copy; + using O = std::optional<T>; + static_assert( std::is_same<O::value_type, T>(), "" ); + static_assert( std::is_default_constructible<O>(), "" ); + { O o; } + static_assert( !std::is_copy_constructible<O>(), "" ); + static_assert( !std::is_copy_assignable<O>(), "" ); + static_assert( std::is_move_constructible<O>(), "" ); + { O o; auto moved_to = std::move(o); } + static_assert( std::is_move_assignable<O>(), "" ); + { O o, p; p = std::move(o); } + } + + { + using T = no_move_constructor; + using O = std::optional<T>; + static_assert( std::is_same<O::value_type, T>(), "" ); + static_assert( std::is_default_constructible<O>(), "" ); + { O o; } + static_assert( std::is_copy_constructible<O>(), "" ); + { O o; auto copy = o; } + static_assert( std::is_copy_assignable<O>(), "" ); + /* + * T should be move constructible due to [12.8/11], which is a new rule in C++1y + * not yet implemented by GCC. Because there is already a special exception in C++11 + * for the generation of the special members that GCC implements (at least some of the + * time), this does not affect the std::optional implementation however. So the assertion + * for T should be changed (or removed altogether) when the time comes, but the rest + * should however remain correct and unchanged. + */ + static_assert( !std::is_move_constructible<T>(), "" ); + static_assert( std::is_move_constructible<O>(), "" ); + { O o; auto moved_to = std::move(o); } + static_assert( std::is_move_assignable<O>(), "" ); + { O o, p; p = std::move(o); } + } + + { + using T = no_move_assignment; + using O = std::optional<T>; + static_assert( std::is_same<O::value_type, T>(), "" ); + static_assert( std::is_default_constructible<O>(), "" ); + { O o; } + static_assert( std::is_copy_constructible<O>(), "" ); + { O o; auto copy = o; } + static_assert( std::is_copy_assignable<O>(), "" ); + { O o, p; p = o; } + static_assert( std::is_move_constructible<O>(), "" ); + { O o; auto moved_to = std::move(o); } + /* + * Paragraph 23 of same leads to a similar situation but with respect to move + * assignment. + */ + static_assert( !std::is_move_assignable<T>(), "" ); + static_assert( std::is_move_assignable<O>(), "" ); + { O o, p; p = std::move(o); } + } + + { + using T = no_move; + using O = std::optional<T>; + static_assert( std::is_same<O::value_type, T>(), "" ); + static_assert( std::is_default_constructible<O>(), "" ); + { O o; } + static_assert( std::is_copy_constructible<O>(), "" ); + { O o; auto copy = o; } + static_assert( std::is_copy_assignable<O>(), "" ); + { O o, p; p = o; } + static_assert( std::is_move_constructible<O>(), "" ); + { O o; auto moved_to = std::move(o); } + static_assert( std::is_move_assignable<O>(), "" ); + { O o, p; p = std::move(o); } + } + + { + using T = only_destructible; + using O = std::optional<T>; + static_assert( std::is_same<O::value_type, T>(), "" ); + static_assert( std::is_default_constructible<O>(), "" ); + { O o; } + static_assert( !std::is_copy_constructible<O>(), "" ); + static_assert( !std::is_copy_assignable<O>(), "" ); + static_assert( !std::is_move_constructible<O>(), "" ); + static_assert( !std::is_move_assignable<O>(), "" ); + } + + { + /* + * Should not complain about 'invalid' specializations as long as + * they're not instantiated. + */ + using A = std::optional<int&>; + using B = std::optional<int&&>; + using C1 = std::optional<std::in_place_t>; + using C2 = std::optional<std::in_place_t const>; + using C3 = std::optional<std::in_place_t volatile>; + using C4 = std::optional<std::in_place_t const volatile>; + using D1 = std::optional<std::nullopt_t>; + using D2 = std::optional<std::nullopt_t const>; + using D3 = std::optional<std::nullopt_t volatile>; + using D4 = std::optional<std::nullopt_t const volatile>; + + using X = std::tuple<A, B, C1, C2, C3, C4, D1, D2, D3, D4>; + } + + { + std::optional<const int> o { 42 }; + static_assert( std::is_same<decltype(o)::value_type, const int>(), "" ); + VERIFY( o ); + VERIFY( *o == 42 ); + } + + { + constexpr std::optional<const int> o { 33 }; + static_assert( std::is_same<decltype(o)::value_type, const int>(), "" ); + static_assert( o, "" ); + static_assert( *o == 33, "" ); + } +} diff --git a/libstdc++-v3/testsuite/20_util/optional/swap/1.cc b/libstdc++-v3/testsuite/20_util/optional/swap/1.cc new file mode 100644 index 00000000000..b077fe4c583 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/swap/1.cc @@ -0,0 +1,95 @@ +// { dg-options "-std=gnu++17" } +// { dg-do run } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <testsuite_hooks.h> + +struct exception {}; + +int counter = 0; + +struct mixin_counter +{ + mixin_counter() { ++counter; } + mixin_counter(mixin_counter const&) { ++counter; } + ~mixin_counter() { --counter; } +}; + +namespace ns +{ + +struct value_type : private mixin_counter +{ + explicit value_type(int state) : state(state) { } + int state; +}; + +int swaps = 0; + +void +swap(value_type& lhs, value_type& rhs) +{ + ++swaps; + using std::swap; + swap(lhs.state, rhs.state); +} + +} // namespace ns + +int main() +{ + using O = std::optional<ns::value_type>; + + VERIFY( ns::swaps == 0 ); + + { + O o, p; + swap(o, p); + VERIFY( !o ); + VERIFY( !p ); + } + + { + O o { std::in_place, 45 }, p; + swap(o, p); + VERIFY( !o ); + VERIFY( p && p->state == 45 ); + } + + { + O o, p { std::in_place, 45 }; + swap(o, p); + VERIFY( o && o->state == 45 ); + VERIFY( !p ); + } + + { + O o { std::in_place, 167 }, p { std::in_place, 999 }; + VERIFY( ns::swaps == 0 ); + + swap(o, p); + + VERIFY( o && o->state == 999 ); + VERIFY( p && p->state == 167 ); + VERIFY( ns::swaps == 1 ); + } + + VERIFY( counter == 0 ); +} diff --git a/libstdc++-v3/testsuite/20_util/optional/typedefs.cc b/libstdc++-v3/testsuite/20_util/optional/typedefs.cc new file mode 100644 index 00000000000..5ec95d850ec --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/typedefs.cc @@ -0,0 +1,33 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <optional> +#include <type_traits> +#include <stdexcept> + +template<typename T> + using check1_t = std::optional<T>; + +using check2_t = std::in_place_t; +using check3_t = std::nullopt_t; +using check4_t = std::bad_optional_access; + +static_assert(std::is_base_of<std::logic_error, check4_t>::value, + "bad_optional_access must derive from logic_error"); diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_debug_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_debug_neg.cc index 36feb3c140c..6d469a54389 100644 --- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_debug_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_debug_neg.cc @@ -28,6 +28,6 @@ int n1 = std::get<1>(a); int n2 = std::get<1>(std::move(a)); int n3 = std::get<1>(ca); -// { dg-error "static assertion failed" "" { target *-*-* } 274 } -// { dg-error "static assertion failed" "" { target *-*-* } 283 } -// { dg-error "static assertion failed" "" { target *-*-* } 291 } +// { dg-error "static assertion failed" "" { target *-*-* } 273 } +// { dg-error "static assertion failed" "" { target *-*-* } 282 } +// { dg-error "static assertion failed" "" { target *-*-* } 290 } diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_debug_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_debug_neg.cc index b761005f8a0..6789f233c75 100644 --- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_debug_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_debug_neg.cc @@ -23,4 +23,4 @@ typedef std::tuple_element<1, std::array<int, 1>>::type type; -// { dg-error "static assertion failed" "" { target *-*-* } 309 } +// { dg-error "static assertion failed" "" { target *-*-* } 308 } diff --git a/libstdc++-v3/testsuite/23_containers/vector/allocator/construction.cc b/libstdc++-v3/testsuite/23_containers/vector/allocator/construction.cc new file mode 100644 index 00000000000..8040949c04c --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/allocator/construction.cc @@ -0,0 +1,105 @@ +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +#include <vector> + +struct Tag { }; + +template<typename T> + struct TaggingAllocator + { + using value_type = T; + + TaggingAllocator() = default; + + template<typename U> + TaggingAllocator(const TaggingAllocator<U>&) { } + + T* + allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); } + + void + deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); } + + template<typename U, typename... Args> + void + construct(U* p, Args&&... args) + { ::new((void*)p) U(Tag{}, std::forward<Args>(args)...); } + + template<typename U, typename... Args> + void + destroy(U* p) + { p->~U(); } + }; + +template<typename T, typename U> + bool + operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&) + { return true; } + +template<typename T, typename U> + bool + operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&) + { return false; } + +struct X +{ + // All constructors must be passed the Tag type. + + // DefaultInsertable into vector<X, TaggingAllocator<X>>, + X(Tag) { } + // CopyInsertable into vector<X, TaggingAllocator<X>>, + X(Tag, const X&) { } + // MoveInsertable into vector<X, TaggingAllocator<X>>, and + X(Tag, X&&) { } + + // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args. + template<typename... Args> + X(Tag, Args&&...) { } + + // not DefaultConstructible, CopyConstructible or MoveConstructible. + X() = delete; + X(const X&) = delete; + X(X&&) = delete; + + // CopyAssignable. + X& operator=(const X&) { return *this; } + + // MoveAssignable. + X& operator=(X&&) { return *this; } + +private: + // Not Destructible. + ~X() { } + + // Erasable from vector<X, TaggingAllocator<X>>. + friend class TaggingAllocator<X>; +}; + +template class std::vector<X, TaggingAllocator<X>>; + +void test01() +{ + std::vector<X, TaggingAllocator<X>> v; + v.reserve(3); + v.emplace_back(); + v.emplace(v.begin()); + v.emplace(v.begin(), 1, 2, 3); +} diff --git a/libstdc++-v3/testsuite/23_containers/vector/check_construct_destroy.cc b/libstdc++-v3/testsuite/23_containers/vector/check_construct_destroy.cc index cf2f7c7a0d7..b92a1521aa2 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/check_construct_destroy.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/check_construct_destroy.cc @@ -49,9 +49,9 @@ int main() c.reserve(100); tracker_allocator_counter::reset(); c.insert(c.begin(), arr10[0]); - ok = check_construct_destroy("Insert element", 1, 0) && ok; + ok = check_construct_destroy("Insert element", 2, 1) && ok; } - ok = check_construct_destroy("Insert element", 1, 11) && ok; + ok = check_construct_destroy("Insert element", 2, 12) && ok; { Container c(arr10, arr10 + 10); diff --git a/libstdc++-v3/testsuite/23_containers/vector/modifiers/emplace/self_emplace.cc b/libstdc++-v3/testsuite/23_containers/vector/modifiers/emplace/self_emplace.cc new file mode 100644 index 00000000000..d452b5b6325 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/modifiers/emplace/self_emplace.cc @@ -0,0 +1,144 @@ +// { dg-options "-std=gnu++11" } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <vector> +#include "testsuite_hooks.h" + +bool test __attribute__((unused)) = true; + +void +test01() +{ + std::vector<std::vector<int>> vv = + { + { 2, 3 }, + { 4, 5 }, + { 0, 1 } + }; + + // Make sure emplace will imply reallocation. + VERIFY( vv.capacity() == 3 ); + + vv.emplace(vv.begin(), vv[0]); + + VERIFY( vv.size() == 4 ); + VERIFY( vv[0].size() == 2 ); + VERIFY( vv[0][0] == 2 ); + VERIFY( vv[0][1] == 3 ); +} + +void +test02() +{ + std::vector<std::vector<int>> vv = + { + { 2, 3 }, + { 4, 5 }, + { 0, 1 } + }; + + // Make sure emplace won't reallocate. + vv.reserve(4); + vv.emplace(vv.begin(), vv[0]); + + VERIFY( vv.size() == 4 ); + VERIFY( vv[0].size() == 2 ); + VERIFY( vv[0][0] == 2 ); + VERIFY( vv[0][1] == 3 ); +} + +struct A +{ + A(int i) : _i(i) + { } + + A(const A& other) : _i(other._i) + { + VERIFY( other._i >= 0 ); + } + + A(A&& other) : _i(other._i) + { + VERIFY( other._i >= 0 ); + + other._i = -1; + } + + A(std::vector<A>::iterator it) : _i(it->_i) + { + VERIFY( it->_i >= 0 ); + } + + A& operator=(const A&) = default; + A& operator=(A&& other) + { + VERIFY(other._i >= 0 ); + + _i = other._i; + other._i = -1; + return *this; + } + + int _i; +}; + +void +test03() +{ + std::vector<A> va = + { + { A(1) }, + { A(2) }, + { A(3) } + }; + + // Make sure emplace will imply reallocation. + VERIFY( va.capacity() == 3 ); + + va.emplace(va.begin(), va.begin()); + + VERIFY( va.size() == 4 ); + VERIFY( va[0]._i == 1 ); +} + +void +test04() +{ + std::vector<A> va = + { + { A(1) }, + { A(2) }, + { A(3) } + }; + + // Make sure emplace won't reallocate. + va.reserve(4); + va.emplace(va.begin(), va.begin()); + + VERIFY( va.size() == 4 ); + VERIFY( va[0]._i == 1 ); +} + +int main() +{ + test01(); + test02(); + test03(); + test04(); +} diff --git a/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/aliasing.cc b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/aliasing.cc new file mode 100644 index 00000000000..2ef13b47ec4 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/aliasing.cc @@ -0,0 +1,79 @@ +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++14" } + +#include <vector> +#include <memory> +#include <testsuite_hooks.h> + +// See https://gcc.gnu.org/ml/libstdc++/2016-07/msg00008.html for background. + +struct T +{ + T(int v = 0) : value(v) { } + T(const T& t); + T& operator=(const T& t); + void make_child() { child = std::make_unique<T>(value + 10); } + std::unique_ptr<T> child; + int value; +}; + +T::T(const T& t) : value(t.value) +{ + if (t.child) + child.reset(new T(*t.child)); +} + +T& T::operator=(const T& t) +{ + value = t.value; + if (t.child) + { + if (child) + *child = *t.child; + else + child.reset(new T(*t.child)); + } + else + child.reset(); + return *this; +} + +void +test01() +{ + std::vector<T> v; + v.reserve(3); + v.push_back(T(1)); + v.back().make_child(); + v.push_back(T(2)); + v.back().make_child(); + + VERIFY(v[1].child->value == 12); + VERIFY(v[1].child->child == nullptr); + + v.insert(v.begin(), *v[1].child); + + VERIFY(v[0].value == 12); + VERIFY(v[0].child == nullptr); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/self_insert.cc b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/self_insert.cc new file mode 100644 index 00000000000..9944cbbfbb7 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/self_insert.cc @@ -0,0 +1,70 @@ +// { dg-options "-std=gnu++11" } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <vector> + +#include "testsuite_hooks.h" + +bool test __attribute__((unused)) = true; + +void test01() +{ + std::vector<std::vector<int>> vv = + { + { 2, 3 }, + { 4, 5 }, + { 0, 1 } + }; + + // Make sure it doesn't reallocate during insertion. + vv.reserve(4); + + vv.insert(vv.begin(), vv[0]); + + VERIFY( vv.size() == 4 ); + VERIFY( vv[0].size() == 2 ); + VERIFY( vv[0][0] == 2 ); + VERIFY( vv[0][1] == 3 ); +} + +void test02() +{ + std::vector<std::vector<int>> vv = + { + { 2, 3 }, + { 4, 5 }, + { 0, 1 } + }; + + // Make sure we will reallocate for insertion. + VERIFY( vv.capacity() == 3 ); + + vv.insert(vv.begin(), vv[0]); + + VERIFY( vv.size() == 4 ); + VERIFY( vv[0].size() == 2 ); + VERIFY( vv[0][0] == 2 ); + VERIFY( vv[0][1] == 3 ); +} + +int main() +{ + test01(); + test02(); +} diff --git a/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert_vs_emplace.cc b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert_vs_emplace.cc index 39a3f031b46..1b461240592 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert_vs_emplace.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert_vs_emplace.cc @@ -223,7 +223,8 @@ test03() void test04() { - const X::special expected{ 0, 3, 1, 0, 3, 0 }; + const X::special expected_ins{ 0, 3, 1, 0, 3, 0 }; + const X::special expected_emp{ 0, 4, 1, 0, 4, 0 }; X::special ins, emp; { std::vector<X> v; @@ -253,8 +254,8 @@ test04() // std::cout << "----\n"; emp = X::sp; } - VERIFY( ins == emp ); - VERIFY( ins == expected ); + VERIFY( ins == expected_ins ); + VERIFY( emp == expected_emp ); } // insert vs emplace xvalue reallocation diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/65913.cc b/libstdc++-v3/testsuite/29_atomics/atomic/65913.cc index 713ef42d03c..32a58ec991b 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic/65913.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic/65913.cc @@ -15,7 +15,8 @@ // with this library; see the file COPYING3. If not see // <http://www.gnu.org/licenses/>. -// { dg-do run { target x86_64-*-linux* powerpc*-*-linux* } } +// { dg-do run } +// { dg-require-atomic-builtins "" } // { dg-options "-std=gnu++11 -O0" } #include <atomic> diff --git a/libstdc++-v3/testsuite/backward/hash_set/check_construct_destroy.cc b/libstdc++-v3/testsuite/backward/hash_set/check_construct_destroy.cc index 5693c76c386..5740fe1bf8b 100644 --- a/libstdc++-v3/testsuite/backward/hash_set/check_construct_destroy.cc +++ b/libstdc++-v3/testsuite/backward/hash_set/check_construct_destroy.cc @@ -39,45 +39,48 @@ int main() int buckets; + // Add 1 to all counts, because the std::vector used internally by the + // hashtable creates and destroys a temporary object using the allocator. + tracker_allocator_counter::reset(); { Container c; buckets = c.bucket_count(); - ok = check_construct_destroy("empty container", buckets, 0) && ok; + ok = check_construct_destroy("empty container", buckets+1, 1) && ok; } - ok = check_construct_destroy("empty container", buckets, buckets) && ok; + ok = check_construct_destroy("empty container", buckets+1, buckets+1) && ok; tracker_allocator_counter::reset(); { Container c(arr10, arr10 + 10); - ok = check_construct_destroy("Construct from range", buckets+10, 0) && ok; + ok = check_construct_destroy("Construct from range", buckets+10+1, 1) && ok; } - ok = check_construct_destroy("Construct from range", buckets+10, buckets+10) && ok; + ok = check_construct_destroy("Construct from range", buckets+10+1, buckets+10+1) && ok; tracker_allocator_counter::reset(); { Container c(arr10, arr10 + 10); c.insert(arr10a[0]); - ok = check_construct_destroy("Insert element", buckets+11, 0) && ok; + ok = check_construct_destroy("Insert element", buckets+11+1, 1) && ok; } - ok = check_construct_destroy("Insert element", buckets+11, buckets+11) && ok; + ok = check_construct_destroy("Insert element", buckets+11+1, buckets+11+1) && ok; tracker_allocator_counter::reset(); { Container c(arr10, arr10 + 10); c.insert(arr10a, arr10a+3); - ok = check_construct_destroy("Insert short range", buckets+13, 0) && ok; + ok = check_construct_destroy("Insert short range", buckets+13+1, 1) && ok; } - ok = check_construct_destroy("Insert short range", buckets+13, buckets+13) && ok; + ok = check_construct_destroy("Insert short range", buckets+13+1, buckets+13+1) && ok; tracker_allocator_counter::reset(); { Container c(arr10, arr10 + 10); c.insert(arr10a, arr10a+10); - ok = check_construct_destroy("Insert long range", buckets+20, 0) && ok; + ok = check_construct_destroy("Insert long range", buckets+20+1, 1) && ok; } - ok = check_construct_destroy("Insert long range", buckets+20, buckets+20) && ok; + ok = check_construct_destroy("Insert long range", buckets+20+1, buckets+20+1) && ok; return ok ? 0 : 1; } diff --git a/libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc b/libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc index ce3f2135889..bb0f754f549 100644 --- a/libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc +++ b/libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc @@ -77,8 +77,38 @@ void test02() } } +static int move_count = 0; + +void test03() +{ + struct MoveEnabled + { + MoveEnabled(MoveEnabled&&) + { + ++move_count; + } + MoveEnabled() = default; + MoveEnabled(const MoveEnabled&) = default; + }; + MoveEnabled m; + MoveEnabled m2 = any_cast<MoveEnabled>(any(m)); + VERIFY(move_count == 1); + MoveEnabled&& m3 = any_cast<MoveEnabled&&>(any(m)); + VERIFY(move_count == 1); + struct MoveDeleted + { + MoveDeleted(MoveDeleted&&) = delete; + MoveDeleted() = default; + MoveDeleted(const MoveDeleted&) = default; + }; + MoveDeleted md; + MoveDeleted&& md2 = any_cast<MoveDeleted>(any(std::move(md))); + MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md))); +} + int main() { test01(); test02(); + test03(); } diff --git a/libstdc++-v3/testsuite/experimental/any/misc/any_cast_neg.cc b/libstdc++-v3/testsuite/experimental/any/misc/any_cast_neg.cc index 1361db89d4c..82957a1f544 100644 --- a/libstdc++-v3/testsuite/experimental/any/misc/any_cast_neg.cc +++ b/libstdc++-v3/testsuite/experimental/any/misc/any_cast_neg.cc @@ -26,5 +26,5 @@ void test01() using std::experimental::any_cast; const any y(1); - any_cast<int&>(y); // { dg-error "qualifiers" "" { target { *-*-* } } 353 } + any_cast<int&>(y); // { dg-error "qualifiers" "" { target { *-*-* } } 368 } } diff --git a/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directories.cc b/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directories.cc index 4be41a6c47c..a52efe4ce5c 100644 --- a/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directories.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/create_directories.cc @@ -65,7 +65,8 @@ test01() VERIFY( b ); VERIFY( is_directory(p/"./d4/../d5") ); - remove_all(p, ec); + std::uintmax_t count = remove_all(p, ec); + VERIFY( count == 6 ); } int diff --git a/libstdc++-v3/testsuite/experimental/optional/cons/value.cc b/libstdc++-v3/testsuite/experimental/optional/cons/value.cc index a916951b874..123a89ede04 100644 --- a/libstdc++-v3/testsuite/experimental/optional/cons/value.cc +++ b/libstdc++-v3/testsuite/experimental/optional/cons/value.cc @@ -22,6 +22,7 @@ #include <testsuite_hooks.h> #include <vector> +#include <string> struct tracker { @@ -236,4 +237,22 @@ int main() VERIFY( result == caught ); } + + { + std::experimental::optional<std::string> os = "foo"; + struct X + { + explicit X(int) {} + X& operator=(int) {return *this;} + }; + std::experimental::optional<X> ox{42}; + std::experimental::optional<int> oi{42}; + std::experimental::optional<X> ox2{oi}; + std::experimental::optional<std::string> os2; + os2 = "foo"; + std::experimental::optional<X> ox3; + ox3 = 42; + std::experimental::optional<X> ox4; + ox4 = oi; + } } diff --git a/libstdc++-v3/testsuite/experimental/optional/cons/value_neg.cc b/libstdc++-v3/testsuite/experimental/optional/cons/value_neg.cc new file mode 100644 index 00000000000..c862a04986a --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/optional/cons/value_neg.cc @@ -0,0 +1,39 @@ +// { dg-options "-std=gnu++14" } +// { dg-do compile } + +// Copyright (C) 2013-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a moved_to of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include <experimental/optional> +#include <testsuite_hooks.h> + +#include <string> +#include <memory> + +int main() +{ + { + struct X + { + explicit X(int) {} + }; + std::experimental::optional<X> ox{42}; + std::experimental::optional<X> ox2 = 42; // { dg-error "conversion" } + std::experimental::optional<std::unique_ptr<int>> oup{new int}; + std::experimental::optional<std::unique_ptr<int>> oup2 = new int; // { dg-error "conversion" } + } +} |