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.syn]/3 |
14 | // Any function returning a valarray<T> is permitted to return an object of |
15 | // another type, provided all the const member functions of valarray<T> are |
16 | // also applicable to this type. |
17 | // |
18 | // Libc++ uses this and returns __val_expr<_Expr> for several operations. |
19 | // |
20 | // The const overloads of |
21 | // valarray::operator[](...) const |
22 | // return propxy objects. These proxies are implicitly convertible to |
23 | // std::valarray. |
24 | // |
25 | // valarray& operator>>=(const valarray& v); |
26 | |
27 | #include <valarray> |
28 | #include <cassert> |
29 | #include <cstddef> |
30 | |
31 | #include "test_macros.h" |
32 | |
33 | template <class A> |
34 | void test(const A& rhs) { |
35 | int input[] = {64, 256, 768, 2048, 5120}; |
36 | int expected[] = {1, 2, 3, 4, 5}; |
37 | const unsigned N = sizeof(input) / sizeof(input[0]); |
38 | std::valarray<int> value(input, N); |
39 | |
40 | value >>= rhs; |
41 | |
42 | assert(value.size() == N); |
43 | for (std::size_t i = 0; i < value.size(); ++i) |
44 | assert(value[i] == expected[i]); |
45 | } |
46 | |
47 | int main(int, char**) { |
48 | int input[] = {6, 7, 8, 9, 10}; |
49 | const unsigned N = sizeof(input) / sizeof(input[0]); |
50 | |
51 | std::valarray<bool> mask(true, N); |
52 | std::size_t indices[] = {0, 1, 2, 3, 4}; |
53 | std::valarray<std::size_t> indirect(indices, N); |
54 | |
55 | std::valarray<int> zero(0, N); |
56 | |
57 | { |
58 | std::valarray<int> value(input, N); |
59 | |
60 | test(rhs: value); |
61 | test(rhs: value[std::slice(0, N, 1)]); |
62 | test(rhs: value[std::gslice(0, std::valarray<std::size_t>(N, 1), std::valarray<std::size_t>(1, 1))]); |
63 | test(rhs: value[mask]); |
64 | test(rhs: value[indirect]); |
65 | test(rhs: value + zero); |
66 | } |
67 | |
68 | { |
69 | const std::valarray<int> value(input, N); |
70 | |
71 | test(rhs: value); |
72 | test(rhs: value[std::slice(0, N, 1)]); |
73 | test(rhs: value[std::gslice(0, std::valarray<std::size_t>(N, 1), std::valarray<std::size_t>(1, 1))]); |
74 | test(rhs: value[mask]); |
75 | test(rhs: value[indirect]); |
76 | test(rhs: value + zero); |
77 | } |
78 | |
79 | return 0; |
80 | } |
81 | |