time_sequencer_unittest.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, 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"
39 
40 using namespace message_filters;
41 
42 struct Header
43 {
44  ros::Time stamp;
45 };
46 
47 
48 struct Msg
49 {
50  Header header;
51  int data;
52 };
55 
56 namespace ros
57 {
58 namespace message_traits
59 {
60 template<>
61 struct TimeStamp<Msg>
62 {
63  static ros::Time value(const Msg& m)
64  {
65  return m.header.stamp;
66  }
67 };
68 }
69 }
70 
71 class Helper
72 {
73 public:
75  : count_(0)
76  {}
77 
78  void cb(const MsgConstPtr&)
79  {
80  ++count_;
81  }
82 
83  int32_t count_;
84 };
85 
87 {
88  TimeSequencer<Msg> seq(ros::Duration(1.0), ros::Duration(0.01), 10);
89  Helper h;
90  seq.registerCallback(boost::bind(&Helper::cb, &h, _1));
91  MsgPtr msg(boost::make_shared<Msg>());
92  msg->header.stamp = ros::Time::now();
93  seq.add(msg);
94 
95  ros::WallDuration(0.1).sleep();
96  ros::spinOnce();
97  ASSERT_EQ(h.count_, 0);
98 
100 
101  ros::WallDuration(0.1).sleep();
102  ros::spinOnce();
103 
104  ASSERT_EQ(h.count_, 1);
105 }
106 
107 TEST(TimeSequencer, compilation)
108 {
109  TimeSequencer<Msg> seq(ros::Duration(1.0), ros::Duration(0.01), 10);
110  TimeSequencer<Msg> seq2(ros::Duration(1.0), ros::Duration(0.01), 10);
111  seq2.connectInput(seq);
112 }
113 
114 struct EventHelper
115 {
116 public:
118  {
119  event_ = evt;
120  }
121 
123 };
124 
125 TEST(TimeSequencer, eventInEventOut)
126 {
127  TimeSequencer<Msg> seq(ros::Duration(1.0), ros::Duration(0.01), 10);
128  TimeSequencer<Msg> seq2(seq, ros::Duration(1.0), ros::Duration(0.01), 10);
129  EventHelper h;
131 
132  ros::MessageEvent<Msg const> evt(boost::make_shared<Msg const>(), ros::Time::now());
133  seq.add(evt);
134 
136  while (!h.event_.getMessage())
137  {
138  ros::WallDuration(0.01).sleep();
139  ros::spinOnce();
140  }
141 
142  EXPECT_EQ(h.event_.getReceiptTime(), evt.getReceiptTime());
143  EXPECT_EQ(h.event_.getMessage(), evt.getMessage());
144 }
145 
146 int main(int argc, char **argv){
147  testing::InitGoogleTest(&argc, argv);
148 
149  ros::init(argc, argv, "time_sequencer_test");
150  ros::NodeHandle nh;
152 
153  return RUN_ALL_TESTS();
154 }
155 
156 
Sequences messages based on the timestamp of their header.
ros::Time getReceiptTime() const
Header header
int32_t count_
Definition: test_chain.cpp:61
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void add(const EventType &evt)
void cb(const ros::MessageEvent< Msg const > &evt)
boost::shared_ptr< Msg const > MsgConstPtr
std_msgs::Header * header(M &m)
ros::MessageEvent< Msg const > event_
static void setNow(const Time &new_now)
TEST(TimeSequencer, simple)
bool sleep() const
void cb(const MsgConstPtr &)
void cb()
Definition: test_chain.cpp:56
boost::shared_ptr< Msg > MsgPtr
static Time now()
void connectInput(F &f)
Connect this filter&#39;s input to another filter&#39;s output.
ROSCPP_DECL void spinOnce()
int main(int argc, char **argv)
boost::shared_ptr< M > getMessage() const
ros::Time stamp
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