aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/23_containers/vector/capacity/max_size.cc
blob: bde4c10ac92643a60df77acc5708ab271ac2cf4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (C) 2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-do run }

#include <vector>
#include <stdexcept>
#include <limits>
#include <testsuite_hooks.h>

typedef std::vector<char> test_type;

typedef test_type::size_type size_type;
typedef test_type::difference_type difference_type;

const difference_type diffmax = std::numeric_limits<difference_type>::max();

void
test01()
{
  test_type v;
  VERIFY( v.max_size() <= diffmax );
}

void
test02()
{
  size_type n = size_type(diffmax) + 1;
  VERIFY( n > test_type().max_size() );

  try {
    test_type v(n);
    VERIFY( false );
  } catch (const std::length_error&) { }

  try {
    test_type v(n, 'x');
    VERIFY( false );
  } catch (const std::length_error&) { }

  try {
    test_type v(n, 'x', test_type::allocator_type());
    VERIFY( false );
  } catch (const std::length_error&) { }
}

#ifdef __GLIBCXX_TYPE_INT_N_0
template<typename T, typename U, bool = (sizeof(T) > sizeof(long long))>
  struct Base_
  {
    typedef T difference_type;
    typedef U size_type;
  };

template<typename T, typename U>
  struct Base_<T, U, false>
  {
    typedef long long difference_type;
    typedef unsigned long long size_type;
  };

typedef Base_<__GLIBCXX_TYPE_INT_N_0, unsigned __GLIBCXX_TYPE_INT_N_0> Base;
#else
struct Base
{
  typedef long long difference_type;
  typedef unsigned long long size_type;
};
#endif

// An iterator with a difference_type larger than ptrdiff_t
struct Iter : Base
{
  typedef std::random_access_iterator_tag iterator_category;
  typedef char value_type;
  typedef const char* pointer;
  typedef const char& reference;
  using Base::difference_type;

  Iter() : n(0) { }
  Iter(size_type n) : n(n) { }

  reference operator*() const { return value; }
  pointer operator->() const { return &value; }

  Iter& operator++() { ++n; return *this; }
  Iter operator++(int) { Iter tmp(*this); ++n; return tmp; }
  Iter& operator--() { --n; return *this; }
  Iter operator--(int) { Iter tmp(*this); --n; return tmp; }

  Iter& operator+=(difference_type d) { n += d; return *this; }
  Iter& operator-=(difference_type d) { n -= d; return *this; }

  difference_type operator-(const Iter& rhs) const { return n - rhs.n; }

  reference operator[](difference_type d) const { return value; }

  bool operator==(const Iter& rhs) const { return n == rhs.n; }
  bool operator!=(const Iter& rhs) const { return n != rhs.n; }
  bool operator<(const Iter& rhs) const { return n < rhs.n; }
  bool operator>(const Iter& rhs) const { return n > rhs.n; }
  bool operator<=(const Iter& rhs) const { return n <= rhs.n; }
  bool operator>=(const Iter& rhs) const { return n >= rhs.n; }

private:
  size_type n;
  static const char value = 'x';
};

const char Iter::value;

Iter operator+(Iter i, Iter::difference_type n) { return i += n; }
Iter operator+(Iter::difference_type n, Iter i) { return i += n; }
Iter operator-(Iter::difference_type n, Iter i) { return i -= n; }

void
test03()
{
  Iter first, last(Iter::size_type(diffmax) + 1);
  VERIFY( std::distance(first, last) > test_type().max_size() );

  try {
    test_type vec(first, last);
    VERIFY(false);
  } catch (const std::length_error&) { }
}

int
main()
{
  test01();
  test02();
  test03();
}