uninitialized_fill.pass.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ForwardIterator, class T>
  10. // void
  11. // uninitialized_fill(ForwardIterator first, ForwardIterator last,
  12. // const T& x);
  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. assert(B::population_ == 0);
  48. #ifndef TEST_HAS_NO_EXCEPTIONS
  49. try
  50. {
  51. std::uninitialized_fill(bp, bp+N, B());
  52. assert(false);
  53. }
  54. catch (...)
  55. {
  56. assert(B::population_ == 0);
  57. }
  58. #endif
  59. B::count_ = 0;
  60. std::uninitialized_fill(bp, bp+2, B());
  61. for (int i = 0; i < 2; ++i)
  62. assert(bp[i].data_ == 1);
  63. assert(B::population_ == 2);
  64. }
  65. {
  66. const int N = 5;
  67. char pool[N*sizeof(Nasty)] = {0};
  68. Nasty* bp = (Nasty*)pool;
  69. Nasty::counter_ = 23;
  70. std::uninitialized_fill(bp, bp+N, Nasty());
  71. for (int i = 0; i < N; ++i)
  72. assert(bp[i].i_ == 23);
  73. }
  74. return 0;
  75. }