xref: /llvm-project/libcxx/test/std/numerics/numarray/template.valarray/valarray.members/apply_cref.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <valarray>
10 
11 // template<class T> class valarray;
12 
13 // valarray apply(value_type f(const value_type&)) const;
14 
15 #include <valarray>
16 #include <cassert>
17 
18 typedef int T;
19 
20 T f(const T& t) {return t + 5;}
21 
22 int main()
23 {
24     {
25         T a1[] = {1, 2, 3, 4,  5,  6,  7,  8,  9, 10};
26         T a2[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
27         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
28         std::valarray<T> v1(a1, N1);
29         std::valarray<T> v2 = v1.apply(f);
30         assert(v2.size() == N1);
31         for (unsigned i = 0; i < N1; ++i)
32             assert(v2[i] == a2[i]);
33     }
34     {
35         const unsigned N1 = 0;
36         std::valarray<T> v1;
37         std::valarray<T> v2 = v1.apply(f);
38         assert(v2.size() == N1);
39     }
40     {
41         T a1[] = {1, 2, 3, 4,  5,  6,  7,  8,  9, 10};
42         T a2[] = {7, 9, 11, 13, 15, 17, 19, 21, 23, 25};
43         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
44         std::valarray<T> v1(a1, N1);
45         std::valarray<T> v2 = (v1+v1).apply(f);
46         assert(v2.size() == N1);
47         for (unsigned i = 0; i < N1; ++i)
48             assert(v2[i] == a2[i]);
49     }
50 }
51