test: Add unit tests for simple array alignment

This commit is contained in:
kd-11
2025-11-21 02:32:31 +03:00
committed by Elad
parent d6a36f4b60
commit ea7183b6bd
3 changed files with 185 additions and 143 deletions
+9 -6
View File
@@ -3,20 +3,22 @@
#include "util/types.hpp"
#include "util/pair.hpp"
struct some_struct
namespace utils
{
u64 v {};
struct some_struct
{
u64 v{};
char s[12] = "Hello World";
bool operator == (const some_struct& r) const
{
return v == r.v && std::memcmp(s, r.s, sizeof(s)) == 0;
}
};
};
TEST(Utils, Pair)
{
some_struct s {};
TEST(Pair, General)
{
some_struct s{};
s.v = 1234;
utils::pair<int, some_struct> p;
@@ -43,4 +45,5 @@ TEST(Utils, Pair)
utils::pair<int, some_struct> p3 = std::move(p);
EXPECT_EQ(p3.first, 666);
EXPECT_EQ(p3.second, s);
}
}
+36
View File
@@ -267,4 +267,40 @@ namespace rsx
EXPECT_EQ(std::memcmp(arr[i].second.s, "Hello World", sizeof(arr[i].second.s)), 0);
}
}
TEST(SimpleArray, DataAlignment_SmallVector)
{
struct alignas(16) some_struct {
char data[16];
};
rsx::simple_array<some_struct> arr(2);
const auto data_ptr = reinterpret_cast<uintptr_t>(arr.data());
EXPECT_EQ(data_ptr & 15, 0);
}
TEST(SimpleArray, DataAlignment_HeapAlloc)
{
struct alignas(16) some_struct {
char data[16];
};
rsx::simple_array<some_struct> arr(128);
const auto data_ptr = reinterpret_cast<uintptr_t>(arr.data());
EXPECT_EQ(data_ptr & 15, 0);
}
TEST(SimpleArray, DataAlignment_Overrides)
{
rsx::simple_array<std::byte, 16> arr(4);
rsx::simple_array<std::byte, 128> arr2(4);
const auto data_ptr1 = reinterpret_cast<uintptr_t>(arr.data());
const auto data_ptr2 = reinterpret_cast<uintptr_t>(arr2.data());
EXPECT_EQ(data_ptr1 & 15, 0);
EXPECT_EQ(data_ptr2 & 127, 0);
}
}
+9 -6
View File
@@ -2,20 +2,22 @@
#include "util/tuple.hpp"
struct some_struct
namespace utils
{
u64 v {};
struct some_struct
{
u64 v{};
char s[12] = "Hello World";
bool operator == (const some_struct& r) const
{
return v == r.v && std::memcmp(s, r.s, sizeof(s)) == 0;
}
};
};
TEST(Utils, Tuple)
{
some_struct s {};
TEST(Tuple, General)
{
some_struct s{};
s.v = 1234;
utils::tuple t0 = {};
@@ -111,4 +113,5 @@ TEST(Utils, Tuple)
EXPECT_TRUE((std::is_same_v<decltype(ta.get<1>()), some_struct&>));
EXPECT_EQ(ta.get<0>(), 2);
EXPECT_EQ(ta.get<1>(), s);
}
}