1 // Copyright (C) 2015-2018 Free Software Foundation, Inc.
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 
24 std::set<const void*> live_objects;
25 
26 struct A {
AA27   A() { live_objects.insert(this); }
~AA28   ~A() { live_objects.erase(this); }
AA29   A(const A& a) { VERIFY(live_objects.count(&a)); live_objects.insert(this); }
30 };
31 
32 void
test01()33 test01()
34 {
35   using std::any;
36 
37   any a;
38   a = a;
39   VERIFY( !a.has_value() );
40 
41   a = A{};
42   a = a;
43   VERIFY( a.has_value() );
44 
45   a.reset();
46   VERIFY( live_objects.empty() );
47 }
48 
49 void
test02()50 test02()
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
60   VERIFY( !x.a.has_value() );
61 
62   x.a = A{};
63   std::swap(x, x); // results in "self-move-assignment" of X::a
64   VERIFY( x.a.has_value() );
65 
66   x.a.reset();
67   VERIFY( live_objects.empty() );
68 }
69 
70 void
test03()71 test03()
72 {
73   using std::any;
74 
75   any a;
76   a.swap(a);
77   VERIFY( !a.has_value() );
78 
79   a = A{};
80   a.swap(a);
81   VERIFY( a.has_value() );
82 
83   a.reset();
84   VERIFY( live_objects.empty() );
85 }
86 
87 int
main()88 main()
89 {
90   test01();
91   test02();
92   test03();
93 }
94