param_load.cpp
Go to the documentation of this file.
00001 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
00002 //
00003 // Permission is hereby granted, free of charge, to any person obtaining a copy
00004 // of this software and associated documentation files (the "Software"), to
00005 // deal in the Software without restriction, including without limitation the
00006 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00007 // sell copies of the Software, and to permit persons to whom the Software is
00008 // furnished to do so, subject to the following conditions:
00009 //
00010 // The above copyright notice and this permission notice shall be included in
00011 // all copies or substantial portions of the Software.
00012 //
00013 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00016 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00019 // SOFTWARE.
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 }


yp-spur
Author(s):
autogenerated on Fri May 10 2019 02:52:19