test_buffer.cpp
Go to the documentation of this file.
00001 /*
00002  * lms_buffer.h
00003  *
00004  *  Author: Mike Purvis <mpurvis@clearpathrobotics.com>
00005  ***************************************************************************
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the Free Software   *
00018  *   Foundation, Inc., 59 Temple Place,                                    *
00019  *   Suite 330, Boston, MA  02111-1307  USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #include "LMS1xx/lms_buffer.h"
00024 #include "gtest/gtest.h"
00025 
00026 #include <errno.h>
00027 #include <unistd.h>
00028 
00029 
00030 class BufferTest : public :: testing :: Test
00031 {
00032   virtual void SetUp()
00033   {
00034     ASSERT_NE(-1, pipe(fds_));
00035     ASSERT_NE(0, fds_[0]);
00036     ASSERT_NE(0, fds_[1]);
00037   }
00038 
00039   virtual void TearDown()
00040   {
00041     close(fds_[0]);
00042     close(fds_[1]);
00043   }
00044 
00045 protected:
00046   LMSBuffer buf_;
00047   int fds_[2];
00048 };
00049 
00050 
00051 TEST_F(BufferTest, single)
00052 {
00053   ASSERT_NE(-1, write(fds_[1], "\x02ghij\x03", 6)) << "Error code: " << errno;
00054   buf_.readFrom(fds_[0]);
00055 
00056   // Final char should have been transformed to null terminator.
00057   ASSERT_STREQ("\x02ghij", buf_.getNextBuffer());
00058 }
00059 
00060 TEST_F(BufferTest, split)
00061 {
00062   ASSERT_NE(-1, write(fds_[1], "\x02ghij", 5)) << "Error code: " << errno;
00063   buf_.readFrom(fds_[0]);
00064   EXPECT_EQ(NULL, buf_.getNextBuffer());
00065 
00066   ASSERT_NE(-1, write(fds_[1], "abcde", 5)) << "Error code: " << errno;
00067   buf_.readFrom(fds_[0]);
00068   EXPECT_EQ(NULL, buf_.getNextBuffer());
00069 
00070   ASSERT_NE(-1, write(fds_[1], "fghijk\x03", 7)) << "Error code: " << errno;
00071   buf_.readFrom(fds_[0]);
00072   EXPECT_STREQ("\x02ghijabcdefghijk", buf_.getNextBuffer());
00073 }
00074 
00075 TEST_F(BufferTest, extra_bytes)
00076 {
00077   ASSERT_NE(-1, write(fds_[1], "abc\x02ghijk\x03stuv", 14)) << "Error code: " << errno;
00078   buf_.readFrom(fds_[0]);
00079   EXPECT_STREQ("\x02ghijk", buf_.getNextBuffer());
00080   buf_.popLastBuffer();
00081 
00082   ASSERT_NE(-1, write(fds_[1], "abc\x02ghjk\x03stuv", 14)) << "Error code: " << errno;
00083   buf_.readFrom(fds_[0]);
00084   EXPECT_STREQ("\x02ghjk", buf_.getNextBuffer());
00085 }
00086 
00087 TEST_F(BufferTest, multiple)
00088 {
00089   ASSERT_NE(-1, write(fds_[1], "\x02gh\x03\x02jk\x03xyz\x02ty\x03", 15)) << "Error code: " << errno;
00090   buf_.readFrom(fds_[0]);
00091 
00092   EXPECT_STREQ("\x02gh", buf_.getNextBuffer());
00093   buf_.popLastBuffer();
00094   EXPECT_STREQ("\x02jk", buf_.getNextBuffer());
00095   buf_.popLastBuffer();
00096   EXPECT_STREQ("\x02ty", buf_.getNextBuffer());
00097   buf_.popLastBuffer();
00098 }
00099 
00100 int main(int argc, char **argv)
00101 {
00102   testing::InitGoogleTest(&argc, argv);
00103   return RUN_ALL_TESTS();
00104 }


lms1xx
Author(s): Konrad Banachowicz
autogenerated on Fri May 5 2017 03:26:26