libcxx

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

uninitialized_fill_n.pass.cpp (1865B)


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