Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
FrequencySeriesTester.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "cheetah/data/test_utils/FrequencySeriesTester.h"
25 #include "cheetah/data/FrequencySeries.h"
26 #include <panda/TypeTraits.h>
27 
28 
29 namespace ska {
30 namespace cheetah {
31 namespace data {
32 namespace test {
33 
34 namespace {
35  template<class Alloc, typename DeviceType, typename Enable>
36  struct ConstructAllocatorHelper;
37 
38  template<class Alloc, typename DeviceType>
39  struct ConstructAllocatorHelper<Alloc, DeviceType, std::true_type>
40  {
41  static inline
42  Alloc construct(DeviceType& device)
43  {
44  return Alloc(device);
45  }
46  };
47 
48  template<class Alloc, typename DeviceType>
49  struct ConstructAllocatorHelper<Alloc, DeviceType, std::false_type> {
50  static inline
51  Alloc construct(DeviceType&)
52  {
53  return Alloc();
54  }
55  };
56  template<class Alloc, typename DeviceType>
57  struct ConstructAllocator
58  {
59  private:
60  template<typename T>
61  using HasDeviceConstructorT = decltype(T(std::declval<DeviceType&>()));
62 
63  public:
64  static inline
65  Alloc construct(DeviceType& device)
66  {
67  return ConstructAllocatorHelper<Alloc, DeviceType, typename std::conditional<panda::HasMethod<Alloc,HasDeviceConstructorT>::value, std::true_type, std::false_type>::type>::construct(device);
68  }
69  };
70 }// namespace
71 
72 template<typename FrequencySeriesT>
73 typename FrequencySeriesTesterTraits<FrequencySeriesT>::Allocator FrequencySeriesTesterTraits<FrequencySeriesT>::allocator(panda::PoolResource<Arch>& device)
74 {
75  return ConstructAllocator<Allocator, panda::PoolResource<Arch>>::construct(device);
76 }
77 
78 template<typename FrequencySeriesTesterTraitsT>
79 FrequencySeriesTester<FrequencySeriesTesterTraitsT>::FrequencySeriesTester()
80 {
81 }
82 
83 TYPED_TEST_P(FrequencySeriesTester, construct_resize)
84 {
85  typedef TypeParam Traits;
86  typedef typename Traits::FrequencySeriesType FrequencySeriesType;
87 
88  Traits traits;
89  for(auto& device : this->_system.devices()) {
90  FrequencySeriesType series(traits.allocator(*device));
91  ASSERT_EQ(series.size(), std::size_t(0));
92  // increase size
93  series.resize(2);
94  ASSERT_EQ(series.size(), std::size_t(2));
95  // reduce ize
96  series.resize(1);
97  ASSERT_EQ(series.size(), std::size_t(1));
98  // resize to exisiting size should do nothing
99  auto it = series.begin();
100  series.resize(1);
101  ASSERT_EQ(series.size(), std::size_t(1));
102  ASSERT_EQ(it, series.begin());
103  }
104 }
105 
106 TYPED_TEST_P(FrequencySeriesTester, begin_end)
107 {
108  typedef TypeParam Traits;
109  typedef typename Traits::FrequencySeriesType FrequencySeriesType;
110 
111  Traits traits;
112  std::size_t n=2;
113  for(auto& device : this->_system.devices()) {
114  FrequencySeriesType series(n, traits.allocator(*device));
115 
116  // begin end on non-const object
117  std::size_t count=0;
118  {
119  auto it = series.begin();
120  ASSERT_EQ(n, std::size_t(std::distance(it, series.end())));
121  while(it != series.end()) {
122  ++count;
123  ++it;
124  }
125  ASSERT_EQ(count, n);
126  }
127 
128  // begin end on const object
129  const FrequencySeriesType& const_series = series;
130  {
131  count=0;
132  auto it = const_series.begin();
133  ASSERT_EQ(n, std::size_t(std::distance(it, const_series.end())));
134  while(it != const_series.end()) {
135  ++count;
136  ++it;
137  }
138  ASSERT_EQ(count, n);
139  }
140 
141  // cbegin cend on const object
142  {
143  count=0;
144  auto it = const_series.cbegin();
145  ASSERT_EQ(n, std::size_t(std::distance(it, series.cend())));
146  while(it != const_series.cend()) {
147  ++count;
148  ++it;
149  }
150  ASSERT_EQ(count, n);
151  }
152  }
153 }
154 
155 TYPED_TEST_P(FrequencySeriesTester, host_conversion)
156 {
157  typedef TypeParam Traits;
158  typedef typename Traits::FrequencySeriesType FrequencySeriesType;
159  typedef typename FrequencySeriesType::ValueType ValueType;
160  typedef FrequencySeries<panda::Cpu, ValueType, std::allocator<ValueType>> HostFrequencySeriesType;
161  FourierFrequencyType df = 5.4 * hz;
162 
163  // some host based data
164  std::size_t n=5;
165  HostFrequencySeriesType host_data(df, n, std::allocator<ValueType>());
166  ValueType value=0;
167  std::transform(host_data.begin(), host_data.end(), host_data.begin(), [&](ValueType const&) { return ++value; });
168 
169  Traits traits;
170  for(auto& device : this->_system.devices()) {
171  // copy host data to target FrequencySeries
172  auto allocator = traits.allocator(*device);
173  FrequencySeriesType series(host_data, allocator);
174  ASSERT_EQ(series.size(), host_data.size());
175  // copy back from target to host
176  HostFrequencySeriesType host_data_copy(series);
177  ASSERT_EQ(host_data_copy.size(), n);
178  auto copy_it=host_data_copy.cbegin();
179  auto it=host_data.cbegin();
180  std::size_t count=0;
181  while(it!=host_data.cend()) {
182  SCOPED_TRACE("element number: " + std::to_string(count));
183  ASSERT_EQ(*it, *copy_it) << host_data[count];
184  ++it;
185  ++copy_it;
186  ++count;
187  }
188  ASSERT_EQ(df,host_data.frequency_step());
189  }
190 }
191 
192 REGISTER_TYPED_TEST_CASE_P(FrequencySeriesTester, construct_resize, begin_end, host_conversion);
193 
194 } // namespace test
195 } // namespace data
196 } // namespace cheetah
197 } // namespace ska
Some limits and constants for FLDO.
Definition: Brdz.h:35