xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/unittests/optional/assignment/6.cc (revision 901e7e84758515fbf39dfc064cb0b45ab146d8b0)
1 // Copyright (C) 2013-2020 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 namespace assign_6 {
19 
20 int counter = 0;
21 
22 struct mixin_counter
23 {
24   mixin_counter() { ++counter; }
25   mixin_counter(mixin_counter const&) { ++counter; }
26   ~mixin_counter() { --counter; }
27 };
28 
29 struct value_type : private mixin_counter
30 {
31   value_type() = default;
32   value_type(int) : state(1) { }
33   value_type(std::initializer_list<char>, const char*) : state(2) { }
34   int state = 0;
35 };
36 
37 static void
38 test ()
39 {
40   using O = gdb::optional<value_type>;
41 
42   // Check emplace
43 
44   {
45     O o;
46     o.emplace();
47     VERIFY( o && o->state == 0 );
48   }
49   {
50     O o { gdb::in_place, 0 };
51     o.emplace();
52     VERIFY( o && o->state == 0 );
53   }
54 
55   {
56     O o;
57     o.emplace(0);
58     VERIFY( o && o->state == 1 );
59   }
60   {
61     O o { gdb::in_place };
62     o.emplace(0);
63     VERIFY( o && o->state == 1 );
64   }
65 
66 #ifndef GDB_OPTIONAL
67   {
68     O o;
69     o.emplace({ 'a' }, "");
70     VERIFY( o && o->state == 2 );
71   }
72   {
73     O o { gdb::in_place };
74     o.emplace({ 'a' }, "");
75     VERIFY( o && o->state == 2 );
76   }
77 #endif
78   {
79     O o;
80     VERIFY(&o.emplace(0) == &*o);
81 #ifndef GDB_OPTIONAL
82     VERIFY(&o.emplace({ 'a' }, "") == &*o);
83 #endif
84   }
85 
86   static_assert( !std::is_constructible<O, std::initializer_list<int>, int>(), "" );
87 
88   VERIFY( counter == 0 );
89 }
90 
91 } // namespace assign_6
92