1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // REQUIRES: has-unix-headers
10 // UNSUPPORTED: !libcpp-has-legacy-debug-mode, c++03, c++11, c++14
11
12 // test container debugging
13
14 #include <string>
15 #include <vector>
16
17 #include "test_macros.h"
18 #include "check_assertion.h"
19 #include "container_debug_tests.h"
20
21 using namespace IteratorDebugChecks;
22
23 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> StringType;
24
25 template <class Container = StringType, ContainerType CT = CT_String>
26 struct StringContainerChecks : BasicContainerChecks<Container, CT> {
27 using Base = BasicContainerChecks<Container, CT_String>;
28 using value_type = typename Container::value_type;
29 using allocator_type = typename Container::allocator_type;
30 using iterator = typename Container::iterator;
31 using const_iterator = typename Container::const_iterator;
32
33 using Base::makeContainer;
34 using Base::makeValueType;
35
36 public:
runStringContainerChecks37 static void run() {
38 Base::run_iterator_tests();
39 Base::run_allocator_aware_tests();
40
41 for (int N : {3, 128}) {
42 FrontOnEmptyContainer(N);
43 BackOnEmptyContainer(N);
44 PopBack(N);
45 }
46 }
47
48 private:
BackOnEmptyContainerStringContainerChecks49 static void BackOnEmptyContainer(int N) {
50 // testing back on empty
51 Container C = makeContainer(N);
52 Container const& CC = C;
53 iterator it = --C.end();
54 (void)C.back();
55 (void)CC.back();
56 C.pop_back();
57 EXPECT_DEATH( C.erase(it) );
58 C.clear();
59 EXPECT_DEATH( C.back() );
60 EXPECT_DEATH( CC.back() );
61 }
62
FrontOnEmptyContainerStringContainerChecks63 static void FrontOnEmptyContainer(int N) {
64 // testing front on empty
65 Container C = makeContainer(N);
66 Container const& CC = C;
67 (void)C.front();
68 (void)CC.front();
69 C.clear();
70 EXPECT_DEATH( C.front() );
71 EXPECT_DEATH( CC.front() );
72 }
73
PopBackStringContainerChecks74 static void PopBack(int N) {
75 // testing pop_back() invalidation
76 Container C1 = makeContainer(N);
77 iterator it1 = C1.end();
78 --it1;
79 C1.pop_back();
80 EXPECT_DEATH( C1.erase(it1) );
81 C1.erase(C1.begin(), C1.end());
82 assert(C1.size() == 0);
83 TEST_LIBCPP_ASSERT_FAILURE(C1.pop_back(), "string::pop_back(): string is already empty");
84 }
85 };
86
main(int,char **)87 int main(int, char**)
88 {
89 StringContainerChecks<>::run();
90
91 return 0;
92 }
93