unix_serial_tests.cc
Go to the documentation of this file.
00001 /* To run these tests you need to change the define below to the serial port 
00002  * with a loop back device attached.
00003  * 
00004  * Alternatively you could use an Arduino:
00005  
00006 void setup()
00007 {
00008  Serial.begin(115200);
00009 }
00010 
00011 void loop()
00012 {
00013  while (Serial.available() > 0) {
00014    Serial.write(Serial.read());
00015  }
00016 }
00017  
00018 */
00019 
00020 #include <string>
00021 #include "gtest/gtest.h"
00022 
00023 #include <boost/bind.hpp>
00024 
00025 // Use FRIEND_TEST... its not as nasty, thats what friends are for
00026 // // OMG this is so nasty...
00027 // #define private public
00028 // #define protected public
00029 
00030 #include "serial/serial.h"
00031 
00032 #if defined(__linux__)
00033 #include <pty.h>
00034 #else
00035 #include <util.h>
00036 #endif
00037 
00038 using namespace serial;
00039 
00040 using std::string;
00041 
00042 namespace {
00043 
00044 class SerialTests : public ::testing::Test {
00045 protected:
00046   virtual void SetUp() {
00047     if (openpty(&master_fd, &slave_fd, name, NULL, NULL) == -1) {
00048       perror("openpty");
00049       exit(127);
00050     }
00051 
00052     ASSERT_TRUE(master_fd > 0);
00053     ASSERT_TRUE(slave_fd > 0);
00054     ASSERT_TRUE(string(name).length() > 0);
00055 
00056     port1 = new Serial(string(name), 115200, Timeout::simpleTimeout(250));
00057   }
00058 
00059   virtual void TearDown() {
00060     port1->close();
00061     delete port1;
00062   }
00063 
00064   Serial * port1;
00065   int master_fd;
00066   int slave_fd;
00067   char name[100];
00068 };
00069 
00070 TEST_F(SerialTests, readWorks) {
00071   write(master_fd, "abc\n", 4);
00072   string r = port1->read(4);
00073   EXPECT_EQ(r, string("abc\n"));
00074 }
00075 
00076 TEST_F(SerialTests, writeWorks) {
00077   char buf[5] = "";
00078   port1->write("abc\n");
00079   read(master_fd, buf, 4);
00080   EXPECT_EQ(string(buf, 4), string("abc\n"));
00081 }
00082 
00083 TEST_F(SerialTests, timeoutWorks) {
00084   // Timeout a read, returns an empty string
00085   string empty = port1->read();
00086   EXPECT_EQ(empty, string(""));
00087   
00088   // Ensure that writing/reading still works after a timeout.
00089   write(master_fd, "abc\n", 4);
00090   string r = port1->read(4);
00091   EXPECT_EQ(r, string("abc\n"));
00092 }
00093 
00094 TEST_F(SerialTests, partialRead) {
00095   // Write some data, but request more than was written.
00096   write(master_fd, "abc\n", 4);
00097 
00098   // Should timeout, but return what was in the buffer.
00099   string empty = port1->read(10);
00100   EXPECT_EQ(empty, string("abc\n"));
00101   
00102   // Ensure that writing/reading still works after a timeout.
00103   write(master_fd, "abc\n", 4);
00104   string r = port1->read(4);
00105   EXPECT_EQ(r, string("abc\n"));
00106 }
00107 
00108 }  // namespace
00109 
00110 int main(int argc, char **argv) {
00111   try {
00112     ::testing::InitGoogleTest(&argc, argv);
00113     return RUN_ALL_TESTS();
00114   } catch (std::exception &e) {
00115     std::cerr << "Unhandled Exception: " << e.what() << std::endl;
00116   }
00117   return 1;
00118 }


serial
Author(s): William Woodall , John Harrison
autogenerated on Thu Mar 28 2019 03:29:52