xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/unittests/optional/assignment/5.cc (revision 2f62cc9c12bc202c40224f32c879f81443fee079)
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_5 {
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 static void
32 test ()
33 {
34   using O = gdb::optional<value_type>;
35 
36   // Check std::nullopt_t and 'default' (= {}) assignment
37 
38 #ifndef GDB_OPTIONAL
39   {
40     O o;
41     o = std::nullopt;
42     VERIFY( !o );
43   }
44 #endif
45 
46 #ifndef GDB_OPTIONAL
47   {
48     O o { gdb::in_place };
49     o = std::nullopt;
50     VERIFY( !o );
51   }
52 #endif
53 
54 #ifndef GDB_OPTIONAL
55   {
56     O o;
57     o = {};
58     VERIFY( !o );
59   }
60 #endif
61 
62 #ifndef GDB_OPTIONAL
63   {
64     O o { gdb::in_place };
65     o = {};
66     VERIFY( !o );
67   }
68 #endif
69   {
70     gdb::optional<std::vector<int>> ovi{{1, 2, 3}};
71     VERIFY(ovi->size() == 3);
72     VERIFY((*ovi)[0] == 1 && (*ovi)[1] == 2 && (*ovi)[2] == 3);
73     ovi = {4, 5, 6, 7};
74     VERIFY(ovi->size() == 4);
75     VERIFY((*ovi)[0] == 4 && (*ovi)[1] == 5 &&
76 	   (*ovi)[2] == 6 && (*ovi)[3] == 7);
77   }
78   VERIFY( counter == 0 );
79 }
80 
81 } // namespace assign_5
82