Remove undefined behavior from some tests. Thanks to Walter Brown for the heads-up.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@251802 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2015-11-02 15:25:53 +00:00
parent 59b5ea57c3
commit 48bcd27b67
2 changed files with 18 additions and 10 deletions

View File

@ -20,13 +20,15 @@
struct B struct B
{ {
static int count_; static int count_;
static int population_;
int data_; int data_;
explicit B() : data_(1) {} explicit B() : data_(1) { ++population_; }
B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;} B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_; ++population_; }
~B() {data_ = 0;} ~B() {data_ = 0; --population_; }
}; };
int B::count_ = 0; int B::count_ = 0;
int B::population_ = 0;
struct Nasty struct Nasty
{ {
@ -45,6 +47,7 @@ int main()
char pool[sizeof(B)*N] = {0}; char pool[sizeof(B)*N] = {0};
B* bp = (B*)pool; B* bp = (B*)pool;
B b[N]; B b[N];
assert(B::population_ == N);
try try
{ {
std::uninitialized_copy(b, b+N, bp); std::uninitialized_copy(b, b+N, bp);
@ -52,14 +55,15 @@ int main()
} }
catch (...) catch (...)
{ {
for (int i = 0; i < N; ++i) assert(B::population_ == N);
assert(bp[i].data_ == 0);
} }
B::count_ = 0; B::count_ = 0;
std::uninitialized_copy(b, b+2, bp); std::uninitialized_copy(b, b+2, bp);
for (int i = 0; i < 2; ++i) for (int i = 0; i < 2; ++i)
assert(bp[i].data_ == 1); assert(bp[i].data_ == 1);
assert(B::population_ == N + 2);
} }
{ {
const int N = 5; const int N = 5;
char pool[sizeof(Nasty)*N] = {0}; char pool[sizeof(Nasty)*N] = {0};

View File

@ -20,12 +20,14 @@
struct B struct B
{ {
static int count_; static int count_;
static int population_;
int data_; int data_;
explicit B() : data_(1) {} explicit B() : data_(1) { ++population_; }
B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;} B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_; ++population_; }
~B() {data_ = 0;} ~B() {data_ = 0; --population_; }
}; };
int B::population_ = 0;
int B::count_ = 0; int B::count_ = 0;
struct Nasty struct Nasty
@ -45,6 +47,7 @@ int main()
char pool[sizeof(B)*N] = {0}; char pool[sizeof(B)*N] = {0};
B* bp = (B*)pool; B* bp = (B*)pool;
B b[N]; B b[N];
assert(B::population_ == N);
try try
{ {
std::uninitialized_copy_n(b, 5, bp); std::uninitialized_copy_n(b, 5, bp);
@ -52,14 +55,15 @@ int main()
} }
catch (...) catch (...)
{ {
for (int i = 0; i < N; ++i) assert(B::population_ == N);
assert(bp[i].data_ == 0);
} }
B::count_ = 0; B::count_ = 0;
std::uninitialized_copy_n(b, 2, bp); std::uninitialized_copy_n(b, 2, bp);
for (int i = 0; i < 2; ++i) for (int i = 0; i < 2; ++i)
assert(bp[i].data_ == 1); assert(bp[i].data_ == 1);
assert(B::population_ == N + 2);
} }
{ {
const int N = 5; const int N = 5;
char pool[sizeof(Nasty)*N] = {0}; char pool[sizeof(Nasty)*N] = {0};