Skip to content

Commit eaf7c94

Browse files
frederick-vs-jaDebadri Basak
authored andcommitted
[libc++][test] Make deallocate_size.pass.cpp MSVC-friendly (llvm#165162)
This patch contains several changes to `deallocate_size.pass.cpp`: 1. `static_cast`-ing some parameters to `size_t` to avoid narrowing. 2. Changing the type of loop variable `i` to `unsigned int` avoid signedness mismatch with the constructor parameter. 3. Separately counting allocations and deallocations in variables `allocated_` and `deallocated_`, and changing their type to `uint64_t`. 4. Avoiding `assert`-ing count of allocations when a `basic_string` is allocated, just `assert`-ing after destruction instead.
1 parent 69d71f3 commit eaf7c94

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
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::uint64_t allocated_;
22+
static std::uint64_t deallocated_;
2123

2224
template <class T, class Sz>
2325
struct test_alloc {
@@ -41,12 +43,12 @@ struct test_alloc {
4143

4244
pointer allocate(size_type n, const void* = nullptr) {
4345
allocated_ += n;
44-
return std::allocator<value_type>().allocate(n);
46+
return std::allocator<value_type>().allocate(static_cast<std::size_t>(n));
4547
}
4648

4749
void deallocate(pointer p, size_type s) {
48-
allocated_ -= s;
49-
std::allocator<value_type>().deallocate(p, s);
50+
deallocated_ += s;
51+
std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));
5052
}
5153

5254
template <class U>
@@ -64,14 +66,13 @@ struct test_alloc {
6466

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

7778
int main(int, char**) {

0 commit comments

Comments
 (0)