Browse Source

uORBTest_UnitTest: astyle

sbg
Ban Siesta 10 years ago committed by Lorenz Meier
parent
commit
8be1b4f19c
  1. 402
      src/modules/uORB/uORBTest_UnitTest.cpp
  2. 136
      src/modules/uORB/uORBTest_UnitTest.hpp

402
src/modules/uORB/uORBTest_UnitTest.cpp

@ -45,245 +45,277 @@ uORBTest::UnitTest &uORBTest::UnitTest::instance()
int uORBTest::UnitTest::pubsublatency_main(void) int uORBTest::UnitTest::pubsublatency_main(void)
{ {
/* poll on test topic and output latency */ /* poll on test topic and output latency */
float latency_integral = 0.0f; float latency_integral = 0.0f;
/* wakeup source(s) */ /* wakeup source(s) */
px4_pollfd_struct_t fds[3]; px4_pollfd_struct_t fds[3];
int test_multi_sub = orb_subscribe_multi(ORB_ID(orb_test), 0); int test_multi_sub = orb_subscribe_multi(ORB_ID(orb_test), 0);
int test_multi_sub_medium = orb_subscribe_multi(ORB_ID(orb_test_medium), 0); int test_multi_sub_medium = orb_subscribe_multi(ORB_ID(orb_test_medium), 0);
int test_multi_sub_large = orb_subscribe_multi(ORB_ID(orb_test_large), 0); int test_multi_sub_large = orb_subscribe_multi(ORB_ID(orb_test_large), 0);
struct orb_test_large t; struct orb_test_large t;
/* clear all ready flags */ /* clear all ready flags */
orb_copy(ORB_ID(orb_test), test_multi_sub, &t); orb_copy(ORB_ID(orb_test), test_multi_sub, &t);
orb_copy(ORB_ID(orb_test_medium), test_multi_sub_medium, &t); orb_copy(ORB_ID(orb_test_medium), test_multi_sub_medium, &t);
orb_copy(ORB_ID(orb_test_large), test_multi_sub_large, &t); orb_copy(ORB_ID(orb_test_large), test_multi_sub_large, &t);
fds[0].fd = test_multi_sub; fds[0].fd = test_multi_sub;
fds[0].events = POLLIN; fds[0].events = POLLIN;
fds[1].fd = test_multi_sub_medium; fds[1].fd = test_multi_sub_medium;
fds[1].events = POLLIN; fds[1].events = POLLIN;
fds[2].fd = test_multi_sub_large; fds[2].fd = test_multi_sub_large;
fds[2].events = POLLIN; fds[2].events = POLLIN;
const unsigned maxruns = 1000; const unsigned maxruns = 1000;
unsigned timingsgroup = 0; unsigned timingsgroup = 0;
unsigned *timings = new unsigned[maxruns]; unsigned *timings = new unsigned[maxruns];
for (unsigned i = 0; i < maxruns; i++) { for (unsigned i = 0; i < maxruns; i++) {
/* wait for up to 500ms for data */ /* wait for up to 500ms for data */
int pret = px4_poll(&fds[0], (sizeof(fds) / sizeof(fds[0])), 500); int pret = px4_poll(&fds[0], (sizeof(fds) / sizeof(fds[0])), 500);
if (fds[0].revents & POLLIN) {
orb_copy(ORB_ID(orb_test), test_multi_sub, &t); if (fds[0].revents & POLLIN) {
timingsgroup = 0; orb_copy(ORB_ID(orb_test), test_multi_sub, &t);
} else if (fds[1].revents & POLLIN) { timingsgroup = 0;
orb_copy(ORB_ID(orb_test_medium), test_multi_sub_medium, &t);
timingsgroup = 1; } else if (fds[1].revents & POLLIN) {
} else if (fds[2].revents & POLLIN) { orb_copy(ORB_ID(orb_test_medium), test_multi_sub_medium, &t);
orb_copy(ORB_ID(orb_test_large), test_multi_sub_large, &t); timingsgroup = 1;
timingsgroup = 2;
} } else if (fds[2].revents & POLLIN) {
orb_copy(ORB_ID(orb_test_large), test_multi_sub_large, &t);
if (pret < 0) { timingsgroup = 2;
warn("poll error %d, %d", pret, errno); }
continue;
} if (pret < 0) {
warn("poll error %d, %d", pret, errno);
hrt_abstime elt = hrt_elapsed_time(&t.time); continue;
latency_integral += elt; }
timings[i] = elt;
} hrt_abstime elt = hrt_elapsed_time(&t.time);
latency_integral += elt;
orb_unsubscribe(test_multi_sub); timings[i] = elt;
orb_unsubscribe(test_multi_sub_medium); }
orb_unsubscribe(test_multi_sub_large);
orb_unsubscribe(test_multi_sub);
if (pubsubtest_print) { orb_unsubscribe(test_multi_sub_medium);
char fname[32]; orb_unsubscribe(test_multi_sub_large);
sprintf(fname, PX4_ROOTFSDIR"/fs/microsd/timings%u.txt", timingsgroup);
FILE *f = fopen(fname, "w"); if (pubsubtest_print) {
if (f == NULL) { char fname[32];
warnx("Error opening file!\n"); sprintf(fname, PX4_ROOTFSDIR"/fs/microsd/timings%u.txt", timingsgroup);
return uORB::ERROR; FILE *f = fopen(fname, "w");
}
if (f == NULL) {
for (unsigned i = 0; i < maxruns; i++) { warnx("Error opening file!\n");
fprintf(f, "%u\n", timings[i]); return uORB::ERROR;
} }
fclose(f); for (unsigned i = 0; i < maxruns; i++) {
} fprintf(f, "%u\n", timings[i]);
}
delete[] timings;
fclose(f);
warnx("mean: %8.4f", static_cast<double>(latency_integral / maxruns)); }
pubsubtest_passed = true; delete[] timings;
if (static_cast<float>(latency_integral / maxruns) > 30.0f) { warnx("mean: %8.4f", static_cast<double>(latency_integral / maxruns));
pubsubtest_res = uORB::ERROR;
} else { pubsubtest_passed = true;
pubsubtest_res = PX4_OK;
} if (static_cast<float>(latency_integral / maxruns) > 30.0f) {
pubsubtest_res = uORB::ERROR;
return pubsubtest_res;
} else {
pubsubtest_res = PX4_OK;
}
return pubsubtest_res;
} }
int uORBTest::UnitTest::test() int uORBTest::UnitTest::test()
{ {
struct orb_test t, u; struct orb_test t, u;
int sfd; int sfd;
orb_advert_t ptopic; orb_advert_t ptopic;
bool updated; bool updated;
t.val = 0;
ptopic = orb_advertise(ORB_ID(orb_test), &t);
if (ptopic == nullptr) {
return test_fail("advertise failed: %d", errno);
}
test_note("publish handle 0x%08x", ptopic);
sfd = orb_subscribe(ORB_ID(orb_test));
t.val = 0; if (sfd < 0) {
ptopic = orb_advertise(ORB_ID(orb_test), &t); return test_fail("subscribe failed: %d", errno);
}
if (ptopic == nullptr) test_note("subscribe fd %d", sfd);
return test_fail("advertise failed: %d", errno); u.val = 1;
test_note("publish handle 0x%08x", ptopic); if (PX4_OK != orb_copy(ORB_ID(orb_test), sfd, &u)) {
sfd = orb_subscribe(ORB_ID(orb_test)); return test_fail("copy(1) failed: %d", errno);
}
if (sfd < 0) if (u.val != t.val) {
return test_fail("subscribe failed: %d", errno); return test_fail("copy(1) mismatch: %d expected %d", u.val, t.val);
}
test_note("subscribe fd %d", sfd); if (PX4_OK != orb_check(sfd, &updated)) {
u.val = 1; return test_fail("check(1) failed");
}
if (PX4_OK != orb_copy(ORB_ID(orb_test), sfd, &u)) if (updated) {
return test_fail("copy(1) failed: %d", errno); return test_fail("spurious updated flag");
}
if (u.val != t.val) t.val = 2;
return test_fail("copy(1) mismatch: %d expected %d", u.val, t.val); test_note("try publish");
if (PX4_OK != orb_check(sfd, &updated)) if (PX4_OK != orb_publish(ORB_ID(orb_test), ptopic, &t)) {
return test_fail("check(1) failed"); return test_fail("publish failed");
}
if (updated) if (PX4_OK != orb_check(sfd, &updated)) {
return test_fail("spurious updated flag"); return test_fail("check(2) failed");
}
t.val = 2; if (!updated) {
test_note("try publish"); return test_fail("missing updated flag");
}
if (PX4_OK != orb_publish(ORB_ID(orb_test), ptopic, &t)) if (PX4_OK != orb_copy(ORB_ID(orb_test), sfd, &u)) {
return test_fail("publish failed"); return test_fail("copy(2) failed: %d", errno);
}
if (PX4_OK != orb_check(sfd, &updated)) if (u.val != t.val) {
return test_fail("check(2) failed"); return test_fail("copy(2) mismatch: %d expected %d", u.val, t.val);
}
if (!updated) orb_unsubscribe(sfd);
return test_fail("missing updated flag");
if (PX4_OK != orb_copy(ORB_ID(orb_test), sfd, &u)) /* this routine tests the multi-topic support */
return test_fail("copy(2) failed: %d", errno); test_note("try multi-topic support");
if (u.val != t.val) int instance0;
return test_fail("copy(2) mismatch: %d expected %d", u.val, t.val); orb_advert_t pfd0 = orb_advertise_multi(ORB_ID(orb_multitest), &t, &instance0, ORB_PRIO_MAX);
orb_unsubscribe(sfd); test_note("advertised");
/* this routine tests the multi-topic support */ int instance1;
test_note("try multi-topic support"); orb_advert_t pfd1 = orb_advertise_multi(ORB_ID(orb_multitest), &t, &instance1, ORB_PRIO_MIN);
int instance0; if (instance0 != 0) {
orb_advert_t pfd0 = orb_advertise_multi(ORB_ID(orb_multitest), &t, &instance0, ORB_PRIO_MAX); return test_fail("mult. id0: %d", instance0);
}
test_note("advertised"); if (instance1 != 1) {
return test_fail("mult. id1: %d", instance1);
}
int instance1; t.val = 103;
orb_advert_t pfd1 = orb_advertise_multi(ORB_ID(orb_multitest), &t, &instance1, ORB_PRIO_MIN);
if (instance0 != 0) if (PX4_OK != orb_publish(ORB_ID(orb_multitest), pfd0, &t)) {
return test_fail("mult. id0: %d", instance0); return test_fail("mult. pub0 fail");
}
if (instance1 != 1) test_note("published");
return test_fail("mult. id1: %d", instance1);
t.val = 103; t.val = 203;
if (PX4_OK != orb_publish(ORB_ID(orb_multitest), pfd0, &t))
return test_fail("mult. pub0 fail");
test_note("published"); if (PX4_OK != orb_publish(ORB_ID(orb_multitest), pfd1, &t)) {
return test_fail("mult. pub1 fail");
}
t.val = 203; /* subscribe to both topics and ensure valid data is received */
if (PX4_OK != orb_publish(ORB_ID(orb_multitest), pfd1, &t)) int sfd0 = orb_subscribe_multi(ORB_ID(orb_multitest), 0);
return test_fail("mult. pub1 fail");
/* subscribe to both topics and ensure valid data is received */ if (PX4_OK != orb_copy(ORB_ID(orb_multitest), sfd0, &u)) {
int sfd0 = orb_subscribe_multi(ORB_ID(orb_multitest), 0); return test_fail("sub #0 copy failed: %d", errno);
}
if (PX4_OK != orb_copy(ORB_ID(orb_multitest), sfd0, &u)) if (u.val != 103) {
return test_fail("sub #0 copy failed: %d", errno); return test_fail("sub #0 val. mismatch: %d", u.val);
}
if (u.val != 103) int sfd1 = orb_subscribe_multi(ORB_ID(orb_multitest), 1);
return test_fail("sub #0 val. mismatch: %d", u.val);
int sfd1 = orb_subscribe_multi(ORB_ID(orb_multitest), 1); if (PX4_OK != orb_copy(ORB_ID(orb_multitest), sfd1, &u)) {
return test_fail("sub #1 copy failed: %d", errno);
}
if (PX4_OK != orb_copy(ORB_ID(orb_multitest), sfd1, &u)) if (u.val != 203) {
return test_fail("sub #1 copy failed: %d", errno); return test_fail("sub #1 val. mismatch: %d", u.val);
}
if (u.val != 203) /* test priorities */
return test_fail("sub #1 val. mismatch: %d", u.val); int prio;
/* test priorities */ if (PX4_OK != orb_priority(sfd0, &prio)) {
int prio; return test_fail("prio #0");
if (PX4_OK != orb_priority(sfd0, &prio)) }
return test_fail("prio #0");
if (prio != ORB_PRIO_MAX) if (prio != ORB_PRIO_MAX) {
return test_fail("prio: %d", prio); return test_fail("prio: %d", prio);
}
if (PX4_OK != orb_priority(sfd1, &prio)) if (PX4_OK != orb_priority(sfd1, &prio)) {
return test_fail("prio #1"); return test_fail("prio #1");
}
if (prio != ORB_PRIO_MIN) if (prio != ORB_PRIO_MIN) {
return test_fail("prio: %d", prio); return test_fail("prio: %d", prio);
}
if (PX4_OK != latency_test<struct orb_test>(ORB_ID(orb_test), false)) if (PX4_OK != latency_test<struct orb_test>(ORB_ID(orb_test), false)) {
return test_fail("latency test failed"); return test_fail("latency test failed");
}
return test_note("PASS"); return test_note("PASS");
} }
int uORBTest::UnitTest::info() int uORBTest::UnitTest::info()
{ {
return OK; return OK;
} }
int uORBTest::UnitTest::test_fail(const char *fmt, ...) int uORBTest::UnitTest::test_fail(const char *fmt, ...)
{ {
va_list ap; va_list ap;
fprintf(stderr, "uORB FAIL: "); fprintf(stderr, "uORB FAIL: ");
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
va_end(ap); va_end(ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
fflush(stderr); fflush(stderr);
return uORB::ERROR; return uORB::ERROR;
} }
int uORBTest::UnitTest::test_note(const char *fmt, ...) int uORBTest::UnitTest::test_note(const char *fmt, ...)
{ {
va_list ap; va_list ap;
fprintf(stderr, "uORB note: "); fprintf(stderr, "uORB note: ");
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
va_end(ap); va_end(ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
fflush(stderr); fflush(stderr);
return OK; return OK;
} }
int uORBTest::UnitTest::pubsubtest_threadEntry(char* const argv[]) int uORBTest::UnitTest::pubsubtest_threadEntry(char *const argv[])
{ {
uORBTest::UnitTest &t = uORBTest::UnitTest::instance(); uORBTest::UnitTest &t = uORBTest::UnitTest::instance();
return t.pubsublatency_main(); return t.pubsublatency_main();
} }

136
src/modules/uORB/uORBTest_UnitTest.hpp

@ -38,101 +38,103 @@
#include <px4_time.h> #include <px4_time.h>
struct orb_test { struct orb_test {
int val; int val;
hrt_abstime time; hrt_abstime time;
}; };
ORB_DEFINE(orb_test, struct orb_test); ORB_DEFINE(orb_test, struct orb_test);
ORB_DEFINE(orb_multitest, struct orb_test); ORB_DEFINE(orb_multitest, struct orb_test);
struct orb_test_medium { struct orb_test_medium {
int val; int val;
hrt_abstime time; hrt_abstime time;
char junk[64]; char junk[64];
}; };
ORB_DEFINE(orb_test_medium, struct orb_test_medium); ORB_DEFINE(orb_test_medium, struct orb_test_medium);
struct orb_test_large { struct orb_test_large {
int val; int val;
hrt_abstime time; hrt_abstime time;
char junk[512]; char junk[512];
}; };
ORB_DEFINE(orb_test_large, struct orb_test_large); ORB_DEFINE(orb_test_large, struct orb_test_large);
namespace uORBTest namespace uORBTest
{ {
class UnitTest; class UnitTest;
} }
class uORBTest::UnitTest class uORBTest::UnitTest
{ {
public: public:
// Singleton pattern // Singleton pattern
static uORBTest::UnitTest &instance(); static uORBTest::UnitTest &instance();
~UnitTest() {} ~UnitTest() {}
int test(); int test();
template<typename S> int latency_test(orb_id_t T, bool print); template<typename S> int latency_test(orb_id_t T, bool print);
int info(); int info();
private: private:
UnitTest() : pubsubtest_passed(false), pubsubtest_print(false) {} UnitTest() : pubsubtest_passed(false), pubsubtest_print(false) {}
// Disallow copy // Disallow copy
UnitTest(const uORBTest::UnitTest &) {}; UnitTest(const uORBTest::UnitTest &) {};
static int pubsubtest_threadEntry(char* const argv[]); static int pubsubtest_threadEntry(char *const argv[]);
int pubsublatency_main(void); int pubsublatency_main(void);
bool pubsubtest_passed; bool pubsubtest_passed;
bool pubsubtest_print; bool pubsubtest_print;
int pubsubtest_res = OK; int pubsubtest_res = OK;
int test_fail(const char *fmt, ...); int test_fail(const char *fmt, ...);
int test_note(const char *fmt, ...); int test_note(const char *fmt, ...);
}; };
template<typename S> template<typename S>
int uORBTest::UnitTest::latency_test(orb_id_t T, bool print) int uORBTest::UnitTest::latency_test(orb_id_t T, bool print)
{ {
test_note("---------------- LATENCY TEST ------------------"); test_note("---------------- LATENCY TEST ------------------");
S t; S t;
t.val = 308; t.val = 308;
t.time = hrt_absolute_time(); t.time = hrt_absolute_time();
orb_advert_t pfd0 = orb_advertise(T, &t); orb_advert_t pfd0 = orb_advertise(T, &t);
char * const args[1] = { NULL }; char *const args[1] = { NULL };
pubsubtest_print = print; pubsubtest_print = print;
pubsubtest_passed = false; pubsubtest_passed = false;
/* test pub / sub latency */ /* test pub / sub latency */
// Can't pass a pointer in args, must be a null terminated // Can't pass a pointer in args, must be a null terminated
// array of strings because the strings are copied to // array of strings because the strings are copied to
// prevent access if the caller data goes out of scope // prevent access if the caller data goes out of scope
int pubsub_task = px4_task_spawn_cmd("uorb_latency", int pubsub_task = px4_task_spawn_cmd("uorb_latency",
SCHED_DEFAULT, SCHED_DEFAULT,
SCHED_PRIORITY_MAX - 5, SCHED_PRIORITY_MAX - 5,
1500, 1500,
(px4_main_t)&uORBTest::UnitTest::pubsubtest_threadEntry, (px4_main_t)&uORBTest::UnitTest::pubsubtest_threadEntry,
args); args);
/* give the test task some data */ /* give the test task some data */
while (!pubsubtest_passed) { while (!pubsubtest_passed) {
t.val = 308; t.val = 308;
t.time = hrt_absolute_time(); t.time = hrt_absolute_time();
if (PX4_OK != orb_publish(T, pfd0, &t))
return test_fail("mult. pub0 timing fail"); if (PX4_OK != orb_publish(T, pfd0, &t)) {
return test_fail("mult. pub0 timing fail");
/* simulate >800 Hz system operation */ }
usleep(1000);
} /* simulate >800 Hz system operation */
usleep(1000);
if (pubsub_task < 0) { }
return test_fail("failed launching task");
} if (pubsub_task < 0) {
return test_fail("failed launching task");
return pubsubtest_res; }
return pubsubtest_res;
} }
#endif // _uORBTest_UnitTest_hpp_ #endif // _uORBTest_UnitTest_hpp_

Loading…
Cancel
Save