mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine kernel buffer. complete the utest for buffer.
This commit is contained in:
parent
8992e217a9
commit
94cf0c1069
15 changed files with 218 additions and 37 deletions
|
@ -49,12 +49,12 @@ bool MockEmptyIO::is_never_timeout(int64_t /*timeout_us*/)
|
|||
return true;
|
||||
}
|
||||
|
||||
int MockEmptyIO::read_fully(const void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/)
|
||||
int MockEmptyIO::read_fully(void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int MockEmptyIO::write(const void* /*buf*/, size_t /*size*/, ssize_t* /*nwrite*/)
|
||||
int MockEmptyIO::write(void* /*buf*/, size_t /*size*/, ssize_t* /*nwrite*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ int MockEmptyIO::writev(const iovec */*iov*/, int /*iov_size*/, ssize_t* /*nwrit
|
|||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int MockEmptyIO::read(const void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/)
|
||||
int MockEmptyIO::read(void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/)
|
||||
{
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -34,6 +34,21 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
// we add an empty macro for upp to show the smart tips.
|
||||
#define VOID
|
||||
|
||||
// the asserts of gtest:
|
||||
// * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual
|
||||
// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2
|
||||
// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2
|
||||
// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2
|
||||
// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2
|
||||
// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2
|
||||
// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2
|
||||
// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2
|
||||
// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case
|
||||
// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case
|
||||
// * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual): Tests that two float values are almost equal.
|
||||
// * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual): Tests that two double values are almost equal.
|
||||
// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): Tests that v1 and v2 are within the given distance to each other.
|
||||
|
||||
#include <srs_protocol_io.hpp>
|
||||
|
||||
class MockEmptyIO : public ISrsProtocolReaderWriter
|
||||
|
@ -46,8 +61,8 @@ public:
|
|||
virtual bool is_never_timeout(int64_t timeout_us);
|
||||
// for handshake.
|
||||
public:
|
||||
virtual int read_fully(const void* buf, size_t size, ssize_t* nread);
|
||||
virtual int write(const void* buf, size_t size, ssize_t* nwrite);
|
||||
virtual int read_fully(void* buf, size_t size, ssize_t* nread);
|
||||
virtual int write(void* buf, size_t size, ssize_t* nwrite);
|
||||
// for protocol
|
||||
public:
|
||||
virtual void set_recv_timeout(int64_t timeout_us);
|
||||
|
@ -61,7 +76,7 @@ public:
|
|||
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite);
|
||||
// for protocol/amf0/msg-codec
|
||||
public:
|
||||
virtual int read(const void* buf, size_t size, ssize_t* nread);
|
||||
virtual int read(void* buf, size_t size, ssize_t* nread);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
115
trunk/src/utest/srs_utest_buffer.cpp
Normal file
115
trunk/src/utest/srs_utest_buffer.cpp
Normal file
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include <srs_utest_buffer.hpp>
|
||||
|
||||
#include <srs_kernel_error.hpp>
|
||||
|
||||
MockBufferReader::MockBufferReader(const char* data)
|
||||
{
|
||||
str = data;
|
||||
}
|
||||
|
||||
MockBufferReader::~MockBufferReader()
|
||||
{
|
||||
}
|
||||
|
||||
int MockBufferReader::read(void* buf, size_t size, ssize_t* nread)
|
||||
{
|
||||
int len = srs_min(str.length(), size);
|
||||
|
||||
memcpy(buf, str.data(), len);
|
||||
|
||||
if (nread) {
|
||||
*nread = len;
|
||||
}
|
||||
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
VOID TEST(BufferTest, DefaultObject)
|
||||
{
|
||||
SrsBuffer b;
|
||||
|
||||
EXPECT_EQ(0, b.length());
|
||||
EXPECT_EQ(NULL, b.bytes());
|
||||
}
|
||||
|
||||
VOID TEST(BufferTest, AppendBytes)
|
||||
{
|
||||
SrsBuffer b;
|
||||
|
||||
char winlin[] = "winlin";
|
||||
b.append(winlin, strlen(winlin));
|
||||
EXPECT_EQ((int)strlen(winlin), b.length());
|
||||
ASSERT_TRUE(NULL != b.bytes());
|
||||
EXPECT_EQ('w', b.bytes()[0]);
|
||||
EXPECT_EQ('n', b.bytes()[5]);
|
||||
|
||||
b.append(winlin, strlen(winlin));
|
||||
EXPECT_EQ(2 * (int)strlen(winlin), b.length());
|
||||
ASSERT_TRUE(NULL != b.bytes());
|
||||
EXPECT_EQ('w', b.bytes()[0]);
|
||||
EXPECT_EQ('n', b.bytes()[5]);
|
||||
EXPECT_EQ('w', b.bytes()[6]);
|
||||
EXPECT_EQ('n', b.bytes()[11]);
|
||||
}
|
||||
|
||||
VOID TEST(BufferTest, EraseBytes)
|
||||
{
|
||||
SrsBuffer b;
|
||||
|
||||
char winlin[] = "winlin";
|
||||
b.append(winlin, strlen(winlin));
|
||||
b.erase(b.length());
|
||||
EXPECT_EQ(0, b.length());
|
||||
|
||||
b.append(winlin, strlen(winlin));
|
||||
b.erase(1);
|
||||
EXPECT_EQ(5, b.length());
|
||||
EXPECT_EQ('i', b.bytes()[0]);
|
||||
EXPECT_EQ('n', b.bytes()[4]);
|
||||
b.erase(2);
|
||||
EXPECT_EQ(3, b.length());
|
||||
EXPECT_EQ('l', b.bytes()[0]);
|
||||
EXPECT_EQ('n', b.bytes()[2]);
|
||||
b.erase(3);
|
||||
EXPECT_EQ(0, b.length());
|
||||
}
|
||||
|
||||
VOID TEST(BufferTest, Grow)
|
||||
{
|
||||
SrsBuffer b;
|
||||
MockBufferReader r("winlin");
|
||||
|
||||
b.grow(&r, 1);
|
||||
EXPECT_EQ(6, b.length());
|
||||
EXPECT_EQ('w', b.bytes()[0]);
|
||||
|
||||
b.grow(&r, 3);
|
||||
EXPECT_EQ(6, b.length());
|
||||
EXPECT_EQ('n', b.bytes()[2]);
|
||||
|
||||
b.grow(&r, 100);
|
||||
EXPECT_EQ(102, b.length());
|
||||
EXPECT_EQ('l', b.bytes()[99]);
|
||||
}
|
46
trunk/src/utest/srs_utest_buffer.hpp
Normal file
46
trunk/src/utest/srs_utest_buffer.hpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRS_UTEST_BUFFER_HPP
|
||||
#define SRS_UTEST_BUFFER_HPP
|
||||
|
||||
/*
|
||||
#include <srs_utest_buffer.hpp>
|
||||
*/
|
||||
#include <srs_utest.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <srs_kernel_buffer.hpp>
|
||||
|
||||
class MockBufferReader: public ISrsBufferReader
|
||||
{
|
||||
private:
|
||||
std::string str;
|
||||
public:
|
||||
MockBufferReader(const char* data);
|
||||
virtual ~MockBufferReader();
|
||||
public:
|
||||
virtual int read(void* buf, size_t size, ssize_t* nread);
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue