test_chain.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 "ros/time.h"
38 #include <ros/init.h>
39 #include "message_filters/chain.h"
40 
41 using namespace message_filters;
42 
43 struct Msg
44 {
45 };
48 
49 class Helper
50 {
51 public:
53  : count_(0)
54  {}
55 
56  void cb()
57  {
58  ++count_;
59  }
60 
61  int32_t count_;
62 };
63 
65 
66 TEST(Chain, simple)
67 {
68  Helper h;
69  Chain<Msg> c;
70  c.addFilter(boost::make_shared<PassThrough<Msg> >());
71  c.registerCallback(boost::bind(&Helper::cb, &h));
72 
73  c.add(boost::make_shared<Msg>());
74  EXPECT_EQ(h.count_, 1);
75  c.add(boost::make_shared<Msg>());
76  EXPECT_EQ(h.count_, 2);
77 }
78 
79 TEST(Chain, multipleFilters)
80 {
81  Helper h;
82  Chain<Msg> c;
83  c.addFilter(boost::make_shared<PassThrough<Msg> >());
84  c.addFilter(boost::make_shared<PassThrough<Msg> >());
85  c.addFilter(boost::make_shared<PassThrough<Msg> >());
86  c.addFilter(boost::make_shared<PassThrough<Msg> >());
87  c.registerCallback(boost::bind(&Helper::cb, &h));
88 
89  c.add(boost::make_shared<Msg>());
90  EXPECT_EQ(h.count_, 1);
91  c.add(boost::make_shared<Msg>());
92  EXPECT_EQ(h.count_, 2);
93 }
94 
95 TEST(Chain, addingFilters)
96 {
97  Helper h;
98  Chain<Msg> c;
99  c.addFilter(boost::make_shared<PassThrough<Msg> >());
100  c.addFilter(boost::make_shared<PassThrough<Msg> >());
101  c.registerCallback(boost::bind(&Helper::cb, &h));
102 
103  c.add(boost::make_shared<Msg>());
104  EXPECT_EQ(h.count_, 1);
105 
106  c.addFilter(boost::make_shared<PassThrough<Msg> >());
107  c.addFilter(boost::make_shared<PassThrough<Msg> >());
108 
109  c.add(boost::make_shared<Msg>());
110  EXPECT_EQ(h.count_, 2);
111 }
112 
113 TEST(Chain, inputFilter)
114 {
115  Helper h;
116  Chain<Msg> c;
117  c.addFilter(boost::make_shared<PassThrough<Msg> >());
118  c.registerCallback(boost::bind(&Helper::cb, &h));
119 
121  c.connectInput(p);
122  p.add(boost::make_shared<Msg>());
123  EXPECT_EQ(h.count_, 1);
124 
125  p.add(boost::make_shared<Msg>());
126  EXPECT_EQ(h.count_, 2);
127 }
128 
129 TEST(Chain, nonSharedPtrFilter)
130 {
131  Helper h;
132  Chain<Msg> c;
134  c.addFilter(&p);
135  c.registerCallback(boost::bind(&Helper::cb, &h));
136 
137  c.add(boost::make_shared<Msg>());
138  EXPECT_EQ(h.count_, 1);
139  c.add(boost::make_shared<Msg>());
140  EXPECT_EQ(h.count_, 2);
141 }
142 
143 TEST(Chain, retrieveFilter)
144 {
145  Chain<Msg> c;
146 
147  ASSERT_FALSE(c.getFilter<PassThrough<Msg> >(0));
148 
149  c.addFilter(boost::make_shared<PassThrough<Msg> >());
150 
151  ASSERT_TRUE(c.getFilter<PassThrough<Msg> >(0));
152  ASSERT_FALSE(c.getFilter<PassThrough<Msg> >(1));
153 }
154 
155 TEST(Chain, retrieveFilterThroughBaseClass)
156 {
157  Chain<Msg> c;
158  ChainBase* cb = &c;
159 
160  ASSERT_FALSE(cb->getFilter<PassThrough<Msg> >(0));
161 
162  c.addFilter(boost::make_shared<PassThrough<Msg> >());
163 
164  ASSERT_TRUE(cb->getFilter<PassThrough<Msg> >(0));
165  ASSERT_FALSE(cb->getFilter<PassThrough<Msg> >(1));
166 }
167 
168 struct PTDerived : public PassThrough<Msg>
169 {
170 
171 };
172 
173 TEST(Chain, retrieveBaseClass)
174 {
175  Chain<Msg> c;
176  c.addFilter(boost::make_shared<PTDerived>());
177  ASSERT_TRUE(c.getFilter<PassThrough<Msg> >(0));
178  ASSERT_TRUE(c.getFilter<PTDerived>(0));
179 }
180 
181 int main(int argc, char **argv){
182  testing::InitGoogleTest(&argc, argv);
183  ros::init(argc, argv, "blah");
184  ros::Time::init();
185  return RUN_ALL_TESTS();
186 }
187 
188 
boost::shared_ptr< Msg const > MsgConstPtr
Definition: test_chain.cpp:47
void connectInput(F &f)
Connect this filter&#39;s input to another filter&#39;s output.
Definition: chain.h:196
int32_t count_
Definition: test_chain.cpp:61
void add(const MConstPtr &msg)
Add a message to the start of this chain.
Definition: chain.h:205
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
TEST(Chain, simple)
Definition: test_chain.cpp:66
boost::shared_ptr< F > getFilter(size_t index) const
Retrieve a filter from this chain by index. Returns an empty shared_ptr if the index is greater than ...
Definition: chain.h:182
boost::shared_ptr< F > getFilter(size_t index) const
Retrieve a filter from this chain by index. Returns an empty shared_ptr if the index is greater than ...
Definition: chain.h:63
int main(int argc, char **argv)
Definition: test_chain.cpp:181
boost::shared_ptr< Msg > MsgPtr
Definition: test_chain.cpp:46
static void init()
void cb()
Definition: test_chain.cpp:56
Chains a dynamic number of simple filters together. Allows retrieval of filters by index after they a...
Definition: chain.h:111
void add(const MConstPtr &msg)
Definition: pass_through.h:71
size_t addFilter(F *filter)
Add a filter to this chain, by bare pointer. Returns the index of that filter in the chain...
Definition: chain.h:144
boost::shared_ptr< PassThrough< Msg > > PassThroughPtr
Definition: test_chain.cpp:64
Base class for Chain, allows you to store multiple chains in the same container. Provides filter retr...
Definition: chain.h:50
Connection registerCallback(const C &callback)
Register a callback to be called when this filter has passed.
Definition: simple_filter.h:73


message_filters
Author(s): Josh Faust, Vijay Pradeep
autogenerated on Sun Aug 26 2018 03:04:00