Skip to content

Commit e32c591

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 07273bb commit e32c591

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,31 @@
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() {
36+
return this->dummy_data_;
37+
}
38+
39+
std::size_t getDummyDataSize() {
40+
return sizeof(this->dummy_data_);
41+
}
42+
private:
43+
alignas(OverAligned) char dummy_data_[alignof(OverAligned) * 3];
44+
};
45+
3046
int new_called = 0;
3147
int delete_called = 0;
32-
33-
alignas(OverAligned) char DummyData[alignof(OverAligned) * 3];
48+
Data data_class;
3449

3550
void* operator new[](std::size_t s, std::align_val_t a) {
36-
assert(s <= sizeof(DummyData));
51+
assert(s <= data_class.getDummyDataSize());
3752
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
3853
++new_called;
39-
return DummyData;
54+
return data_class.getDummyData();
4055
}
4156

4257
void operator delete[](void*, std::align_val_t) noexcept {
@@ -49,7 +64,7 @@ int main(int, char**) {
4964
{
5065
new_called = delete_called = 0;
5166
OverAligned* x = new OverAligned[3];
52-
assert(static_cast<void*>(x) == DummyData);
67+
assert(static_cast<void*>(x) == data_class.getDummyData());
5368
assert(new_called == 1);
5469

5570
delete[] x;

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,31 @@
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() {
36+
return this->dummy_data_;
37+
}
38+
39+
std::size_t getDummyDataSize() {
40+
return sizeof(this->dummy_data_);
41+
}
42+
private:
43+
alignas(OverAligned) char dummy_data_[alignof(OverAligned)];
44+
};
45+
3046
int new_called = 0;
3147
int delete_called = 0;
32-
33-
alignas(OverAligned) char DummyData[alignof(OverAligned)];
48+
Data data_class;
3449

3550
void* operator new(std::size_t s, std::align_val_t a) {
36-
assert(s <= sizeof(DummyData));
51+
assert(s <= data_class.getDummyDataSize());
3752
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
3853
++new_called;
39-
return DummyData;
54+
return data_class.getDummyData();
4055
}
4156

4257
void operator delete(void*, std::align_val_t) noexcept {
@@ -49,7 +64,7 @@ int main(int, char**) {
4964
{
5065
new_called = delete_called = 0;
5166
OverAligned* x = new OverAligned;
52-
assert(static_cast<void*>(x) == DummyData);
67+
assert(static_cast<void*>(x) == data_class.getDummyData());
5368
assert(new_called == 1);
5469

5570
delete x;

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,31 @@
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() {
35+
return this->dummy_data_;
36+
}
37+
38+
std::size_t getDummyDataSize() {
39+
return sizeof(this->dummy_data_);
40+
}
41+
private:
42+
alignas(OverAligned) char dummy_data_[alignof(OverAligned)];
43+
};
44+
2945
int new_nothrow_called = 0;
3046
int delete_called = 0;
31-
32-
alignas(OverAligned) char DummyData[alignof(OverAligned)];
47+
Data data_class;
3348

3449
void* operator new(std::size_t s, std::align_val_t a, std::nothrow_t const&) noexcept {
35-
assert(s <= sizeof(DummyData));
50+
assert(s <= data_class.getDummyDataSize());
3651
assert(static_cast<std::size_t>(a) == alignof(OverAligned));
3752
++new_nothrow_called;
38-
return DummyData;
53+
return data_class.getDummyData();
3954
}
4055

4156
void operator delete(void*, std::align_val_t) noexcept {
@@ -48,7 +63,7 @@ int main(int, char**) {
4863
{
4964
new_nothrow_called = delete_called = 0;
5065
OverAligned* x = new (std::nothrow) OverAligned;
51-
assert(static_cast<void*>(x) == DummyData);
66+
assert(static_cast<void*>(x) == data_class.getDummyData());
5267
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_nothrow_called == 1);
5368

5469
delete x;

0 commit comments

Comments
 (0)