Skip to content

Commit d597c41

Browse files
[libc++][test] Make deallocate_size.pass.cpp MSVC-STL-friendly
This patch contains several changes to `deallocate_size.pass.cpp`: 1. `static_cast`-ing parameters to suitable types to avoid narrowing. 2. Allows `allocated_ == 1` for MSVC STL because it dynamically allocates one container proxy object in debug modes. 3. Changes the type of `allocated_` to possibly larger and seemingly more appropriate `ptrdiff_t`.
1 parent ce61550 commit d597c41

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212

1313
#include <string>
1414
#include <cassert>
15+
#include <cstddef>
1516
#include <cstdint>
1617
#include <type_traits>
1718

1819
#include "test_macros.h"
1920

20-
static int allocated_;
21+
static std::ptrdiff_t allocated_;
2122

2223
template <class T, class Sz>
2324
struct test_alloc {
@@ -40,13 +41,13 @@ struct test_alloc {
4041
TEST_CONSTEXPR test_alloc(const test_alloc<U, Sz>&) TEST_NOEXCEPT {}
4142

4243
pointer allocate(size_type n, const void* = nullptr) {
43-
allocated_ += n;
44-
return std::allocator<value_type>().allocate(n);
44+
allocated_ += static_cast<std::ptrdiff_t>(n);
45+
return std::allocator<value_type>().allocate(static_cast<std::size_t>(n));
4546
}
4647

4748
void deallocate(pointer p, size_type s) {
48-
allocated_ -= s;
49-
std::allocator<value_type>().deallocate(p, s);
49+
allocated_ -= static_cast<std::ptrdiff_t>(s);
50+
std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));
5051
}
5152

5253
template <class U>
@@ -68,7 +69,11 @@ void test() {
6869
using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
6970
{
7071
Str s(i, 't');
72+
#ifdef _MSVC_STL_VERSION // MSVC STL dynamically allocates one container proxy object in debug modes.
73+
assert(allocated_ == 0 || allocated_ == 1 || allocated_ >= i);
74+
#else // ^^^ MSVC STL / other vvv
7175
assert(allocated_ == 0 || allocated_ >= i);
76+
#endif // ^^^ other ^^^
7277
}
7378
}
7479
assert(allocated_ == 0);

0 commit comments

Comments
 (0)