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


serial
Author(s): William Woodall , John Harrison
autogenerated on Wed Mar 9 2022 03:10:03