Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Kestrel Collaboration
Kestrel Firmware
serial
Commits
68fb40e0
Commit
68fb40e0
authored
13 years ago
by
John Harrison
Browse files
Options
Download
Email Patches
Plain Diff
Adding my basic tests
parent
6138acee
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
20 deletions
+74
-20
tests/serial_tests.cc
tests/serial_tests.cc
+74
-20
No files found.
tests/serial_tests.cc
View file @
68fb40e0
...
...
@@ -3,39 +3,57 @@
*
* Alternatively you could use an Arduino:
void setup()
{
Serial.begin(115200);
}
void setup()
{
Serial.begin(115200);
}
void loop()
{
while (Serial.available() > 0) {
Serial.write(Serial.read());
}
void loop()
{
while (Serial.available() > 0) {
Serial.write(Serial.read());
}
}
*/
#define SERIAL_PORT_NAME "/dev/tty.usbserial"
*/
#include <string>
#include "gtest/gtest.h"
#include <boost/bind.hpp>
// OMG this is so nasty...
#define private public
#define protected public
// 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"
#ifdef __linux__
#include <pty.h>
#else
#include <util.h>
#endif
using
namespace
serial
;
using
std
::
string
;
namespace
{
class
SerialTests
:
public
::
testing
::
Test
{
protected:
virtual
void
SetUp
()
{
port1
=
new
Serial
(
SERIAL_PORT_NAME
,
115200
,
250
);
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
,
250
);
}
virtual
void
TearDown
()
{
...
...
@@ -44,12 +62,48 @@ protected:
}
Serial
*
port1
;
int
master_fd
;
int
slave_fd
;
char
name
[
100
];
};
// TEST_F(SerialTests, throwsOnInvalidPort) {
//
// }
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
"
));
}
}
// namespace
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment