Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/hostnumerics-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ jobs:
cmake -S shared/hostnumerics/tests/package \
-B "${RUNNER_TEMP}/hostnumerics-package-test" \
-G Ninja \
-DHOSTNUMERICS_SOURCE_DIR="${GITHUB_WORKSPACE}/shared/hostnumerics" \
-DCMAKE_PREFIX_PATH="${RUNNER_TEMP}/hostnumerics-install"
cmake --build "${RUNNER_TEMP}/hostnumerics-package-test" --parallel "$(nproc)"
ctest --test-dir "${RUNNER_TEMP}/hostnumerics-package-test" \
Expand All @@ -106,6 +107,7 @@ jobs:
run: |
cmake -S shared/hostnumerics/tests/embedded \
-B "${RUNNER_TEMP}/hostnumerics-embedded-test" \
-DHOSTNUMERICS_SOURCE_DIR="${GITHUB_WORKSPACE}/shared/hostnumerics" \
-G Ninja
cmake --build "${RUNNER_TEMP}/hostnumerics-embedded-test" --parallel "$(nproc)"
ctest --test-dir "${RUNNER_TEMP}/hostnumerics-embedded-test" \
Expand Down
1 change: 1 addition & 0 deletions shared/hostnumerics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ add_library(roc::hostnumerics ALIAS hostnumerics)
target_link_libraries(hostnumerics INTERFACE roc::hostnumerics-core)

if(HOSTNUMERICS_BUILD_TESTING)
list(APPEND CMAKE_MODULE_PATH "${HostNumerics_SOURCE_DIR}/cmake")
enable_testing()
add_subdirectory(tests)
endif()
Expand Down
4 changes: 3 additions & 1 deletion shared/hostnumerics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ reference. Compare representative input-generation and reference-computation
timings, as well as their effect on complete test and benchmark runs, before
removing an old accelerated path.

C++ tests use GoogleTest 1.12.1 or newer. Test builds fetch 1.17.0, matching the shared superbuild, when no installed package is available; set `HOSTNUMERICS_ENABLE_FETCH=OFF` to require an existing package. GoogleTest is not installed or exported with HostNumerics.
Comment thread
newling marked this conversation as resolved.
Resolved

## Build and test

The standalone build requires CMake 3.25.2 or newer, a C++20 compiler, and Ninja.
Expand Down Expand Up @@ -97,7 +99,7 @@ are then unnecessary.
when building HostNumerics standalone and `OFF` when another project includes it
with `add_subdirectory`. CI covers ON/ON with Ninja Multi-Config and OFF/OFF with
Ninja, including installation and an external C++ consumer for both. It also
checks the embedded defaults.
checks the embedded defaults. When configuring the package or embedded test project directly, pass `-DHOSTNUMERICS_SOURCE_DIR=/path/to/hostnumerics` so it can locate the shared test source and CMake helper.

Install into a local prefix with:

Expand Down
32 changes: 32 additions & 0 deletions shared/hostnumerics/cmake/HostNumericsTestDependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT

option(HOSTNUMERICS_ENABLE_FETCH "Allow test builds to fetch missing dependencies."
${PROJECT_IS_TOP_LEVEL})

if(TARGET GTest::gtest_main)
return()
endif()
Comment thread
newling marked this conversation as resolved.
Resolved
# Basic assertions work with older installed packages too.
find_package(GTest 1.12.1 QUIET)
if(NOT GTest_FOUND)
if(NOT HOSTNUMERICS_ENABLE_FETCH)
message(FATAL_ERROR "HostNumerics tests require GTest 1.12.1 or newer.")
endif()
include(FetchContent)
# GoogleTest belongs to the test build, never the installed component.
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
# Match the parent runtime on MSVC, independently of static/shared libraries.
if(MSVC AND (NOT CMAKE_MSVC_RUNTIME_LIBRARY OR CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "DLL"))
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
# Match the pin in cmake/modules/shared_third_party.cmake in the superbuild.
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.tar.gz
URL_HASH SHA256=65fab701d9829d38cb77c14acdc431d2108bfdbf8979e40eb8ae567edf10b27c
SYSTEM
)
FetchContent_MakeAvailable(googletest)
endif()
6 changes: 4 additions & 2 deletions shared/hostnumerics/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Copyright Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT

include(HostNumericsTestDependencies)

add_executable(hostnumerics-version-test version_test.cpp)
target_link_libraries(hostnumerics-version-test PRIVATE roc::hostnumerics)
target_link_libraries(hostnumerics-version-test PRIVATE roc::hostnumerics GTest::gtest_main)
add_test(NAME hostnumerics-version COMMAND hostnumerics-version-test)

add_executable(hostnumerics-scalar-type-test scalar_type_test.cpp)
target_link_libraries(hostnumerics-scalar-type-test PRIVATE roc::hostnumerics-core)
target_link_libraries(hostnumerics-scalar-type-test PRIVATE roc::hostnumerics-core GTest::gtest_main)
add_test(NAME hostnumerics-scalar-type COMMAND hostnumerics-scalar-type-test)
14 changes: 10 additions & 4 deletions shared/hostnumerics/tests/embedded/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
cmake_minimum_required(VERSION 3.25.2)
project(HostNumericsEmbeddedTest LANGUAGES CXX)

if(NOT HOSTNUMERICS_SOURCE_DIR)
message(FATAL_ERROR "Set HOSTNUMERICS_SOURCE_DIR to the HostNumerics source tree.")
endif()

set(CMAKE_DISABLE_FIND_PACKAGE_Python TRUE)
set(CMAKE_DISABLE_FIND_PACKAGE_nanobind TRUE)
add_subdirectory(../.. hostnumerics)
if(HOSTNUMERICS_BUILD_TESTING OR HOSTNUMERICS_BUILD_PYTHON)
add_subdirectory("${HOSTNUMERICS_SOURCE_DIR}" hostnumerics)
if(HOSTNUMERICS_BUILD_TESTING OR HOSTNUMERICS_BUILD_PYTHON OR TARGET GTest::gtest)
message(FATAL_ERROR "Embedded HostNumerics should default to no tests or Python.")
endif()

add_executable(hostnumerics-embedded-test ../version_test.cpp)
target_link_libraries(hostnumerics-embedded-test PRIVATE roc::hostnumerics)
list(APPEND CMAKE_MODULE_PATH "${HOSTNUMERICS_SOURCE_DIR}/cmake")
include(HostNumericsTestDependencies)
add_executable(hostnumerics-embedded-test "${HOSTNUMERICS_SOURCE_DIR}/tests/version_test.cpp")
target_link_libraries(hostnumerics-embedded-test PRIVATE roc::hostnumerics GTest::gtest_main)

enable_testing()
add_test(NAME hostnumerics-embedded-test COMMAND hostnumerics-embedded-test)
10 changes: 8 additions & 2 deletions shared/hostnumerics/tests/package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ project(HostNumericsPackageTest LANGUAGES CXX)

find_package(HostNumerics CONFIG REQUIRED)

add_executable(hostnumerics-package-test ../version_test.cpp)
target_link_libraries(hostnumerics-package-test PRIVATE roc::hostnumerics)
if(NOT HOSTNUMERICS_SOURCE_DIR)
message(FATAL_ERROR "Set HOSTNUMERICS_SOURCE_DIR to the HostNumerics source tree.")
endif()

list(APPEND CMAKE_MODULE_PATH "${HOSTNUMERICS_SOURCE_DIR}/cmake")
include(HostNumericsTestDependencies)
add_executable(hostnumerics-package-test "${HOSTNUMERICS_SOURCE_DIR}/tests/version_test.cpp")
target_link_libraries(hostnumerics-package-test PRIVATE roc::hostnumerics GTest::gtest_main)

enable_testing()
add_test(NAME hostnumerics-package-test COMMAND hostnumerics-package-test)
54 changes: 27 additions & 27 deletions shared/hostnumerics/tests/scalar_type_test.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT

#include <gtest/gtest.h>

#include <complex>
#include <initializer_list>
#include <roc/hostnumerics/scalar_type.hpp>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>

int main() {
TEST(Core, ScalarTypes) {
using namespace roc::hostnumerics;
// nativeScalarType<std::string> is a compilation error. Use NativeScalar<T>
// to query whether T has a mapping without causing an error.
Expand Down Expand Up @@ -39,30 +41,26 @@ int main() {
static_assert(!NativeScalar<std::string>);
static_assert(!NativeScalar<std::string_view>);

const auto require = [](bool condition, const char* message) {
if (!condition) throw std::runtime_error(message);
};

static_assert(scalarTypeInfo(ScalarType::Float32).exponentBits == 8);

// IEEE binary16 has a five-bit exponent, ten fraction bits, and bias 15.
const auto& half = scalarTypeInfo(ScalarType::Float16);
require(half.name == "f16" && half.category == ScalarCategory::FloatingPoint &&
ASSERT_TRUE(half.name == "f16" && half.category == ScalarCategory::FloatingPoint &&
half.storageBits == 16 && half.exponentBits == 5 && half.mantissaBits == 10 &&
half.exponentBias == 15 && half.supportsNaN && half.supportsInfinity &&
!half.isPacked(),
"Incorrect binary16 metadata.");
!half.isPacked())
<< "Incorrect binary16 metadata.";
const auto& complex = scalarTypeInfo(ScalarType::ComplexFloat32);
require(complex.category == ScalarCategory::Complex && complex.storageBits == 64 &&
complex.exponentBits == 8 && complex.mantissaBits == 23,
"Complex metadata must count both components' storage.");
ASSERT_TRUE(complex.category == ScalarCategory::Complex && complex.storageBits == 64 &&
complex.exponentBits == 8 && complex.mantissaBits == 23)
<< "Complex metadata must count both components' storage.";
const auto& packed = scalarTypeInfo(ScalarType::Float6E3M2);
require(packed.storageBits == 6 && packed.isPacked() && !packed.supportsNaN &&
!packed.supportsInfinity,
"Incorrect finite six-bit encoding metadata.");
require(scalarTypeInfo(ScalarType::Int4).category == ScalarCategory::SignedInteger &&
scalarTypeInfo(ScalarType::E8M0).category == ScalarCategory::FloatingPoint,
"Incorrect integer or floating-point category.");
ASSERT_TRUE(packed.storageBits == 6 && packed.isPacked() && !packed.supportsNaN &&
!packed.supportsInfinity)
<< "Incorrect finite six-bit encoding metadata.";
ASSERT_TRUE(scalarTypeInfo(ScalarType::Int4).category == ScalarCategory::SignedInteger &&
scalarTypeInfo(ScalarType::E8M0).category == ScalarCategory::FloatingPoint)
<< "Incorrect integer or floating-point category.";

// Check each identifier independently of its position in the metadata table.
constexpr std::pair<ScalarType, std::string_view> expectedNames[] = {
Expand Down Expand Up @@ -96,25 +94,27 @@ int main() {
};
static_assert(std::size(expectedNames) == scalarTypeCount);
for (const auto& [type, name] : expectedNames) {
require(scalarTypeName(type) == name, "Scalar identifier has the wrong metadata row.");
require(scalarTypeInfo(type).category < ScalarCategory::Count,
"Scalar metadata has an invalid category.");
ASSERT_TRUE(scalarTypeName(type) == name)
<< "Scalar identifier has the wrong metadata row.";
ASSERT_TRUE(scalarTypeInfo(type).category < ScalarCategory::Count)
<< "Scalar metadata has an invalid category.";
}

for (ScalarType type :
{ScalarType::E4M3, ScalarType::E5M3, ScalarType::E8M0, ScalarType::E8M0Zero}) {
const auto& info = scalarTypeInfo(type);
require(info.category == ScalarCategory::FloatingPoint && !info.isSigned,
"Unsigned floating-point metadata mismatch.");
ASSERT_TRUE(info.category == ScalarCategory::FloatingPoint && !info.isSigned)
<< "Unsigned floating-point metadata mismatch.";
}
require(scalarTypeInfo(ScalarType::Float32).isSigned &&
ASSERT_TRUE(scalarTypeInfo(ScalarType::Float32).isSigned &&
scalarTypeInfo(ScalarType::ComplexFloat32).isSigned &&
scalarTypeInfo(ScalarType::Int4).isSigned &&
!scalarTypeInfo(ScalarType::UInt8).isSigned &&
!scalarTypeInfo(ScalarType::Boolean).isSigned,
"Scalar signedness metadata mismatch.");
!scalarTypeInfo(ScalarType::Boolean).isSigned)
<< "Scalar signedness metadata mismatch.";

for (ScalarType invalid : {ScalarType::Count, static_cast<ScalarType>(0xffff)}) {
require(!isConcreteScalarType(invalid), "Invalid scalar type classified as concrete.");
ASSERT_FALSE(isConcreteScalarType(invalid))
<< "Invalid scalar type classified as concrete.";
}
}
7 changes: 5 additions & 2 deletions shared/hostnumerics/tests/version_test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT

#include <gtest/gtest.h>

#include <roc/hostnumerics/scalar_type.hpp>
#include <roc/hostnumerics/version.hpp>

int main() {
TEST(Core, Version) {
using namespace roc::hostnumerics;
return version() == "0.1.0" && scalarTypeInfo(ScalarType::Float16).storageBits == 16 ? 0 : 1;
EXPECT_EQ(version(), "0.1.0");
EXPECT_EQ(scalarTypeInfo(ScalarType::Float16).storageBits, 16);
}
Loading