apply_value.pass.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. // <valarray>
  10. // template<class T> class valarray;
  11. // valarray apply(value_type f(value_type)) const;
  12. #include <valarray>
  13. #include <cassert>
  14. typedef int T;
  15. T f(T t) {return t + 5;}
  16. int main()
  17. {
  18. {
  19. T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  20. T a2[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  21. const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
  22. std::valarray<T> v1(a1, N1);
  23. std::valarray<T> v2 = v1.apply(f);
  24. assert(v2.size() == N1);
  25. for (unsigned i = 0; i < N1; ++i)
  26. assert(v2[i] == a2[i]);
  27. }
  28. {
  29. const unsigned N1 = 0;
  30. std::valarray<T> v1;
  31. std::valarray<T> v2 = v1.apply(f);
  32. assert(v2.size() == N1);
  33. }
  34. {
  35. T a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  36. T a2[] = {7, 9, 11, 13, 15, 17, 19, 21, 23, 25};
  37. const unsigned N1 = sizeof(a1)/sizeof(a1[0]);
  38. std::valarray<T> v1(a1, N1);
  39. std::valarray<T> v2 = (v1+v1).apply(f);
  40. assert(v2.size() == N1);
  41. for (unsigned i = 0; i < N1; ++i)
  42. assert(v2[i] == a2[i]);
  43. }
  44. }