interval_calc_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>
38 
39 using namespace std;
40 using namespace settlerlib;
41 
42 
43 const unsigned int NA = 9;
44 static const double dataA[NA][2] = { { 0, 0},
45  { 1, 1},
46  { 2, 2},
47  { 3, 3},
48  { 4, 4},
49  { 3, 5},
50  { 2, 6},
51  { 1, 7},
52  { 0, 8} };
53 
58 {
60  signal.setMaxSize(20);
61 
62  for (unsigned int i=0; i<NA; i++)
63  {
64  DeflatedPtr deflated(new Deflated);
65  deflated->header.stamp = ros::Time(i,0);
66  deflated->channels_.resize(2);
67  deflated->channels_[0] = dataA[i][0];
68  deflated->channels_[1] = dataA[i][1];
69 
70  signal.add(deflated);
71  }
72 
73  return signal;
74 }
75 
80 {
81 
83  signal.setMaxSize(20);
84 
85  for (unsigned int i=0; i<NA; i++)
86  {
87  DeflatedPtr deflated(new Deflated);
88  deflated->header.stamp = ros::Time(i,0);
89  if (i > 4)
90  deflated->header.stamp = ros::Time(i+10,0);
91  else
92  deflated->header.stamp = ros::Time(i,0);
93  deflated->channels_.resize(2);
94  deflated->channels_[0] = dataA[i][0];
95  deflated->channels_[1] = dataA[i][1];
96 
97  signal.add(deflated);
98  }
99 
100  return signal;
101 }
102 
103 // A pretty simple test where the first channel exceeds the tolerance first
105 {
107 
108  vector<double> tol(2);
109  tol[0] = 2.5;
110  tol[1] = 3.5;
111  ros::Duration max_step(2,0);
112 
113  calibration_msgs::Interval interval = IntervalCalc::computeLatestInterval(signal, tol, max_step);
114  EXPECT_EQ(interval.start.sec, (unsigned int) 6);
115  EXPECT_EQ(interval.end.sec, (unsigned int) 8);
116 }
117 
118 // Another simple test, but this time the 2nd channel will exceed the tolerance first
120 {
122 
123  vector<double> tol(2);
124  tol[0] = 4.5;
125  tol[1] = 3.5;
126  ros::Duration max_step(2,0);
127 
128  calibration_msgs::Interval interval = IntervalCalc::computeLatestInterval(signal, tol, max_step);
129  EXPECT_EQ(interval.start.sec, (unsigned int) 5);
130  EXPECT_EQ(interval.end.sec, (unsigned int) 8);
131 }
132 
133 // See what happens if our max step is really small
134 TEST(IntervalCalc, maxStep1)
135 {
137 
138  vector<double> tol(2);
139  tol[0] = 4.5;
140  tol[1] = 3.5;
141  ros::Duration max_step;
142  max_step.fromSec(.5);
143 
144  calibration_msgs::Interval interval = IntervalCalc::computeLatestInterval(signal, tol, max_step);
145  EXPECT_EQ(interval.start.sec, (unsigned int) 8);
146  EXPECT_EQ(interval.end.sec, (unsigned int) 8);
147 }
148 
149 // See what happens if there's a big gap in time in our signal
150 TEST(IntervalCalc, maxStep2)
151 {
153 
154  vector<double> tol(2);
155  tol[0] = 100;
156  tol[1] = 100;
157  ros::Duration max_step(5,0);
158 
159  calibration_msgs::Interval interval = IntervalCalc::computeLatestInterval(signal, tol, max_step);
160  EXPECT_EQ(interval.start.sec, (unsigned int) 15);
161  EXPECT_EQ(interval.end.sec, (unsigned int) 18);
162 }
163 
164 int main(int argc, char **argv){
165  testing::InitGoogleTest(&argc, argv);
166  return RUN_ALL_TESTS();
167 }
static const double dataA[NA][2]
const unsigned int NA
Adds helper routines to the STL Deque for holding messages with headers.
Definition: sorted_deque.h:58
void setMaxSize(unsigned int max_size)
Set the maximum # of elements this deque can hold. Older elems are popped once the length is exeeded...
Definition: sorted_deque.h:100
int main(int argc, char **argv)
SortedDeque< DeflatedConstPtr > generateSignal2()
Duration & fromSec(double t)
SortedDeque< DeflatedConstPtr > generateSignal1()
void add(const M &msg)
Add a new element to the deque, correctly sorted by timestamp.
Definition: sorted_deque.h:109
TEST(IntervalCalc, easy1)


settlerlib
Author(s): Vijay Pradeep
autogenerated on Thu Jun 6 2019 19:17:23