diff --git a/emper/lib/template_util.hpp b/emper/lib/template_util.hpp new file mode 100644 index 0000000000000000000000000000000000000000..830b1745ca3beed9daae91730fe9761b3e564efd --- /dev/null +++ b/emper/lib/template_util.hpp @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright © 2020 Florian Schmaus +#include <cstddef> + +namespace emper::lib::template_util { + +// https://stackoverflow.com/a/66043076/194894 +template <typename T> +auto getSize(T& t) -> size_t { + typename T::size_type size = t.size(); + size_t value_type_size = sizeof(typename T::value_type); + + return size * value_type_size; +} + +} // namespace emper::lib::template_util diff --git a/tests/SimpleLawsTest.cpp b/tests/SimpleLawsTest.cpp index 217d2fd25b39a19673e5887185cc40d686c412bc..e7a6ac59901d744dbcf14273b366df9c48f11d3a 100644 --- a/tests/SimpleLawsTest.cpp +++ b/tests/SimpleLawsTest.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: LGPL-3.0-or-later // Copyright © 2020-2021 Florian Schmaus +#include <array> #include <atomic> // for atomic, __atomic_base #include <cstdint> // for uint64_t, UINT64_MAX #include <cstdlib> // for free, exit, EXIT_FAILURE @@ -12,8 +13,11 @@ #include "Fiber.hpp" // for Fiber, Fiber::NOT_AFFINE #include "Runtime.hpp" // for Runtime #include "emper-common.h" // for UNUSED_ARG, workeraffini... +#include "lib/template_util.hpp" #include "strategies/laws/LawsStrategyFactory.hpp" +namespace tu = emper::lib::template_util; + class RuntimeStrategyFactory; static const unsigned int ROUND_COUNT = 10; @@ -22,8 +26,7 @@ static const unsigned int PAYLOAD_COUNT = 4096; using FiberData = struct ALIGN_TO_CACHE_LINE { // 4096 * 8 byte (64 bit) = 32 KiB = L1 cache size of most systems - // NOLINTNEXTLINE(modernize-avoid-c-arrays) - uint64_t payload[PAYLOAD_COUNT]; + std::array<uint64_t, PAYLOAD_COUNT> payload; CPS* cps; unsigned int fiberNum; }; @@ -56,7 +59,9 @@ static void alphaFun() { for (unsigned int i = 0; i < FIBER_COUNT; ++i) { FiberData& currentFiberData = fiberData[i]; - memset(currentFiberData.payload, 0, sizeof(uint64_t) * PAYLOAD_COUNT); + auto& payload = currentFiberData.payload; + memset(payload.data(), 0, tu::getSize(payload)); + currentFiberData.fiberNum = i; currentFiberData.cps = nullptr; diff --git a/tests/TemplateUtilTest.cpp b/tests/TemplateUtilTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87698495bc4a710d5ea06b1d52abf5840f1ec089 --- /dev/null +++ b/tests/TemplateUtilTest.cpp @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright © 2021 Florian Schmaus +#include <gtest/gtest.h> + +#include <array> +#include <cstddef> +#include <cstdint> +#include <memory> + +#include "lib/template_util.hpp" + +namespace tu = emper::lib::template_util; + +template <typename T> +void test_getSize(T& t, size_t expected) { + size_t actual = tu::getSize(t); + ASSERT_EQ(actual, expected); +} + +// NOLINTNEXTLINE(modernize-use-trailing-return-type) +TEST(getSize, simple_uint8_t) { + const size_t bytes = 42; + std::array<uint8_t, bytes> array; + test_getSize(array, bytes); +} + +// NOLINTNEXTLINE(modernize-use-trailing-return-type) +TEST(getSize, simple_uint32_t) { + const size_t elements = 42; + std::array<uint32_t, elements> array; + test_getSize(array, elements * 4); +} diff --git a/tests/meson.build b/tests/meson.build index caaf52bd455314fa4e8b733a6d87c8ca8f6b0e9f..a08c6b54b4fe262af560d8f8973bd70b7e015a49 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -144,6 +144,11 @@ tests = { 'test_suite': 'io', 'args': ['10', '10000'], }, + 'TemplateUtilTest.cpp': + { + 'gtest': true, + 'is_parallel': true, + }, } undef_ndebug = '-UNDEBUG' @@ -163,6 +168,10 @@ io_uring_enabled = get_option('io') parallel_ok = (not io_uring_enabled) +# Meson integration for GTest and GMock +# See https://mesonbuild.com/Dependencies.html#gtest-and-gmock +gtest_dep = dependency('gtest', main: true) + foreach source, test_dict : tests # TODO: Use meson fs (filesystem) module once meson >= 0.53 is in # buster-backports, instead of split('.')[0] @@ -171,6 +180,11 @@ foreach source, test_dict : tests test_name = source.split('.')[0] test_deps = [thread_dep, test_fixtures] + + if test_dict.get('gtest', false) + test_deps += gtest_dep + endif + if test_dict.has_key('dependencies') test_deps += test_dict['dependencies'] endif