gtest_buffer.cpp
Go to the documentation of this file.
1 /*
2  @file
3 
4  @author victor
5 
6  @copyright (c) 2018 PAL Robotics SL. All Rights Reserved
7 */
8 
10 #include <boost/circular_buffer.hpp>
11 #include <gtest/gtest.h>
12 #include <ros/ros.h>
13 #include <climits>
14 #include <cfloat>
15 #include <memory>
16 using namespace std;
17 
18 namespace pal_statistics
19 {
20 template <class T>
21 class MyAlloc
22 {
23 public:
24  // type definitions
25  typedef T value_type;
26  typedef T* pointer;
27  typedef const T* const_pointer;
28  typedef T& reference;
29  typedef const T& const_reference;
30  typedef std::size_t size_type;
31  typedef std::ptrdiff_t difference_type;
32 
33  // rebind allocator to type U
34  template <class U>
35  struct rebind
36  {
37  typedef MyAlloc<U> other;
38  };
39 
40  // return address of values
41  pointer address(reference value) const
42  {
43  return &value;
44  }
45  const_pointer address(const_reference value) const
46  {
47  return &value;
48  }
49 
50  /* constructors and destructor
51  * - nothing to do because the allocator has no state
52  */
53  MyAlloc() throw()
54  {
55  }
56  MyAlloc(const MyAlloc&) throw()
57  {
58  }
59  template <class U>
60  MyAlloc(const MyAlloc<U>&) throw()
61  {
62  }
63  ~MyAlloc() throw()
64  {
65  }
66 
67  // return maximum number of elements that can be allocated
68  size_type max_size() const throw()
69  {
70  return std::numeric_limits<std::size_t>::max() / sizeof(T);
71  }
72 
73  // allocate but don't initialize num elements of type T
74  pointer allocate(size_type num, const void* = 0)
75  {
76  // print message and allocate memory with global new
77  std::cerr << typeid(T).name() << " allocate " << num << " element(s)"
78  << " of size " << sizeof(T) << std::endl;
79  pointer ret = (pointer)(::operator new(num * sizeof(T)));
80  std::cerr << " allocated at: " << (void*)ret << std::endl;
81  return ret;
82  }
83 
84  // initialize elements of allocated storage p with value value
85  void construct(pointer p, const T& value)
86  {
87  // initialize memory with placement new
88  new ((void*)p) T(value);
89  }
90 
91  // destroy elements of initialized storage p
92  void destroy(pointer p)
93  {
94  // destroy objects by calling their destructor
95  p->~T();
96  }
97 
98  // deallocate storage p of deleted elements
100  {
101  // print message and deallocate memory with global delete
102  std::cerr << typeid(T).name() << " deallocate " << num << " element(s)"
103  << " of size " << sizeof(T) << " at: " << (void*)p << std::endl;
104  ::operator delete((void*)p);
105  }
106 };
107 
108 // return that all specializations of this allocator are interchangeable
109 template <class T1, class T2>
110 bool operator==(const MyAlloc<T1>&, const MyAlloc<T2>&) throw()
111 {
112  return true;
113 }
114 template <class T1, class T2>
115 bool operator!=(const MyAlloc<T1>&, const MyAlloc<T2>&) throw()
116 {
117  return false;
118 }
119 
120 
121 
122 TEST(BufferTest, basicTest)
123 {
125  buffer.set_capacity(5, 0);
126 
127 
128  for (int i = 0; i < buffer.capacity(); ++i)
129  {
130  buffer.push_back() = i;
131  }
132 
133  for (int i = 0; i < buffer.capacity(); ++i)
134  {
135  ROS_INFO_STREAM(i);
136  EXPECT_EQ(i, buffer.front());
137  buffer.pop_front();
138  }
139 }
140 
141 TEST(BufferTest, buffer)
142 {
143  typedef std::vector<int, MyAlloc<int>> MyVector;
144 
145 
146 
147  ROS_INFO_STREAM("Resizing vector");
148  MyAlloc<int> my_alloc;
149  MyVector v(my_alloc);
150  v.resize(1000);
151 
152  MyAlloc<MyVector> my_buffer_alloc;
153  ROS_INFO_STREAM("Creating circular buffer");
154  StaticCircularBuffer<MyVector, MyAlloc<MyVector>> buffer(10, v, my_buffer_alloc);
155 
156  ASSERT_EQ(buffer.size(), 0);
157  ASSERT_EQ(buffer.capacity(), 10);
158 
159  ROS_INFO_STREAM("Size of type " << sizeof(std::vector<int>) << " size of container "
160  << v.size() * sizeof(int));
161  ROS_INFO_STREAM("Pushing first elements of vector");
162  for (int i = 0; i < 10; ++i)
163  {
164  buffer.push_back();
165  ASSERT_EQ(i + 1, buffer.size());
166  }
167 
168  ROS_INFO_STREAM("Pushing second elements of vector");
169  for (int i = 0; i < 10; ++i)
170  {
171  buffer.push_back();
172  ASSERT_EQ(10, buffer.size());
173  }
174  /*
175  ROS_INFO_STREAM("Increasing size of container");
176  v.resize(1001);
177  ROS_INFO_STREAM("Pushing first elements of vector");
178  for (int i = 0; i < 10; ++i)
179  {
180  buffer.push_back(v);
181  }
182 
183  ROS_INFO_STREAM("Pushing second elements of vector");
184  for (int i = 0; i < 10; ++i)
185  {
186  buffer.push_back(v);
187  }
188 
189 
190  ROS_INFO_STREAM("Reducing size of container");
191  v.resize(100);
192  ROS_INFO_STREAM("Pushing first elements of vector");
193  for (int i = 0; i < 10; ++i)
194  {
195  buffer.push_back(v);
196  }
197 
198  ROS_INFO_STREAM("Pushing second elements of vector");
199  for (int i = 0; i < 10; ++i)
200  {
201  buffer.push_back(v);
202  }
203 
204 
205  ROS_INFO_STREAM("Increasing size of container");
206  v.resize(3000);
207  ROS_INFO_STREAM("Resizing and clearing buffer");
208  buffer.clear();
209  buffer.resize(10, v);
210  buffer.clear();
211  ROS_INFO_STREAM("Pushing first elements of vector");
212  for (int i = 0; i < 10; ++i)
213  {
214  buffer.push_back(v);
215  }
216 
217  ROS_INFO_STREAM("Pushing second elements of vector");
218  for (int i = 0; i < 10; ++i)
219  {
220  buffer.push_back(v);
221  }
222 
223  ROS_INFO_STREAM("Pushing and popping");
224  for (int i = 0; i < 10; ++i)
225  {
226  buffer.pop_front();
227  buffer.push_back(v);
228  }
229  */
230  ROS_WARN_STREAM("Destructor");
231 }
232 
233 TEST(BufferTest, circularBuffer)
234 {
235  typedef std::vector<int, MyAlloc<int>> MyVector;
236 
237 
238 
239  ROS_INFO_STREAM("Resizing vector");
240  MyAlloc<int> my_alloc;
241  MyVector v(my_alloc);
242  v.resize(1000);
243 
244  MyAlloc<MyVector> my_buffer_alloc;
245  ROS_INFO_STREAM("Creating circular buffer");
246  boost::circular_buffer<MyVector, MyAlloc<MyVector>> buffer(10, v, my_buffer_alloc);
247 
248  ROS_INFO_STREAM("Size of type " << sizeof(std::vector<int>) << " size of container "
249  << v.size() * sizeof(int));
250  ROS_INFO_STREAM("Pushing first elements of vector");
251  for (int i = 0; i < 10; ++i)
252  {
253  buffer.push_back(v);
254  // ASSERT_EQ(i+1, buffer.size());
255  }
256 
257  ROS_INFO_STREAM("Pushing second elements of vector");
258  for (int i = 0; i < 10; ++i)
259  {
260  buffer.push_back(v);
261  // ASSERT_EQ(10, buffer.size());
262  }
263 
264  ROS_INFO_STREAM("Increasing size of container");
265  v.resize(1001);
266  ROS_INFO_STREAM("Pushing first elements of vector");
267  for (int i = 0; i < 10; ++i)
268  {
269  buffer.push_back(v);
270  }
271 
272  ROS_INFO_STREAM("Pushing second elements of vector");
273  for (int i = 0; i < 10; ++i)
274  {
275  buffer.push_back(v);
276  }
277 
278 
279  ROS_INFO_STREAM("Reducing size of container");
280  v.resize(100);
281  ROS_INFO_STREAM("Pushing first elements of vector");
282  for (int i = 0; i < 10; ++i)
283  {
284  buffer.push_back(v);
285  }
286 
287  ROS_INFO_STREAM("Pushing second elements of vector");
288  for (int i = 0; i < 10; ++i)
289  {
290  buffer.push_back(v);
291  }
292 
293 
294  ROS_INFO_STREAM("Increasing size of container");
295  v.resize(3000);
296  ROS_INFO_STREAM("Resizing and clearing buffer");
297  buffer.clear();
298  buffer.resize(10, v);
299  buffer.clear();
300  ROS_INFO_STREAM("Pushing first elements of vector");
301  for (int i = 0; i < 10; ++i)
302  {
303  buffer.push_back(v);
304  }
305 
306  ROS_INFO_STREAM("Pushing second elements of vector");
307  for (int i = 0; i < 10; ++i)
308  {
309  buffer.push_back(v);
310  }
311 
312  ROS_INFO_STREAM("Popping");
313  for (int i = 0; i < 10; ++i)
314  {
315  buffer.pop_front();
316  }
317 
318  ROS_INFO_STREAM("Pushing after pop ");
319  for (int i = 0; i < 20; ++i)
320  {
321  buffer.push_back(v);
322  }
323 
324 
325  ROS_WARN_STREAM("Destructor");
326 }
327 } // namespace pal_statistics
328 
329 int main(int argc, char** argv)
330 {
331  ros::init(argc, argv, "buffer_test");
332 
333  testing::InitGoogleTest(&argc, argv);
334  return RUN_ALL_TESTS();
335 }
pal_statistics::MyAlloc::MyAlloc
MyAlloc(const MyAlloc &)
Definition: gtest_buffer.cpp:56
pal_statistics::MyAlloc::MyAlloc
MyAlloc()
Definition: gtest_buffer.cpp:53
pal_statistics::operator!=
bool operator!=(const MyAlloc< T1 > &, const MyAlloc< T2 > &)
Definition: gtest_buffer.cpp:115
StaticCircularBuffer::size
size_t size() const
Definition: static_circular_buffer.h:86
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
pal_statistics::MyAlloc::address
pointer address(reference value) const
Definition: gtest_buffer.cpp:41
ros.h
pal_statistics::MyAlloc::size_type
std::size_t size_type
Definition: gtest_buffer.cpp:30
pal_statistics::MyAlloc::deallocate
void deallocate(pointer p, size_type num)
Definition: gtest_buffer.cpp:99
StaticCircularBuffer::push_back
T & push_back()
push_back Increases the buffer size (not capacity) by one, and returns a reference to the last item i...
Definition: static_circular_buffer.h:109
StaticCircularBuffer::capacity
size_t capacity() const
Definition: static_circular_buffer.h:81
pal_statistics::MyAlloc::max_size
size_type max_size() const
Definition: gtest_buffer.cpp:68
pal_statistics::MyAlloc::const_reference
const typedef T & const_reference
Definition: gtest_buffer.cpp:29
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
pal_statistics::MyAlloc::reference
T & reference
Definition: gtest_buffer.cpp:28
operator==
bool operator==(const in6_addr a, const in6_addr b)
pal_statistics::MyAlloc::~MyAlloc
~MyAlloc()
Definition: gtest_buffer.cpp:63
pal_statistics::MyAlloc::value_type
T value_type
Definition: gtest_buffer.cpp:25
pal_statistics::MyAlloc
Definition: gtest_buffer.cpp:21
pal_statistics
Definition: extract_rosbag_signals.h:14
pal_statistics::MyAlloc::destroy
void destroy(pointer p)
Definition: gtest_buffer.cpp:92
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
main
int main(int argc, char **argv)
Definition: gtest_buffer.cpp:329
pal_statistics::MyAlloc::MyAlloc
MyAlloc(const MyAlloc< U > &)
Definition: gtest_buffer.cpp:60
pal_statistics::MyAlloc::rebind
Definition: gtest_buffer.cpp:35
pal_statistics::MyAlloc::const_pointer
const typedef T * const_pointer
Definition: gtest_buffer.cpp:27
static_circular_buffer.h
std
pal_statistics::MyAlloc::allocate
pointer allocate(size_type num, const void *=0)
Definition: gtest_buffer.cpp:74
StaticCircularBuffer::set_capacity
void set_capacity(size_t max_size, const T &val)
set_capacity Allocates memory for max_size copies of val
Definition: static_circular_buffer.h:74
pal_statistics::MyAlloc::pointer
T * pointer
Definition: gtest_buffer.cpp:26
pal_statistics::TEST
TEST(BufferTest, circularBuffer)
Definition: gtest_buffer.cpp:233
pal_statistics::MyAlloc::address
const_pointer address(const_reference value) const
Definition: gtest_buffer.cpp:45
StaticCircularBuffer::pop_front
void pop_front()
pop_front Reduces buffer size by one, advancing the begin iterator
Definition: static_circular_buffer.h:128
StaticCircularBuffer::front
T & front()
Definition: static_circular_buffer.h:96
pal_statistics::MyAlloc::construct
void construct(pointer p, const T &value)
Definition: gtest_buffer.cpp:85
pal_statistics::MyAlloc::rebind::other
MyAlloc< U > other
Definition: gtest_buffer.cpp:37
pal_statistics::MyAlloc::difference_type
std::ptrdiff_t difference_type
Definition: gtest_buffer.cpp:31
StaticCircularBuffer
CircularBuffer implementation that does not perform allocations/deallocations outside of the construc...
Definition: static_circular_buffer.h:41


pal_statistics
Author(s):
autogenerated on Fri Aug 2 2024 08:29:35