From 8eb1d544f89e6015743ddacfc86bbf06fffdb935 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 2 Nov 2015 15:25:53 +0000 Subject: [PATCH] Remove undefined behavior from some tests. Thanks to Walter Brown for the heads-up. llvm-svn: 251802 --- .../uninitialized.copy/uninitialized_copy.pass.cpp | 14 +++++++++----- .../uninitialized_copy_n.pass.cpp | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/uninitialized_copy.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/uninitialized_copy.pass.cpp index f431335db732..dcf34ac47825 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/uninitialized_copy.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/uninitialized_copy.pass.cpp @@ -20,13 +20,15 @@ struct B { static int count_; + static int population_; int data_; - explicit B() : data_(1) {} - B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;} - ~B() {data_ = 0;} + explicit B() : data_(1) { ++population_; } + B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_; ++population_; } + ~B() {data_ = 0; --population_; } }; int B::count_ = 0; +int B::population_ = 0; struct Nasty { @@ -45,6 +47,7 @@ int main() char pool[sizeof(B)*N] = {0}; B* bp = (B*)pool; B b[N]; + assert(B::population_ == N); try { std::uninitialized_copy(b, b+N, bp); @@ -52,14 +55,15 @@ int main() } catch (...) { - for (int i = 0; i < N; ++i) - assert(bp[i].data_ == 0); + assert(B::population_ == N); } B::count_ = 0; std::uninitialized_copy(b, b+2, bp); for (int i = 0; i < 2; ++i) assert(bp[i].data_ == 1); + assert(B::population_ == N + 2); } + { const int N = 5; char pool[sizeof(Nasty)*N] = {0}; diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/uninitialized_copy_n.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/uninitialized_copy_n.pass.cpp index 3b2007b969c3..69e39423fc09 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/uninitialized_copy_n.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/uninitialized_copy_n.pass.cpp @@ -20,12 +20,14 @@ struct B { static int count_; + static int population_; int data_; - explicit B() : data_(1) {} - B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;} - ~B() {data_ = 0;} + explicit B() : data_(1) { ++population_; } + B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_; ++population_; } + ~B() {data_ = 0; --population_; } }; +int B::population_ = 0; int B::count_ = 0; struct Nasty @@ -45,6 +47,7 @@ int main() char pool[sizeof(B)*N] = {0}; B* bp = (B*)pool; B b[N]; + assert(B::population_ == N); try { std::uninitialized_copy_n(b, 5, bp); @@ -52,14 +55,15 @@ int main() } catch (...) { - for (int i = 0; i < N; ++i) - assert(bp[i].data_ == 0); + assert(B::population_ == N); } B::count_ = 0; std::uninitialized_copy_n(b, 2, bp); for (int i = 0; i < 2; ++i) assert(bp[i].data_ == 1); + assert(B::population_ == N + 2); } + { const int N = 5; char pool[sizeof(Nasty)*N] = {0};