Patchwork Yet another debug check for unordered containers

login
register
mail settings
Submitter François Dumont
Date Oct. 18, 2012, 8:13 p.m.
Message ID <508062F2.3070206@gmail.com>
Download mbox | patch
Permalink /patch/192430/
State New
Headers show

Comments

François Dumont - Oct. 18, 2012, 8:13 p.m.
On 10/18/2012 11:12 AM, Paolo Carlini wrote:
> On 10/17/2012 10:05 PM, François Dumont wrote:
>> Ok to commit ?
> Ok, thanks.
>
> Paolo.
>

Attached patch applied.

2012-10-18  François Dumont  <fdumont@gcc.gnu.org>

     * include/debug/formatter.h (_Debug_msg_id): Add
     __msg_valid_load_factor.
     * include/debug/macros.h (__glibcxx_check_max_load_factor): New.
     * include/debug/unordered_set
     (unordered_set<>::max_load_factor(float)): Check max load factor
     is positive.
     (unordered_multiset<>::max_load_factor(float)): Likewise.
     * include/debug/unordered_map
     (unordered_map<>::max_load_factor(float)): Likewise.
     (unordered_multimap<>::max_load_factor(float)): Likewise.
     * testsuite/23_containers/unordered_map/debug/max_load_factor_neg.cc:
     New.
     * testsuite/23_containers/unordered_multimap/debug/
     max_load_factor_neg.cc: New.
     * testsuite/23_containers/unordered_set/debug/max_load_factor_neg.cc:
     New.
     * testsuite/23_containers/unordered_multiset/debug/
     max_load_factor_neg.cc: New.

François

Patch

Index: src/c++11/debug.cc
===================================================================
--- src/c++11/debug.cc	(revision 192512)
+++ src/c++11/debug.cc	(working copy)
@@ -180,7 +180,8 @@ 
     "function requires a non-empty iterator range [%1.name;, %2.name;)",
     "attempt to self move assign",
     "attempt to access container with out-of-bounds bucket index %2;,"
-    " container only holds %3; buckets"
+    " container only holds %3; buckets",
+    "load factor shall be positive"
   };
 
   void
Index: include/debug/formatter.h
===================================================================
--- include/debug/formatter.h	(revision 192512)
+++ include/debug/formatter.h	(working copy)
@@ -113,7 +113,8 @@ 
     // self move assign
     __msg_self_move_assign,
     // unordered container buckets
-    __msg_bucket_index_oob
+    __msg_bucket_index_oob,
+    __msg_valid_load_factor
   };
 
   class _Error_formatter
Index: include/debug/unordered_set
===================================================================
--- include/debug/unordered_set	(revision 192512)
+++ include/debug/unordered_set	(working copy)
@@ -230,6 +230,17 @@ 
 	return _Base::bucket_size(__b);
       }
 
+      float
+      max_load_factor() const noexcept
+      { return _Base::max_load_factor(); }
+
+      void
+      max_load_factor(float __f)
+      {
+	__glibcxx_check_max_load_factor(__f);
+	_Base::max_load_factor(__f);
+      }
+
       template<typename... _Args>
 	std::pair<iterator, bool>
 	emplace(_Args&&... __args)
@@ -665,6 +676,17 @@ 
 	return _Base::bucket_size(__b);
       }
 
+      float
+      max_load_factor() const noexcept
+      { return _Base::max_load_factor(); }
+
+      void
+      max_load_factor(float __f)
+      {
+	__glibcxx_check_max_load_factor(__f);
+	_Base::max_load_factor(__f);
+      }
+
       template<typename... _Args>
 	iterator
 	emplace(_Args&&... __args)
Index: include/debug/macros.h
===================================================================
--- include/debug/macros.h	(revision 192512)
+++ include/debug/macros.h	(working copy)
@@ -324,9 +324,15 @@ 
 // Verify that the container is not self move assigned
 #define __glibcxx_check_self_move_assign(_Other)			\
 _GLIBCXX_DEBUG_VERIFY(this != &_Other,					\
-		      _M_message(__gnu_debug::__msg_self_move_assign)   \
+		      _M_message(__gnu_debug::__msg_self_move_assign)	\
                       ._M_sequence(*this, "this"))
 
+// Verify that load factor is position
+#define __glibcxx_check_max_load_factor(_F)				\
+_GLIBCXX_DEBUG_VERIFY(_F > 0.0f,					\
+		      _M_message(__gnu_debug::__msg_valid_load_factor)	\
+                      ._M_sequence(*this, "this"))
+
 #ifdef _GLIBCXX_DEBUG_PEDANTIC
 #  define __glibcxx_check_string(_String) _GLIBCXX_DEBUG_ASSERT(_String != 0)
 #  define __glibcxx_check_string_len(_String,_Len) \
Index: include/debug/unordered_map
===================================================================
--- include/debug/unordered_map	(revision 192512)
+++ include/debug/unordered_map	(working copy)
@@ -230,6 +230,17 @@ 
 	return _Base::bucket_size(__b);
       }
 
+      float
+      max_load_factor() const noexcept
+      { return _Base::max_load_factor(); }
+
+      void
+      max_load_factor(float __f)
+      {
+	__glibcxx_check_max_load_factor(__f);
+	_Base::max_load_factor(__f);
+      }
+
       template<typename... _Args>
 	std::pair<iterator, bool>
 	emplace(_Args&&... __args)
@@ -670,6 +681,17 @@ 
 	return _Base::bucket_size(__b);
       }
 
+      float
+      max_load_factor() const noexcept
+      { return _Base::max_load_factor(); }
+
+      void
+      max_load_factor(float __f)
+      {
+	__glibcxx_check_max_load_factor(__f);
+	_Base::max_load_factor(__f);
+      }
+
       template<typename... _Args>
 	iterator
 	emplace(_Args&&... __args)
Index: testsuite/23_containers/unordered_map/debug/max_load_factor_neg.cc
===================================================================
--- testsuite/23_containers/unordered_map/debug/max_load_factor_neg.cc	(revision 0)
+++ testsuite/23_containers/unordered_map/debug/max_load_factor_neg.cc	(revision 0)
@@ -0,0 +1,34 @@ 
+// Copyright (C) 2012 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-require-debug-mode "" }
+// { dg-options "-std=c++11" }
+// { dg-do run { xfail *-*-* } }
+
+#include <unordered_map>
+
+void test01()
+{
+  std::unordered_multimap<int, int> um;
+  um.max_load_factor(-1.0f);
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/23_containers/unordered_multimap/debug/max_load_factor_neg.cc
===================================================================
--- testsuite/23_containers/unordered_multimap/debug/max_load_factor_neg.cc	(revision 0)
+++ testsuite/23_containers/unordered_multimap/debug/max_load_factor_neg.cc	(revision 0)
@@ -0,0 +1,34 @@ 
+// Copyright (C) 2012 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-require-debug-mode "" }
+// { dg-options "-std=c++11" }
+// { dg-do run { xfail *-*-* } }
+
+#include <unordered_map>
+
+void test01()
+{
+  std::unordered_map<int, int> um;
+  um.max_load_factor(-1.0f);
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/23_containers/unordered_multiset/debug/max_load_factor_neg.cc
===================================================================
--- testsuite/23_containers/unordered_multiset/debug/max_load_factor_neg.cc	(revision 0)
+++ testsuite/23_containers/unordered_multiset/debug/max_load_factor_neg.cc	(revision 0)
@@ -0,0 +1,34 @@ 
+// Copyright (C) 2012 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-require-debug-mode "" }
+// { dg-options "-std=c++11" }
+// { dg-do run { xfail *-*-* } }
+
+#include <unordered_set>
+
+void test01()
+{
+  std::unordered_multiset<int> us;
+  us.max_load_factor(-1.0f);
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/23_containers/unordered_set/debug/max_load_factor_neg.cc
===================================================================
--- testsuite/23_containers/unordered_set/debug/max_load_factor_neg.cc	(revision 0)
+++ testsuite/23_containers/unordered_set/debug/max_load_factor_neg.cc	(revision 0)
@@ -0,0 +1,34 @@ 
+// Copyright (C) 2012 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-require-debug-mode "" }
+// { dg-options "-std=c++11" }
+// { dg-do run { xfail *-*-* } }
+
+#include <unordered_set>
+
+void test01()
+{
+  std::unordered_set<int> us;
+  us.max_load_factor(-1.0f);
+}
+
+int main()
+{
+  test01();
+  return 0;
+}