libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

uninitialized_fill.pass.cpp (1836B)


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