xref: /llvm-project/libcxx/test/std/numerics/numarray/valarray.range/begin-end.pass.cpp (revision a281bdd51bdb486300b7ff7144d9a4ee56177a15)
1*a281bdd5SArthur O'Dwyer //===----------------------------------------------------------------------===//
2*a281bdd5SArthur O'Dwyer //
3*a281bdd5SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*a281bdd5SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5*a281bdd5SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*a281bdd5SArthur O'Dwyer //
7*a281bdd5SArthur O'Dwyer //===----------------------------------------------------------------------===//
8*a281bdd5SArthur O'Dwyer 
9*a281bdd5SArthur O'Dwyer // <valarray>
10*a281bdd5SArthur O'Dwyer 
11*a281bdd5SArthur O'Dwyer // template<class T> class valarray;
12*a281bdd5SArthur O'Dwyer 
13*a281bdd5SArthur O'Dwyer // template <class T> unspecified begin(valarray<T>& v);
14*a281bdd5SArthur O'Dwyer // template <class T> unspecified begin(const valarray<T>& v);
15*a281bdd5SArthur O'Dwyer // template <class T> unspecified end(valarray<T>& v);
16*a281bdd5SArthur O'Dwyer // template <class T> unspecified end(const valarray<T>& v);
17*a281bdd5SArthur O'Dwyer 
18*a281bdd5SArthur O'Dwyer #include <valarray>
19*a281bdd5SArthur O'Dwyer #include <cassert>
20*a281bdd5SArthur O'Dwyer #include <iterator>
21*a281bdd5SArthur O'Dwyer #include <type_traits>
22*a281bdd5SArthur O'Dwyer 
23*a281bdd5SArthur O'Dwyer #include "test_macros.h"
24*a281bdd5SArthur O'Dwyer 
main(int,char **)25*a281bdd5SArthur O'Dwyer int main(int, char**)
26*a281bdd5SArthur O'Dwyer {
27*a281bdd5SArthur O'Dwyer   {
28*a281bdd5SArthur O'Dwyer     int a[] = {1, 2, 3, 4, 5};
29*a281bdd5SArthur O'Dwyer     std::valarray<int> v(a, 5);
30*a281bdd5SArthur O'Dwyer     const std::valarray<int>& cv = v;
31*a281bdd5SArthur O'Dwyer     using It = decltype(std::begin(v));
32*a281bdd5SArthur O'Dwyer     using CIt = decltype(std::begin(cv));
33*a281bdd5SArthur O'Dwyer     static_assert(std::is_base_of<std::random_access_iterator_tag, std::iterator_traits<It>::iterator_category>::value, "");
34*a281bdd5SArthur O'Dwyer     static_assert(std::is_base_of<std::random_access_iterator_tag, std::iterator_traits<CIt>::iterator_category>::value, "");
35*a281bdd5SArthur O'Dwyer     ASSERT_SAME_TYPE(decltype(*std::begin(v)), int&);
36*a281bdd5SArthur O'Dwyer     ASSERT_SAME_TYPE(decltype(*std::begin(cv)), const int&);
37*a281bdd5SArthur O'Dwyer     assert(&*std::begin(v) == &v[0]);
38*a281bdd5SArthur O'Dwyer     assert(&*std::begin(cv) == &cv[0]);
39*a281bdd5SArthur O'Dwyer     *std::begin(v) = 10;
40*a281bdd5SArthur O'Dwyer     assert(v[0] == 10);
41*a281bdd5SArthur O'Dwyer 
42*a281bdd5SArthur O'Dwyer     ASSERT_SAME_TYPE(decltype(std::end(v)), It);
43*a281bdd5SArthur O'Dwyer     ASSERT_SAME_TYPE(decltype(std::end(cv)), CIt);
44*a281bdd5SArthur O'Dwyer     assert(&*std::prev(std::end(v)) == &v[4]);
45*a281bdd5SArthur O'Dwyer     assert(&*std::prev(std::end(cv)) == &cv[4]);
46*a281bdd5SArthur O'Dwyer   }
47*a281bdd5SArthur O'Dwyer #if TEST_STD_VER >= 11
48*a281bdd5SArthur O'Dwyer   {
49*a281bdd5SArthur O'Dwyer     int a[] = {1, 2, 3, 4, 5};
50*a281bdd5SArthur O'Dwyer     std::valarray<int> v(a, 5);
51*a281bdd5SArthur O'Dwyer     int sum = 0;
52*a281bdd5SArthur O'Dwyer     for (int& i : v) {
53*a281bdd5SArthur O'Dwyer       sum += i;
54*a281bdd5SArthur O'Dwyer     }
55*a281bdd5SArthur O'Dwyer     assert(sum == 15);
56*a281bdd5SArthur O'Dwyer   }
57*a281bdd5SArthur O'Dwyer   {
58*a281bdd5SArthur O'Dwyer     int a[] = {1, 2, 3, 4, 5};
59*a281bdd5SArthur O'Dwyer     const std::valarray<int> cv(a, 5);
60*a281bdd5SArthur O'Dwyer     int sum = 0;
61*a281bdd5SArthur O'Dwyer     for (const int& i : cv) {
62*a281bdd5SArthur O'Dwyer       sum += i;
63*a281bdd5SArthur O'Dwyer     }
64*a281bdd5SArthur O'Dwyer     assert(sum == 15);
65*a281bdd5SArthur O'Dwyer   }
66*a281bdd5SArthur O'Dwyer #endif
67*a281bdd5SArthur O'Dwyer 
68*a281bdd5SArthur O'Dwyer   return 0;
69*a281bdd5SArthur O'Dwyer }
70