From 55b31b4e6934fb2e9dda6d7f0f2792b6c3420c05 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Wed, 23 Nov 2016 01:18:56 +0000 Subject: [libcxx] Fix max_size() across all containers Summary: The `max_size()` method of containers should respect both the allocator's reported `max_size` and the range of the `difference_type`. This patch makes all containers choose the smallest of those two values. Reviewers: mclow.lists, EricWF Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D26885 llvm-svn: 287729 --- .../associative/map/map.access/max_size.pass.cpp | 38 ++++++++++++----- .../associative/multimap/max_size.pass.cpp | 38 ++++++++++++----- .../associative/multiset/max_size.pass.cpp | 35 +++++++++++----- .../containers/associative/set/max_size.pass.cpp | 35 +++++++++++----- .../deque/deque.capacity/max_size.pass.cpp | 47 +++++++++++++++++++++ .../sequences/forwardlist/max_size.pass.cpp | 37 +++++++++++------ .../sequences/list/list.capacity/max_size.pass.cpp | 47 +++++++++++++++++++++ .../vector/vector.capacity/max_size.pass.cpp | 48 ++++++++++++++++++++++ .../containers/unord/unord.map/max_size.pass.cpp | 41 ++++++++++++------ .../unord/unord.multimap/max_size.pass.cpp | 43 +++++++++++++------ .../unord/unord.multiset/max_size.pass.cpp | 40 +++++++++++++----- .../containers/unord/unord.set/max_size.pass.cpp | 36 +++++++++++----- libcxx/test/support/test_allocator.h | 6 +++ 13 files changed, 391 insertions(+), 100 deletions(-) create mode 100644 libcxx/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp create mode 100644 libcxx/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp create mode 100644 libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp (limited to 'libcxx/test') diff --git a/libcxx/test/std/containers/associative/map/map.access/max_size.pass.cpp b/libcxx/test/std/containers/associative/map/map.access/max_size.pass.cpp index c67d8b1f674..82a817a1f4a 100644 --- a/libcxx/test/std/containers/associative/map/map.access/max_size.pass.cpp +++ b/libcxx/test/std/containers/associative/map/map.access/max_size.pass.cpp @@ -13,23 +13,39 @@ // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { - { - typedef std::map M; - M m; - assert(m.max_size() != 0); + typedef std::pair KV; + { + typedef limited_allocator A; + typedef std::map, A> C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::map, A> C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - typedef std::map, min_allocator>> M; - M m; - assert(m.max_size() != 0); + typedef std::map C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/std/containers/associative/multimap/max_size.pass.cpp b/libcxx/test/std/containers/associative/multimap/max_size.pass.cpp index b7cf226d8f1..8d5ec1148a2 100644 --- a/libcxx/test/std/containers/associative/multimap/max_size.pass.cpp +++ b/libcxx/test/std/containers/associative/multimap/max_size.pass.cpp @@ -13,23 +13,39 @@ // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { - { - typedef std::multimap M; - M m; - assert(m.max_size() != 0); + typedef std::pair KV; + { + typedef limited_allocator A; + typedef std::multimap, A> C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::multimap, A> C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - typedef std::multimap, min_allocator>> M; - M m; - assert(m.max_size() != 0); + typedef std::multimap C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/std/containers/associative/multiset/max_size.pass.cpp b/libcxx/test/std/containers/associative/multiset/max_size.pass.cpp index 79492c9b1c4..8ca34ba8273 100644 --- a/libcxx/test/std/containers/associative/multiset/max_size.pass.cpp +++ b/libcxx/test/std/containers/associative/multiset/max_size.pass.cpp @@ -13,23 +13,38 @@ // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { { - typedef std::multiset M; - M m; - assert(m.max_size() != 0); + typedef limited_allocator A; + typedef std::multiset, A> C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::multiset, A> C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - typedef std::multiset, min_allocator> M; - M m; - assert(m.max_size() != 0); + typedef std::multiset C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/std/containers/associative/set/max_size.pass.cpp b/libcxx/test/std/containers/associative/set/max_size.pass.cpp index 9df6a4157e1..c894eb51b1e 100644 --- a/libcxx/test/std/containers/associative/set/max_size.pass.cpp +++ b/libcxx/test/std/containers/associative/set/max_size.pass.cpp @@ -13,23 +13,38 @@ // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { { - typedef std::set M; - M m; - assert(m.max_size() != 0); + typedef limited_allocator A; + typedef std::set, A> C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::set, A> C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - typedef std::set, min_allocator> M; - M m; - assert(m.max_size() != 0); + typedef std::set C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp b/libcxx/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp new file mode 100644 index 00000000000..11ce9d2f689 --- /dev/null +++ b/libcxx/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// size_type max_size() const; + +#include +#include +#include +#include + +#include "test_allocator.h" +#include "test_macros.h" + +int main() { + { + typedef limited_allocator A; + typedef std::deque C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::deque C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); + } + { + typedef std::deque C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); + } +} diff --git a/libcxx/test/std/containers/sequences/forwardlist/max_size.pass.cpp b/libcxx/test/std/containers/sequences/forwardlist/max_size.pass.cpp index a7f39bf90fb..916d12a9f67 100644 --- a/libcxx/test/std/containers/sequences/forwardlist/max_size.pass.cpp +++ b/libcxx/test/std/containers/sequences/forwardlist/max_size.pass.cpp @@ -11,25 +11,38 @@ // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { { - typedef int T; - typedef std::forward_list C; - C c; - assert(c.max_size() > 0); + typedef limited_allocator A; + typedef std::forward_list C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::forward_list C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - typedef int T; - typedef std::forward_list> C; - C c; - assert(c.max_size() > 0); + typedef std::forward_list C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp b/libcxx/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp new file mode 100644 index 00000000000..bd1b65e63d7 --- /dev/null +++ b/libcxx/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// size_type max_size() const noexcept + +#include +#include +#include +#include + +#include "test_allocator.h" +#include "test_macros.h" + +int main() { + { + typedef limited_allocator A; + typedef std::list C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::list C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); + } + { + typedef std::list C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); + } +} diff --git a/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp new file mode 100644 index 00000000000..5f7a6268d55 --- /dev/null +++ b/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp @@ -0,0 +1,48 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// size_type max_size() const; + +#include +#include +#include +#include + +#include "test_allocator.h" +#include "test_macros.h" + + +int main() { + { + typedef limited_allocator A; + typedef std::vector C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::vector C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); + } + { + typedef std::vector C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); + } +} diff --git a/libcxx/test/std/containers/unord/unord.map/max_size.pass.cpp b/libcxx/test/std/containers/unord/unord.map/max_size.pass.cpp index 9c1ca18c305..15274198146 100644 --- a/libcxx/test/std/containers/unord/unord.map/max_size.pass.cpp +++ b/libcxx/test/std/containers/unord/unord.map/max_size.pass.cpp @@ -9,28 +9,45 @@ // -// template , class Pred = equal_to, -// class Alloc = allocator>> // class unordered_map // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { - { - std::unordered_map u; - assert(u.max_size() > 0); + typedef std::pair KV; + { + typedef limited_allocator A; + typedef std::unordered_map, std::equal_to, A> + C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::unordered_map, std::equal_to, A> + C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - std::unordered_map, std::equal_to, - min_allocator>> u; - assert(u.max_size() > 0); + typedef std::unordered_map C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/std/containers/unord/unord.multimap/max_size.pass.cpp b/libcxx/test/std/containers/unord/unord.multimap/max_size.pass.cpp index 3bf1f1496f6..5b58bac385a 100644 --- a/libcxx/test/std/containers/unord/unord.multimap/max_size.pass.cpp +++ b/libcxx/test/std/containers/unord/unord.multimap/max_size.pass.cpp @@ -9,28 +9,47 @@ // -// template , class Pred = equal_to, -// class Alloc = allocator>> // class unordered_multimap // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { - { - std::unordered_multimap u; - assert(u.max_size() > 0); + typedef std::pair KV; + { + typedef limited_allocator A; + typedef std::unordered_multimap, + std::equal_to, A> + C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::unordered_multimap, + std::equal_to, A> + C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - std::unordered_multimap, std::equal_to, - min_allocator>> u; - assert(u.max_size() > 0); + typedef std::unordered_multimap C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/std/containers/unord/unord.multiset/max_size.pass.cpp b/libcxx/test/std/containers/unord/unord.multiset/max_size.pass.cpp index b26ad73fed2..eac4db8b0a9 100644 --- a/libcxx/test/std/containers/unord/unord.multiset/max_size.pass.cpp +++ b/libcxx/test/std/containers/unord/unord.multiset/max_size.pass.cpp @@ -9,28 +9,46 @@ // -// template , class Pred = equal_to, -// class Alloc = allocator> // class unordered_multiset // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { { - std::unordered_multiset u; - assert(u.max_size() > 0); + typedef limited_allocator A; + typedef std::unordered_multiset, std::equal_to, + A> + C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::unordered_multiset, std::equal_to, + A> + C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - std::unordered_multiset, - std::equal_to, min_allocator> u; - assert(u.max_size() > 0); + typedef std::unordered_multiset C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/std/containers/unord/unord.set/max_size.pass.cpp b/libcxx/test/std/containers/unord/unord.set/max_size.pass.cpp index 3135ad99473..1b902660d48 100644 --- a/libcxx/test/std/containers/unord/unord.set/max_size.pass.cpp +++ b/libcxx/test/std/containers/unord/unord.set/max_size.pass.cpp @@ -9,28 +9,42 @@ // -// template , class Pred = equal_to, -// class Alloc = allocator> // class unordered_set // size_type max_size() const; -#include #include +#include +#include +#include -#include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" int main() { { - std::unordered_set u; - assert(u.max_size() > 0); + typedef limited_allocator A; + typedef std::unordered_set, std::equal_to, A> C; + C c; + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + typedef limited_allocator A; + typedef std::unordered_set, std::equal_to, A> C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); } -#if TEST_STD_VER >= 11 { - std::unordered_set, - std::equal_to, min_allocator> u; - assert(u.max_size() > 0); + typedef std::unordered_set C; + const C::difference_type max_dist = + std::numeric_limits::max(); + C c; + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(c.get_allocator())); } -#endif } diff --git a/libcxx/test/support/test_allocator.h b/libcxx/test/support/test_allocator.h index 4e93ca5a918..7ce60025f68 100644 --- a/libcxx/test/support/test_allocator.h +++ b/libcxx/test/support/test_allocator.h @@ -20,6 +20,12 @@ #include "test_macros.h" +template +inline size_t alloc_max_size(Alloc const &a) { + typedef std::allocator_traits AT; + return AT::max_size(a); +} + class test_alloc_base { protected: -- cgit v1.2.3