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 shift(int i) const;
14 
15 #include <valarray>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 
main(int,char **)20 int main(int, char**)
21 {
22     {
23         typedef int T;
24         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
25         T a2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
26         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
27         std::valarray<T> v1(a1, N1);
28         std::valarray<T> v2 = v1.shift(0);
29         assert(v2.size() == N1);
30         for (unsigned i = 0; i < N1; ++i)
31             assert(v2[i] == a2[i]);
32     }
33     {
34         typedef int T;
35         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
36         T a2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 0};
37         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
38         std::valarray<T> v1(a1, N1);
39         std::valarray<T> v2 = v1.shift(1);
40         assert(v2.size() == N1);
41         for (unsigned i = 0; i < N1; ++i)
42             assert(v2[i] == a2[i]);
43     }
44     {
45         typedef int T;
46         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
47         T a2[] = {10, 0, 0, 0, 0, 0, 0, 0, 0, 0};
48         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
49         std::valarray<T> v1(a1, N1);
50         std::valarray<T> v2 = v1.shift(9);
51         assert(v2.size() == N1);
52         for (unsigned i = 0; i < N1; ++i)
53             assert(v2[i] == a2[i]);
54     }
55     {
56         typedef int T;
57         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
58         T a2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
59         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
60         std::valarray<T> v1(a1, N1);
61         std::valarray<T> v2 = v1.shift(90);
62         assert(v2.size() == N1);
63         for (unsigned i = 0; i < N1; ++i)
64             assert(v2[i] == a2[i]);
65     }
66     {
67         typedef int T;
68         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
69         T a2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
70         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
71         std::valarray<T> v1(a1, N1);
72         std::valarray<T> v2 = v1.shift(-1);
73         assert(v2.size() == N1);
74         for (unsigned i = 0; i < N1; ++i)
75             assert(v2[i] == a2[i]);
76     }
77     {
78         typedef int T;
79         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
80         T a2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
81         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
82         std::valarray<T> v1(a1, N1);
83         std::valarray<T> v2 = v1.shift(-9);
84         assert(v2.size() == N1);
85         for (unsigned i = 0; i < N1; ++i)
86             assert(v2[i] == a2[i]);
87     }
88     {
89         typedef int T;
90         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
91         T a2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
92         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
93         std::valarray<T> v1(a1, N1);
94         std::valarray<T> v2 = v1.shift(-90);
95         assert(v2.size() == N1);
96         for (unsigned i = 0; i < N1; ++i)
97             assert(v2[i] == a2[i]);
98     }
99     {
100         typedef int T;
101         const unsigned N1 = 0;
102         std::valarray<T> v1;
103         std::valarray<T> v2 = v1.shift(-90);
104         assert(v2.size() == N1);
105     }
106     {
107         typedef int T;
108         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
109         T a2[] = {8, 10, 12, 14, 16, 18, 20, 0, 0, 0};
110         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
111         std::valarray<T> v1(a1, N1);
112         std::valarray<T> v2 = (v1 + v1).shift(3);
113         assert(v2.size() == N1);
114         for (unsigned i = 0; i < N1; ++i)
115             assert(v2[i] == a2[i]);
116     }
117     {
118         typedef int T;
119         T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
120         T a2[] = {0, 0, 0, 2, 4, 6, 8, 10, 12, 14};
121         const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
122         std::valarray<T> v1(a1, N1);
123         std::valarray<T> v2 = (v1 + v1).shift(-3);
124         assert(v2.size() == N1);
125         for (unsigned i = 0; i < N1; ++i)
126             assert(v2[i] == a2[i]);
127     }
128 
129   return 0;
130 }
131