test_buffer.cpp
Go to the documentation of this file.
1 /*
2  * lms_buffer.h
3  *
4  * Author: Mike Purvis <mpurvis@clearpathrobotics.com>
5  ***************************************************************************
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the Free Software *
18  * Foundation, Inc., 59 Temple Place, *
19  * Suite 330, Boston, MA 02111-1307 USA *
20  * *
21  ***************************************************************************/
22 
23 #include "LMS1xx/lms_buffer.h"
24 #include "gtest/gtest.h"
25 
26 #include <errno.h>
27 #include <unistd.h>
28 
29 
30 class BufferTest : public :: testing :: Test
31 {
32  virtual void SetUp()
33  {
34  ASSERT_NE(-1, pipe(fds_));
35  ASSERT_NE(0, fds_[0]);
36  ASSERT_NE(0, fds_[1]);
37  }
38 
39  virtual void TearDown()
40  {
41  close(fds_[0]);
42  close(fds_[1]);
43  }
44 
45 protected:
47  int fds_[2];
48 };
49 
50 
52 {
53  ASSERT_NE(-1, write(fds_[1], "\x02ghij\x03", 6)) << "Error code: " << errno;
54  buf_.readFrom(fds_[0]);
55 
56  // Final char should have been transformed to null terminator.
57  ASSERT_STREQ("\x02ghij", buf_.getNextBuffer());
58 }
59 
61 {
62  ASSERT_NE(-1, write(fds_[1], "\x02ghij", 5)) << "Error code: " << errno;
63  buf_.readFrom(fds_[0]);
64  EXPECT_EQ(NULL, buf_.getNextBuffer());
65 
66  ASSERT_NE(-1, write(fds_[1], "abcde", 5)) << "Error code: " << errno;
67  buf_.readFrom(fds_[0]);
68  EXPECT_EQ(NULL, buf_.getNextBuffer());
69 
70  ASSERT_NE(-1, write(fds_[1], "fghijk\x03", 7)) << "Error code: " << errno;
71  buf_.readFrom(fds_[0]);
72  EXPECT_STREQ("\x02ghijabcdefghijk", buf_.getNextBuffer());
73 }
74 
75 TEST_F(BufferTest, extra_bytes)
76 {
77  ASSERT_NE(-1, write(fds_[1], "abc\x02ghijk\x03stuv", 14)) << "Error code: " << errno;
78  buf_.readFrom(fds_[0]);
79  EXPECT_STREQ("\x02ghijk", buf_.getNextBuffer());
81 
82  ASSERT_NE(-1, write(fds_[1], "abc\x02ghjk\x03stuv", 14)) << "Error code: " << errno;
83  buf_.readFrom(fds_[0]);
84  EXPECT_STREQ("\x02ghjk", buf_.getNextBuffer());
85 }
86 
87 TEST_F(BufferTest, multiple)
88 {
89  ASSERT_NE(-1, write(fds_[1], "\x02gh\x03\x02jk\x03xyz\x02ty\x03", 15)) << "Error code: " << errno;
90  buf_.readFrom(fds_[0]);
91 
92  EXPECT_STREQ("\x02gh", buf_.getNextBuffer());
94  EXPECT_STREQ("\x02jk", buf_.getNextBuffer());
96  EXPECT_STREQ("\x02ty", buf_.getNextBuffer());
98 }
99 
100 int main(int argc, char **argv)
101 {
102  testing::InitGoogleTest(&argc, argv);
103  return RUN_ALL_TESTS();
104 }
char * getNextBuffer()
Definition: lms_buffer.h:58
void readFrom(int fd)
Definition: lms_buffer.h:42
int fds_[2]
Definition: test_buffer.cpp:47
int main(int argc, char **argv)
virtual void TearDown()
Definition: test_buffer.cpp:39
virtual void SetUp()
Definition: test_buffer.cpp:32
TEST_F(BufferTest, single)
Definition: test_buffer.cpp:51
LMSBuffer buf_
Definition: test_buffer.cpp:46
void popLastBuffer()
Definition: lms_buffer.h:99


lms1xx
Author(s): Konrad Banachowicz
autogenerated on Wed Jan 22 2020 03:36:59