[libcxx-commits] [libcxx] [libc++] Refactor tests for aligned allocation and sized deallocation (PR #117915)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 27 11:34:13 PST 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 43ee6f7a01fca8cf08e1029c54acc23240b86fca 3f86a7f5985529503eefce84faa6486d052d8319 --extensions h,cpp -- libcxx/test/benchmarks/allocation.bench.cpp libcxx/test/libcxx/memory/shared_ptr_array.pass.cpp libcxx/test/std/language.support/support.dynamic/align_val_t.pass.cpp libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.pass.cpp libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.pass.cpp libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.pass.cpp libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.pass.cpp libcxx/test/std/language.support/support.dynamic/new.delete/types.h libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/sized_delete_array.pass.cpp libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.pass.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.pass.cpp
index c9b59ecaff..8045fca37b 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.pass.cpp
@@ -34,15 +34,15 @@ void my_new_handler() {
 }
 
 int main(int, char**) {
-    test_with_interesting_alignments([](std::size_t size, std::size_t alignment) {
-        void* x = operator new[](size, static_cast<std::align_val_t>(alignment));
-        assert(x != nullptr);
-        assert(reinterpret_cast<std::uintptr_t>(x) % alignment == 0);
-        operator delete[](x, static_cast<std::align_val_t>(alignment));
-    });
+  test_with_interesting_alignments([](std::size_t size, std::size_t alignment) {
+    void* x = operator new[](size, static_cast<std::align_val_t>(alignment));
+    assert(x != nullptr);
+    assert(reinterpret_cast<std::uintptr_t>(x) % alignment == 0);
+    operator delete[](x, static_cast<std::align_val_t>(alignment));
+  });
 
-    // Test that the new handler is called if allocation fails
-    {
+  // Test that the new handler is called if allocation fails
+  {
 #ifndef TEST_HAS_NO_EXCEPTIONS
         std::set_new_handler(my_new_handler);
         try {
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.pass.cpp
index 1bdae8d597..7c410a8a55 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.pass.cpp
@@ -34,20 +34,19 @@ void my_new_handler() {
 }
 
 int main(int, char**) {
-    test_with_interesting_alignments([](std::size_t size, std::size_t alignment) {
-        void* x = operator new[](size, static_cast<std::align_val_t>(alignment), std::nothrow);
-        assert(x != nullptr);
-        assert(reinterpret_cast<std::uintptr_t>(x) % alignment == 0);
-        operator delete[](x, static_cast<std::align_val_t>(alignment), std::nothrow);
-    });
+  test_with_interesting_alignments([](std::size_t size, std::size_t alignment) {
+    void* x = operator new[](size, static_cast<std::align_val_t>(alignment), std::nothrow);
+    assert(x != nullptr);
+    assert(reinterpret_cast<std::uintptr_t>(x) % alignment == 0);
+    operator delete[](x, static_cast<std::align_val_t>(alignment), std::nothrow);
+  });
 
-    // Test that the new handler is called and we return nullptr if allocation fails
-    {
-        std::set_new_handler(my_new_handler);
-        void* x = operator new[](std::numeric_limits<std::size_t>::max(),
-                                 static_cast<std::align_val_t>(32), std::nothrow);
-        assert(new_handler_called == 1);
-        assert(x == nullptr);
+  // Test that the new handler is called and we return nullptr if allocation fails
+  {
+    std::set_new_handler(my_new_handler);
+    void* x = operator new[](std::numeric_limits<std::size_t>::max(), static_cast<std::align_val_t>(32), std::nothrow);
+    assert(new_handler_called == 1);
+    assert(x == nullptr);
     }
 
     // Test that a new expression constructs the right object
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/sized_delete_array.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/sized_delete_array.pass.cpp
index a7991c8659..4e8cbcd07a 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/sized_delete_array.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/sized_delete_array.pass.cpp
@@ -30,11 +30,11 @@
 // XFAIL: target={{.+}}-aix{{.*}}, target={{.+}}-zos{{.*}}
 
 #if !defined(__cpp_sized_deallocation)
-# error __cpp_sized_deallocation should be defined
+#  error __cpp_sized_deallocation should be defined
 #endif
 
 #if !(__cpp_sized_deallocation >= 201309L)
-# error expected __cpp_sized_deallocation >= 201309L
+#  error expected __cpp_sized_deallocation >= 201309L
 #endif
 
 #include <new>
@@ -44,26 +44,23 @@
 
 #include "test_macros.h"
 
-int unsized_delete_called = 0;
+int unsized_delete_called         = 0;
 int unsized_delete_nothrow_called = 0;
-int sized_delete_called = 0;
+int sized_delete_called           = 0;
 
-void operator delete[](void* p) TEST_NOEXCEPT
-{
-    ++unsized_delete_called;
-    std::free(p);
+void operator delete[](void* p) TEST_NOEXCEPT {
+  ++unsized_delete_called;
+  std::free(p);
 }
 
-void operator delete[](void* p, const std::nothrow_t&) TEST_NOEXCEPT
-{
-    ++unsized_delete_nothrow_called;
-    std::free(p);
+void operator delete[](void* p, const std::nothrow_t&) TEST_NOEXCEPT {
+  ++unsized_delete_nothrow_called;
+  std::free(p);
 }
 
-void operator delete[](void* p, std::size_t) TEST_NOEXCEPT
-{
-    ++sized_delete_called;
-    std::free(p);
+void operator delete[](void* p, std::size_t) TEST_NOEXCEPT {
+  ++sized_delete_called;
+  std::free(p);
 }
 
 // NOTE: Use a class with a non-trivial destructor as the test type in order
@@ -75,19 +72,20 @@ void operator delete[](void* p, std::size_t) TEST_NOEXCEPT
 //   with two parameters is selected.
 // - Otherwise, it is unspecified which of the two deallocation functions is
 //   selected.
-struct A { ~A() {} };
-
-int main(int, char**)
-{
-    A* x = new A[3];
-    assert(0 == unsized_delete_called);
-    assert(0 == unsized_delete_nothrow_called);
-    assert(0 == sized_delete_called);
-
-    delete [] x;
-    assert(0 == unsized_delete_called);
-    assert(0 == unsized_delete_nothrow_called);
-    assert(1 == sized_delete_called);
-
-    return 0;
+struct A {
+  ~A() {}
+};
+
+int main(int, char**) {
+  A* x = new A[3];
+  assert(0 == unsized_delete_called);
+  assert(0 == unsized_delete_nothrow_called);
+  assert(0 == sized_delete_called);
+
+  delete[] x;
+  assert(0 == unsized_delete_called);
+  assert(0 == unsized_delete_nothrow_called);
+  assert(1 == sized_delete_called);
+
+  return 0;
 }
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.pass.cpp
index 5d321f0828..854f5b40eb 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.pass.cpp
@@ -34,15 +34,15 @@ void my_new_handler() {
 }
 
 int main(int, char**) {
-    test_with_interesting_alignments([](std::size_t size, std::size_t alignment) {
-        void* x = operator new(size, static_cast<std::align_val_t>(alignment));
-        assert(x != nullptr);
-        assert(reinterpret_cast<std::uintptr_t>(x) % alignment == 0);
-        operator delete(x, static_cast<std::align_val_t>(alignment));
-    });
+  test_with_interesting_alignments([](std::size_t size, std::size_t alignment) {
+    void* x = operator new(size, static_cast<std::align_val_t>(alignment));
+    assert(x != nullptr);
+    assert(reinterpret_cast<std::uintptr_t>(x) % alignment == 0);
+    operator delete(x, static_cast<std::align_val_t>(alignment));
+  });
 
-    // Test that the new handler is called if allocation fails
-    {
+  // Test that the new handler is called if allocation fails
+  {
 #ifndef TEST_HAS_NO_EXCEPTIONS
         std::set_new_handler(my_new_handler);
         try {
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.pass.cpp
index 3e5ba57411..5cf193a614 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.pass.cpp
@@ -34,20 +34,19 @@ void my_new_handler() {
 }
 
 int main(int, char**) {
-    test_with_interesting_alignments([](std::size_t size, std::size_t alignment) {
-        void* x = operator new(size, static_cast<std::align_val_t>(alignment), std::nothrow);
-        assert(x != nullptr);
-        assert(reinterpret_cast<std::uintptr_t>(x) % alignment == 0);
-        operator delete(x, static_cast<std::align_val_t>(alignment), std::nothrow);
-    });
+  test_with_interesting_alignments([](std::size_t size, std::size_t alignment) {
+    void* x = operator new(size, static_cast<std::align_val_t>(alignment), std::nothrow);
+    assert(x != nullptr);
+    assert(reinterpret_cast<std::uintptr_t>(x) % alignment == 0);
+    operator delete(x, static_cast<std::align_val_t>(alignment), std::nothrow);
+  });
 
-    // Test that the new handler is called and we return nullptr if allocation fails
-    {
-        std::set_new_handler(my_new_handler);
-        void* x = operator new(std::numeric_limits<std::size_t>::max(),
-                               static_cast<std::align_val_t>(32), std::nothrow);
-        assert(new_handler_called == 1);
-        assert(x == nullptr);
+  // Test that the new handler is called and we return nullptr if allocation fails
+  {
+    std::set_new_handler(my_new_handler);
+    void* x = operator new(std::numeric_limits<std::size_t>::max(), static_cast<std::align_val_t>(32), std::nothrow);
+    assert(new_handler_called == 1);
+    assert(x == nullptr);
     }
 
     // Test that a new expression constructs the right object
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.pass.cpp
index 85d014c072..d2c52d8bfd 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/sized_delete.pass.cpp
@@ -30,11 +30,11 @@
 // XFAIL: target={{.+}}-aix{{.*}}, target={{.+}}-zos{{.*}}
 
 #if !defined(__cpp_sized_deallocation)
-# error __cpp_sized_deallocation should be defined
+#  error __cpp_sized_deallocation should be defined
 #endif
 
 #if !(__cpp_sized_deallocation >= 201309L)
-# error expected __cpp_sized_deallocation >= 201309L
+#  error expected __cpp_sized_deallocation >= 201309L
 #endif
 
 #include <new>
@@ -44,41 +44,37 @@
 
 #include "test_macros.h"
 
-int unsized_delete_called = 0;
+int unsized_delete_called         = 0;
 int unsized_delete_nothrow_called = 0;
-int sized_delete_called = 0;
+int sized_delete_called           = 0;
 
-void operator delete(void* p) TEST_NOEXCEPT
-{
-    ++unsized_delete_called;
-    std::free(p);
+void operator delete(void* p) TEST_NOEXCEPT {
+  ++unsized_delete_called;
+  std::free(p);
 }
 
-void operator delete(void* p, const std::nothrow_t&) TEST_NOEXCEPT
-{
-    ++unsized_delete_nothrow_called;
-    std::free(p);
+void operator delete(void* p, const std::nothrow_t&) TEST_NOEXCEPT {
+  ++unsized_delete_nothrow_called;
+  std::free(p);
 }
 
-void operator delete(void* p, std::size_t) TEST_NOEXCEPT
-{
-    ++sized_delete_called;
-    std::free(p);
+void operator delete(void* p, std::size_t) TEST_NOEXCEPT {
+  ++sized_delete_called;
+  std::free(p);
 }
 
-int main(int, char**)
-{
-    int *x = new int(42);
-    DoNotOptimize(x);
-    assert(0 == unsized_delete_called);
-    assert(0 == unsized_delete_nothrow_called);
-    assert(0 == sized_delete_called);
-
-    delete x;
-    DoNotOptimize(x);
-    assert(0 == unsized_delete_called);
-    assert(1 == sized_delete_called);
-    assert(0 == unsized_delete_nothrow_called);
-
-    return 0;
+int main(int, char**) {
+  int* x = new int(42);
+  DoNotOptimize(x);
+  assert(0 == unsized_delete_called);
+  assert(0 == unsized_delete_nothrow_called);
+  assert(0 == sized_delete_called);
+
+  delete x;
+  DoNotOptimize(x);
+  assert(0 == unsized_delete_called);
+  assert(1 == sized_delete_called);
+  assert(0 == unsized_delete_nothrow_called);
+
+  return 0;
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/117915


More information about the libcxx-commits mailing list