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