Skip to content

Commit

Permalink
Rename MemAccessStrategy.cpp to BaseStrategy.cpp
Browse files Browse the repository at this point in the history
- clean up of the test
- prepare for addition iterator tests
- add cmake helper function for tests
- add memory allocation function for tests
  • Loading branch information
SimeonEhrig committed Feb 8, 2022
1 parent 5b3f091 commit 44bd5db
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 137 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ if(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
#########################################

if(BUILD_TESTING)
include("${PROJECT_SOURCE_DIR}/cmake/testUtils.cmake")
include(CTest)
enable_testing()
add_subdirectory("test/")
Expand Down
60 changes: 60 additions & 0 deletions cmake/testUtils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2022 Simeon Ehrig
#
# This file is part of vikunja.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Create an executable file for the test, link it to the required test targets and add it to ctest.
#
# vikunja_add_default_test(TARGET <name>
# SOURCE <list of source files>
# [INCLUDE <list of include paths>]
# )
#
# * TARGET: name of the executable, will be prefixed with `test_`
# * SOURCE: path of the source file(s)
# * INCLUDE: optional path(s) to include folder(s)
macro(vikunja_add_default_test)
set(_MACRO_PREFIX "vikTest")
set(_SINGLE_ARG TARGET)
set(_MULTI_ARG SOURCE INCLUDE)

cmake_parse_arguments(
"${_MACRO_PREFIX}"
"" # option
"${_SINGLE_ARG}"
"${_MULTI_ARG}"
"${ARGN}"
)

if(NOT DEFINED ${_MACRO_PREFIX}_TARGET)
message(FATAL_ERROR "vikunja_add_default_test: no target name defined")
endif()

if(NOT DEFINED ${_MACRO_PREFIX}_SOURCE)
message(FATAL_ERROR "vikunja_add_default_test: no source files defined")
endif()

set(_TARGET_NAME "test_${${_MACRO_PREFIX}_TARGET}")

alpaka_add_executable(
${_TARGET_NAME}
${${_MACRO_PREFIX}_SOURCE}
)

if(DEFINED ${_MACRO_PREFIX}_INCLUDE)
message(VERBOSE "vikunja_add_default_test: add ${${_MACRO_PREFIX}_INCLUDE} include paths to ${_TARGET_NAME}")
target_include_directories(${_TARGET_NAME} PRIVATE ${${_MACRO_PREFIX}_INCLUDE})
endif()

target_link_libraries(${_TARGET_NAME}
PRIVATE
vikunja::internalvikunja
vikunja::testSetup
)

add_test(NAME ${_TARGET_NAME} COMMAND ${_TARGET_NAME} ${_VIKUNJA_TEST_OPTIONS})

endmacro()
58 changes: 57 additions & 1 deletion test/include/vikunja/test/AlpakaSetup.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2021 Hauke Mewes, Simeon Ehrig
/* Copyright 2022 Hauke Mewes, Simeon Ehrig
*
* This file is part of vikunja.
*
Expand Down Expand Up @@ -50,6 +50,62 @@ namespace vikunja
, queueAcc{devAcc}
{
}

/**
* @brief Allocate 1D memory on host.
*
* @tparam TData Type of the memory.
* @param size Size of the memory.
* @return auto Alpaka memory buffer on the host.
*/
template<typename TData>
auto allocHost(Idx size)
{
using Vec = alpaka::Vec<alpaka::DimInt<1>, Idx>;
return alpaka::allocBuf<TData, Idx>(devHost, Vec::all(size));
}

/**
* @brief Allocate ND memory on host.
*
* @tparam TData Type of the memory.
* @tparam TExtentDim alpaka::Vec<N, Idx>
* @param size Vector with the sizes for each memory dimension.
* @return auto Alpaka memory buffer on the host.
*/
template<typename TData, typename TExtentDim>
auto allocHost(alpaka::Vec<TExtentDim, Idx> size)
{
return alpaka::allocBuf<TData, Idx>(devHost, size);
}

/**
* @brief Allocate 1D memory on device.
*
* @tparam TData Type of the memory.
* @param size Size of the memory.
* @return auto Alpaka memory buffer on the device.
*/
template<typename TData>
auto allocDev(Idx size)
{
using Vec = alpaka::Vec<alpaka::DimInt<1>, Idx>;
return alpaka::allocBuf<TData, Idx>(devAcc, Vec::all(size));
}

/**
* @brief Allocate ND memory on device.
*
* @tparam TData Type of the memory.
* @tparam TExtentDim alpaka::Vec<N, Idx>
* @param size Vector with the sizes for each memory dimension.
* @return auto Alpaka memory buffer on the device.
*/
template<typename TData, typename TExtentDim>
auto allocDev(alpaka::Vec<TExtentDim, Idx> size)
{
return alpaka::allocBuf<TData, Idx>(devAcc, size);
}
};
} // namespace test
} // namespace vikunja
18 changes: 2 additions & 16 deletions test/unit/access/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Hauke Mewes, Simeon Ehrig
# Copyright 2022 Hauke Mewes, Simeon Ehrig
#
# This file is part of vikunja.
#
Expand All @@ -8,18 +8,4 @@

cmake_minimum_required(VERSION 3.18)

set(_TARGET_NAME "test_memAccessStrategy")


alpaka_add_executable(
${_TARGET_NAME}
src/MemAccessStrategy.cpp
)

target_link_libraries(${_TARGET_NAME}
PRIVATE
vikunja::internalvikunja
vikunja::testSetup
)

add_test(NAME ${_TARGET_NAME} COMMAND ${_TARGET_NAME} ${_VIKUNJA_TEST_OPTIONS})
vikunja_add_default_test(TARGET "memAccessBaseStrategy" SOURCE "src/BaseStrategy.cpp")
61 changes: 61 additions & 0 deletions test/unit/access/src/BaseStrategy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Copyright 2022 Hauke Mewes, Simeon Ehrig
*
* This file is part of vikunja.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <vikunja/access/BaseStrategy.hpp>
#include <vikunja/access/PolicyBasedBlockStrategy.hpp>

#include <alpaka/alpaka.hpp>

#include <vector>

#include <catch2/catch.hpp>

using namespace vikunja::MemAccess;

TEST_CASE("BaseStrategy", "[MemAccessStrategy]")
{
using Idx = std::size_t;
constexpr auto size = static_cast<Idx>(64);


BaseStrategy<Idx> zeroFirst(0, size);
BaseStrategy<Idx> zeroSecond(0, size);
BaseStrategy<Idx> one(1, size);

SECTION("test comparison operators")
{
REQUIRE(zeroFirst == zeroSecond);
REQUIRE(zeroSecond == zeroFirst);
REQUIRE(zeroFirst != one);
REQUIRE(one != zeroFirst);
REQUIRE(zeroFirst < one);
REQUIRE(one > zeroFirst);
REQUIRE_FALSE(zeroFirst < zeroSecond);
REQUIRE_FALSE(zeroFirst > zeroSecond);
REQUIRE(zeroFirst <= zeroSecond);
REQUIRE(zeroSecond <= zeroFirst);
REQUIRE(zeroFirst <= one);
}
SECTION("test access operators")
{
REQUIRE(*zeroFirst == 0);
REQUIRE(*one == 1);
*zeroFirst = 2;
REQUIRE(*zeroFirst == 2);
REQUIRE(*zeroSecond == 0);
REQUIRE_FALSE(*zeroFirst == *zeroSecond);
}
SECTION("test copy operator")
{
BaseStrategy<Idx> copyOfZeroFirst(zeroFirst);
REQUIRE(copyOfZeroFirst == zeroFirst);
*copyOfZeroFirst = 3;
REQUIRE(copyOfZeroFirst != zeroFirst);
}
}
120 changes: 0 additions & 120 deletions test/unit/access/src/MemAccessStrategy.cpp

This file was deleted.

0 comments on commit 44bd5db

Please sign in to comment.