]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/optional/swap/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / optional / swap / 1.cc
CommitLineData
52066eae 1// { dg-do run { target c++14 } }
2b5ab1e4 2
99dee823 3// Copyright (C) 2013-2021 Free Software Foundation, Inc.
2b5ab1e4
MB
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
c7cbb4da 16// You should have received a copy of the GNU General Public License along
2b5ab1e4
MB
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
19
20#include <experimental/optional>
21#include <testsuite_hooks.h>
22
23struct exception {};
24
25int counter = 0;
26
27struct mixin_counter
28{
29 mixin_counter() { ++counter; }
30 mixin_counter(mixin_counter const&) { ++counter; }
31 ~mixin_counter() { --counter; }
32};
33
34namespace ns
35{
36
37struct value_type : private mixin_counter
38{
39 explicit value_type(int state) : state(state) { }
40 int state;
41};
42
43int swaps = 0;
44
45void
46swap(value_type& lhs, value_type& rhs)
47{
48 ++swaps;
49 using std::swap;
50 swap(lhs.state, rhs.state);
51}
52
53} // namespace ns
54
55int main()
56{
57 using O = std::experimental::optional<ns::value_type>;
58
59 VERIFY( ns::swaps == 0 );
60
61 {
62 O o, p;
63 swap(o, p);
64 VERIFY( !o );
65 VERIFY( !p );
66 }
67
68 {
69 O o { std::experimental::in_place, 45 }, p;
70 swap(o, p);
71 VERIFY( !o );
72 VERIFY( p && p->state == 45 );
73 }
74
75 {
76 O o, p { std::experimental::in_place, 45 };
77 swap(o, p);
78 VERIFY( o && o->state == 45 );
79 VERIFY( !p );
80 }
81
82 {
83 O o { std::experimental::in_place, 167 }, p { std::experimental::in_place, 999 };
84 VERIFY( ns::swaps == 0 );
85
86 swap(o, p);
87
88 VERIFY( o && o->state == 999 );
89 VERIFY( p && p->state == 167 );
90 VERIFY( ns::swaps == 1 );
91 }
92
93 VERIFY( counter == 0 );
94}