1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 03:34:53 +08:00
serial/tests/unix_serial_tests.cc

117 lines
2.4 KiB
C++
Raw Normal View History

2012-01-23 14:28:14 -06:00
/* To run these tests you need to change the define below to the serial port
* with a loop back device attached.
*
* Alternatively you could use an Arduino:
2012-02-06 22:44:30 -06:00
void setup()
{
Serial.begin(115200);
}
2012-01-23 14:28:14 -06:00
2012-02-06 22:44:30 -06:00
void loop()
{
while (Serial.available() > 0) {
Serial.write(Serial.read());
2012-01-23 14:28:14 -06:00
}
2012-02-06 22:44:30 -06:00
}
2012-01-23 14:28:14 -06:00
2012-02-06 22:44:30 -06:00
*/
2012-01-23 14:28:14 -06:00
2012-02-06 22:44:30 -06:00
#include <string>
2012-01-23 14:28:14 -06:00
#include "gtest/gtest.h"
2012-02-06 22:44:30 -06:00
// Use FRIEND_TEST... its not as nasty, thats what friends are for
// // OMG this is so nasty...
// #define private public
// #define protected public
#include "serial/serial.h"
2012-02-06 22:44:30 -06:00
#if defined(__linux__)
2012-02-06 22:44:30 -06:00
#include <pty.h>
#else
#include <util.h>
#endif
2012-01-23 14:28:14 -06:00
using namespace serial;
2012-02-06 22:44:30 -06:00
using std::string;
2012-01-23 14:28:14 -06:00
namespace {
2012-01-23 14:28:14 -06:00
class SerialTests : public ::testing::Test {
protected:
virtual void SetUp() {
2012-02-06 22:44:30 -06:00
if (openpty(&master_fd, &slave_fd, name, NULL, NULL) == -1) {
perror("openpty");
exit(127);
}
ASSERT_TRUE(master_fd > 0);
ASSERT_TRUE(slave_fd > 0);
ASSERT_TRUE(string(name).length() > 0);
port1 = new Serial(string(name), 115200, Timeout::simpleTimeout(250));
}
2012-01-23 14:28:14 -06:00
virtual void TearDown() {
port1->close();
delete port1;
}
2012-01-23 14:28:14 -06:00
Serial * port1;
2012-02-06 22:44:30 -06:00
int master_fd;
int slave_fd;
char name[100];
2012-01-23 14:28:14 -06:00
};
2012-02-06 22:44:30 -06:00
TEST_F(SerialTests, readWorks) {
write(master_fd, "abc\n", 4);
string r = port1->read(4);
EXPECT_EQ(r, string("abc\n"));
}
TEST_F(SerialTests, writeWorks) {
char buf[5] = "";
port1->write("abc\n");
read(master_fd, buf, 4);
EXPECT_EQ(string(buf, 4), string("abc\n"));
}
TEST_F(SerialTests, timeoutWorks) {
// Timeout a read, returns an empty string
string empty = port1->read();
EXPECT_EQ(empty, string(""));
// Ensure that writing/reading still works after a timeout.
write(master_fd, "abc\n", 4);
string r = port1->read(4);
EXPECT_EQ(r, string("abc\n"));
}
TEST_F(SerialTests, partialRead) {
// Write some data, but request more than was written.
write(master_fd, "abc\n", 4);
// Should timeout, but return what was in the buffer.
string empty = port1->read(10);
EXPECT_EQ(empty, string("abc\n"));
// Ensure that writing/reading still works after a timeout.
write(master_fd, "abc\n", 4);
string r = port1->read(4);
EXPECT_EQ(r, string("abc\n"));
}
2012-01-23 14:28:14 -06:00
} // namespace
int main(int argc, char **argv) {
try {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
} catch (std::exception &e) {
std::cerr << "Unhandled Exception: " << e.what() << std::endl;
}
2012-01-23 14:28:14 -06:00
return 1;
}