From 23426476da849e198e0b4b607430b8d6a81ea21c Mon Sep 17 00:00:00 2001 From: "E. G. Patrick Bos" Date: Thu, 5 Oct 2017 16:03:30 +0200 Subject: [PATCH] Tried making gtest work with template types Fails, because gtest uses typedef all over the place, which at some point breaks template compatibility. I guess already when defining MyTypes it fails, since there the template arguments of the "_front" classes are already lost. If that were done using an alias and the aliasing was propagated properly, it might work, but that would be a gtest overhaul. --- test/c2c_1D.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/test/c2c_1D.cpp b/test/c2c_1D.cpp index 9eb67d3..a2bdacc 100644 --- a/test/c2c_1D.cpp +++ b/test/c2c_1D.cpp @@ -12,6 +12,7 @@ #include #include #include +#include // pow #include @@ -20,11 +21,42 @@ template class TransformAndInvert : public ::testing::Test {}; -typedef ::testing::Types MyTypes; +// Define a class that holds the two types we want to pass to our GoogleTest typed tests +// (https://stackoverflow.com/a/29382470/1199693). The variadic template parameter `typename...` +// is because `xarray` and `xtensor` have more template parameters than we care about here. +template class _container, typename _precision> +struct TDefs { + template using container = _container; + using precision = _precision; +}; + +// make a uniform template interface for xarray and xtensor (where dim does nothing for xarray) +template using xarray_front = xt::xarray; +template using xtensor_front = xt::xtensor; + +// the GoogleTest list of typed test cases +typedef ::testing::Types< + TDefs, + TDefs, + TDefs, + TDefs, + TDefs, + TDefs + > MyTypes; TYPED_TEST_CASE(TransformAndInvert, MyTypes); -TYPED_TEST(TransformAndInvert, R2C_1D) { - xt::xarray a = xt::random::rand({8}, 0, std::numeric_limits::max()/8); +// forward declaration (definition below) +template auto generate_data(size_t n); +std::size_t data_size = 4; + +TYPED_TEST(TransformAndInvert, R2C2R_1D) { + std::size_t dim = 1; + + using precision = typename TypeParam::precision; + template using container = class TypeParam::template container; +//using container = typename TypeParam::template container; + // actual test logic + container a = generate_data(data_size); auto a_fourier = xt::fftw::fft(a); @@ -34,4 +66,13 @@ TYPED_TEST(TransformAndInvert, R2C_1D) { std::cout << "fourier transform of input: " << a_fourier << std::endl; std::cout << "real output: " << should_be_a << std::endl; ASSERT_TRUE(xt::allclose(a, should_be_a)); -} \ No newline at end of file +} + + +// Generates a dim-dimensional array of size n in each dimension, filled with random numbers between 0 and the numeric +// limit of type T divided by pow(n, dim) (the latter to keep the FFTs from generating infs and nans). +template +auto generate_data(std::size_t n) { + return xt::random::rand(std::array().fill(n), + 0, std::numeric_limits::max() / std::pow(n, dim)); +}