PR libstdc++/78448 limit vector::max_size and deque::max_size
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / deque / capacity / max_size.cc
blob3dabdd055447e10ec1142ef4ec929dfa7be17db9
1 // Copyright (C) 2018 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run }
20 #include <deque>
21 #include <stdexcept>
22 #include <limits>
23 #include <testsuite_hooks.h>
25 using test_type = std::deque<char>;
27 typedef test_type::size_type size_type;
28 typedef test_type::difference_type difference_type;
30 const difference_type diffmax = std::numeric_limits<difference_type>::max();
32 void
33 test01()
35 test_type v;
36 VERIFY( v.max_size() <= diffmax );
39 void
40 test02()
42 size_type n = size_type(diffmax) + 1;
43 VERIFY( n > test_type().max_size() );
45 try {
46 test_type v(n);
47 VERIFY( false );
48 } catch (const std::length_error&) { }
50 try {
51 test_type v(n, 'x');
52 VERIFY( false );
53 } catch (const std::length_error&) { }
55 try {
56 test_type v(n, 'x', test_type::allocator_type());
57 VERIFY( false );
58 } catch (const std::length_error&) { }
61 #ifdef __GLIBCXX_TYPE_INT_N_0
62 template<typename T, typename U, bool = (sizeof(T) > sizeof(long long))>
63 struct Base_
65 typedef T difference_type;
66 typedef U size_type;
69 template<typename T, typename U>
70 struct Base_<T, U, false>
72 typedef long long difference_type;
73 typedef unsigned long long size_type;
76 typedef Base_<__GLIBCXX_TYPE_INT_N_0, unsigned __GLIBCXX_TYPE_INT_N_0> Base;
77 #else
78 struct Base
80 typedef long long difference_type;
81 typedef unsigned long long size_type;
83 #endif
85 // An iterator with a difference_type larger than ptrdiff_t
86 struct Iter : Base
88 typedef std::random_access_iterator_tag iterator_category;
89 typedef char value_type;
90 typedef const char* pointer;
91 typedef const char& reference;
92 using Base::difference_type;
94 Iter() : n(0) { }
95 Iter(size_type n) : n(n) { }
97 reference operator*() const { return value; }
98 pointer operator->() const { return &value; }
100 Iter& operator++() { ++n; return *this; }
101 Iter operator++(int) { Iter tmp(*this); ++n; return tmp; }
102 Iter& operator--() { --n; return *this; }
103 Iter operator--(int) { Iter tmp(*this); --n; return tmp; }
105 Iter& operator+=(difference_type d) { n += d; return *this; }
106 Iter& operator-=(difference_type d) { n -= d; return *this; }
108 difference_type operator-(const Iter& rhs) const { return n - rhs.n; }
110 reference operator[](difference_type d) const { return value; }
112 bool operator==(const Iter& rhs) const { return n == rhs.n; }
113 bool operator!=(const Iter& rhs) const { return n != rhs.n; }
114 bool operator<(const Iter& rhs) const { return n < rhs.n; }
115 bool operator>(const Iter& rhs) const { return n > rhs.n; }
116 bool operator<=(const Iter& rhs) const { return n <= rhs.n; }
117 bool operator>=(const Iter& rhs) const { return n >= rhs.n; }
119 private:
120 size_type n;
121 static const char value = 'x';
124 Iter operator+(Iter i, Iter::difference_type n) { return i += n; }
125 Iter operator+(Iter::difference_type n, Iter i) { return i += n; }
126 Iter operator-(Iter::difference_type n, Iter i) { return i -= n; }
128 void
129 test03()
131 Iter first, last(Iter::size_type(diffmax) + 1);
132 VERIFY( std::distance(first, last) > test_type().max_size() );
134 try {
135 test_type vec(first, last);
136 VERIFY(false);
137 } catch (const std::length_error&) { }
141 main()
143 test01();
144 test02();
145 test03();