test_dispatcher.cpp
Go to the documentation of this file.
1 // Bring in my package's API, which is what I'm testing
3 
4 // Bring in gtest
5 #include <gtest/gtest.h>
6 
7 class Counter {
8 public:
9  size_t counter_;
10  Counter() : counter_(0) {}
11  void count(const can::Frame &msg) {
12  ++counter_;
13  }
14 };
15 
16 TEST(DispatcherTest, testFilteredDispatcher)
17 {
19  Counter counter1;
20  Counter counter2;
21  std::vector<can::CommInterface::FrameListenerConstSharedPtr> listeners;
22  const size_t max_id = (1<<11);
23  for(size_t i=0; i < max_id; i+=2) {
24  listeners.push_back(dispatcher.createListener(can::MsgHeader(i), can::CommInterface::FrameDelegate(&counter1, &Counter::count)));
25  listeners.push_back(dispatcher.createListener(can::MsgHeader(i+1), can::CommInterface::FrameDelegate(&counter2, &Counter::count)));
26  }
27 
28  boost::chrono::steady_clock::time_point start = boost::chrono::steady_clock::now();
29  const size_t num = 1000 * max_id;
30  for(size_t i=0; i < num; ++i) {
31  dispatcher.dispatch(can::Frame(can::MsgHeader(i%max_id)));
32  }
33  boost::chrono::steady_clock::time_point now = boost::chrono::steady_clock::now();
34  double diff = boost::chrono::duration_cast<boost::chrono::duration<double> >(now-start).count();
35 
36  EXPECT_EQ(num, counter1.counter_+ counter2.counter_);
37  EXPECT_EQ(counter1.counter_, counter2.counter_);
38  std::cout << std::fixed << diff << "\t" << num << "\t" << num / diff << std::endl;
39 
40 }
41 
42 TEST(DispatcherTest, testSimpleDispatcher)
43 {
45  Counter counter;
46  can::CommInterface::FrameListenerConstSharedPtr listener = dispatcher.createListener(can::CommInterface::FrameDelegate(&counter, &Counter::count));
47 
48  boost::chrono::steady_clock::time_point start = boost::chrono::steady_clock::now();
49  const size_t max_id = (1<<11);
50  const size_t num = 1000*max_id;
51  for(size_t i=0; i < num; ++i) {
52  dispatcher.dispatch(can::Frame(can::MsgHeader(i%max_id)));
53  }
54  boost::chrono::steady_clock::time_point now = boost::chrono::steady_clock::now();
55  double diff = boost::chrono::duration_cast<boost::chrono::duration<double> >(now-start).count();
56 
57  EXPECT_EQ(num, counter.counter_);
58  std::cout << std::fixed << diff << "\t" << num << "\t" << num / diff << std::endl;
59 }
60 
61 TEST(DispatcherTest, testDelegateOnly)
62 {
63  Counter counter;
64  can::CommInterface::FrameDelegate delegate(&counter, &Counter::count);
65 
66  boost::chrono::steady_clock::time_point start = boost::chrono::steady_clock::now();
67  const size_t max_id = (1<<11);
68  const size_t num = 10000*max_id;
69  for(size_t i=0; i < num; ++i) {
70  delegate(can::Frame(can::MsgHeader(i%max_id)));
71  }
72  boost::chrono::steady_clock::time_point now = boost::chrono::steady_clock::now();
73  double diff = boost::chrono::duration_cast<boost::chrono::duration<double> >(now-start).count();
74 
75  EXPECT_EQ(num, counter.counter_);
76  std::cout << std::fixed << diff << "\t" << num << "\t" << num / diff << std::endl;
77 
78 }
79 
80 // Run all the tests that were declared with TEST()
81 int main(int argc, char **argv){
82 testing::InitGoogleTest(&argc, argv);
83 return RUN_ALL_TESTS();
84 }
size_t counter_
int main(int argc, char **argv)
TEST(DispatcherTest, testFilteredDispatcher)
void dispatch(const K &key, const typename BaseClass::Type &obj)
Definition: dispatcher.h:98
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:140
void dispatch(const Type &obj)
Definition: dispatcher.h:70
void count(const can::Frame &msg)
BaseClass::ListenerConstSharedPtr createListener(const K &key, const typename BaseClass::Callable &callable)
Definition: dispatcher.h:85
ListenerConstSharedPtr createListener(const Callable &callable)
Definition: dispatcher.h:66


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Mon Feb 28 2022 23:28:00