PX4 Firmware
PX4 Autopilot Software http://px4.io
SF0XTest.cpp
Go to the documentation of this file.
1 #include <unit_test.h>
2 
3 #include "../sf0x_parser.h"
4 
5 #include <systemlib/err.h>
6 
7 #include <cstdio>
8 #include <cstring>
9 #include <unistd.h>
10 
11 extern "C" __EXPORT int sf0x_tests_main(int argc, char *argv[]);
12 
13 class SF0XTest : public UnitTest
14 {
15 public:
16  virtual bool run_tests();
17 
18 private:
19  bool sf0xTest();
20 };
21 
23 {
25 
26  return (_tests_failed == 0);
27 }
28 
30 {
31  const char _LINE_MAX = 20;
32  //char _linebuf[_LINE_MAX];
33  //_linebuf[0] = '\0';
34 
35  const char *lines[] = {"0.01\r\n",
36  "0.02\r\n",
37  "0.03\r\n",
38  "0.04\r\n",
39  "0",
40  ".",
41  "0",
42  "5",
43  "\r",
44  "\n",
45  "0",
46  "3\r",
47  "\n",
48  "\r\n",
49  "0.06",
50  "\r\n"
51  };
52 
54  float dist_m;
55  char _parserbuf[_LINE_MAX];
56  unsigned _parsebuf_index = 0;
57 
58  for (unsigned l = 0; l < sizeof(lines) / sizeof(lines[0]); l++) {
59  //printf("\n%s", _linebuf);
60 
61  int parse_ret = -1;
62 
63  for (int i = 0; i < (ssize_t)strlen(lines[l]); i++) {
64  parse_ret = sf0x_parser(lines[l][i], _parserbuf, &_parsebuf_index, &state, &dist_m);
65 
66  if (parse_ret == 0) {
67  if (l == 0) {
68  ut_test(dist_m - 0.010000f < 0.001f);
69 
70  } else if (l == 1) {
71  ut_test(dist_m - 0.020000f < 0.001f);
72 
73  } else if (l == 2) {
74  ut_test(dist_m - 0.030000f < 0.001f);
75 
76  } else if (l == 3) {
77  ut_test(dist_m - 0.040000f < 0.001f);
78  }
79 
80  //printf("\nparsed: %f %s\n", dist_m, (parse_ret == 0) ? "OK" : "");
81  }
82  }
83 
84  //printf("%s", lines[l]);
85 
86  }
87 
88  return true;
89 }
90 
static enum @74 state
#define ut_declare_test_c(test_function, test_class)
Definition: unit_test.h:40
int sf0x_parser(char c, char *parserbuf, unsigned *parserbuf_index, enum SF0X_PARSE_STATE *state, float *dist)
Definition: sf0x_parser.cpp:61
Definition: I2C.hpp:51
Base class to be used for unit tests.
Definition: unit_test.h:54
int _tests_failed
The number of unit tests which failed.
Definition: unit_test.h:206
#define ut_test(test)
Used to assert a value within a unit test.
Definition: unit_test.h:124
__EXPORT int sf0x_tests_main(int argc, char *argv[])
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
bool sf0xTest()
Definition: SF0XTest.cpp:29
virtual bool run_tests()
Override to run your unit tests.
Definition: SF0XTest.cpp:22
SF0X_PARSE_STATE
Definition: sf0x_parser.h:43
#define ut_run_test(test)
Runs a single unit test.
Definition: unit_test.h:96