test_posix.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <uavcan_posix/dynamic_node_id_server/file_event_tracer.hpp>
6 #include <uavcan_posix/dynamic_node_id_server/file_storage_backend.hpp>
8 #include <iostream>
9 #include <iomanip>
10 #include "debug.hpp"
11 
12 int main(int argc, const char** argv)
13 {
14  (void)argc;
15  (void)argv;
16  try
17  {
18  ENFORCE(0 == std::system("mkdir -p /tmp/uavcan_posix/dynamic_node_id_server"));
19 
20  /*
21  * Event tracer test
22  */
23  {
24  using namespace uavcan::dynamic_node_id_server;
25 
26  const std::string event_log_file("/tmp/uavcan_posix/dynamic_node_id_server/event.log");
27 
28  uavcan_posix::dynamic_node_id_server::FileEventTracer tracer;
29  ENFORCE(0 <= tracer.init(event_log_file.c_str()));
30 
31  // Adding a line
32  static_cast<IEventTracer&>(tracer).onEvent(TraceError, 123456);
33  ENFORCE(0 == std::system(("cat " + event_log_file).c_str()));
34 
35  // Removing the log file
36  ENFORCE(0 == std::system(("rm -f " + event_log_file).c_str()));
37 
38  // Adding another line
39  static_cast<IEventTracer&>(tracer).onEvent(TraceError, 789123);
40  ENFORCE(0 == std::system(("cat " + event_log_file).c_str()));
41  }
42 
43  /*
44  * Storage backend test
45  */
46  {
47  using namespace uavcan::dynamic_node_id_server;
48 
49  uavcan_posix::dynamic_node_id_server::FileStorageBackend backend;
50  ENFORCE(0 <= backend.init("/tmp/uavcan_posix/dynamic_node_id_server/storage"));
51 
52  auto print_key = [&](const char* key) {
53  std::cout << static_cast<IStorageBackend&>(backend).get(key).c_str() << std::endl;
54  };
55 
56  print_key("foobar");
57 
58  static_cast<IStorageBackend&>(backend).set("foobar", "0123456789abcdef0123456789abcdef");
59  static_cast<IStorageBackend&>(backend).set("the_answer", "42");
60 
61  print_key("foobar");
62  print_key("the_answer");
63  print_key("nonexistent");
64  }
65 
66  return 0;
67  }
68  catch (const std::exception& ex)
69  {
70  std::cerr << "Exception: " << ex.what() << std::endl;
71  return 1;
72  }
73 }
TraceError
TraceError
Definition: event.hpp:23
ENFORCE
#define ENFORCE(x)
Definition: platform_specific_components/linux/libuavcan/apps/debug.hpp:13
main
int main(int argc, const char **argv)
Definition: test_posix.cpp:12
uavcan::dynamic_node_id_server::IStorageBackend
Definition: storage_backend.hpp:22
uavcan_linux.hpp
debug.hpp
set
ROSCPP_DECL void set(const std::string &key, bool b)
uavcan::dynamic_node_id_server::IEventTracer
Definition: event.hpp:90
uavcan::dynamic_node_id_server
Definition: abstract_server.hpp:16


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03