unix_serial_tests.cc
Go to the documentation of this file.
1 /* To run these tests you need to change the define below to the serial port
2  * with a loop back device attached.
3  *
4  * Alternatively you could use an Arduino:
5 
6 void setup()
7 {
8  Serial.begin(115200);
9 }
10 
11 void loop()
12 {
13  while (Serial.available() > 0) {
14  Serial.write(Serial.read());
15  }
16 }
17 
18 */
19 
20 #include <string>
21 #include "gtest/gtest.h"
22 
23 #include <boost/bind.hpp>
24 
25 // Use FRIEND_TEST... its not as nasty, thats what friends are for
26 // // OMG this is so nasty...
27 // #define private public
28 // #define protected public
29 
30 #include "serial/serial.h"
31 
32 #if defined(__linux__)
33 #include <pty.h>
34 #else
35 #include <util.h>
36 #endif
37 
38 using namespace serial;
39 
40 using std::string;
41 
42 namespace {
43 
44 class SerialTests : public ::testing::Test {
45 protected:
46  virtual void SetUp() {
47  if (openpty(&master_fd, &slave_fd, name, NULL, NULL) == -1) {
48  perror("openpty");
49  exit(127);
50  }
51 
52  ASSERT_TRUE(master_fd > 0);
53  ASSERT_TRUE(slave_fd > 0);
54  ASSERT_TRUE(string(name).length() > 0);
55 
56  port1 = new Serial(string(name), 115200, Timeout::simpleTimeout(250));
57  }
58 
59  virtual void TearDown() {
60  port1->close();
61  delete port1;
62  }
63 
64  Serial * port1;
65  int master_fd;
66  int slave_fd;
67  char name[100];
68 };
69 
70 TEST_F(SerialTests, readWorks) {
71  write(master_fd, "abc\n", 4);
72  string r = port1->read(4);
73  EXPECT_EQ(r, string("abc\n"));
74 }
75 
76 TEST_F(SerialTests, writeWorks) {
77  char buf[5] = "";
78  port1->write("abc\n");
79  read(master_fd, buf, 4);
80  EXPECT_EQ(string(buf, 4), string("abc\n"));
81 }
82 
83 TEST_F(SerialTests, timeoutWorks) {
84  // Timeout a read, returns an empty string
85  string empty = port1->read();
86  EXPECT_EQ(empty, string(""));
87 
88  // Ensure that writing/reading still works after a timeout.
89  write(master_fd, "abc\n", 4);
90  string r = port1->read(4);
91  EXPECT_EQ(r, string("abc\n"));
92 }
93 
94 TEST_F(SerialTests, partialRead) {
95  // Write some data, but request more than was written.
96  write(master_fd, "abc\n", 4);
97 
98  // Should timeout, but return what was in the buffer.
99  string empty = port1->read(10);
100  EXPECT_EQ(empty, string("abc\n"));
101 
102  // Ensure that writing/reading still works after a timeout.
103  write(master_fd, "abc\n", 4);
104  string r = port1->read(4);
105  EXPECT_EQ(r, string("abc\n"));
106 }
107 
108 } // namespace
109 
110 int main(int argc, char **argv) {
111  try {
112  ::testing::InitGoogleTest(&argc, argv);
113  return RUN_ALL_TESTS();
114  } catch (std::exception &e) {
115  std::cerr << "Unhandled Exception: " << e.what() << std::endl;
116  }
117  return 1;
118 }
Definition: unix.h:47
static Timeout simpleTimeout(uint32_t timeout)
Definition: serial.h:112
int main(int argc, char **argv)


serial
Author(s): William Woodall , John Harrison
autogenerated on Thu Jan 9 2020 07:18:58