Skip to content

Commit

Permalink
Make sizeof test to runtime, query the C++ compiler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Geod24 committed Feb 10, 2024
1 parent 2466155 commit 178a830
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
10 changes: 10 additions & 0 deletions extras/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,15 @@
#include <list>
#include <vector>

namespace stdcpp::test {
template<typename T>
std::size_t cppSizeOf() {
return sizeof(T);
}
};

template class std::list<int>;
template std::size_t stdcpp::test::cppSizeOf<std::list<int> >();

template class std::vector<int>;
template std::size_t stdcpp::test::cppSizeOf<std::vector<int> >();
12 changes: 12 additions & 0 deletions source/stdcpp/test/base.d
Original file line number Diff line number Diff line change
@@ -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) ();
10 changes: 6 additions & 4 deletions source/stdcpp/test/list.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions source/stdcpp/test/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -98,8 +104,5 @@ else version (CppRuntime_Clang)
vec3.swap(vec);
assert(vec3.size == 9); // after swap
assert(vec.size == 7);



}
}

0 comments on commit 178a830

Please sign in to comment.