00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023
00024 #include <stdexcept>
00025
00026 #include <gtest/gtest.h>
00027
00028 #ifdef __cplusplus
00029 extern "C" {
00030 #endif // __cplusplus
00031
00032 #include <param.h>
00033 #include <ypparam.h>
00034
00035 #ifdef __cplusplus
00036 }
00037 #endif // __cplusplus
00038
00039 extern Parameters g_param;
00040
00041 class ParamLoadTest : public ::testing::Test
00042 {
00043 protected:
00044 FILE *fp;
00045
00046 virtual void SetUp()
00047 {
00048 fp = tmpfile();
00049 if (fp == NULL)
00050 throw std::runtime_error("Failed to create tmpfile");
00051 }
00052 };
00053
00054 bool skipKeys(const int key)
00055 {
00056 if (static_cast<YPSpur_param>(key) == YP_PARAM_VERSION ||
00057 static_cast<YPSpur_param>(key) == YP_PARAM_ENCODER_DENOMINATOR ||
00058 static_cast<YPSpur_param>(key) == YP_PARAM_RADIUS_R ||
00059 static_cast<YPSpur_param>(key) == YP_PARAM_RADIUS_L)
00060 return true;
00061 return false;
00062 }
00063
00064 TEST_F(ParamLoadTest, LeftRightCommon)
00065 {
00066 char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
00067
00068 fprintf(fp, "VERSION: %f\n", YP_PARAM_SUPPORTED_VERSION);
00069 for (int i = 0; i < YP_PARAM_NUM; i++)
00070 {
00071 if (param_names[i][0] == '_' || skipKeys(i))
00072 continue;
00073 fprintf(fp, "%s: %f\n", param_names[i], i * 0.1 + 10.0);
00074 }
00075 fflush(fp);
00076 rewind(fp);
00077 ASSERT_GT(set_paramptr(fp), 0);
00078 ASSERT_EQ(g_param.num_motor_enable, 2);
00079
00080 for (int i = 0; i < YP_PARAM_NUM; i++)
00081 {
00082 if (param_names[i][0] == '_' || skipKeys(i))
00083 continue;
00084 ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_RIGHT), i * 0.1 + 10.0);
00085 ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_LEFT), i * 0.1 + 10.0);
00086 }
00087 }
00088
00089 TEST_F(ParamLoadTest, LeftRightDifferent)
00090 {
00091 char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
00092
00093 fprintf(fp, "VERSION: %f\n", YP_PARAM_SUPPORTED_VERSION);
00094 for (int i = 0; i < YP_PARAM_NUM; i++)
00095 {
00096 if (param_names[i][0] == '_' || skipKeys(i))
00097 continue;
00098 fprintf(fp, "%s[0]: %f\n", param_names[i], i * 0.1 + 10.0);
00099 fprintf(fp, "%s[1]: %f\n", param_names[i], i * 0.1 + 100.0);
00100 }
00101 fflush(fp);
00102 rewind(fp);
00103 ASSERT_GT(set_paramptr(fp), 0);
00104 ASSERT_EQ(g_param.num_motor_enable, 2);
00105
00106 for (int i = 0; i < YP_PARAM_NUM; i++)
00107 {
00108 if (param_names[i][0] == '_' || skipKeys(i))
00109 continue;
00110 ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_RIGHT), i * 0.1 + 10.0);
00111 ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_LEFT), i * 0.1 + 100.0);
00112 }
00113 }
00114
00115 TEST_F(ParamLoadTest, NoNewline)
00116 {
00117 char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
00118
00119 fprintf(fp, "VERSION: %f", YP_PARAM_SUPPORTED_VERSION);
00120 for (int i = 0; i < YP_PARAM_NUM; i++)
00121 {
00122 if (param_names[i][0] == '_' || skipKeys(i))
00123 continue;
00124 fprintf(fp, "\n%s: %f", param_names[i], i * 0.1 + 10.0);
00125 }
00126 fflush(fp);
00127 rewind(fp);
00128 ASSERT_GT(set_paramptr(fp), 0);
00129 ASSERT_EQ(g_param.num_motor_enable, 2);
00130
00131 for (int i = 0; i < YP_PARAM_NUM; i++)
00132 {
00133 if (param_names[i][0] == '_' || skipKeys(i))
00134 continue;
00135 ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_RIGHT), i * 0.1 + 10.0);
00136 ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_LEFT), i * 0.1 + 10.0);
00137 }
00138 }
00139
00140 TEST_F(ParamLoadTest, EncoderDenominatorUnsupported)
00141 {
00142 char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
00143
00144 fprintf(fp, "VERSION: 4.0\n");
00145 fprintf(fp, "ENCODER_DENOMINATOR: 5.0\n");
00146 for (int i = 0; i < YP_PARAM_NUM; i++)
00147 {
00148 if (param_names[i][0] == '_' || skipKeys(i))
00149 continue;
00150 fprintf(fp, "%s: %f\n", param_names[i], i * 0.1 + 10.0);
00151 }
00152 fflush(fp);
00153 rewind(fp);
00154 ASSERT_EQ(set_paramptr(fp), 0);
00155 }
00156
00157 TEST_F(ParamLoadTest, EncoderDenominatorSupported)
00158 {
00159 char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
00160
00161 fprintf(fp, "VERSION: %f\n", YP_PARAM_SUPPORTED_VERSION);
00162 fprintf(fp, "ENCODER_DENOMINATOR: 5.0\n");
00163 for (int i = 0; i < YP_PARAM_NUM; i++)
00164 {
00165 if (param_names[i][0] == '_' || skipKeys(i))
00166 continue;
00167 fprintf(fp, "%s: %f\n", param_names[i], i * 0.1 + 10.0);
00168 }
00169 fflush(fp);
00170 rewind(fp);
00171 ASSERT_GT(set_paramptr(fp), 0);
00172 ASSERT_EQ(g_param.num_motor_enable, 2);
00173
00174 ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(YP_PARAM_ENCODER_DENOMINATOR), MOTOR_RIGHT), 5.0);
00175 ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(YP_PARAM_ENCODER_DENOMINATOR), MOTOR_LEFT), 5.0);
00176 }
00177
00178 int main(int argc, char **argv)
00179 {
00180 testing::InitGoogleTest(&argc, argv);
00181
00182 return RUN_ALL_TESTS();
00183 }