• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // test container debugging
16 
17 #define _LIBCPP_DEBUG 1
18 #define _LIBCPP_DEBUG_USE_EXCEPTIONS
19 #include <string>
20 #include <vector>
21 
22 #include "test_macros.h"
23 #include "debug_mode_helper.h"
24 
25 using namespace IteratorDebugChecks;
26 
27 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>>  StringType;
28 
29 template <class Container = StringType, ContainerType CT = CT_String>
30 struct StringContainerChecks : BasicContainerChecks<Container, CT> {
31   using Base = BasicContainerChecks<Container, CT_String>;
32   using value_type = typename Container::value_type;
33   using allocator_type = typename Container::allocator_type;
34   using iterator = typename Container::iterator;
35   using const_iterator = typename Container::const_iterator;
36 
37   using Base::makeContainer;
38   using Base::makeValueType;
39 
40 public:
runStringContainerChecks41   static void run() {
42     Base::run_iterator_tests();
43     Base::run_allocator_aware_tests();
44     try {
45       for (int N : {3, 128}) {
46         FrontOnEmptyContainer(N);
47         BackOnEmptyContainer(N);
48         PopBack(N);
49       }
50     } catch (...) {
51       assert(false && "uncaught debug exception");
52     }
53   }
54 
55 private:
BackOnEmptyContainerStringContainerChecks56   static void BackOnEmptyContainer(int N) {
57     CHECKPOINT("testing back on empty");
58     Container C = makeContainer(N);
59     Container const& CC = C;
60     iterator it = --C.end();
61     (void)C.back();
62     (void)CC.back();
63     C.pop_back();
64     CHECK_DEBUG_THROWS( C.erase(it) );
65     C.clear();
66     CHECK_DEBUG_THROWS( C.back() );
67     CHECK_DEBUG_THROWS( CC.back() );
68   }
69 
FrontOnEmptyContainerStringContainerChecks70   static void FrontOnEmptyContainer(int N) {
71     CHECKPOINT("testing front on empty");
72     Container C = makeContainer(N);
73     Container const& CC = C;
74     (void)C.front();
75     (void)CC.front();
76     C.clear();
77     CHECK_DEBUG_THROWS( C.front() );
78     CHECK_DEBUG_THROWS( CC.front() );
79   }
80 
PopBackStringContainerChecks81   static void PopBack(int N) {
82     CHECKPOINT("testing pop_back() invalidation");
83     Container C1 = makeContainer(N);
84     iterator it1 = C1.end();
85     --it1;
86     C1.pop_back();
87     CHECK_DEBUG_THROWS( C1.erase(it1) );
88     C1.erase(C1.begin(), C1.end());
89     assert(C1.size() == 0);
90     CHECK_DEBUG_THROWS( C1.pop_back() );
91   }
92 };
93 
main()94 int main()
95 {
96   StringContainerChecks<>::run();
97 }
98