test_chain.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2010, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #include <gtest/gtest.h>
00036 
00037 #include "ros/time.h"
00038 #include <ros/init.h>
00039 #include "message_filters/chain.h"
00040 
00041 using namespace message_filters;
00042 
00043 struct Msg
00044 {
00045 };
00046 typedef boost::shared_ptr<Msg> MsgPtr;
00047 typedef boost::shared_ptr<Msg const> MsgConstPtr;
00048 
00049 class Helper
00050 {
00051 public:
00052   Helper()
00053   : count_(0)
00054   {}
00055 
00056   void cb()
00057   {
00058     ++count_;
00059   }
00060 
00061   int32_t count_;
00062 };
00063 
00064 typedef boost::shared_ptr<PassThrough<Msg> > PassThroughPtr;
00065 
00066 TEST(Chain, simple)
00067 {
00068   Helper h;
00069   Chain<Msg> c;
00070   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00071   c.registerCallback(boost::bind(&Helper::cb, &h));
00072 
00073   c.add(boost::make_shared<Msg>());
00074   EXPECT_EQ(h.count_, 1);
00075   c.add(boost::make_shared<Msg>());
00076   EXPECT_EQ(h.count_, 2);
00077 }
00078 
00079 TEST(Chain, multipleFilters)
00080 {
00081   Helper h;
00082   Chain<Msg> c;
00083   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00084   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00085   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00086   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00087   c.registerCallback(boost::bind(&Helper::cb, &h));
00088 
00089   c.add(boost::make_shared<Msg>());
00090   EXPECT_EQ(h.count_, 1);
00091   c.add(boost::make_shared<Msg>());
00092   EXPECT_EQ(h.count_, 2);
00093 }
00094 
00095 TEST(Chain, addingFilters)
00096 {
00097   Helper h;
00098   Chain<Msg> c;
00099   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00100   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00101   c.registerCallback(boost::bind(&Helper::cb, &h));
00102 
00103   c.add(boost::make_shared<Msg>());
00104   EXPECT_EQ(h.count_, 1);
00105 
00106   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00107   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00108 
00109   c.add(boost::make_shared<Msg>());
00110   EXPECT_EQ(h.count_, 2);
00111 }
00112 
00113 TEST(Chain, inputFilter)
00114 {
00115   Helper h;
00116   Chain<Msg> c;
00117   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00118   c.registerCallback(boost::bind(&Helper::cb, &h));
00119 
00120   PassThrough<Msg> p;
00121   c.connectInput(p);
00122   p.add(boost::make_shared<Msg>());
00123   EXPECT_EQ(h.count_, 1);
00124 
00125   p.add(boost::make_shared<Msg>());
00126   EXPECT_EQ(h.count_, 2);
00127 }
00128 
00129 TEST(Chain, nonSharedPtrFilter)
00130 {
00131   Helper h;
00132   Chain<Msg> c;
00133   PassThrough<Msg> p;
00134   c.addFilter(&p);
00135   c.registerCallback(boost::bind(&Helper::cb, &h));
00136 
00137   c.add(boost::make_shared<Msg>());
00138   EXPECT_EQ(h.count_, 1);
00139   c.add(boost::make_shared<Msg>());
00140   EXPECT_EQ(h.count_, 2);
00141 }
00142 
00143 TEST(Chain, retrieveFilter)
00144 {
00145   Chain<Msg> c;
00146 
00147   ASSERT_FALSE(c.getFilter<PassThrough<Msg> >(0));
00148 
00149   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00150 
00151   ASSERT_TRUE(c.getFilter<PassThrough<Msg> >(0));
00152   ASSERT_FALSE(c.getFilter<PassThrough<Msg> >(1));
00153 }
00154 
00155 TEST(Chain, retrieveFilterThroughBaseClass)
00156 {
00157   Chain<Msg> c;
00158   ChainBase* cb = &c;
00159 
00160   ASSERT_FALSE(cb->getFilter<PassThrough<Msg> >(0));
00161 
00162   c.addFilter(boost::make_shared<PassThrough<Msg> >());
00163 
00164   ASSERT_TRUE(cb->getFilter<PassThrough<Msg> >(0));
00165   ASSERT_FALSE(cb->getFilter<PassThrough<Msg> >(1));
00166 }
00167 
00168 struct PTDerived : public PassThrough<Msg>
00169 {
00170 
00171 };
00172 
00173 TEST(Chain, retrieveBaseClass)
00174 {
00175   Chain<Msg> c;
00176   c.addFilter(boost::make_shared<PTDerived>());
00177   ASSERT_TRUE(c.getFilter<PassThrough<Msg> >(0));
00178   ASSERT_TRUE(c.getFilter<PTDerived>(0));
00179 }
00180 
00181 int main(int argc, char **argv){
00182   testing::InitGoogleTest(&argc, argv);
00183   ros::init(argc, argv, "blah");
00184   ros::Time::init();
00185   return RUN_ALL_TESTS();
00186 }
00187 
00188 


message_filters
Author(s): Josh Faust, Vijay Pradeep
autogenerated on Tue Mar 7 2017 04:01:23