]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/adjacent_difference/lwg2055.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / adjacent_difference / lwg2055.cc
1 // Copyright (C) 2018-2022 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++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <numeric>
22 #include <iterator>
23 #include <testsuite_hooks.h>
24
25 struct Int
26 {
27 Int(int v) : val(v) { }
28
29 ~Int() = default;
30
31 Int(const Int& x) : val(x.val), copies(x.copies), moved_from(x.moved_from)
32 { ++copies; }
33
34 Int(Int&& x) : val(x.val), copies(x.copies), moved_from(x.moved_from)
35 { x.moved_from = true; }
36
37 Int& operator=(const Int& x)
38 {
39 val = x.val;
40 copies = x.copies + 1;
41 moved_from = x.moved_from;
42 return *this;
43 }
44
45 Int& operator=(Int&& x)
46 {
47 val = x.val;
48 copies = x.copies;
49 moved_from = x.moved_from;
50 x.moved_from = true;
51 return *this;
52 }
53
54 int val = 0;
55 int copies = 0;
56 bool moved_from = false;
57 };
58
59 Int operator-(Int x, Int y) { x.val -= y.val; return x; }
60
61 struct Subtract
62 {
63 Int operator()(Int x, Int y) const { x.val -= y.val; return x; }
64 };
65
66 void
67 test01()
68 {
69 Int i[] = { 0, 1, 2, 3, 4 };
70 std::adjacent_difference(std::begin(i), std::end(i), std::begin(i));
71 for (const auto& r : i)
72 {
73 VERIFY( r.copies == 2 );
74 VERIFY( !r.moved_from );
75 }
76 }
77
78 void
79 test02()
80 {
81 Int i[] = { 0, 1, 2, 3, 4 };
82 std::adjacent_difference(std::begin(i), std::end(i), std::begin(i),
83 Subtract{});
84 for (const auto& r : i)
85 {
86 VERIFY( r.copies == 2 );
87 VERIFY( !r.moved_from );
88 }
89 }
90
91 void
92 test03()
93 {
94 Int i[] = { 0, 1, 2, 3, 4 };
95 std::adjacent_difference(std::make_move_iterator(std::begin(i)),
96 std::make_move_iterator(std::end(i)),
97 std::begin(i));
98 for (const auto& r : i)
99 {
100 VERIFY( r.copies == 1 );
101 VERIFY( !r.moved_from );
102 }
103 }
104
105 void
106 test04()
107 {
108 Int i[] = { 0, 1, 2, 3, 4 };
109 std::adjacent_difference(std::make_move_iterator(std::begin(i)),
110 std::make_move_iterator(std::end(i)),
111 std::begin(i), Subtract{});
112 for (const auto& r : i)
113 {
114 VERIFY( r.copies == 1 );
115 VERIFY( !r.moved_from );
116 }
117 }
118
119 int
120 main()
121 {
122 test01();
123 test02();
124 test03();
125 test04();
126 }