Added debug tests for indexing, pop_back and both forms of erase. Added an improved error message for erasing a single element with end().
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@177929 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/vector b/include/vector
index d9c5c2f..11d9a1b 100644
--- a/include/vector
+++ b/include/vector
@@ -1550,6 +1550,8 @@
"vector::erase(iterator) called with an iterator not"
" referring to this vector");
#endif
+ _LIBCPP_ASSERT(__position != end(),
+ "vector::erase(iterator) called with a non-dereferenceable iterator");
pointer __p = const_cast<pointer>(&*__position);
iterator __r = __make_iter(__p);
this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));
diff --git a/test/containers/sequences/vector/db_cindex.pass.cpp b/test/containers/sequences/vector/db_cindex.pass.cpp
new file mode 100644
index 0000000..5fef131
--- /dev/null
+++ b/test/containers/sequences/vector/db_cindex.pass.cpp
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Index const vector out of bounds.
+
+#if _LIBCPP_DEBUG2 >= 1
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+
+#include <vector>
+#include <cassert>
+#include <iterator>
+#include <exception>
+#include <cstdlib>
+
+void f1()
+{
+ std::exit(0);
+}
+
+int main()
+{
+ std::set_terminate(f1);
+ typedef int T;
+ typedef std::vector<T> C;
+ const C c(1);
+ assert(c[0] == 0);
+ assert(c[1] == 0);
+ assert(false);
+}
+
+#else
+
+int main()
+{
+}
+
+#endif
diff --git a/test/containers/sequences/vector/db_index.pass.cpp b/test/containers/sequences/vector/db_index.pass.cpp
new file mode 100644
index 0000000..e906dec
--- /dev/null
+++ b/test/containers/sequences/vector/db_index.pass.cpp
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Index vector out of bounds.
+
+#if _LIBCPP_DEBUG2 >= 1
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+
+#include <vector>
+#include <cassert>
+#include <iterator>
+#include <exception>
+#include <cstdlib>
+
+void f1()
+{
+ std::exit(0);
+}
+
+int main()
+{
+ std::set_terminate(f1);
+ typedef int T;
+ typedef std::vector<T> C;
+ C c(1);
+ assert(c[0] == 0);
+ c.clear();
+ assert(c[0] == 0);
+ assert(false);
+}
+
+#else
+
+int main()
+{
+}
+
+#endif
diff --git a/test/containers/sequences/vector/vector.modifiers/erase_iter_db1.pass.cpp b/test/containers/sequences/vector/vector.modifiers/erase_iter_db1.pass.cpp
new file mode 100644
index 0000000..2966934
--- /dev/null
+++ b/test/containers/sequences/vector/vector.modifiers/erase_iter_db1.pass.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Call erase(const_iterator position) with end()
+
+#if _LIBCPP_DEBUG2 >= 1
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+
+#include <vector>
+#include <cassert>
+#include <cstdlib>
+#include <exception>
+
+void f1()
+{
+ std::exit(0);
+}
+
+int main()
+{
+ std::set_terminate(f1);
+ int a1[] = {1, 2, 3};
+ std::vector<int> l1(a1, a1+3);
+ std::vector<int>::const_iterator i = l1.end();
+ l1.erase(i);
+ assert(false);
+}
+
+#else
+
+int main()
+{
+}
+
+#endif
diff --git a/test/containers/sequences/vector/vector.modifiers/erase_iter_db2.pass.cpp b/test/containers/sequences/vector/vector.modifiers/erase_iter_db2.pass.cpp
new file mode 100644
index 0000000..f67ddb4
--- /dev/null
+++ b/test/containers/sequences/vector/vector.modifiers/erase_iter_db2.pass.cpp
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Call erase(const_iterator position) with iterator from another container
+
+#if _LIBCPP_DEBUG2 >= 1
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+
+#include <vector>
+#include <cassert>
+#include <cstdlib>
+#include <exception>
+
+void f1()
+{
+ std::exit(0);
+}
+
+int main()
+{
+ std::set_terminate(f1);
+ int a1[] = {1, 2, 3};
+ std::vector<int> l1(a1, a1+3);
+ std::vector<int> l2(a1, a1+3);
+ std::vector<int>::const_iterator i = l2.begin();
+ l1.erase(i);
+ assert(false);
+}
+
+#else
+
+int main()
+{
+}
+
+#endif
diff --git a/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db1.pass.cpp b/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db1.pass.cpp
new file mode 100644
index 0000000..11395ba
--- /dev/null
+++ b/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db1.pass.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Call erase(const_iterator first, const_iterator last); with first iterator from another container
+
+#if _LIBCPP_DEBUG2 >= 1
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+
+#include <vector>
+#include <cassert>
+#include <exception>
+#include <cstdlib>
+
+void f1()
+{
+ std::exit(0);
+}
+
+int main()
+{
+ std::set_terminate(f1);
+ int a1[] = {1, 2, 3};
+ std::vector<int> l1(a1, a1+3);
+ std::vector<int> l2(a1, a1+3);
+ std::vector<int>::iterator i = l1.erase(l2.cbegin(), l1.cbegin()+1);
+ assert(false);
+}
+
+#else
+
+int main()
+{
+}
+
+#endif
diff --git a/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db2.pass.cpp b/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db2.pass.cpp
new file mode 100644
index 0000000..202fef4
--- /dev/null
+++ b/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db2.pass.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Call erase(const_iterator first, const_iterator last); with second iterator from another container
+
+#if _LIBCPP_DEBUG2 >= 1
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+
+#include <vector>
+#include <cassert>
+#include <exception>
+#include <cstdlib>
+
+void f1()
+{
+ std::exit(0);
+}
+
+int main()
+{
+ std::set_terminate(f1);
+ int a1[] = {1, 2, 3};
+ std::vector<int> l1(a1, a1+3);
+ std::vector<int> l2(a1, a1+3);
+ std::vector<int>::iterator i = l1.erase(l1.cbegin(), l2.cbegin()+1);
+ assert(false);
+}
+
+#else
+
+int main()
+{
+}
+
+#endif
diff --git a/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db3.pass.cpp b/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db3.pass.cpp
new file mode 100644
index 0000000..6df60bd
--- /dev/null
+++ b/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db3.pass.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Call erase(const_iterator first, const_iterator last); with both iterators from another container
+
+#if _LIBCPP_DEBUG2 >= 1
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+
+#include <vector>
+#include <cassert>
+#include <exception>
+#include <cstdlib>
+
+void f1()
+{
+ std::exit(0);
+}
+
+int main()
+{
+ std::set_terminate(f1);
+ int a1[] = {1, 2, 3};
+ std::vector<int> l1(a1, a1+3);
+ std::vector<int> l2(a1, a1+3);
+ std::vector<int>::iterator i = l1.erase(l2.cbegin(), l2.cbegin()+1);
+ assert(false);
+}
+
+#else
+
+int main()
+{
+}
+
+#endif
diff --git a/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db4.pass.cpp b/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db4.pass.cpp
new file mode 100644
index 0000000..a8bc2be
--- /dev/null
+++ b/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db4.pass.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Call erase(const_iterator first, const_iterator last); with a bad range
+
+#if _LIBCPP_DEBUG2 >= 1
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+
+#include <vector>
+#include <cassert>
+#include <exception>
+#include <cstdlib>
+
+void f1()
+{
+ std::exit(0);
+}
+
+int main()
+{
+ std::set_terminate(f1);
+ int a1[] = {1, 2, 3};
+ std::vector<int> l1(a1, a1+3);
+ std::vector<int>::iterator i = l1.erase(l1.cbegin()+1, l1.cbegin());
+ assert(false);
+}
+
+#else
+
+int main()
+{
+}
+
+#endif
diff --git a/test/containers/sequences/vector/vector.modifiers/pop_back.pass.cpp b/test/containers/sequences/vector/vector.modifiers/pop_back.pass.cpp
new file mode 100644
index 0000000..ac6ee04
--- /dev/null
+++ b/test/containers/sequences/vector/vector.modifiers/pop_back.pass.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// void pop_back();
+
+#if _LIBCPP_DEBUG2 >= 1
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::terminate())
+#endif
+
+#include <vector>
+#include <cassert>
+#include "../../../stack_allocator.h"
+
+#if _LIBCPP_DEBUG2 >= 1
+#include <cstdlib>
+#include <exception>
+
+void f1()
+{
+ std::exit(0);
+}
+#endif
+
+int main()
+{
+#if _LIBCPP_DEBUG2 >= 1
+ std::set_terminate(f1);
+#endif
+ {
+ std::vector<int> c;
+ c.push_back(1);
+ assert(c.size() == 1);
+ c.pop_back();
+ assert(c.size() == 0);
+#if _LIBCPP_DEBUG2 >= 1
+ c.pop_back();
+ assert(false);
+#endif
+ }
+}