[libc++] Use stack buffers for uninitialized storage in tests.

This makes the tests more minimal, and in particular it avoids relying on a complete `<cstdlib>`, which may not be available on all platforms.

Differential Revision: https://reviews.llvm.org/D137188
This commit is contained in:
Konstantin Varlamov 2022-11-01 19:20:08 -07:00
parent 9ae6e6a502
commit aa2b05f0b9
3 changed files with 6 additions and 10 deletions

View File

@ -19,7 +19,6 @@
#include <memory_resource> #include <memory_resource>
#include <cassert> #include <cassert>
#include <cstdlib>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -38,13 +37,13 @@ int main(int, char**) {
typedef default_constructible T; typedef default_constructible T;
typedef std::pair<T, T> P; typedef std::pair<T, T> P;
typedef std::pmr::polymorphic_allocator<void> A; typedef std::pmr::polymorphic_allocator<void> A;
P* ptr = (P*)std::malloc(sizeof(P)); alignas(P) char buffer[sizeof(P)];
P* ptr = reinterpret_cast<P*>(buffer);
A a; A a;
a.construct(ptr); a.construct(ptr);
assert(constructed == 2); assert(constructed == 2);
assert(ptr->first.x == 42); assert(ptr->first.x == 42);
assert(ptr->second.x == 42); assert(ptr->second.x == 42);
std::free(ptr);
} }
return 0; return 0;

View File

@ -23,7 +23,6 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <cassert> #include <cassert>
#include <cstdlib>
#include "test_macros.h" #include "test_macros.h"
@ -105,13 +104,12 @@ void test_evil() {
PMA pma(std::pmr::new_delete_resource()); PMA pma(std::pmr::new_delete_resource());
{ {
using Pair = std::pair<W1, W2>; using Pair = std::pair<W1, W2>;
void* where = std::malloc(sizeof(Pair)); alignas(Pair) char buffer[sizeof(Pair)];
Pair* p = (Pair*)where; Pair* p = reinterpret_cast<Pair*>(buffer);
pma.construct(p, std::piecewise_construct, std::make_tuple(42), std::make_tuple(42)); pma.construct(p, std::piecewise_construct, std::make_tuple(42), std::make_tuple(42));
assert(p->first.holds(42, pma)); assert(p->first.holds(42, pma));
assert(p->second.holds(42, pma)); assert(p->second.holds(42, pma));
pma.destroy(p); pma.destroy(p);
std::free(where);
} }
} }

View File

@ -19,7 +19,6 @@
#include <memory_resource> #include <memory_resource>
#include <cassert> #include <cassert>
#include <cstdlib>
#include <new> #include <new>
#include <type_traits> #include <type_traits>
@ -39,11 +38,11 @@ int main(int, char**) {
ASSERT_SAME_TYPE(decltype(a.destroy((destroyable*)nullptr)), void); ASSERT_SAME_TYPE(decltype(a.destroy((destroyable*)nullptr)), void);
} }
{ {
destroyable* ptr = ::new (std::malloc(sizeof(destroyable))) destroyable(); alignas(destroyable) char buffer[sizeof(destroyable)];
destroyable* ptr = ::new (buffer) destroyable();
assert(count == 1); assert(count == 1);
A{}.destroy(ptr); A{}.destroy(ptr);
assert(count == 0); assert(count == 0);
std::free(ptr);
} }
return 0; return 0;