]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/any/assign/self.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / any / assign / self.cc
CommitLineData
8d9254fc 1// Copyright (C) 2015-2020 Free Software Foundation, Inc.
52e86221
VV
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-options "-std=gnu++17" }
19
20#include <any>
21#include <set>
22#include <testsuite_hooks.h>
23
24std::set<const void*> live_objects;
25
26struct A {
27 A() { live_objects.insert(this); }
28 ~A() { live_objects.erase(this); }
29 A(const A& a) { VERIFY(live_objects.count(&a)); live_objects.insert(this); }
30};
31
32void
33test01()
34{
35 using std::any;
36
37 any a;
38 a = a;
25a69162 39 VERIFY( !a.has_value() );
52e86221
VV
40
41 a = A{};
42 a = a;
25a69162 43 VERIFY( a.has_value() );
52e86221 44
25a69162 45 a.reset();
52e86221
VV
46 VERIFY( live_objects.empty() );
47}
48
49void
50test02()
51{
52 using std::any;
53
54 struct X {
55 any a;
56 };
57
58 X x;
59 std::swap(x, x); // results in "self-move-assignment" of X::a
25a69162 60 VERIFY( !x.a.has_value() );
52e86221
VV
61
62 x.a = A{};
63 std::swap(x, x); // results in "self-move-assignment" of X::a
25a69162 64 VERIFY( x.a.has_value() );
52e86221 65
25a69162 66 x.a.reset();
52e86221
VV
67 VERIFY( live_objects.empty() );
68}
69
70void
71test03()
72{
73 using std::any;
74
75 any a;
76 a.swap(a);
25a69162 77 VERIFY( !a.has_value() );
52e86221
VV
78
79 a = A{};
80 a.swap(a);
25a69162 81 VERIFY( a.has_value() );
52e86221 82
25a69162 83 a.reset();
52e86221
VV
84 VERIFY( live_objects.empty() );
85}
86
87int
88main()
89{
90 test01();
91 test02();
92 test03();
93}