param_load.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include <stdexcept>
25 
26 #include <gtest/gtest.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif // __cplusplus
31 
32 #include <param.h>
33 #include <ypparam.h>
34 
35 #ifdef __cplusplus
36 }
37 #endif // __cplusplus
38 
39 extern Parameters g_param;
40 
41 class ParamLoadTest : public ::testing::Test
42 {
43 protected:
44  FILE *fp;
45 
46  virtual void SetUp()
47  {
48  fp = tmpfile();
49  if (fp == NULL)
50  throw std::runtime_error("Failed to create tmpfile");
51  }
52 };
53 
54 bool skipKeys(const int key)
55 {
56  if (static_cast<YPSpur_param>(key) == YP_PARAM_VERSION ||
57  static_cast<YPSpur_param>(key) == YP_PARAM_ENCODER_DENOMINATOR ||
58  static_cast<YPSpur_param>(key) == YP_PARAM_RADIUS_R ||
59  static_cast<YPSpur_param>(key) == YP_PARAM_RADIUS_L)
60  return true;
61  return false;
62 }
63 
64 TEST_F(ParamLoadTest, LeftRightCommon)
65 {
66  char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
67 
68  fprintf(fp, "VERSION: %f\n", YP_PARAM_SUPPORTED_VERSION);
69  for (int i = 0; i < YP_PARAM_NUM; i++)
70  {
71  if (param_names[i][0] == '_' || skipKeys(i))
72  continue;
73  fprintf(fp, "%s: %f\n", param_names[i], i * 0.1 + 10.0);
74  }
75  fflush(fp);
76  rewind(fp);
77  ASSERT_GT(set_paramptr(fp), 0);
78  ASSERT_EQ(g_param.num_motor_enable, 2);
79 
80  for (int i = 0; i < YP_PARAM_NUM; i++)
81  {
82  if (param_names[i][0] == '_' || skipKeys(i))
83  continue;
84  ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_RIGHT), i * 0.1 + 10.0);
85  ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_LEFT), i * 0.1 + 10.0);
86  }
87 }
88 
89 TEST_F(ParamLoadTest, LeftRightDifferent)
90 {
91  char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
92 
93  fprintf(fp, "VERSION: %f\n", YP_PARAM_SUPPORTED_VERSION);
94  for (int i = 0; i < YP_PARAM_NUM; i++)
95  {
96  if (param_names[i][0] == '_' || skipKeys(i))
97  continue;
98  fprintf(fp, "%s[0]: %f\n", param_names[i], i * 0.1 + 10.0);
99  fprintf(fp, "%s[1]: %f\n", param_names[i], i * 0.1 + 100.0);
100  }
101  fflush(fp);
102  rewind(fp);
103  ASSERT_GT(set_paramptr(fp), 0);
104  ASSERT_EQ(g_param.num_motor_enable, 2);
105 
106  for (int i = 0; i < YP_PARAM_NUM; i++)
107  {
108  if (param_names[i][0] == '_' || skipKeys(i))
109  continue;
110  ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_RIGHT), i * 0.1 + 10.0);
111  ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_LEFT), i * 0.1 + 100.0);
112  }
113 }
114 
116 {
117  char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
118 
119  fprintf(fp, "VERSION: %f", YP_PARAM_SUPPORTED_VERSION);
120  for (int i = 0; i < YP_PARAM_NUM; i++)
121  {
122  if (param_names[i][0] == '_' || skipKeys(i))
123  continue;
124  fprintf(fp, "\n%s: %f", param_names[i], i * 0.1 + 10.0);
125  }
126  fflush(fp);
127  rewind(fp);
128  ASSERT_GT(set_paramptr(fp), 0);
129  ASSERT_EQ(g_param.num_motor_enable, 2);
130 
131  for (int i = 0; i < YP_PARAM_NUM; i++)
132  {
133  if (param_names[i][0] == '_' || skipKeys(i))
134  continue;
135  ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_RIGHT), i * 0.1 + 10.0);
136  ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(i), MOTOR_LEFT), i * 0.1 + 10.0);
137  }
138 }
139 
140 TEST_F(ParamLoadTest, EncoderDenominatorUnsupported)
141 {
142  char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
143 
144  fprintf(fp, "VERSION: 4.0\n");
145  fprintf(fp, "ENCODER_DENOMINATOR: 5.0\n");
146  for (int i = 0; i < YP_PARAM_NUM; i++)
147  {
148  if (param_names[i][0] == '_' || skipKeys(i))
149  continue;
150  fprintf(fp, "%s: %f\n", param_names[i], i * 0.1 + 10.0);
151  }
152  fflush(fp);
153  rewind(fp);
154  ASSERT_EQ(set_paramptr(fp), 0);
155 }
156 
157 TEST_F(ParamLoadTest, EncoderDenominatorSupported)
158 {
159  char param_names[YP_PARAM_NUM][32] = YP_PARAM_NAME;
160 
161  fprintf(fp, "VERSION: %f\n", YP_PARAM_SUPPORTED_VERSION);
162  fprintf(fp, "ENCODER_DENOMINATOR: 5.0\n");
163  for (int i = 0; i < YP_PARAM_NUM; i++)
164  {
165  if (param_names[i][0] == '_' || skipKeys(i))
166  continue;
167  fprintf(fp, "%s: %f\n", param_names[i], i * 0.1 + 10.0);
168  }
169  fflush(fp);
170  rewind(fp);
171  ASSERT_GT(set_paramptr(fp), 0);
172  ASSERT_EQ(g_param.num_motor_enable, 2);
173 
174  ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(YP_PARAM_ENCODER_DENOMINATOR), MOTOR_RIGHT), 5.0);
175  ASSERT_DOUBLE_EQ(p(static_cast<YPSpur_param>(YP_PARAM_ENCODER_DENOMINATOR), MOTOR_LEFT), 5.0);
176 }
177 
178 int main(int argc, char **argv)
179 {
180  testing::InitGoogleTest(&argc, argv);
181 
182  return RUN_ALL_TESTS();
183 }
TEST_F(ParamLoadTest, LeftRightCommon)
Definition: param_load.cpp:64
double p(YPSpur_param id, enum motor_id motor)
Definition: param.c:79
パラメータの最大値
Definition: ypparam.h:221
#define YP_PARAM_SUPPORTED_VERSION
Definition: ypparam.h:472
int main(int argc, char **argv)
Definition: param_load.cpp:178
int set_paramptr(FILE *paramfile)
Definition: param.c:454
int num_motor_enable
Definition: param.h:86
Parameters g_param
Definition: param.c:61
virtual void SetUp()
Definition: param_load.cpp:46
bool skipKeys(const int key)
Definition: param_load.cpp:54
#define YP_PARAM_NAME
Definition: ypparam.h:225


yp-spur
Author(s):
autogenerated on Fri May 7 2021 02:12:17