uninitialized_copy.pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <memory>
  9. // template <class InputIterator, class ForwardIterator>
  10. // ForwardIterator
  11. // uninitialized_copy(InputIterator first, InputIterator last,
  12. // ForwardIterator result);
  13. #include <memory>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. struct B
  17. {
  18. static int count_;
  19. static int population_;
  20. int data_;
  21. explicit B() : data_(1) { ++population_; }
  22. B(const B &b) {
  23. ++count_;
  24. if (count_ == 3)
  25. TEST_THROW(1);
  26. data_ = b.data_;
  27. ++population_;
  28. }
  29. ~B() {data_ = 0; --population_; }
  30. };
  31. int B::count_ = 0;
  32. int B::population_ = 0;
  33. struct Nasty
  34. {
  35. Nasty() : i_ ( counter_++ ) {}
  36. Nasty * operator &() const { return NULL; }
  37. int i_;
  38. static int counter_;
  39. };
  40. int Nasty::counter_ = 0;
  41. int main(int, char**)
  42. {
  43. {
  44. const int N = 5;
  45. char pool[sizeof(B)*N] = {0};
  46. B* bp = (B*)pool;
  47. B b[N];
  48. assert(B::population_ == N);
  49. #ifndef TEST_HAS_NO_EXCEPTIONS
  50. try
  51. {
  52. std::uninitialized_copy(b, b+N, bp);
  53. assert(false);
  54. }
  55. catch (...)
  56. {
  57. assert(B::population_ == N);
  58. }
  59. #endif
  60. B::count_ = 0;
  61. std::uninitialized_copy(b, b+2, bp);
  62. for (int i = 0; i < 2; ++i)
  63. assert(bp[i].data_ == 1);
  64. assert(B::population_ == N + 2);
  65. }
  66. {
  67. const int N = 5;
  68. char pool[sizeof(Nasty)*N] = {0};
  69. Nasty * p = (Nasty *) pool;
  70. Nasty arr[N];
  71. std::uninitialized_copy(arr, arr+N, p);
  72. for (int i = 0; i < N; ++i) {
  73. assert(arr[i].i_ == i);
  74. assert( p[i].i_ == i);
  75. }
  76. }
  77. return 0;
  78. }