| 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 | // { dg-do run { target c++11 } } |
| 19 | |
| 20 | #include <valarray> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | const std::valarray<int> v{ |
| 24 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 25 | }; |
| 26 | |
| 27 | bool |
| 28 | all_of(const std::valarray<bool>& vals) |
| 29 | { |
| 30 | for (bool b : vals) |
| 31 | if (!b) |
| 32 | return false; |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | void |
| 37 | test01() |
| 38 | { |
| 39 | // PR libstdc++/83860 |
| 40 | const std::valarray<int> va(v), vb(v), vc(v); |
| 41 | auto sum = va + vb + vc; |
| 42 | std::valarray<int> vsum = sum; |
| 43 | VERIFY( all_of( vsum == (3 * v) ) ); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | test02() |
| 48 | { |
| 49 | auto neg = -(-v); |
| 50 | std::valarray<int> vneg = neg; |
| 51 | VERIFY( all_of( vneg == v ) ); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | test03() |
| 56 | { |
| 57 | const std::valarray<int> va(v), vb(v); |
| 58 | auto diff = va + -vb; |
| 59 | std::valarray<int> vdiff = diff; |
| 60 | VERIFY( all_of( vdiff == (va - vb) ) ); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | test04() |
| 65 | { |
| 66 | const std::valarray<int> va(v), vb(v); |
| 67 | auto sum = -va + -vb; |
| 68 | std::valarray<int> vsum = sum; |
| 69 | VERIFY( all_of( vsum == (-2 * v) ) ); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | test05() |
| 74 | { |
| 75 | const std::valarray<int> va(v), vb(v); |
| 76 | auto sum = -(-va + -vb); |
| 77 | std::valarray<int> vsum = sum; |
| 78 | VERIFY( all_of( vsum == (2 * v) ) ); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | test06() |
| 83 | { |
| 84 | auto prod = 3 * +v * 2; |
| 85 | std::valarray<int> vprod = prod; |
| 86 | VERIFY( all_of( vprod == (6 * v) ) ); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | test07() |
| 91 | { |
| 92 | const std::valarray<int> va(v), vb(v); |
| 93 | auto valfun = [](int i) { return i; }; |
| 94 | auto reffun = [](const int& i) { return i; }; |
| 95 | auto sum = (va.apply(valfun) + vb.apply(reffun)); |
| 96 | std::valarray<int> vsum = sum; |
| 97 | VERIFY( all_of( vsum == (va + vb) ) ); |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | main() |
| 102 | { |
| 103 | test01(); |
| 104 | test02(); |
| 105 | test03(); |
| 106 | test04(); |
| 107 | test05(); |
| 108 | test06(); |
| 109 | test07(); |
| 110 | } |