55 lines · cpp
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-exception6//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#include "test_macros.h"19 20typedef int T;21 22T f(const T& t) {return t + 5;}23 24int main(int, char**)25{26 {27 T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};28 T a2[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};29 const unsigned N1 = sizeof(a1)/sizeof(a1[0]);30 std::valarray<T> v1(a1, N1);31 std::valarray<T> v2 = v1.apply(f);32 assert(v2.size() == N1);33 for (unsigned i = 0; i < N1; ++i)34 assert(v2[i] == a2[i]);35 }36 {37 const unsigned N1 = 0;38 std::valarray<T> v1;39 std::valarray<T> v2 = v1.apply(f);40 assert(v2.size() == N1);41 }42 {43 T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};44 T a2[] = {7, 9, 11, 13, 15, 17, 19, 21, 23, 25};45 const unsigned N1 = sizeof(a1)/sizeof(a1[0]);46 std::valarray<T> v1(a1, N1);47 std::valarray<T> v2 = (v1+v1).apply(f);48 assert(v2.size() == N1);49 for (unsigned i = 0; i < N1; ++i)50 assert(v2[i] == a2[i]);51 }52 53 return 0;54}55