21 #include "gtest/gtest.h"
30 #if defined(__linux__)
42 class SerialTests :
public ::testing::Test {
44 virtual void SetUp() {
45 if (openpty(&master_fd, &slave_fd, name, NULL, NULL) == -1) {
50 ASSERT_TRUE(master_fd > 0);
51 ASSERT_TRUE(slave_fd > 0);
52 ASSERT_TRUE(
string(name).length() > 0);
57 virtual void TearDown() {
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"));
74 TEST_F(SerialTests, writeWorks) {
76 port1->write(
"abc\n");
77 read(master_fd, buf, 4);
78 EXPECT_EQ(
string(buf, 4),
string(
"abc\n"));
81 TEST_F(SerialTests, timeoutWorks) {
83 string empty = port1->read();
84 EXPECT_EQ(empty,
string(
""));
87 write(master_fd,
"abc\n", 4);
88 string r = port1->read(4);
89 EXPECT_EQ(r,
string(
"abc\n"));
92 TEST_F(SerialTests, partialRead) {
94 write(master_fd,
"abc\n", 4);
97 string empty = port1->read(10);
98 EXPECT_EQ(empty,
string(
"abc\n"));
101 write(master_fd,
"abc\n", 4);
102 string r = port1->read(4);
103 EXPECT_EQ(r,
string(
"abc\n"));
108 int main(
int argc,
char **argv) {
110 ::testing::InitGoogleTest(&argc, argv);
111 return RUN_ALL_TESTS();
112 }
catch (std::exception &e) {
113 std::cerr <<
"Unhandled Exception: " << e.what() << std::endl;