Debian Codegraph @

libstdc++-v3/testsuite/26_numerics/valarray/87641.cc

raw
1 // Copyright (C) 2018-2025 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 #include <valarray>
19 #include <testsuite_hooks.h>
20
21 void
22 test01()
23 {
24 // PR libstdc++/87641
25 std::valarray<int> v1(3);
26 v1[0] = 1;
27 v1[1] = 2;
28 v1[2] = 3;
29 std::valarray< std::valarray<int> > v2(v1, 3);
30 std::valarray<int> v3 = v2.sum();
31 VERIFY( v3.size() == v1.size() );
32 VERIFY( v3[0] == 3 );
33 VERIFY( v3[1] == 6 );
34 VERIFY( v3[2] == 9 );
35 }
36
37 struct X
38 {
39 X() : val(1) { }
40
41 X& operator+=(const X& x) { val += x.val; return *this; }
42 bool operator==(const X& x) const { return val == x.val; }
43
44 int val;
45 };
46
47 void
48 test02()
49 {
50 std::valarray<X> v1(1);
51 VERIFY( v1.sum() == v1[0] );
52
53 std::valarray<X> v2(2);
54 VERIFY( v2.sum().val == 2 );
55 }
56
57 struct Y
58 {
59 X& operator+=(const Y&) { throw 1; }
60 };
61
62 void
63 test03()
64 {
65 std::valarray<Y> v1(1);
66 (void) v1.sum(); // no addition performed for a single element
67 }
68
69 int
70 main()
71 {
72 test01();
73 test02();
74 test03();
75 }