1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <valarray> 11 12 // template<class T> class valarray; 13 14 // valarray apply(value_type f(const value_type&)) const; 15 16 #include <valarray> 17 #include <cassert> 18 19 typedef int T; 20 21 T f(const T& t) {return t + 5;} 22 23 int main() 24 { 25 { 26 T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 27 T a2[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; 28 const unsigned N1 = sizeof(a1)/sizeof(a1[0]); 29 std::valarray<T> v1(a1, N1); 30 std::valarray<T> v2 = v1.apply(f); 31 assert(v2.size() == N1); 32 for (unsigned i = 0; i < N1; ++i) 33 assert(v2[i] == a2[i]); 34 } 35 { 36 const unsigned N1 = 0; 37 std::valarray<T> v1; 38 std::valarray<T> v2 = v1.apply(f); 39 assert(v2.size() == N1); 40 } 41 { 42 T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 43 T a2[] = {7, 9, 11, 13, 15, 17, 19, 21, 23, 25}; 44 const unsigned N1 = sizeof(a1)/sizeof(a1[0]); 45 std::valarray<T> v1(a1, N1); 46 std::valarray<T> v2 = (v1+v1).apply(f); 47 assert(v2.size() == N1); 48 for (unsigned i = 0; i < N1; ++i) 49 assert(v2[i] == a2[i]); 50 } 51 } 52