test_object_pool.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #include <gtest/gtest.h>
36 
37 #include "lockfree/object_pool.h"
38 
39 #include <set>
40 
41 using namespace lockfree;
42 
43 TEST(ObjectPool, oneElement)
44 {
45  ObjectPool<uint32_t> pool(1, 5);
46 
48  ASSERT_TRUE(item);
49  EXPECT_EQ(*item, 5UL);
50  *item = 6;
51  ASSERT_FALSE(pool.allocateShared());
52  item.reset();
53  item = pool.allocateShared();
54  ASSERT_TRUE(item);
55  EXPECT_EQ(*item, 6UL);
56 }
57 
58 TEST(ObjectPool, multipleElements)
59 {
60  const uint32_t count = 5;
61  ObjectPool<uint32_t> pool(count, 5);
62 
63  std::vector<boost::shared_ptr<uint32_t> > items;
64  items.reserve(count);
65 
66  for (uint32_t i = 0; i < count; ++i)
67  {
68  items.push_back(pool.allocateShared());
69  ASSERT_TRUE(items[i]);
70  EXPECT_EQ(*items[i], 5UL);
71  *items[i] = i;
72  }
73 
74  ASSERT_FALSE(pool.allocateShared());
75  items.pop_back();
76  items.push_back(pool.allocateShared());
77  ASSERT_TRUE(items.back());
78  ASSERT_FALSE(pool.allocateShared());
79 
80  std::set<boost::shared_ptr<uint32_t> > set;
81  set.insert(items.begin(), items.end());
82  EXPECT_EQ(set.size(), count);
83 }
84 
85 int main(int argc, char** argv)
86 {
87  testing::InitGoogleTest(&argc, argv);
88  return RUN_ALL_TESTS();
89 }
int main(int argc, char **argv)
A fixed-count lock-free pool of the same type of object. Supports both bare- and shared-pointer alloc...
Definition: object_pool.h:48
boost::shared_ptr< T > allocateShared()
Allocate a single object from the pool, returning a shared pointer.
Definition: object_pool.h:278
TEST(ObjectPool, oneElement)


lockfree
Author(s): Josh Faust
autogenerated on Fri Apr 5 2019 02:16:37