From 178a8307198ac1e1f9a5964466d8d5baa4a83cc9 Mon Sep 17 00:00:00 2001 From: Mathias Lang Date: Sat, 10 Feb 2024 14:24:33 +0100 Subject: [PATCH] Make sizeof test to runtime, query the C++ compiler We should strive to query the C++ compiler as much as possible instead of relying on hardcoded values, as it ensures that any change (new standard, new compiler, new platform) will catch the bug, which relying on hardcoded value will not. --- extras/test.cpp | 10 ++++++++++ source/stdcpp/test/base.d | 12 ++++++++++++ source/stdcpp/test/list.d | 10 ++++++---- source/stdcpp/test/vector.d | 13 ++++++++----- 4 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 source/stdcpp/test/base.d diff --git a/extras/test.cpp b/extras/test.cpp index 89cfd7f..39fa88c 100644 --- a/extras/test.cpp +++ b/extras/test.cpp @@ -10,5 +10,15 @@ #include #include +namespace stdcpp::test { + template + std::size_t cppSizeOf() { + return sizeof(T); + } +}; + template class std::list; +template std::size_t stdcpp::test::cppSizeOf >(); + template class std::vector; +template std::size_t stdcpp::test::cppSizeOf >(); diff --git a/source/stdcpp/test/base.d b/source/stdcpp/test/base.d new file mode 100644 index 0000000..4b2bc02 --- /dev/null +++ b/source/stdcpp/test/base.d @@ -0,0 +1,12 @@ +/******************************************************************************* + + Utilities to be used in tests + +*******************************************************************************/ + +module stdcpp.test.base; + +version (unittest): + +/// Returns: the `sizeof` of `T` as seen by the C++ compiler +extern(C++, "stdcpp", "test") size_t cppSizeOf (T) (); diff --git a/source/stdcpp/test/list.d b/source/stdcpp/test/list.d index 9de7608..aef2f0a 100644 --- a/source/stdcpp/test/list.d +++ b/source/stdcpp/test/list.d @@ -6,15 +6,17 @@ module stdcpp.test.list; +import stdcpp.test.base; import stdcpp.list; +/// Test that the sizes matches unittest { - version (CppRuntime_Microsoft) - static assert(list!int.sizeof == 16); - else - static assert(list!int.sizeof == 24); + assert(cppSizeOf!(list!int) == list!int.sizeof); +} +unittest +{ auto p = list!int(5); p.push_back(5); assert(p.size() == 6); diff --git a/source/stdcpp/test/vector.d b/source/stdcpp/test/vector.d index 74b85b9..2302eaf 100644 --- a/source/stdcpp/test/vector.d +++ b/source/stdcpp/test/vector.d @@ -6,7 +6,15 @@ module stdcpp.test.vector; +import stdcpp.test.base; import stdcpp.vector; + +/// Test that the sizes matches +unittest +{ + assert(cppSizeOf!(vector!int) == vector!int.sizeof); +} + version (CppRuntime_Gcc) { unittest @@ -39,7 +47,6 @@ version (CppRuntime_Gcc) assert(p.capacity() == 12); p.resize(5); assert(p.length == 5); - assert(p.sizeof == 24);//verifying three pointers p.assign(3,8); assert(p.length == 3); p.push_back(4); @@ -82,7 +89,6 @@ else version (CppRuntime_Clang) assert(vec.empty == 0); vec.reserve(6); assert(vec.capacity == 6); - assert(vec.sizeof == 24); vec.assign(3,8); assert(vec.length == 3); assert(vec[0] == 8); @@ -98,8 +104,5 @@ else version (CppRuntime_Clang) vec3.swap(vec); assert(vec3.size == 9); // after swap assert(vec.size == 7); - - - } }