[libcxx] r287729 - [libcxx] Fix max_size() across all containers

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 22 17:18:57 PST 2016


Author: ericwf
Date: Tue Nov 22 19:18:56 2016
New Revision: 287729

URL: http://llvm.org/viewvc/llvm-project?rev=287729&view=rev
Log:
[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

Added:
    libcxx/trunk/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp
    libcxx/trunk/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp
    libcxx/trunk/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp
Modified:
    libcxx/trunk/.clang-format
    libcxx/trunk/include/__hash_table
    libcxx/trunk/include/__tree
    libcxx/trunk/include/deque
    libcxx/trunk/include/forward_list
    libcxx/trunk/include/list
    libcxx/trunk/include/vector
    libcxx/trunk/test/std/containers/associative/map/map.access/max_size.pass.cpp
    libcxx/trunk/test/std/containers/associative/multimap/max_size.pass.cpp
    libcxx/trunk/test/std/containers/associative/multiset/max_size.pass.cpp
    libcxx/trunk/test/std/containers/associative/set/max_size.pass.cpp
    libcxx/trunk/test/std/containers/sequences/forwardlist/max_size.pass.cpp
    libcxx/trunk/test/std/containers/unord/unord.map/max_size.pass.cpp
    libcxx/trunk/test/std/containers/unord/unord.multimap/max_size.pass.cpp
    libcxx/trunk/test/std/containers/unord/unord.multiset/max_size.pass.cpp
    libcxx/trunk/test/std/containers/unord/unord.set/max_size.pass.cpp
    libcxx/trunk/test/support/test_allocator.h

Modified: libcxx/trunk/.clang-format
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/.clang-format?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/.clang-format (original)
+++ libcxx/trunk/.clang-format Tue Nov 22 19:18:56 2016
@@ -1 +1,7 @@
 BasedOnStyle: LLVM
+
+---
+Language: Cpp
+
+AlwaysBreakTemplateDeclarations: true
+---

Modified: libcxx/trunk/include/__hash_table
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__hash_table?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/include/__hash_table (original)
+++ libcxx/trunk/include/__hash_table Tue Nov 22 19:18:56 2016
@@ -1004,8 +1004,11 @@ public:
     _LIBCPP_INLINE_VISIBILITY
     size_type max_size() const _NOEXCEPT
     {
-        return allocator_traits<__pointer_allocator>::max_size(
-            __bucket_list_.get_deleter().__alloc());
+        return std::min<size_type>(
+            allocator_traits<__pointer_allocator>::max_size(
+              __bucket_list_.get_deleter().__alloc()),
+            numeric_limits<difference_type >::max()
+        );
     }
 
     pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
@@ -1192,7 +1195,7 @@ public:
 
     _LIBCPP_INLINE_VISIBILITY
     size_type max_bucket_count() const _NOEXCEPT
-        {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());}
+        {return max_size(); }
     size_type bucket_size(size_type __n) const;
     _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
     {

Modified: libcxx/trunk/include/__tree
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__tree?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/include/__tree (original)
+++ libcxx/trunk/include/__tree Tue Nov 22 19:18:56 2016
@@ -1106,7 +1106,9 @@ public:
 
     _LIBCPP_INLINE_VISIBILITY
     size_type max_size() const _NOEXCEPT
-        {return __node_traits::max_size(__node_alloc());}
+        {return std::min<size_type>(
+                __node_traits::max_size(__node_alloc()),
+                numeric_limits<difference_type >::max());}
 
     void clear() _NOEXCEPT;
 

Modified: libcxx/trunk/include/deque
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/deque?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/include/deque (original)
+++ libcxx/trunk/include/deque Tue Nov 22 19:18:56 2016
@@ -1310,7 +1310,9 @@ public:
     size_type size() const _NOEXCEPT {return __base::size();}
     _LIBCPP_INLINE_VISIBILITY
     size_type max_size() const _NOEXCEPT
-        {return __alloc_traits::max_size(__base::__alloc());}
+        {return std::min<size_type>(
+            __alloc_traits::max_size(__base::__alloc()),
+            numeric_limits<difference_type>::max());}
     void resize(size_type __n);
     void resize(size_type __n, const value_type& __v);
     void shrink_to_fit() _NOEXCEPT;

Modified: libcxx/trunk/include/forward_list
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/forward_list?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/include/forward_list (original)
+++ libcxx/trunk/include/forward_list Tue Nov 22 19:18:56 2016
@@ -734,8 +734,11 @@ public:
     bool empty() const _NOEXCEPT
         {return base::__before_begin()->__next_ == nullptr;}
     _LIBCPP_INLINE_VISIBILITY
-    size_type max_size() const _NOEXCEPT
-        {return numeric_limits<size_type>::max();}
+    size_type max_size() const _NOEXCEPT {
+        return std::min<size_type>(
+            __node_traits::max_size(base::__alloc()),
+            numeric_limits<difference_type>::max());
+    }
 
     _LIBCPP_INLINE_VISIBILITY
     reference       front()       {return base::__before_begin()->__next_->__value_;}

Modified: libcxx/trunk/include/list
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/list?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/include/list (original)
+++ libcxx/trunk/include/list Tue Nov 22 19:18:56 2016
@@ -571,6 +571,10 @@ protected:
         {return __size_alloc_.second();}
 
     _LIBCPP_INLINE_VISIBILITY
+    size_type __node_alloc_max_size() const _NOEXCEPT {
+        return __node_alloc_traits::max_size(__node_alloc());
+    }
+    _LIBCPP_INLINE_VISIBILITY
     static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT;
 
     _LIBCPP_INLINE_VISIBILITY
@@ -904,7 +908,11 @@ public:
     bool empty() const _NOEXCEPT         {return base::empty();}
     _LIBCPP_INLINE_VISIBILITY
     size_type max_size() const _NOEXCEPT
-        {return numeric_limits<difference_type>::max();}
+        {
+            return std::min<size_type>(
+                base::__node_alloc_max_size(),
+                numeric_limits<difference_type >::max());
+        }
 
     _LIBCPP_INLINE_VISIBILITY
           iterator begin() _NOEXCEPT        {return base::begin();}

Modified: libcxx/trunk/include/vector
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/vector?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/include/vector (original)
+++ libcxx/trunk/include/vector Tue Nov 22 19:18:56 2016
@@ -941,7 +941,8 @@ template <class _Tp, class _Allocator>
 typename vector<_Tp, _Allocator>::size_type
 vector<_Tp, _Allocator>::max_size() const _NOEXCEPT
 {
-    return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2);  // end() >= begin(), always
+    return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()),
+                                 numeric_limits<difference_type>::max());
 }
 
 //  Precondition:  __new_size > capacity()

Modified: libcxx/trunk/test/std/containers/associative/map/map.access/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/associative/map/map.access/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/associative/map/map.access/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/associative/map/map.access/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -13,23 +13,39 @@
 
 // size_type max_size() const;
 
-#include <map>
 #include <cassert>
+#include <limits>
+#include <map>
+#include <type_traits>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
-    {
-    typedef std::map<int, double> M;
-    M m;
-    assert(m.max_size() != 0);
+  typedef std::pair<const int, int> KV;
+  {
+    typedef limited_allocator<KV, 10> A;
+    typedef std::map<int, int, std::less<int>, A> C;
+    C c;
+    assert(c.max_size() <= 10);
+    LIBCPP_ASSERT(c.max_size() == 10);
+  }
+  {
+    typedef limited_allocator<KV, (size_t)-1> A;
+    typedef std::map<int, int, std::less<int>, A> C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    LIBCPP_ASSERT(c.max_size() == max_dist);
     }
-#if TEST_STD_VER >= 11
     {
-    typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
-    M m;
-    assert(m.max_size() != 0);
+      typedef std::map<char, int> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Modified: libcxx/trunk/test/std/containers/associative/multimap/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/associative/multimap/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/associative/multimap/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/associative/multimap/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -13,23 +13,39 @@
 
 // size_type max_size() const;
 
-#include <map>
 #include <cassert>
+#include <limits>
+#include <map>
+#include <type_traits>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
-    {
-    typedef std::multimap<int, double> M;
-    M m;
-    assert(m.max_size() != 0);
+  typedef std::pair<const int, int> KV;
+  {
+    typedef limited_allocator<KV, 10> A;
+    typedef std::multimap<int, int, std::less<int>, A> C;
+    C c;
+    assert(c.max_size() <= 10);
+    LIBCPP_ASSERT(c.max_size() == 10);
+  }
+  {
+    typedef limited_allocator<KV, (size_t)-1> A;
+    typedef std::multimap<int, int, std::less<int>, A> C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    LIBCPP_ASSERT(c.max_size() == max_dist);
     }
-#if TEST_STD_VER >= 11
     {
-    typedef std::multimap<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
-    M m;
-    assert(m.max_size() != 0);
+      typedef std::multimap<char, int> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Modified: libcxx/trunk/test/std/containers/associative/multiset/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/associative/multiset/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/associative/multiset/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/associative/multiset/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -13,23 +13,38 @@
 
 // size_type max_size() const;
 
-#include <set>
 #include <cassert>
+#include <limits>
+#include <set>
+#include <type_traits>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
     {
-    typedef std::multiset<int> M;
-    M m;
-    assert(m.max_size() != 0);
+      typedef limited_allocator<int, 10> A;
+      typedef std::multiset<int, std::less<int>, A> C;
+      C c;
+      assert(c.max_size() <= 10);
+      LIBCPP_ASSERT(c.max_size() == 10);
+    }
+    {
+      typedef limited_allocator<int, (size_t)-1> A;
+      typedef std::multiset<int, std::less<int>, A> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      LIBCPP_ASSERT(c.max_size() == max_dist);
     }
-#if TEST_STD_VER >= 11
     {
-    typedef std::multiset<int, std::less<int>, min_allocator<int>> M;
-    M m;
-    assert(m.max_size() != 0);
+      typedef std::multiset<char> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Modified: libcxx/trunk/test/std/containers/associative/set/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/associative/set/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/associative/set/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/associative/set/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -13,23 +13,38 @@
 
 // size_type max_size() const;
 
-#include <set>
 #include <cassert>
+#include <limits>
+#include <set>
+#include <type_traits>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
     {
-    typedef std::set<int> M;
-    M m;
-    assert(m.max_size() != 0);
+      typedef limited_allocator<int, 10> A;
+      typedef std::set<int, std::less<int>, A> C;
+      C c;
+      assert(c.max_size() <= 10);
+      LIBCPP_ASSERT(c.max_size() == 10);
+    }
+    {
+      typedef limited_allocator<int, (size_t)-1> A;
+      typedef std::set<int, std::less<int>, A> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      LIBCPP_ASSERT(c.max_size() == max_dist);
     }
-#if TEST_STD_VER >= 11
     {
-    typedef std::set<int, std::less<int>, min_allocator<int>> M;
-    M m;
-    assert(m.max_size() != 0);
+      typedef std::set<char> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Added: libcxx/trunk/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp?rev=287729&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/sequences/deque/deque.capacity/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <deque>
+
+// size_type max_size() const;
+
+#include <cassert>
+#include <deque>
+#include <limits>
+#include <type_traits>
+
+#include "test_allocator.h"
+#include "test_macros.h"
+
+int main() {
+  {
+    typedef limited_allocator<int, 10> A;
+    typedef std::deque<int, A> C;
+    C c;
+    assert(c.max_size() <= 10);
+    LIBCPP_ASSERT(c.max_size() == 10);
+  }
+  {
+    typedef limited_allocator<int, (size_t)-1> A;
+    typedef std::deque<int, A> C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    LIBCPP_ASSERT(c.max_size() == max_dist);
+  }
+  {
+    typedef std::deque<char> C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    assert(c.max_size() <= alloc_max_size(c.get_allocator()));
+  }
+}

Modified: libcxx/trunk/test/std/containers/sequences/forwardlist/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/forwardlist/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/forwardlist/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/forwardlist/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -11,25 +11,38 @@
 
 // size_type max_size() const;
 
-#include <forward_list>
 #include <cassert>
+#include <forward_list>
+#include <limits>
+#include <type_traits>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
     {
-        typedef int T;
-        typedef std::forward_list<T> C;
-        C c;
-        assert(c.max_size() > 0);
+      typedef limited_allocator<int, 10> A;
+      typedef std::forward_list<int, A> C;
+      C c;
+      assert(c.max_size() <= 10);
+      LIBCPP_ASSERT(c.max_size() == 10);
+    }
+    {
+      typedef limited_allocator<int, (size_t)-1> A;
+      typedef std::forward_list<int, A> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::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<T, min_allocator<T>> C;
-        C c;
-        assert(c.max_size() > 0);
+      typedef std::forward_list<char> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Added: libcxx/trunk/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp?rev=287729&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/sequences/list/list.capacity/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <list>
+
+// size_type max_size() const noexcept
+
+#include <cassert>
+#include <limits>
+#include <list>
+#include <type_traits>
+
+#include "test_allocator.h"
+#include "test_macros.h"
+
+int main() {
+  {
+    typedef limited_allocator<int, 10> A;
+    typedef std::list<int, A> C;
+    C c;
+    assert(c.max_size() <= 10);
+    LIBCPP_ASSERT(c.max_size() == 10);
+  }
+  {
+    typedef limited_allocator<int, (size_t)-1> A;
+    typedef std::list<int, A> C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    LIBCPP_ASSERT(c.max_size() == max_dist);
+  }
+  {
+    typedef std::list<char> C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    assert(c.max_size() <= alloc_max_size(c.get_allocator()));
+  }
+}

Added: libcxx/trunk/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp?rev=287729&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// size_type max_size() const;
+
+#include <cassert>
+#include <limits>
+#include <type_traits>
+#include <vector>
+
+#include "test_allocator.h"
+#include "test_macros.h"
+
+
+int main() {
+  {
+    typedef limited_allocator<int, 10> A;
+    typedef std::vector<int, A> C;
+    C c;
+    assert(c.max_size() <= 10);
+    LIBCPP_ASSERT(c.max_size() == 10);
+  }
+  {
+    typedef limited_allocator<int, (size_t)-1> A;
+    typedef std::vector<int, A> C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    LIBCPP_ASSERT(c.max_size() == max_dist);
+  }
+  {
+    typedef std::vector<char> C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    assert(c.max_size() <= alloc_max_size(c.get_allocator()));
+  }
+}

Modified: libcxx/trunk/test/std/containers/unord/unord.map/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/unord/unord.map/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/unord/unord.map/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/unord/unord.map/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -9,28 +9,45 @@
 
 // <unordered_map>
 
-// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
-//           class Alloc = allocator<pair<const Key, T>>>
 // class unordered_map
 
 // size_type max_size() const;
 
-#include <unordered_map>
 #include <cassert>
+#include <limits>
+#include <type_traits>
+#include <unordered_map>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
-    {
-        std::unordered_map<int, int> u;
-        assert(u.max_size() > 0);
+  typedef std::pair<const int, int> KV;
+  {
+    typedef limited_allocator<KV, 10> A;
+    typedef std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, A>
+        C;
+    C c;
+    assert(c.max_size() <= 10);
+    LIBCPP_ASSERT(c.max_size() == 10);
+  }
+  {
+    typedef limited_allocator<KV, (size_t)-1> A;
+    typedef std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, A>
+        C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    LIBCPP_ASSERT(c.max_size() == max_dist);
     }
-#if TEST_STD_VER >= 11
     {
-        std::unordered_map<int, int, std::hash<int>, std::equal_to<int>,
-                                    min_allocator<std::pair<const int, int>>> u;
-        assert(u.max_size() > 0);
+      typedef std::unordered_map<char, int> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Modified: libcxx/trunk/test/std/containers/unord/unord.multimap/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/unord/unord.multimap/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/unord/unord.multimap/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/unord/unord.multimap/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -9,28 +9,47 @@
 
 // <unordered_map>
 
-// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
-//           class Alloc = allocator<pair<const Key, T>>>
 // class unordered_multimap
 
 // size_type max_size() const;
 
-#include <unordered_map>
 #include <cassert>
+#include <limits>
+#include <type_traits>
+#include <unordered_map>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
-    {
-        std::unordered_multimap<int, int> u;
-        assert(u.max_size() > 0);
+  typedef std::pair<const int, int> KV;
+  {
+    typedef limited_allocator<KV, 10> A;
+    typedef std::unordered_multimap<int, int, std::hash<int>,
+                                    std::equal_to<int>, A>
+        C;
+    C c;
+    assert(c.max_size() <= 10);
+    LIBCPP_ASSERT(c.max_size() == 10);
+  }
+  {
+    typedef limited_allocator<KV, (size_t)-1> A;
+    typedef std::unordered_multimap<int, int, std::hash<int>,
+                                    std::equal_to<int>, A>
+        C;
+    const C::difference_type max_dist =
+        std::numeric_limits<C::difference_type>::max();
+    C c;
+    assert(c.max_size() <= max_dist);
+    LIBCPP_ASSERT(c.max_size() == max_dist);
     }
-#if TEST_STD_VER >= 11
     {
-        std::unordered_multimap<int, int, std::hash<int>, std::equal_to<int>,
-                            min_allocator<std::pair<const int, int>>> u;
-        assert(u.max_size() > 0);
+      typedef std::unordered_multimap<char, int> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Modified: libcxx/trunk/test/std/containers/unord/unord.multiset/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/unord/unord.multiset/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/unord/unord.multiset/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/unord/unord.multiset/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -9,28 +9,46 @@
 
 // <unordered_set>
 
-// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
-//           class Alloc = allocator<Value>>
 // class unordered_multiset
 
 // size_type max_size() const;
 
-#include <unordered_set>
 #include <cassert>
+#include <limits>
+#include <type_traits>
+#include <unordered_set>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
     {
-        std::unordered_multiset<int> u;
-        assert(u.max_size() > 0);
+      typedef limited_allocator<int, 10> A;
+      typedef std::unordered_multiset<int, std::hash<int>, std::equal_to<int>,
+                                      A>
+          C;
+      C c;
+      assert(c.max_size() <= 10);
+      LIBCPP_ASSERT(c.max_size() == 10);
+    }
+    {
+      typedef limited_allocator<int, (size_t)-1> A;
+      typedef std::unordered_multiset<int, std::hash<int>, std::equal_to<int>,
+                                      A>
+          C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      LIBCPP_ASSERT(c.max_size() == max_dist);
     }
-#if TEST_STD_VER >= 11
     {
-        std::unordered_multiset<int, std::hash<int>,
-                                      std::equal_to<int>, min_allocator<int>> u;
-        assert(u.max_size() > 0);
+      typedef std::unordered_multiset<char> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Modified: libcxx/trunk/test/std/containers/unord/unord.set/max_size.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/unord/unord.set/max_size.pass.cpp?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/unord/unord.set/max_size.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/unord/unord.set/max_size.pass.cpp Tue Nov 22 19:18:56 2016
@@ -9,28 +9,42 @@
 
 // <unordered_set>
 
-// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
-//           class Alloc = allocator<Value>>
 // class unordered_set
 
 // size_type max_size() const;
 
-#include <unordered_set>
 #include <cassert>
+#include <limits>
+#include <type_traits>
+#include <unordered_set>
 
-#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
 
 int main()
 {
     {
-        std::unordered_set<int> u;
-        assert(u.max_size() > 0);
+      typedef limited_allocator<int, 10> A;
+      typedef std::unordered_set<int, std::hash<int>, std::equal_to<int>, A> C;
+      C c;
+      assert(c.max_size() <= 10);
+      LIBCPP_ASSERT(c.max_size() == 10);
+    }
+    {
+      typedef limited_allocator<int, (size_t)-1> A;
+      typedef std::unordered_set<int, std::hash<int>, std::equal_to<int>, A> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      LIBCPP_ASSERT(c.max_size() == max_dist);
     }
-#if TEST_STD_VER >= 11
     {
-        std::unordered_set<int, std::hash<int>,
-                                      std::equal_to<int>, min_allocator<int>> u;
-        assert(u.max_size() > 0);
+      typedef std::unordered_set<char> C;
+      const C::difference_type max_dist =
+          std::numeric_limits<C::difference_type>::max();
+      C c;
+      assert(c.max_size() <= max_dist);
+      assert(c.max_size() <= alloc_max_size(c.get_allocator()));
     }
-#endif
 }

Modified: libcxx/trunk/test/support/test_allocator.h
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/test_allocator.h?rev=287729&r1=287728&r2=287729&view=diff
==============================================================================
--- libcxx/trunk/test/support/test_allocator.h (original)
+++ libcxx/trunk/test/support/test_allocator.h Tue Nov 22 19:18:56 2016
@@ -20,6 +20,12 @@
 
 #include "test_macros.h"
 
+template <class Alloc>
+inline size_t alloc_max_size(Alloc const &a) {
+  typedef std::allocator_traits<Alloc> AT;
+  return AT::max_size(a);
+}
+
 class test_alloc_base
 {
 protected:




More information about the cfe-commits mailing list