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, boost::placeholders::_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 
Helper::cb
void cb(const MsgConstPtr &)
Definition: time_sequencer_unittest.cpp:78
Helper
Definition: test_chain.cpp:49
ros::MessageEvent::getReceiptTime
ros::Time getReceiptTime() const
ros::WallDuration::sleep
bool sleep() const
Msg
Definition: msg_cache_unittest.cpp:50
message_filters::TimeSequencer
Sequences messages based on the timestamp of their header.
Definition: time_sequencer.h:107
EventHelper
Definition: msg_cache_unittest.cpp:194
Header
Definition: msg_cache_unittest.cpp:44
boost::shared_ptr< Msg >
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
Helper::cb
void cb()
Definition: test_chain.cpp:56
time_sequencer.h
message_filters::TimeSequencer::connectInput
void connectInput(F &f)
Connect this filter's input to another filter's output.
Definition: time_sequencer.h:155
ros
time.h
ros::spinOnce
ROSCPP_DECL void spinOnce()
ros::Time::setNow
static void setNow(const Time &new_now)
main
int main(int argc, char **argv)
Definition: time_sequencer_unittest.cpp:146
Helper::Helper
Helper()
Definition: time_sequencer_unittest.cpp:74
MsgPtr
boost::shared_ptr< Msg > MsgPtr
Definition: time_sequencer_unittest.cpp:53
Msg::header
Header header
Definition: msg_cache_unittest.cpp:52
EventHelper::cb
void cb(const ros::MessageEvent< Msg const > &evt)
Definition: time_sequencer_unittest.cpp:117
ros::Time
ros::message_traits::TimeStamp< Msg >::value
static ros::Time value(const Msg &m)
Definition: time_sequencer_unittest.cpp:63
EventHelper::event_
ros::MessageEvent< Msg const > event_
Definition: msg_cache_unittest.cpp:202
message_filters::SimpleFilter::registerCallback
Connection registerCallback(const C &callback)
Register a callback to be called when this filter has passed.
Definition: simple_filter.h:137
message_filters
Definition: cache.h:47
ros::MessageEvent::getMessage
boost::shared_ptr< M > getMessage() const
MsgConstPtr
boost::shared_ptr< Msg const > MsgConstPtr
Definition: time_sequencer_unittest.cpp:54
Header::stamp
ros::Time stamp
Definition: msg_cache_unittest.cpp:46
ros::WallDuration
header
const std::string header
ros::Duration
ros::NodeHandle
ros::MessageEvent
Helper::count_
int32_t count_
Definition: test_chain.cpp:61
ros::Time::now
static Time now()
TEST
TEST(TimeSequencer, simple)
Definition: time_sequencer_unittest.cpp:86
message_filters::TimeSequencer::add
void add(const EventType &evt)
Definition: time_sequencer.h:167


message_filters
Author(s): Josh Faust, Vijay Pradeep, Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:01:54