Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

#include <string>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <type_traits>

#include "test_macros.h"

static int allocated_;
static std::size_t allocated_;
static std::size_t deallocated_;

template <class T, class Sz>
struct test_alloc {
Expand All @@ -40,13 +42,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::size_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);
deallocated_ += static_cast<std::size_t>(s);
std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));
}

template <class U>
Expand All @@ -65,13 +67,11 @@ struct test_alloc {
template <class Sz>
void test() {
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);
TEST_MAYBE_UNUSED std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> > s(i, 't');
}
assert(allocated_ == deallocated_);
}
assert(allocated_ == 0);
}

int main(int, char**) {
Expand Down
10 changes: 10 additions & 0 deletions libcxx/test/support/test_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,16 @@ inline Tp const& DoNotOptimize(Tp const& value) {
#define TEST_NO_UNIQUE_ADDRESS
#endif

#if TEST_STD_VER >= 17 && __has_cpp_attribute(maybe_unused)
# define TEST_MAYBE_UNUSED [[maybe_unused]]
#elif __has_cpp_attribute(__maybe_unused__)
# define TEST_MAYBE_UNUSED [[__maybe_unused__]]
#elif __has_cpp_attribute(gnu::unused)
# define TEST_MAYBE_UNUSED [[gnu::unused]]
#else
# define TEST_MAYBE_UNUSED
#endif

#ifdef _LIBCPP_SHORT_WCHAR
# define TEST_SHORT_WCHAR
#endif
Expand Down
Loading