]> 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-2023 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 Int operator*(Int x, Int y) { x.val *= y.val; return x; }
61
62 struct Add
63 {
64 Int operator()(Int x, Int y) const { x.val += y.val; return x; }
65 };
66
67 struct Multiply
68 {
69 Int operator()(Int x, Int y) const { x.val *= y.val; return x; }
70 };
71
72 void
73 test01()
74 {
75 Int i[] = { 0, 1, 2, 3, 4 };
76 Int j[] = { 5, 6, 7, 8, 9 };
77 Int res = std::inner_product(std::begin(i), std::end(i), std::begin(j),
78 Int{0});
79 VERIFY( res.copies == 0 );
80 VERIFY( !res.moved_from );
81 for (const auto& r : i)
82 VERIFY( !r.moved_from );
83 for (const auto& r : j)
84 VERIFY( !r.moved_from );
85 }
86
87 void
88 test02()
89 {
90 Int i[] = { 0, 1, 2, 3, 4 };
91 Int j[] = { 5, 6, 7, 8, 9 };
92 Int res = std::inner_product(std::begin(i), std::end(i), std::begin(j),
93 Int{0}, Add{}, Multiply{});
94 VERIFY( res.copies == 0 );
95 VERIFY( !res.moved_from );
96 for (const auto& r : i)
97 VERIFY( !r.moved_from );
98 for (const auto& r : j)
99 VERIFY( !r.moved_from );
100 }
101
102 int
103 main()
104 {
105 test01();
106 test02();
107 }