Initial commit

This commit is contained in:
Bram Veenboer
2025-08-01 13:56:02 +02:00
commit a189a80cc2
20 changed files with 500 additions and 0 deletions

24
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,24 @@
include(FetchContent)
FetchContent_Declare(
catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.9.0)
FetchContent_MakeAvailable(catch2)
# Lookup backend test
add_executable(test_lookup test_lookup.cpp)
target_link_libraries(test_lookup PRIVATE trigdx Catch2::Catch2WithMain)
# MKL backend test
if(USE_MKL)
add_executable(test_mkl test_mkl.cpp)
target_link_libraries(test_mkl PRIVATE trigdx Catch2::Catch2WithMain)
endif()
include(CTest)
add_test(NAME test_lookup COMMAND test_lookup)
if(USE_MKL)
add_test(NAME test_mkl COMMAND test_mkl)
endif()

19
tests/test_lookup.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include <catch2/catch_test_macros.hpp>
#include <trigdx/lookup.hpp>
#include "test_utils.hpp"
TEST_CASE("sinf") {
test_sinf<LookupBackend<16384>>(1e-2f);
test_sinf<LookupBackend<32768>>(1e-2f);
}
TEST_CASE("cosf") {
test_cosf<LookupBackend<16384>>(1e-2f);
test_cosf<LookupBackend<32768>>(1e-2f);
}
TEST_CASE("sincosf") {
test_sincosf<LookupBackend<16384>>(1e-2f);
test_sincosf<LookupBackend<32768>>(1e-2f);
}

10
tests/test_mkl.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include <catch2/catch_test_macros.hpp>
#include <trigdx/mkl.hpp>
#include "test_utils.hpp"
TEST_CASE("sinf") { test_sinf<MKLBackend>(1e-6f); }
TEST_CASE("cosf") { test_cosf<MKLBackend>(1e-6f); }
TEST_CASE("sincosf") { test_sincosf<MKLBackend>(1e-6f); }

68
tests/test_utils.hpp Normal file
View File

@@ -0,0 +1,68 @@
#pragma once
#include <algorithm>
#include <cmath>
#include <vector>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <trigdx/reference.hpp>
const size_t N = 1e7;
template <typename Backend> inline void test_sinf(float tol) {
std::vector<float> x(N), s_ref(N), s(N);
for (size_t i = 0; i < N; ++i) {
x[i] = float(i) * 0.01f;
}
ReferenceBackend ref;
Backend backend;
backend.init();
ref.compute_sinf(N, x.data(), s_ref.data());
backend.compute_sinf(N, x.data(), s.data());
for (size_t i = 0; i < N; ++i) {
REQUIRE_THAT(s[i], Catch::Matchers::WithinAbs(s_ref[i], tol));
}
}
template <typename Backend> inline void test_cosf(float tol) {
std::vector<float> x(N), c_ref(N), c(N);
for (size_t i = 0; i < N; ++i) {
x[i] = float(i) * 0.01f;
}
ReferenceBackend ref;
Backend backend;
backend.init();
ref.compute_cosf(N, x.data(), c_ref.data());
backend.compute_cosf(N, x.data(), c.data());
for (size_t i = 0; i < N; ++i) {
REQUIRE_THAT(c[i], Catch::Matchers::WithinAbs(c_ref[i], tol));
}
}
template <typename Backend> inline void test_sincosf(float tol) {
std::vector<float> x(N), s_ref(N), c_ref(N), s(N), c(N);
for (size_t i = 0; i < N; ++i) {
x[i] = float(i) * 0.01f;
}
ReferenceBackend ref;
Backend backend;
backend.init();
ref.compute_sincosf(N, x.data(), s_ref.data(), c_ref.data());
backend.compute_sincosf(N, x.data(), s.data(), c.data());
for (size_t i = 0; i < N; ++i) {
REQUIRE_THAT(s[i], Catch::Matchers::WithinAbs(s_ref[i], tol));
REQUIRE_THAT(c[i], Catch::Matchers::WithinAbs(c_ref[i], tol));
}
}