Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/test/statistics/timeline_test.cppGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 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 Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived 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 #include <gtest/gtest.h> 00035 00036 #include "../src/recognition/lib/engagement_macros.h" 00037 #include "../src/recognition/statistics/timeline.h" 00038 #include "../src/recognition/lib/engagement_params.h" 00039 00043 TEST(TimelineTestSuite, testConstructor) 00044 { 00045 Timeline time = Timeline(); 00046 00047 EXPECT_EQ(0, time.getFailureRate()); 00048 EXPECT_EQ(0, time.getMaxDelay()); 00049 EXPECT_EQ(0, time.getMaxDuration()); 00050 EXPECT_EQ(0, time.getMaxTBCE()); 00051 EXPECT_EQ(0, time.getMeanDelay()); 00052 EXPECT_EQ(0, time.getMeanDuration()); 00053 EXPECT_EQ(0, time.getMeanTBCE()); 00054 EXPECT_EQ(0, time.getRecentFailureRate()); 00055 EXPECT_EQ(0, time.getRecentMaxDelay()); 00056 EXPECT_EQ(0, time.getRecentMaxDuration()); 00057 EXPECT_EQ(0, time.getRecentMaxTBCE()); 00058 EXPECT_EQ(0, time.getRecentMeanDelay()); 00059 EXPECT_EQ(0, time.getRecentMeanDuration()); 00060 EXPECT_EQ(0, time.getRecentMeanTBCE()); 00061 } 00062 00066 TEST(TimelineTestSuite, testWindowSize) 00067 { 00068 Timeline time = Timeline(); 00069 00070 double default_size = 5.0; 00071 00072 // Grab the conf node handle 00073 ros::NodeHandle *conf = EngagementParams::getConfHandle(); 00074 00075 // Load the default size from the param server 00076 if (conf) 00077 conf->param(engagement::WINDOW_SIZE_PARAM, default_size, (double)5.0); 00078 00079 EXPECT_EQ(default_size, time.getWindowSize()); 00080 time.setWindowSize(1000); 00081 EXPECT_EQ(1000, time.getWindowSize()); 00082 time.setWindowSize(-1000); 00083 EXPECT_EQ(1000, time.getWindowSize()); 00084 time.setWindowSize(0); 00085 EXPECT_EQ(0, time.getWindowSize()); 00086 time.setWindowSize(9999999); 00087 EXPECT_EQ(9999999, time.getWindowSize()); 00088 } 00089 00093 TEST(TimelineTestSuite, testAddEvent) 00094 { 00095 bool run_test = true; 00096 double default_size = 5.0; 00097 00098 // Grab the conf node handle 00099 ros::NodeHandle *conf = EngagementParams::getConfHandle(); 00100 00101 // Attempt to grab the param from the server 00102 if (conf) 00103 conf->param("run_sleep_tests", run_test, true); 00104 00105 // Do not run the below tests if the param is false 00106 if (!run_test) 00107 return; 00108 00109 Timeline time = Timeline(); 00110 int threshold = 2; 00111 00112 // Load the default size from the param server 00113 if (conf) 00114 conf->param(engagement::WINDOW_SIZE_PARAM, default_size, (double)5.0); 00115 00116 EXPECT_EQ(default_size, time.getWindowSize()); 00117 00118 // The following test cases were written for a window size of 10.0 seconds 00119 if (time.getWindowSize() == 10.0) 00120 { 00121 // Add the event 00122 time.addEvent("DG", true, CURRENT_TIME, 1000, 5000); 00123 00124 EXPECT_TRUE(abs(time.getMeanTBCE() - 0) <= threshold); 00125 EXPECT_TRUE(abs(time.getMaxTBCE() - 0) <= threshold); 00126 EXPECT_TRUE(abs(time.getMeanDelay() - 1000) <= threshold); 00127 EXPECT_TRUE(abs(time.getMaxDelay() - 1000) <= threshold); 00128 EXPECT_TRUE(abs(time.getMeanDuration() - 5000) <= threshold); 00129 EXPECT_TRUE(abs(time.getMaxDuration() - 5000) <= threshold); 00130 EXPECT_EQ(0, time.getFailureRate()); 00131 EXPECT_TRUE(abs(time.getRecentMeanTBCE() - 0) <= threshold); 00132 EXPECT_TRUE(abs(time.getRecentMaxTBCE() - 0) <= threshold); 00133 EXPECT_TRUE(abs(time.getRecentMeanDelay() - 1000) <= threshold); 00134 EXPECT_TRUE(abs(time.getRecentMaxDelay() - 1000) <= threshold); 00135 EXPECT_TRUE(abs(time.getRecentMeanDuration() - 5000) <= threshold); 00136 EXPECT_TRUE(abs(time.getRecentMaxDuration() - 5000) <= threshold); 00137 EXPECT_EQ(0, time.getRecentFailureRate()); 00138 00139 // Sleep for ten seconds 00140 printf("Sleeping for ten seconds...\n"); 00141 sleep(10); 00142 00143 // Add the event 00144 time.addEvent("MFG", true, CURRENT_TIME, 3000, 7000); 00145 00146 EXPECT_TRUE(abs(time.getMeanTBCE() - 10000) <= threshold); 00147 EXPECT_TRUE(abs(time.getMaxTBCE() - 10000) <= threshold); 00148 EXPECT_TRUE(abs(time.getMeanDelay() - 2000) <= threshold); 00149 EXPECT_TRUE(abs(time.getMaxDelay() - 3000) <= threshold); 00150 EXPECT_TRUE(abs(time.getMeanDuration() - 6000) <= threshold); 00151 EXPECT_TRUE(abs(time.getMaxDuration() - 7000) <= threshold); 00152 EXPECT_EQ(0, time.getFailureRate() - 0); 00153 EXPECT_TRUE(abs(time.getRecentMeanTBCE() - 0) <= threshold); 00154 EXPECT_TRUE(abs(time.getRecentMaxTBCE() - 0) <= threshold); 00155 EXPECT_TRUE(abs(time.getRecentMeanDelay() - 3000) <= threshold); 00156 EXPECT_TRUE(abs(time.getRecentMaxDelay() - 3000) <= threshold); 00157 EXPECT_TRUE(abs(time.getRecentMeanDuration() - 7000) <= threshold); 00158 EXPECT_TRUE(abs(time.getRecentMaxDuration() - 7000) <= threshold); 00159 EXPECT_EQ(0, time.getRecentFailureRate() - 0); 00160 00161 // Sleep for five seconds 00162 printf("Sleeping for five seconds...\n"); 00163 sleep(3); 00164 00165 // Add the event 00166 time.addEvent("AP", false, CURRENT_TIME, 8000, 8000); 00167 00168 EXPECT_TRUE(abs(time.getMeanTBCE() - 6500) <= threshold); 00169 EXPECT_TRUE(abs(time.getMaxTBCE() - 10000) <= threshold); 00170 EXPECT_TRUE(abs(time.getMeanDelay() - 4000) <= threshold); 00171 EXPECT_TRUE(abs(time.getMaxDelay() - 8000) <= threshold); 00172 EXPECT_TRUE(abs(time.getMeanDuration() - 6666) <= threshold); 00173 EXPECT_TRUE(abs(time.getMaxDuration() - 8000) <= threshold); 00174 EXPECT_EQ(4, time.getFailureRate()); 00175 EXPECT_TRUE(abs(time.getRecentMeanTBCE() - 0) <= threshold); 00176 EXPECT_TRUE(abs(time.getRecentMaxTBCE() - 0) <= threshold); 00177 EXPECT_TRUE(abs(time.getRecentMeanDelay() - 5500) <= threshold); 00178 EXPECT_TRUE(abs(time.getRecentMaxDelay() - 8000) <= threshold); 00179 EXPECT_TRUE(abs(time.getRecentMeanDuration() - 7500) <= threshold); 00180 EXPECT_TRUE(abs(time.getRecentMaxDuration() - 8000) <= threshold); 00181 EXPECT_EQ(6, time.getRecentFailureRate()); 00182 00183 // Sleep for five more seconds 00184 printf("Sleeping for five seconds...\n"); 00185 sleep(5); 00186 00187 // Add the event 00188 time.addEvent("DG", false, CURRENT_TIME, 0, 500); 00189 00190 EXPECT_TRUE(abs(time.getMeanTBCE() - 6000) <= threshold); 00191 EXPECT_TRUE(abs(time.getMaxTBCE() - 10000) <= threshold); 00192 EXPECT_TRUE(abs(time.getMeanDelay() - 3000) <= threshold); 00193 EXPECT_TRUE(abs(time.getMaxDelay() - 8000) <= threshold); 00194 EXPECT_TRUE(abs(time.getMeanDuration() - 5125) <= threshold); 00195 EXPECT_TRUE(abs(time.getMaxDuration() - 8000) <= threshold); 00196 EXPECT_EQ(6, time.getFailureRate()); 00197 EXPECT_TRUE(abs(time.getRecentMeanTBCE() - 0) <= threshold); 00198 EXPECT_TRUE(abs(time.getRecentMaxTBCE() - 0) <= threshold); 00199 EXPECT_TRUE(abs(time.getRecentMeanDelay() - 3666) <= threshold); 00200 EXPECT_TRUE(abs(time.getRecentMaxDelay() - 8000) <= threshold); 00201 EXPECT_TRUE(abs(time.getRecentMeanDuration() - 5166) <= threshold); 00202 EXPECT_TRUE(abs(time.getRecentMaxDuration() - 8000) <= threshold); 00203 EXPECT_EQ(12, time.getRecentFailureRate()); 00204 00205 // Sleep for five more seconds 00206 printf("Sleeping for five seconds...\n"); 00207 sleep(5); 00208 00209 // Add the event (BC events CANNOT FAIL) 00210 time.addEvent("BC", true, CURRENT_TIME, 0, 100); 00211 00212 EXPECT_TRUE(abs(time.getMeanTBCE() - 5750) <= threshold); 00213 EXPECT_TRUE(abs(time.getMaxTBCE() - 10000) <= threshold); 00214 EXPECT_TRUE(abs(time.getMeanDelay() - 3000) <= threshold); 00215 EXPECT_TRUE(abs(time.getMaxDelay() - 8000) <= threshold); 00216 EXPECT_TRUE(abs(time.getMeanDuration() - 4120) <= threshold); 00217 EXPECT_TRUE(abs(time.getMaxDuration() - 8000) <= threshold); 00218 EXPECT_EQ(5, time.getFailureRate()); 00219 EXPECT_TRUE(abs(time.getRecentMeanTBCE() - 5000) <= threshold); 00220 EXPECT_TRUE(abs(time.getRecentMaxTBCE() - 5000) <= threshold); 00221 EXPECT_TRUE(abs(time.getRecentMeanDelay() - 0) <= threshold); 00222 EXPECT_TRUE(abs(time.getRecentMaxDelay() - 0) <= threshold); 00223 EXPECT_TRUE(abs(time.getRecentMeanDuration() - 300) <= threshold); 00224 EXPECT_TRUE(abs(time.getRecentMaxDuration() - 500) <= threshold); 00225 EXPECT_EQ(6, time.getRecentFailureRate()); 00226 } 00227 else 00228 ROS_WARN("testAddEvent will fail unless window size is 10.0"); 00229 } |