Skip to content

Commit f765ebe

Browse files
committed
[libcxx] Better define the DummyData for tests
The tests utilise a DummyData block to assign a block of memory rather than calling `malloc` and `free` as part of `new` and `delete` operations. However, this is defined as a global variable which causes issues when comparing the memory address to the locally stored variable within the test. To get around this, the DummyData block is now part of a class, which is defined globally. This is then used to compare the pointers and ensure the operations are working correctly.
1 parent 82bf517 commit f765ebe

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,28 @@
2727
#include "test_macros.h"
2828
#include "../types.h"
2929

30+
class Data {
31+
public:
32+
Data() = default;
33+
~Data() = default;
34+
35+
char* getDummyData() { return this->dummy_data_; }
36+
37+
std::size_t getDummyDataSize() { return sizeof(this->dummy_data_); }
38+
39+
private:
40+
alignas(OverAligned) char dummy_data_[alignof(OverAligned) * 3];
41+
};
42+
3043
int new_called = 0;
3144
int delete_called = 0;
32-
33-
alignas(OverAligned) char DummyData[alignof(OverAligned) * 3];
45+
Data data_class;
3446

3547
void* operator new[](std::size_t s, std::align_val_t a) {
36-
assert(s <= sizeof(DummyData));
37-
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
38-
++new_called;
39-
return DummyData;
48+
assert(s <= data_class.getDummyDataSize());
49+
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
50+
++new_called;
51+
return data_class.getDummyData();
4052
}
4153

4254
void operator delete[](void*, std::align_val_t) noexcept {
@@ -49,7 +61,7 @@ int main(int, char**) {
4961
{
5062
new_called = delete_called = 0;
5163
OverAligned* x = new OverAligned[3];
52-
assert(static_cast<void*>(x) == DummyData);
64+
assert(static_cast<void*>(x) == data_class.getDummyData());
5365
assert(new_called == 1);
5466

5567
delete[] x;

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,28 @@
2727
#include "test_macros.h"
2828
#include "../types.h"
2929

30+
class Data {
31+
public:
32+
Data() = default;
33+
~Data() = default;
34+
35+
char* getDummyData() { return this->dummy_data_; }
36+
37+
std::size_t getDummyDataSize() { return sizeof(this->dummy_data_); }
38+
39+
private:
40+
alignas(OverAligned) char dummy_data_[alignof(OverAligned)];
41+
};
42+
3043
int new_called = 0;
3144
int delete_called = 0;
32-
33-
alignas(OverAligned) char DummyData[alignof(OverAligned)];
45+
Data data_class;
3446

3547
void* operator new(std::size_t s, std::align_val_t a) {
36-
assert(s <= sizeof(DummyData));
37-
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
38-
++new_called;
39-
return DummyData;
48+
assert(s <= data_class.getDummyDataSize());
49+
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
50+
++new_called;
51+
return data_class.getDummyData();
4052
}
4153

4254
void operator delete(void*, std::align_val_t) noexcept {
@@ -49,7 +61,7 @@ int main(int, char**) {
4961
{
5062
new_called = delete_called = 0;
5163
OverAligned* x = new OverAligned;
52-
assert(static_cast<void*>(x) == DummyData);
64+
assert(static_cast<void*>(x) == data_class.getDummyData());
5365
assert(new_called == 1);
5466

5567
delete x;

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,28 @@
2626
#include "test_macros.h"
2727
#include "../types.h"
2828

29+
class Data {
30+
public:
31+
Data() = default;
32+
~Data() = default;
33+
34+
char* getDummyData() { return this->dummy_data_; }
35+
36+
std::size_t getDummyDataSize() { return sizeof(this->dummy_data_); }
37+
38+
private:
39+
alignas(OverAligned) char dummy_data_[alignof(OverAligned)];
40+
};
41+
2942
int new_nothrow_called = 0;
3043
int delete_called = 0;
31-
32-
alignas(OverAligned) char DummyData[alignof(OverAligned)];
44+
Data data_class;
3345

3446
void* operator new(std::size_t s, std::align_val_t a, std::nothrow_t const&) noexcept {
35-
assert(s <= sizeof(DummyData));
36-
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
37-
++new_nothrow_called;
38-
return DummyData;
47+
assert(s <= data_class.getDummyDataSize());
48+
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
49+
++new_nothrow_called;
50+
return data_class.getDummyData();
3951
}
4052

4153
void operator delete(void*, std::align_val_t) noexcept {
@@ -48,7 +60,7 @@ int main(int, char**) {
4860
{
4961
new_nothrow_called = delete_called = 0;
5062
OverAligned* x = new (std::nothrow) OverAligned;
51-
assert(static_cast<void*>(x) == DummyData);
63+
assert(static_cast<void*>(x) == data_class.getDummyData());
5264
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_nothrow_called == 1);
5365

5466
delete x;

0 commit comments

Comments
 (0)