| 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 |
// template <class T> unspecified begin(valarray<T>& v); |
| 14 |
// template <class T> unspecified begin(const valarray<T>& v); |
| 15 |
// template <class T> unspecified end(valarray<T>& v); |
| 16 |
// template <class T> unspecified end(const valarray<T>& v); |
| 17 |
|
| 18 |
#include <valarray> |
| 19 |
#include <cassert> |
| 20 |
#include <iterator> |
| 21 |
#include <type_traits> |
| 22 |
|
| 23 |
#include "test_macros.h" |
| 24 |
|
| 25 |
int main(int, char**) |
| 26 |
{ |
| 27 |
{ |
| 28 |
int a[] = {1, 2, 3, 4, 5}; |
| 29 |
std::valarray<int> v(a, 5); |
| 30 |
const std::valarray<int>& cv = v; |
| 31 |
using It = decltype(std::begin(v)); |
| 32 |
using CIt = decltype(std::begin(cv)); |
| 33 |
static_assert(std::is_base_of<std::random_access_iterator_tag, std::iterator_traits<It>::iterator_category>::value, ""); |
| 34 |
static_assert(std::is_base_of<std::random_access_iterator_tag, std::iterator_traits<CIt>::iterator_category>::value, ""); |
| 35 |
ASSERT_SAME_TYPE(decltype(*std::begin(v)), int&); |
| 36 |
ASSERT_SAME_TYPE(decltype(*std::begin(cv)), const int&); |
| 37 |
assert(&*std::begin(v) == &v[0]); |
| 38 |
assert(&*std::begin(cv) == &cv[0]); |
| 39 |
*std::begin(v) = 10; |
| 40 |
assert(v[0] == 10); |
| 41 |
|
| 42 |
ASSERT_SAME_TYPE(decltype(std::end(v)), It); |
| 43 |
ASSERT_SAME_TYPE(decltype(std::end(cv)), CIt); |
| 44 |
assert(&*std::prev(std::end(v)) == &v[4]); |
| 45 |
assert(&*std::prev(std::end(cv)) == &cv[4]); |
| 46 |
} |
| 47 |
#if TEST_STD_VER >= 11 |
| 48 |
{ |
| 49 |
int a[] = {1, 2, 3, 4, 5}; |
| 50 |
std::valarray<int> v(a, 5); |
| 51 |
int sum = 0; |
| 52 |
for (int& i : v) { |
| 53 |
sum += i; |
| 54 |
} |
| 55 |
assert(sum == 15); |
| 56 |
} |
| 57 |
{ |
| 58 |
int a[] = {1, 2, 3, 4, 5}; |
| 59 |
const std::valarray<int> cv(a, 5); |
| 60 |
int sum = 0; |
| 61 |
for (const int& i : cv) { |
| 62 |
sum += i; |
| 63 |
} |
| 64 |
assert(sum == 15); |
| 65 |
} |
| 66 |
#endif |
| 67 |
|
| 68 |
return 0; |
| 69 |
} |