00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <gtest/gtest.h>
00036 #include <diagnostic_updater/diagnostic_updater.h>
00037 #include <diagnostic_updater/update_functions.h>
00038 #include <diagnostic_updater/DiagnosticStatusWrapper.h>
00039 #include <unistd.h>
00040
00041 using namespace diagnostic_updater;
00042
00043 class TestClass
00044 {
00045 public:
00046 void unwrapped(diagnostic_msgs::DiagnosticStatus &s)
00047 {
00048 }
00049
00050 void wrapped(DiagnosticStatusWrapper &s)
00051 {
00052 }
00053 };
00054
00055 TEST(DiagnosticUpdater, testDiagnosticUpdater)
00056 {
00057 class classFunction : public DiagnosticTask
00058 {
00059 public:
00060 classFunction() : DiagnosticTask("classFunction")
00061 {}
00062
00063 void run(DiagnosticStatusWrapper &s)
00064 {
00065 s.summary(0, "Test is running");
00066 s.addf("Value", "%f", 5);
00067 s.add("String", "Toto");
00068 s.add("Floating", 5.55);
00069 s.add("Integer", 5);
00070 s.addf("Formatted %s %i", "Hello", 5);
00071 s.add("Bool", true);
00072 }
00073 };
00074
00075 TestClass c;
00076 ros::NodeHandle nh;
00077
00078 Updater updater;
00079
00080 updater.add("wrapped", &c, &TestClass::wrapped);
00081
00082 classFunction cf;
00083 updater.add(cf);
00084 }
00085
00086 TEST(DiagnosticUpdater, testDiagnosticStatusWrapper)
00087 {
00088 DiagnosticStatusWrapper stat;
00089
00090 const char *message = "dummy";
00091 int level = 1;
00092 stat.summary(level, message);
00093 EXPECT_STREQ(message, stat.message.c_str()) << "DiagnosticStatusWrapper::summary failed to set message";
00094 EXPECT_EQ(level, stat.level) << "DiagnosticStatusWrapper::summary failed to set level";
00095
00096 stat.addf("toto", "%.1f", 5.0);
00097 stat.add("baba", 5);
00098 stat.addf("foo", "%05i", 27);
00099
00100 stat.add("bool", true);
00101 stat.add("bool2", false);
00102
00103 EXPECT_STREQ("5.0", stat.values[0].value.c_str()) << "Bad value, adding a value with addf";
00104 EXPECT_STREQ("5", stat.values[1].value.c_str()) << "Bad value, adding a string with add";
00105 EXPECT_STREQ("00027", stat.values[2].value.c_str()) << "Bad value, adding a string with addf";
00106 EXPECT_STREQ("toto", stat.values[0].key.c_str()) << "Bad label, adding a value with add";
00107 EXPECT_STREQ("baba", stat.values[1].key.c_str()) << "Bad label, adding a string with add";
00108 EXPECT_STREQ("foo", stat.values[2].key.c_str()) << "Bad label, adding a string with addf";
00109
00110 EXPECT_STREQ("bool", stat.values[3].key.c_str()) << "Bad label, adding a true bool key with add";
00111 EXPECT_STREQ("True", stat.values[3].value.c_str()) << "Bad label, adding a true bool with add";
00112
00113 EXPECT_STREQ("bool2", stat.values[4].key.c_str()) << "Bad label, adding a false bool key with add";
00114 EXPECT_STREQ("False", stat.values[4].value.c_str()) << "Bad label, adding a false bool with add";
00115 }
00116
00117 TEST(DiagnosticUpdater, testFrequencyStatus)
00118 {
00119 double minFreq = 10;
00120 double maxFreq = 20;
00121
00122 FrequencyStatus fs(FrequencyStatusParam(&minFreq, &maxFreq, 0.5, 2));
00123
00124 DiagnosticStatusWrapper stat[5];
00125 fs.tick();
00126 usleep(20000);
00127 fs.run(stat[0]);
00128 usleep(50000);
00129 fs.tick();
00130 fs.run(stat[1]);
00131 usleep(300000);
00132 fs.tick();
00133 fs.run(stat[2]);
00134 usleep(150000);
00135 fs.tick();
00136 fs.run(stat[3]);
00137 fs.clear();
00138 fs.run(stat[4]);
00139
00140 EXPECT_EQ(1, stat[0].level) << "max frequency exceeded but not reported";
00141 EXPECT_EQ(0, stat[1].level) << "within max frequency but reported error";
00142 EXPECT_EQ(0, stat[2].level) << "within min frequency but reported error";
00143 EXPECT_EQ(1, stat[3].level) << "min frequency exceeded but not reported";
00144 EXPECT_EQ(2, stat[4].level) << "freshly cleared should fail";
00145 EXPECT_STREQ("", stat[0].name.c_str()) << "Name should not be set by FrequencyStatus";
00146 EXPECT_STREQ("Frequency Status", fs.getName().c_str()) << "Name should be \"Frequency Status\"";
00147 }
00148
00149 TEST(DiagnosticUpdater, testTimeStampStatus)
00150 {
00151 TimeStampStatus ts(DefaultTimeStampStatusParam);
00152
00153 DiagnosticStatusWrapper stat[5];
00154 ts.run(stat[0]);
00155 ts.tick(ros::Time::now().toSec() + 2);
00156 ts.run(stat[1]);
00157 ts.tick(ros::Time::now());
00158 ts.run(stat[2]);
00159 ts.tick(ros::Time::now().toSec() - 4);
00160 ts.run(stat[3]);
00161 ts.tick(ros::Time::now().toSec() - 6);
00162 ts.run(stat[4]);
00163
00164 EXPECT_EQ(1, stat[0].level) << "no data should return a warning";
00165 EXPECT_EQ(2, stat[1].level) << "too far future not reported";
00166 EXPECT_EQ(0, stat[2].level) << "now not accepted";
00167 EXPECT_EQ(0, stat[3].level) << "4 seconds ago not accepted";
00168 EXPECT_EQ(2, stat[4].level) << "too far past not reported";
00169 EXPECT_STREQ("", stat[0].name.c_str()) << "Name should not be set by TimeStapmStatus";
00170 EXPECT_STREQ("Timestamp Status", ts.getName().c_str()) << "Name should be \"Timestamp Status\"";
00171 }
00172
00173 int main(int argc, char **argv){
00174 ros::init(argc, argv, "test_node");
00175 testing::InitGoogleTest(&argc, argv);
00176 return RUN_ALL_TESTS();
00177 }
00178