JayBeams  0.1
Another project to have fun coding.
ut_allocator.cpp
Go to the documentation of this file.
1 #include <jb/fftw/allocator.hpp>
2 #include <jb/fftw/plan.hpp>
4 #include <valgrind/valgrind.h>
5 
6 #include <boost/test/unit_test.hpp>
7 #include <vector>
8 
9 namespace {
10 
11 template <typename T>
12 using aligned_vector = std::vector<T, jb::fftw::allocator<T>>;
13 
14 template <typename precision_t>
15 void test_plan_real2complex() {
16  int nsamples = 1 << 15;
17  int tol = nsamples;
18 
19  typedef std::complex<precision_t> complex;
20 
21  aligned_vector<precision_t> in(nsamples);
22  aligned_vector<complex> tmp(nsamples);
23  aligned_vector<precision_t> out(nsamples);
24 
25  std::size_t h = nsamples / 2;
26  for (std::size_t i = 0; i != h; ++i) {
27  in[i] = i - h / 4.0;
28  in[i + h] = h / 4.0 - i;
29  }
30 
31  auto dir = jb::fftw::create_forward_plan(in, tmp);
32  auto inv = jb::fftw::create_backward_plan(tmp, out);
33 
34  dir.execute(in, tmp);
35  inv.execute(tmp, out);
36  for (std::size_t i = 0; i != std::size_t(nsamples); ++i) {
37  out[i] /= nsamples;
38  }
39  bool res = jb::testing::check_collection_close_enough(out, in, tol);
40  BOOST_CHECK_MESSAGE(res, "collections are not within tolerance=" << tol);
41 }
42 
43 } // anonymous namespace
44 
45 /**
46  * @test Verify that we can use jb::fftw::allocator<>
47  */
48 BOOST_AUTO_TEST_CASE(fftw_allocator_double) {
49  test_plan_real2complex<double>();
50 }
51 
52 /**
53  * @test Verify that we can use jb::fftw::allocator<>
54  */
55 BOOST_AUTO_TEST_CASE(fftw_allocator_float) {
56  test_plan_real2complex<float>();
57 }
58 
59 /**
60  * @test Verify that we can use jb::fftw::allocator<>
61  */
62 BOOST_AUTO_TEST_CASE(fftw_allocator_long_double) {
63  if (RUNNING_ON_VALGRIND > 0) {
64  BOOST_TEST_MESSAGE("long double not supported by valgrind, skip test");
65  return;
66  }
67  test_plan_real2complex<long double>();
68 }
std::vector< T, jb::fftw::allocator< T > > aligned_vector
Alias std::vector with properly allocated storage for FFTW3.
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_allocator_double)