15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier
95a83710eSEric Fiselier // <valarray>
105a83710eSEric Fiselier
115a83710eSEric Fiselier // template<class T> class valarray;
125a83710eSEric Fiselier
135a83710eSEric Fiselier // valarray apply(value_type f(const value_type&)) const;
145a83710eSEric Fiselier
155a83710eSEric Fiselier #include <valarray>
165a83710eSEric Fiselier #include <cassert>
175a83710eSEric Fiselier
18*7fc6a556SMarshall Clow #include "test_macros.h"
19*7fc6a556SMarshall Clow
205a83710eSEric Fiselier typedef int T;
215a83710eSEric Fiselier
f(const T & t)225a83710eSEric Fiselier T f(const T& t) {return t + 5;}
235a83710eSEric Fiselier
main(int,char **)242df59c50SJF Bastien int main(int, char**)
255a83710eSEric Fiselier {
265a83710eSEric Fiselier {
275a83710eSEric Fiselier T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
285a83710eSEric Fiselier T a2[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
295a83710eSEric Fiselier const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
305a83710eSEric Fiselier std::valarray<T> v1(a1, N1);
315a83710eSEric Fiselier std::valarray<T> v2 = v1.apply(f);
325a83710eSEric Fiselier assert(v2.size() == N1);
335a83710eSEric Fiselier for (unsigned i = 0; i < N1; ++i)
345a83710eSEric Fiselier assert(v2[i] == a2[i]);
355a83710eSEric Fiselier }
365a83710eSEric Fiselier {
375a83710eSEric Fiselier const unsigned N1 = 0;
385a83710eSEric Fiselier std::valarray<T> v1;
395a83710eSEric Fiselier std::valarray<T> v2 = v1.apply(f);
405a83710eSEric Fiselier assert(v2.size() == N1);
415a83710eSEric Fiselier }
425a83710eSEric Fiselier {
435a83710eSEric Fiselier T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
445a83710eSEric Fiselier T a2[] = {7, 9, 11, 13, 15, 17, 19, 21, 23, 25};
455a83710eSEric Fiselier const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
465a83710eSEric Fiselier std::valarray<T> v1(a1, N1);
475a83710eSEric Fiselier std::valarray<T> v2 = (v1+v1).apply(f);
485a83710eSEric Fiselier assert(v2.size() == N1);
495a83710eSEric Fiselier for (unsigned i = 0; i < N1; ++i)
505a83710eSEric Fiselier assert(v2[i] == a2[i]);
515a83710eSEric Fiselier }
522df59c50SJF Bastien
532df59c50SJF Bastien return 0;
545a83710eSEric Fiselier }
55