Home | History | Annotate | Download | only in containers
      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 // UNSUPPORTED: c++98, c++03, c++11, c++14
     11 // UNSUPPORTED: libcpp-no-exceptions, libcpp-no-if-constexpr
     12 // MODULES_DEFINES: _LIBCPP_DEBUG=1
     13 // MODULES_DEFINES: _LIBCPP_DEBUG_USE_EXCEPTIONS
     14 
     15 // Can't test the system lib because this test enables debug mode
     16 // UNSUPPORTED: with_system_cxx_lib
     17 
     18 // test container debugging
     19 
     20 #define _LIBCPP_DEBUG 1
     21 #define _LIBCPP_DEBUG_USE_EXCEPTIONS
     22 #include <string>
     23 #include <vector>
     24 
     25 #include "test_macros.h"
     26 #include "debug_mode_helper.h"
     27 
     28 using namespace IteratorDebugChecks;
     29 
     30 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>>  StringType;
     31 
     32 template <class Container = StringType, ContainerType CT = CT_String>
     33 struct StringContainerChecks : BasicContainerChecks<Container, CT> {
     34   using Base = BasicContainerChecks<Container, CT_String>;
     35   using value_type = typename Container::value_type;
     36   using allocator_type = typename Container::allocator_type;
     37   using iterator = typename Container::iterator;
     38   using const_iterator = typename Container::const_iterator;
     39 
     40   using Base::makeContainer;
     41   using Base::makeValueType;
     42 
     43 public:
     44   static void run() {
     45     Base::run_iterator_tests();
     46     Base::run_allocator_aware_tests();
     47     try {
     48       for (int N : {3, 128}) {
     49         FrontOnEmptyContainer(N);
     50         BackOnEmptyContainer(N);
     51         PopBack(N);
     52       }
     53     } catch (...) {
     54       assert(false && "uncaught debug exception");
     55     }
     56   }
     57 
     58 private:
     59   static void BackOnEmptyContainer(int N) {
     60     CHECKPOINT("testing back on empty");
     61     Container C = makeContainer(N);
     62     Container const& CC = C;
     63     iterator it = --C.end();
     64     (void)C.back();
     65     (void)CC.back();
     66     C.pop_back();
     67     CHECK_DEBUG_THROWS( C.erase(it) );
     68     C.clear();
     69     CHECK_DEBUG_THROWS( C.back() );
     70     CHECK_DEBUG_THROWS( CC.back() );
     71   }
     72 
     73   static void FrontOnEmptyContainer(int N) {
     74     CHECKPOINT("testing front on empty");
     75     Container C = makeContainer(N);
     76     Container const& CC = C;
     77     (void)C.front();
     78     (void)CC.front();
     79     C.clear();
     80     CHECK_DEBUG_THROWS( C.front() );
     81     CHECK_DEBUG_THROWS( CC.front() );
     82   }
     83 
     84   static void PopBack(int N) {
     85     CHECKPOINT("testing pop_back() invalidation");
     86     Container C1 = makeContainer(N);
     87     iterator it1 = C1.end();
     88     --it1;
     89     C1.pop_back();
     90     CHECK_DEBUG_THROWS( C1.erase(it1) );
     91     C1.erase(C1.begin(), C1.end());
     92     assert(C1.size() == 0);
     93     CHECK_DEBUG_THROWS( C1.pop_back() );
     94   }
     95 };
     96 
     97 int main()
     98 {
     99   StringContainerChecks<>::run();
    100 }
    101