On 11/04/2025 08:36, Tomasz Kaminski wrote:


On Thu, Apr 10, 2025 at 10:47 PM Jonathan Wakely <jwak...@redhat.com> wrote:

    On 10/04/25 22:36 +0200, François Dumont wrote:
    >After running the test with -fno-exceptions option we rather need
    this
    >patch.
    >
    >Ok to commit ?
    >
    >François
    >
    >
    >On 10/04/2025 21:08, François Dumont wrote:
    >>Hi
    >>
    >>    No problem detected now that we really test std::deque
    >>shrink_to_fit implementation.
    >>
    >>    libstdc++: Add std::deque<>::shrink_to_fit test
    >>
    >>    The existing test is currently testing std::vector. Make it
    test
    >>std::deque.
    >>
    >>    libstdc++-v3/ChangeLog:
    >>
    >>            *
    >>testsuite/23_containers/deque/capacity/shrink_to_fit.cc: Adapt test
    >>            to check std::deque shrink_to_fit method.
    >>
    >>Tested under Linux x64.
    >>
    >>Ok to commit ?
    >>
    >>François

    >diff --git
    a/libstdc++-v3/testsuite/23_containers/deque/capacity/shrink_to_fit.cc
    b/libstdc++-v3/testsuite/23_containers/deque/capacity/shrink_to_fit.cc
    >index 7cb67079214..9c8b3a926e8 100644
    >---
    a/libstdc++-v3/testsuite/23_containers/deque/capacity/shrink_to_fit.cc
    >+++
    b/libstdc++-v3/testsuite/23_containers/deque/capacity/shrink_to_fit.cc
    >@@ -1,4 +1,5 @@
    > // { dg-do run { target c++11 } }
    >+// { dg-add-options no_pch }

    Tests using replacement_memory_operators.h need:

    // { dg-require-effective-target std_allocator_new }
    // { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } }

    See e.g. 23_containers/unordered_set/96088.cc

Thanks, I new I needed to add something like that but then forgot to amend my test.



    >
    > // 2010-01-08  Paolo Carlini  <paolo.carl...@oracle.com>
    >
    >@@ -19,18 +20,39 @@
    > // with this library; see the file COPYING3.  If not see
    > // <http://www.gnu.org/licenses/>.
    >
    >-#include <vector>
    >+#define _GLIBCXX_DEQUE_BUF_SIZE sizeof(int) * 3

    Couldn't the test just create more elements, instead of modifying the
    internals? We should test it using the default parameters, no?

Sounds better indeed, done using std::__deque_buf_size extension.


In from_range test I have used a class that contains some padding,
to be able to fill the deque buffer, while still test with few elements,

        struct EightInBuf

        {
  EightInBuf(int x) : elems{x}
  { }

 private:
   int elems[512 / (sizeof(int) * 8)];

  friend constexpr bool operator==(EightInBuf const& lhs, int rhs)
  { return lhs.elems[0] == rhs; }
 };     
        

        


It is a nice alternative even if you are still relying on the 512 implementation detail hidden by _GLIBCXX_DEQUE_BUF_SIZE macro.

This is why I preferred to use__deque_buf_size.


    >+#include <deque>
    > #include <testsuite_hooks.h>
    >+#include <replacement_memory_operators.h>
    >
    > // libstdc++/42573
    > void test01()
    > {
    >- std::vector<int> d(100);
    >+  __gnu_test::counter::reset();
    >+
    >+ std::deque<int> d;
    >   d.push_back(1);
    >   d.push_back(1);
    >-  // VERIFY( d.size() < d.capacity() );
    >+  d.push_back(1);
    >+  d.pop_front();
    >+  VERIFY( d.size() == 2 );
    >+  // 1 node array allocation + 2 node allocation = 3.
    >+  VERIFY( __gnu_test::counter::count() == 3 );
    >+  VERIFY( __gnu_test::counter::get()._M_increments == 3 );
    >+
    >   d.shrink_to_fit();
    >-  // VERIFY( d.size() == d.capacity() );
    >+  VERIFY( d.size() == 2 );
    >+#if __cpp_exceptions
    >+  // 1 node array allocation + 1 node allocation = 2.
    >+  const int expected_count = 2;
    >+  const int expected_increments = 2;
    >+#else

    It would be useful to add a comment here saying that shrink_to_fit()
    is a no-op for -fno-exceptions because we choose not to reallocate.

Added.

Ok to commit ?

François

diff --git 
a/libstdc++-v3/testsuite/23_containers/deque/capacity/shrink_to_fit.cc 
b/libstdc++-v3/testsuite/23_containers/deque/capacity/shrink_to_fit.cc
index 7cb67079214..768268bebe4 100644
--- a/libstdc++-v3/testsuite/23_containers/deque/capacity/shrink_to_fit.cc
+++ b/libstdc++-v3/testsuite/23_containers/deque/capacity/shrink_to_fit.cc
@@ -1,4 +1,6 @@
 // { dg-do run { target c++11 } }
+// { dg-require-effective-target std_allocator_new }
+// { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } }
 
 // 2010-01-08  Paolo Carlini  <paolo.carl...@oracle.com>
 
@@ -19,18 +21,40 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <vector>
+#include <deque>
 #include <testsuite_hooks.h>
+#include <replacement_memory_operators.h>
 
 // libstdc++/42573
 void test01()
 {
-  std::vector<int> d(100);
-  d.push_back(1);
-  d.push_back(1);
-  // VERIFY( d.size() < d.capacity() );
+  __gnu_test::counter::reset();
+
+  std::deque<size_t> d;
+  for (size_t i = 0; i != std::__deque_buf_size(sizeof(size_t)); ++i)
+    d.push_back(i);
+
+  // No shrink if 1st buffer is full, create some front capacity.
+  d.pop_front();
+
+  // 1 node array allocation + 2 node allocation = 3.
+  VERIFY( __gnu_test::counter::count() == 3 );
+  VERIFY( __gnu_test::counter::get()._M_increments == 3 );
+
   d.shrink_to_fit();
-  // VERIFY( d.size() == d.capacity() );
+
+  // No reallocation if no exception support, shrink_to_fit is then a
+  // no-op.
+#if __cpp_exceptions
+  // 1 node array allocation + 1 node allocation = 2.
+  const int expected_count = 2;
+  const int expected_increments = 2;
+#else
+  const int expected_count = 3;
+  const int expected_increments = 0;
+#endif
+  VERIFY( __gnu_test::counter::count() == expected_count );
+  VERIFY( __gnu_test::counter::get()._M_increments == 3 + expected_increments 
);
 }
 
 int main()
diff --git a/libstdc++-v3/testsuite/util/replacement_memory_operators.h 
b/libstdc++-v3/testsuite/util/replacement_memory_operators.h
index 2516cd24ae1..69afa77754d 100644
--- a/libstdc++-v3/testsuite/util/replacement_memory_operators.h
+++ b/libstdc++-v3/testsuite/util/replacement_memory_operators.h
@@ -36,8 +36,12 @@ namespace __gnu_test
 
     ~counter() THROW (counter_error)
     {
+#if __cpp_exceptions
       if (_M_throw && _M_count != 0)
        throw counter_error();
+#else
+      VERIFY( !_M_throw || _M_count == 0 );
+#endif
     }
 
     static void
@@ -133,8 +137,12 @@ void* operator new(std::size_t size) THROW(std::bad_alloc)
 {
   std::printf("operator new is called \n");
   void* p = std::malloc(size);
+#if __cpp_exceptions
   if (!p)
     throw std::bad_alloc();
+#else
+  VERIFY( p );
+#endif
   __gnu_test::counter::increment();
   return p;
 }

Reply via email to