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;
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;
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 }
can::CommInterface::FrameListenerConstSharedPtr
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:140
can::Frame
Definition: interface.h:62
main
int main(int argc, char **argv)
Definition: test_dispatcher.cpp:81
TEST
TEST(DispatcherTest, testFilteredDispatcher)
Definition: test_dispatcher.cpp:16
Counter::count
void count(const can::Frame &msg)
Definition: test_dispatcher.cpp:11
dispatcher.h
can::MsgHeader
Definition: interface.h:49
can::FilteredDispatcher::createListener
BaseClass::ListenerConstSharedPtr createListener(const K &key, const typename BaseClass::Callable &callable)
Definition: dispatcher.h:85
can::DelegateHelper
Definition: delegates.h:9
Counter
Definition: test_dispatcher.cpp:7
can::SimpleDispatcher
Definition: dispatcher.h:14
can::SimpleDispatcher::dispatch
void dispatch(const Type &obj)
Definition: dispatcher.h:70
Counter::Counter
Counter()
Definition: test_dispatcher.cpp:10
can::SimpleDispatcher::createListener
ListenerConstSharedPtr createListener(const Callable &callable)
Definition: dispatcher.h:66
can::FilteredDispatcher
Definition: dispatcher.h:80
Counter::counter_
size_t counter_
Definition: test_dispatcher.cpp:9
can::FilteredDispatcher::dispatch
void dispatch(const K &key, const typename BaseClass::Type &obj)
Definition: dispatcher.h:98


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Wed Mar 2 2022 00:52:25