interval_calc_unittest.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, 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 #include <settlerlib/interval_calc.h>
00037 #include <settlerlib/sorted_deque.h>
00038 
00039 using namespace std;
00040 using namespace settlerlib;
00041 
00042 
00043 const unsigned int NA = 9;
00044 static const double dataA[NA][2] = { { 0,  0},
00045                                      { 1,  1},
00046                                      { 2,  2},
00047                                      { 3,  3},
00048                                      { 4,  4},
00049                                      { 3,  5},
00050                                      { 2,  6},
00051                                      { 1,  7},
00052                                      { 0,  8} };
00053 
00057 SortedDeque<DeflatedConstPtr> generateSignal1()
00058 {
00059   SortedDeque<DeflatedConstPtr> signal(&SortedDeque<DeflatedConstPtr>::getPtrStamp) ;
00060   signal.setMaxSize(20);
00061 
00062   for (unsigned int i=0; i<NA; i++)
00063   {
00064     DeflatedPtr deflated(new Deflated);
00065     deflated->header.stamp = ros::Time(i,0);
00066     deflated->channels_.resize(2);
00067     deflated->channels_[0] = dataA[i][0];
00068     deflated->channels_[1] = dataA[i][1];
00069 
00070     signal.add(deflated);
00071   }
00072 
00073   return signal;
00074 }
00075 
00079 SortedDeque<DeflatedConstPtr> generateSignal2()
00080 {
00081 
00082   SortedDeque<DeflatedConstPtr> signal(&SortedDeque<DeflatedConstPtr>::getPtrStamp) ;
00083   signal.setMaxSize(20);
00084 
00085   for (unsigned int i=0; i<NA; i++)
00086   {
00087     DeflatedPtr deflated(new Deflated);
00088     deflated->header.stamp = ros::Time(i,0);
00089     if (i > 4)
00090       deflated->header.stamp = ros::Time(i+10,0);
00091     else
00092       deflated->header.stamp = ros::Time(i,0);
00093     deflated->channels_.resize(2);
00094     deflated->channels_[0] = dataA[i][0];
00095     deflated->channels_[1] = dataA[i][1];
00096 
00097     signal.add(deflated);
00098   }
00099 
00100   return signal;
00101 }
00102 
00103 // A pretty simple test where the first channel exceeds the tolerance first
00104 TEST(IntervalCalc, easy1)
00105 {
00106   SortedDeque<DeflatedConstPtr> signal = generateSignal1();
00107 
00108   vector<double> tol(2);
00109   tol[0] = 2.5;
00110   tol[1] = 3.5;
00111   ros::Duration max_step(2,0);
00112 
00113   calibration_msgs::Interval interval = IntervalCalc::computeLatestInterval(signal, tol, max_step);
00114   EXPECT_EQ(interval.start.sec, (unsigned int) 6);
00115   EXPECT_EQ(interval.end.sec,   (unsigned int) 8);
00116 }
00117 
00118 // Another simple test, but this time the 2nd channel will exceed the tolerance first
00119 TEST(IntervalCalc, easy2)
00120 {
00121   SortedDeque<DeflatedConstPtr> signal = generateSignal1();
00122 
00123   vector<double> tol(2);
00124   tol[0] = 4.5;
00125   tol[1] = 3.5;
00126   ros::Duration max_step(2,0);
00127 
00128   calibration_msgs::Interval interval = IntervalCalc::computeLatestInterval(signal, tol, max_step);
00129   EXPECT_EQ(interval.start.sec, (unsigned int) 5);
00130   EXPECT_EQ(interval.end.sec,   (unsigned int) 8);
00131 }
00132 
00133 // See what happens if our max step is really small
00134 TEST(IntervalCalc, maxStep1)
00135 {
00136   SortedDeque<DeflatedConstPtr> signal = generateSignal1();
00137 
00138   vector<double> tol(2);
00139   tol[0] = 4.5;
00140   tol[1] = 3.5;
00141   ros::Duration max_step;
00142   max_step.fromSec(.5);
00143 
00144   calibration_msgs::Interval interval = IntervalCalc::computeLatestInterval(signal, tol, max_step);
00145   EXPECT_EQ(interval.start.sec, (unsigned int) 8);
00146   EXPECT_EQ(interval.end.sec,   (unsigned int) 8);
00147 }
00148 
00149 // See what happens if there's a big gap in time in our signal
00150 TEST(IntervalCalc, maxStep2)
00151 {
00152   SortedDeque<DeflatedConstPtr> signal = generateSignal2();
00153 
00154   vector<double> tol(2);
00155   tol[0] = 100;
00156   tol[1] = 100;
00157   ros::Duration max_step(5,0);
00158 
00159   calibration_msgs::Interval interval = IntervalCalc::computeLatestInterval(signal, tol, max_step);
00160   EXPECT_EQ(interval.start.sec, (unsigned int) 15);
00161   EXPECT_EQ(interval.end.sec,   (unsigned int) 18);
00162 }
00163 
00164 int main(int argc, char **argv){
00165   testing::InitGoogleTest(&argc, argv);
00166   return RUN_ALL_TESTS();
00167 }


settlerlib
Author(s): Vijay Pradeep
autogenerated on Sat Jun 8 2019 19:41:50