test_scip2.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <memory>
20 #include <ostream>
21 #include <string>
22 #include <vector>
23 
24 #include <scip2/protocol.h>
25 #include <scip2/connection.h>
26 
28 {
29 public:
30  void spin() final
31  {
32  }
33  void stop() final
34  {
35  }
36  void send(const std::string&, CallbackSend = CallbackSend()) final
37  {
38  }
39  void startWatchdog(const boost::posix_time::time_duration&) final
40  {
41  }
42  void feed(boost::asio::streambuf& buf, const boost::posix_time::ptime& time_read)
43  {
44  receive(buf, time_read);
45  }
46 };
47 
48 TEST(SCIP2, MultipleResponses)
49 {
50  std::shared_ptr<ConnectionDummy> dev(new ConnectionDummy());
51  scip2::Protocol p(dev);
52 
53  const auto now = boost::posix_time::microsec_clock::universal_time();
54  int num_receive = 0;
55 
56  const auto cb =
57  [&num_receive, now](
58  const boost::posix_time::ptime& time_read,
59  const std::string& echo_back,
60  const std::string& status)
61  {
62  num_receive++;
63  EXPECT_EQ(now, time_read);
64  EXPECT_EQ("00", status);
65  };
67 
68  /*
69  Input data:
70 
71  QT // First QT response
72  00P
73 
74  FOO // Unknown response
75  BAR
76  QT // This line must be ignored as a part of unknown command
77 
78  QT // This must be ignored as it doesn't have correct status line
79 
80  QT // Second QT response
81  00P
82 
83  */
84  boost::asio::streambuf buf;
85  std::ostream os(&buf);
86  os << "QT\n00P\n\nF";
87  dev->feed(buf, now);
88  ASSERT_EQ(1, num_receive);
89 
90  os << "OO\nBAR\nQT\n\n";
91  dev->feed(buf, now);
92  ASSERT_EQ(1, num_receive);
93 
94  os << "QT\n\nQT\n";
95  dev->feed(buf, now);
96  ASSERT_EQ(1, num_receive);
97 
98  os << "00P\n\n";
99  dev->feed(buf, now);
100  ASSERT_EQ(2, num_receive);
101 
102  os << "\n\n";
103  dev->feed(buf, now);
104  ASSERT_EQ(2, num_receive);
105 }
106 
107 int main(int argc, char** argv)
108 {
109  testing::InitGoogleTest(&argc, argv);
110 
111  return RUN_ALL_TESTS();
112 }
ConnectionDummy::stop
void stop() final
Definition: test_scip2.cpp:33
ConnectionDummy::spin
void spin() final
Definition: test_scip2.cpp:30
scip2::Connection
Definition: connection.h:33
ConnectionDummy::startWatchdog
void startWatchdog(const boost::posix_time::time_duration &) final
Definition: test_scip2.cpp:39
protocol.h
scip2::Protocol::registerCallback
void registerCallback(typename TResponse::Callback cb)
Definition: protocol.h:91
ConnectionDummy::feed
void feed(boost::asio::streambuf &buf, const boost::posix_time::ptime &time_read)
Definition: test_scip2.cpp:42
scip2::Connection::receive
void receive(boost::asio::streambuf &buf, const boost::posix_time::ptime &time_read)
Definition: connection.h:59
TEST
TEST(SCIP2, MultipleResponses)
Definition: test_scip2.cpp:48
ConnectionDummy
Definition: test_scip2.cpp:27
connection.h
main
int main(int argc, char **argv)
Definition: test_scip2.cpp:107
scip2::ResponseQT
Definition: quit.h:28
scip2::Protocol
Definition: protocol.h:33
ConnectionDummy::send
void send(const std::string &, CallbackSend=CallbackSend()) final
Definition: test_scip2.cpp:36
scip2::Connection::CallbackSend
boost::function< void(const boost::posix_time::ptime &)> CallbackSend
Definition: connection.h:43


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Wed Dec 18 2024 03:10:57