[libcxx-commits] [libcxx] [libc++][test] Make `deallocate_size.pass.cpp` MSVC-STL-friendly (PR #165162)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 27 19:54:01 PDT 2025


https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/165162

>From 44e2430afb2320296df3fb9af0367135a10ab80d Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Mon, 27 Oct 2025 01:14:34 +0800
Subject: [PATCH] [libc++][test] Make `deallocate_size.pass.cpp` MSVC-friendly

This patch contains several changes to `deallocate_size.pass.cpp`:
1. `static_cast`-ing parameters to suitable types to avoid narrowing.
2. Changeing the type of `allocated_` to possibly larger and seemingly
more appropriate `ptrdiff_t`.
3. Avoiding `assert`-ing count of allocations when a `basic_string` is
allocated, just `assert`-ing after destruction instead.
---
 .../string.capacity/deallocate_size.pass.cpp    | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
index 00f9e2b846783..08735f84bb0aa 100644
--- a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
@@ -12,12 +12,13 @@
 
 #include <string>
 #include <cassert>
+#include <cstddef>
 #include <cstdint>
 #include <type_traits>
 
 #include "test_macros.h"
 
-static int allocated_;
+static std::ptrdiff_t allocated_;
 
 template <class T, class Sz>
 struct test_alloc {
@@ -40,13 +41,13 @@ struct test_alloc {
   TEST_CONSTEXPR test_alloc(const test_alloc<U, Sz>&) TEST_NOEXCEPT {}
 
   pointer allocate(size_type n, const void* = nullptr) {
-    allocated_ += n;
-    return std::allocator<value_type>().allocate(n);
+    allocated_ += static_cast<std::ptrdiff_t>(n);
+    return std::allocator<value_type>().allocate(static_cast<std::size_t>(n));
   }
 
   void deallocate(pointer p, size_type s) {
-    allocated_ -= s;
-    std::allocator<value_type>().deallocate(p, s);
+    allocated_ -= static_cast<std::ptrdiff_t>(s);
+    std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));
   }
 
   template <class U>
@@ -64,14 +65,14 @@ struct test_alloc {
 
 template <class Sz>
 void test() {
+  using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
   for (int i = 1; i < 1000; ++i) {
-    using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
     {
       Str s(i, 't');
-      assert(allocated_ == 0 || allocated_ >= i);
+      (void)s;
     }
+    assert(allocated_ == 0);
   }
-  assert(allocated_ == 0);
 }
 
 int main(int, char**) {



More information about the libcxx-commits mailing list