nonconst_subscriptions.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 /* Author: Josh Faust */
00031 
00032 /*
00033  * Subscribe intraprocess, ensuring that no copy happens
00034  */
00035 
00036 #include <string>
00037 
00038 #include <gtest/gtest.h>
00039 
00040 #include <stdlib.h>
00041 
00042 #include "ros/ros.h"
00043 
00044 #include "test_roscpp/TestEmpty.h"
00045 
00046 
00047 struct ConstHelper
00048 {
00049   void callback(const test_roscpp::TestEmptyConstPtr& msg)
00050   {
00051     message_ = msg;
00052   }
00053 
00054   test_roscpp::TestEmptyConstPtr message_;
00055 };
00056 
00057 struct NonConstHelper
00058 {
00059   void callback(const test_roscpp::TestEmptyPtr& msg)
00060   {
00061     message_ = msg;
00062   }
00063 
00064   test_roscpp::TestEmptyPtr message_;
00065 };
00066 
00067 TEST(NonConstSubscriptions, oneNonConstSubscriber)
00068 {
00069   NonConstHelper h;
00070   ros::NodeHandle nh;
00071   ros::Subscriber sub = nh.subscribe("test", 0, &NonConstHelper::callback, &h);
00072   ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test", 0);
00073 
00074   test_roscpp::TestEmptyPtr msg(new test_roscpp::TestEmpty);
00075   pub.publish(msg);
00076   ros::spinOnce();
00077 
00078   ASSERT_TRUE(h.message_);
00079   EXPECT_EQ(h.message_, msg);
00080 }
00081 
00082 TEST(NonConstSubscriptions, oneConstOneNonConst)
00083 {
00084   NonConstHelper h;
00085   ConstHelper h2;
00086   ros::NodeHandle nh;
00087   ros::Subscriber sub = nh.subscribe("test", 0, &NonConstHelper::callback, &h);
00088   ros::Subscriber sub2 = nh.subscribe("test", 0, &ConstHelper::callback, &h2);
00089   ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test", 0);
00090 
00091   test_roscpp::TestEmptyPtr msg(new test_roscpp::TestEmpty);
00092   pub.publish(msg);
00093   ros::spinOnce();
00094 
00095   ASSERT_TRUE(h.message_);
00096   EXPECT_NE(h.message_, msg);
00097   EXPECT_EQ(h2.message_, msg);
00098 }
00099 
00100 TEST(NonConstSubscriptions, twoNonConst)
00101 {
00102   NonConstHelper h;
00103   NonConstHelper h2;
00104   ros::NodeHandle nh;
00105   ros::Subscriber sub = nh.subscribe("test", 0, &NonConstHelper::callback, &h);
00106   ros::Subscriber sub2 = nh.subscribe("test", 0, &NonConstHelper::callback, &h2);
00107   ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test", 0);
00108 
00109   test_roscpp::TestEmptyPtr msg(new test_roscpp::TestEmpty);
00110   pub.publish(msg);
00111   ros::spinOnce();
00112 
00113   ASSERT_TRUE(h.message_);
00114   EXPECT_NE(h.message_, msg);
00115 }
00116 
00117 TEST(NonConstSubscriptions, twoConst)
00118 {
00119   ConstHelper h;
00120   ConstHelper h2;
00121   ros::NodeHandle nh;
00122   ros::Subscriber sub = nh.subscribe("test", 0, &ConstHelper::callback, &h);
00123   ros::Subscriber sub2 = nh.subscribe("test", 0, &ConstHelper::callback, &h2);
00124   ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test", 0);
00125 
00126   test_roscpp::TestEmptyPtr msg(new test_roscpp::TestEmpty);
00127   pub.publish(msg);
00128   ros::spinOnce();
00129 
00130   ASSERT_TRUE(h.message_);
00131   EXPECT_EQ(h.message_, msg);
00132   EXPECT_EQ(h2.message_, msg);
00133 }
00134 
00135 int main(int argc, char** argv)
00136 {
00137   testing::InitGoogleTest(&argc, argv);
00138   ros::init(argc, argv, "intraprocess_subscriptions");
00139 
00140   ros::NodeHandle nh;
00141 
00142   return RUN_ALL_TESTS();
00143 }
00144 


test_roscpp
Author(s): Morgan Quigley mquigley@cs.stanford.edu, Ken Conley kwc@willowgarage.com
autogenerated on Sat Dec 28 2013 17:36:21