JayBeams  0.1
Another project to have fun coding.
ut_traits.cpp
Go to the documentation of this file.
1 #include <jb/fftw/traits.hpp>
3 #include <valgrind/valgrind.h>
4 
5 #include <boost/test/unit_test.hpp>
6 #include <algorithm>
7 
8 namespace {
9 
10 template <typename precision_t>
11 void test_fftw_traits() {
12  int nsamples = 32768;
13  int tol = nsamples;
14  using tested = jb::fftw::traits<precision_t>;
15  using fftw_complex_type = typename tested::fftw_complex_type;
16 
17  fftw_complex_type* in = static_cast<fftw_complex_type*>(
18  tested::allocate(nsamples * sizeof(fftw_complex_type)));
19  fftw_complex_type* tmp = static_cast<fftw_complex_type*>(
20  tested::allocate(nsamples * sizeof(fftw_complex_type)));
21  fftw_complex_type* out = static_cast<fftw_complex_type*>(
22  tested::allocate(nsamples * sizeof(fftw_complex_type)));
23 
24  std::size_t h = nsamples / 2;
25  for (std::size_t i = 0; i != h; ++i) {
26  in[i][0] = i - h / 4.0;
27  in[i][1] = 0;
28  in[i + h][0] = h / 4.0 - i;
29  in[i + h][1] = 0;
30  }
31 
32  using plan_type = typename tested::fftw_plan_type;
33  plan_type dir = tested::create_forward_plan(
34  nsamples, in, tmp, FFTW_ESTIMATE | FFTW_UNALIGNED | FFTW_PRESERVE_INPUT);
35  plan_type inv = tested::create_backward_plan(
36  nsamples, tmp, out, FFTW_ESTIMATE | FFTW_UNALIGNED | FFTW_PRESERVE_INPUT);
37 
38  tested::execute_plan(dir, in, tmp);
39  tested::execute_plan(inv, tmp, out);
40  for (std::size_t i = 0; i != std::size_t(nsamples); ++i) {
41  out[i][0] /= nsamples;
42  out[i][1] /= nsamples;
43  }
44  bool res = jb::testing::check_collection_close_enough(nsamples, out, in, tol);
45  BOOST_CHECK_MESSAGE(res, "collections are not within tolerance=" << tol);
46 
47  tested::destroy_plan(inv);
48  tested::destroy_plan(dir);
49  tested::release(out);
50  tested::release(tmp);
51  tested::release(in);
52 }
53 
54 } // anonymous namespace
55 
56 /**
57  * @test Verify that we can compile jb::fftw::traits<double>
58  */
59 BOOST_AUTO_TEST_CASE(fftw_traits_double) {
60  test_fftw_traits<double>();
61 }
62 
63 /**
64  * @test Verify that we can compile jb::fftw::traits<float>
65  */
66 BOOST_AUTO_TEST_CASE(fftw_traits_float) {
67  test_fftw_traits<float>();
68 }
69 
70 /**
71  * @test Verify that we can compile jb::fftw::traits<long double>
72  */
73 BOOST_AUTO_TEST_CASE(fftw_traits_long_double) {
74  if (RUNNING_ON_VALGRIND > 0) {
75  BOOST_TEST_MESSAGE("long double not supported by valgrind, skip test");
76  return;
77  }
78  test_fftw_traits<long double>();
79 }
Wrap fftw_* types and operations to treat floating point values generically.
Definition: traits.hpp:22
plan< in_array_type, out_array_type > create_forward_plan(in_array_type const &in, out_array_type &out, int flags=default_plan_flags)
Create a plan to compute many DFTs given the input and output arrays.
Definition: plan.hpp:231
plan< in_array_type, out_array_type > create_backward_plan(in_array_type const &in, out_array_type &out, int flags=default_plan_flags)
Create a plan to compute many inverse DFT given the input and output arrays.
Definition: plan.hpp:261
bool check_collection_close_enough(collection_t const &a, collection_t const &b, int tol=1, int max_differences_printed=JB_TESTING_MAX_DIFFERENCES_PRINTED)
Given two collections of numbers of the same value type, find the differences that are out of a given...
std::size_t nsamples(container_type const &a)
Count the elements in the last dimension of a vector-like container.
BOOST_AUTO_TEST_CASE(fftw_traits_double)
Definition: ut_traits.cpp:59