generate_n.pass.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // <algorithm>
  10. // template<class Iter, IntegralLike Size, Callable Generator>
  11. // requires OutputIterator<Iter, Generator::result_type>
  12. // && CopyConstructible<Generator>
  13. // void
  14. // generate_n(Iter first, Size n, Generator gen);
  15. #ifdef _MSC_VER
  16. #pragma warning(disable: 4244) // conversion from 'const double' to 'int', possible loss of data
  17. #endif
  18. #include <algorithm>
  19. #include <cassert>
  20. #include "test_macros.h"
  21. #include "test_iterators.h"
  22. #include "user_defined_integral.hpp"
  23. struct gen_test
  24. {
  25. int operator()() const {return 2;}
  26. };
  27. template <class Iter, class Size>
  28. void
  29. test2()
  30. {
  31. const unsigned n = 4;
  32. int ia[n] = {0};
  33. assert(std::generate_n(Iter(ia), Size(n), gen_test()) == Iter(ia+n));
  34. assert(ia[0] == 2);
  35. assert(ia[1] == 2);
  36. assert(ia[2] == 2);
  37. assert(ia[3] == 2);
  38. }
  39. template <class Iter>
  40. void
  41. test()
  42. {
  43. test2<Iter, int>();
  44. test2<Iter, unsigned int>();
  45. test2<Iter, long>();
  46. test2<Iter, unsigned long>();
  47. test2<Iter, UserDefinedIntegral<unsigned> >();
  48. test2<Iter, float>();
  49. test2<Iter, double>(); // this is PR#35498
  50. test2<Iter, long double>();
  51. }
  52. int main()
  53. {
  54. test<forward_iterator<int*> >();
  55. test<bidirectional_iterator<int*> >();
  56. test<random_access_iterator<int*> >();
  57. test<int*>();
  58. }