[libcxx] r177929 - Added debug tests for indexing, pop_back and both forms of erase. Added an improved error message for erasing a single element with end().

Howard Hinnant hhinnant at apple.com
Mon Mar 25 15:12:26 PDT 2013


Author: hhinnant
Date: Mon Mar 25 17:12:26 2013
New Revision: 177929

URL: http://llvm.org/viewvc/llvm-project?rev=177929&view=rev
Log:
Added debug tests for indexing, pop_back and both forms of erase.  Added an improved error message for erasing a single element with end().

Added:
    libcxx/trunk/test/containers/sequences/vector/db_cindex.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/db_index.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db1.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db2.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db1.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db2.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db3.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db4.pass.cpp
    libcxx/trunk/test/containers/sequences/vector/vector.modifiers/pop_back.pass.cpp
Modified:
    libcxx/trunk/include/vector

Modified: libcxx/trunk/include/vector
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/vector?rev=177929&r1=177928&r2=177929&view=diff
==============================================================================
--- libcxx/trunk/include/vector (original)
+++ libcxx/trunk/include/vector Mon Mar 25 17:12:26 2013
@@ -1550,6 +1550,8 @@ vector<_Tp, _Allocator>::erase(const_ite
         "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));

Added: libcxx/trunk/test/containers/sequences/vector/db_cindex.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/db_cindex.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/db_cindex.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/db_cindex.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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

Added: libcxx/trunk/test/containers/sequences/vector/db_index.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/db_index.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/db_index.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/db_index.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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

Added: libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db1.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db1.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db1.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db1.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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

Added: libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db2.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db2.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db2.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_db2.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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

Added: libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db1.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db1.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db1.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db1.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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

Added: libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db2.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db2.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db2.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db2.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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

Added: libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db3.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db3.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db3.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db3.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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

Added: libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db4.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db4.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db4.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter_db4.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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

Added: libcxx/trunk/test/containers/sequences/vector/vector.modifiers/pop_back.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.modifiers/pop_back.pass.cpp?rev=177929&view=auto
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.modifiers/pop_back.pass.cpp (added)
+++ libcxx/trunk/test/containers/sequences/vector/vector.modifiers/pop_back.pass.cpp Mon Mar 25 17:12:26 2013
@@ -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        
+    }
+}





More information about the cfe-commits mailing list