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 // <deque>
10
11 // reference operator[](size_type __i);
12 // const_reference operator[](size_type __i) const;
13 //
14 // reference at(size_type __i);
15 // const_reference at(size_type __i) const;
16 //
17 // reference front();
18 // const_reference front() const;
19 //
20 // reference back();
21 // const_reference back() const;
22 // libc++ marks these as 'noexcept'
23
24 #include <deque>
25 #include <cassert>
26
27 #include "min_allocator.h"
28 #include "test_macros.h"
29
30 template <class C>
31 C
make(int size,int start=0)32 make(int size, int start = 0 )
33 {
34 const int b = 4096 / sizeof(int);
35 int init = 0;
36 if (start > 0)
37 {
38 init = (start+1) / b + ((start+1) % b != 0);
39 init *= b;
40 --init;
41 }
42 C c(init, 0);
43 for (int i = 0; i < init-start; ++i)
44 c.pop_back();
45 for (int i = 0; i < size; ++i)
46 c.push_back(i);
47 for (int i = 0; i < start; ++i)
48 c.pop_front();
49 return c;
50 }
51
main(int,char **)52 int main(int, char**)
53 {
54 {
55 typedef std::deque<int> C;
56 C c = make<std::deque<int> >(10);
57 ASSERT_SAME_TYPE(decltype(c[0]), C::reference);
58 LIBCPP_ASSERT_NOEXCEPT( c[0]);
59 LIBCPP_ASSERT_NOEXCEPT( c.front());
60 ASSERT_SAME_TYPE(decltype(c.front()), C::reference);
61 LIBCPP_ASSERT_NOEXCEPT( c.back());
62 ASSERT_SAME_TYPE(decltype(c.back()), C::reference);
63 for (int i = 0; i < 10; ++i)
64 assert(c[i] == i);
65 for (int i = 0; i < 10; ++i)
66 assert(c.at(i) == i);
67 assert(c.front() == 0);
68 assert(c.back() == 9);
69 }
70 {
71 typedef std::deque<int> C;
72 const C c = make<std::deque<int> >(10);
73 ASSERT_SAME_TYPE(decltype(c[0]), C::const_reference);
74 LIBCPP_ASSERT_NOEXCEPT( c[0]);
75 LIBCPP_ASSERT_NOEXCEPT( c.front());
76 ASSERT_SAME_TYPE(decltype(c.front()), C::const_reference);
77 LIBCPP_ASSERT_NOEXCEPT( c.back());
78 ASSERT_SAME_TYPE(decltype(c.back()), C::const_reference);
79 for (int i = 0; i < 10; ++i)
80 assert(c[i] == i);
81 for (int i = 0; i < 10; ++i)
82 assert(c.at(i) == i);
83 assert(c.front() == 0);
84 assert(c.back() == 9);
85 }
86 #if TEST_STD_VER >= 11
87 {
88 typedef std::deque<int, min_allocator<int>> C;
89 C c = make<std::deque<int, min_allocator<int>> >(10);
90 ASSERT_SAME_TYPE(decltype(c[0]), C::reference);
91 LIBCPP_ASSERT_NOEXCEPT( c[0]);
92 LIBCPP_ASSERT_NOEXCEPT( c.front());
93 ASSERT_SAME_TYPE(decltype(c.front()), C::reference);
94 LIBCPP_ASSERT_NOEXCEPT( c.back());
95 ASSERT_SAME_TYPE(decltype(c.back()), C::reference);
96 for (int i = 0; i < 10; ++i)
97 assert(c[i] == i);
98 for (int i = 0; i < 10; ++i)
99 assert(c.at(i) == i);
100 assert(c.front() == 0);
101 assert(c.back() == 9);
102 }
103 {
104 typedef std::deque<int, min_allocator<int>> C;
105 const C c = make<std::deque<int, min_allocator<int>> >(10);
106 ASSERT_SAME_TYPE(decltype(c[0]), C::const_reference);
107 LIBCPP_ASSERT_NOEXCEPT( c[0]);
108 LIBCPP_ASSERT_NOEXCEPT( c.front());
109 ASSERT_SAME_TYPE(decltype(c.front()), C::const_reference);
110 LIBCPP_ASSERT_NOEXCEPT( c.back());
111 ASSERT_SAME_TYPE(decltype(c.back()), C::const_reference);
112 for (int i = 0; i < 10; ++i)
113 assert(c[i] == i);
114 for (int i = 0; i < 10; ++i)
115 assert(c.at(i) == i);
116 assert(c.front() == 0);
117 assert(c.back() == 9);
118 }
119 #endif
120
121 return 0;
122 }
123