Skip to content

Commit 44e2430

Browse files
[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.
1 parent 66b4815 commit 44e2430

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

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

Lines changed: 9 additions & 8 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>
@@ -64,14 +65,14 @@ struct test_alloc {
6465

6566
template <class Sz>
6667
void test() {
68+
using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
6769
for (int i = 1; i < 1000; ++i) {
68-
using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
6970
{
7071
Str s(i, 't');
71-
assert(allocated_ == 0 || allocated_ >= i);
72+
(void)s;
7273
}
74+
assert(allocated_ == 0);
7375
}
74-
assert(allocated_ == 0);
7576
}
7677

7778
int main(int, char**) {

0 commit comments

Comments
 (0)