mirror of
https://github.com/ossrs/srs.git
synced 2025-02-13 03:41:55 +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
2
trunk/configure
vendored
2
trunk/configure
vendored
|
@ -516,7 +516,7 @@ if [ $SRS_LIBRTMP = YES ]; then
|
||||||
fi
|
fi
|
||||||
#
|
#
|
||||||
# utest, the unit-test cases of srs, base on gtest1.6
|
# utest, the unit-test cases of srs, base on gtest1.6
|
||||||
MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_handshake")
|
MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_handshake" "srs_utest_buffer")
|
||||||
ModuleLibIncs=(${SRS_OBJS} ${LibSTRoot})
|
ModuleLibIncs=(${SRS_OBJS} ${LibSTRoot})
|
||||||
ModuleLibFiles=(${LibSTfile} ${LibHttpParserfile} ${LibSSLfile})
|
ModuleLibFiles=(${LibSTfile} ${LibHttpParserfile} ${LibSSLfile})
|
||||||
MODULE_DEPENDS=("CORE" "KERNEL" "RTMP" "APP")
|
MODULE_DEPENDS=("CORE" "KERNEL" "RTMP" "APP")
|
||||||
|
|
|
@ -328,7 +328,7 @@ SrsHttpHandler* SrsHttpHandler::res_body(stringstream& ss, string body)
|
||||||
|
|
||||||
int SrsHttpHandler::res_flush(SrsSocket* skt, stringstream& ss)
|
int SrsHttpHandler::res_flush(SrsSocket* skt, stringstream& ss)
|
||||||
{
|
{
|
||||||
return skt->write(ss.str().c_str(), ss.str().length(), NULL);
|
return skt->write((void*)ss.str().c_str(), ss.str().length(), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsHttpHandler::res_options(SrsSocket* skt)
|
int SrsHttpHandler::res_options(SrsSocket* skt)
|
||||||
|
|
|
@ -83,7 +83,7 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res)
|
||||||
SrsSocket skt(stfd);
|
SrsSocket skt(stfd);
|
||||||
|
|
||||||
std::string data = ss.str();
|
std::string data = ss.str();
|
||||||
if ((ret = skt.write(data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
|
if ((ret = skt.write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
|
||||||
// disconnect when error.
|
// disconnect when error.
|
||||||
disconnect();
|
disconnect();
|
||||||
|
|
||||||
|
|
|
@ -72,11 +72,11 @@ int64_t SrsSocket::get_send_bytes()
|
||||||
return send_bytes;
|
return send_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsSocket::read(const void* buf, size_t size, ssize_t* nread)
|
int SrsSocket::read(void* buf, size_t size, ssize_t* nread)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
ssize_t nb_read = st_read(stfd, (void*)buf, size, recv_timeout);
|
ssize_t nb_read = st_read(stfd, buf, size, recv_timeout);
|
||||||
if (nread) {
|
if (nread) {
|
||||||
*nread = nb_read;
|
*nread = nb_read;
|
||||||
}
|
}
|
||||||
|
@ -100,11 +100,11 @@ int SrsSocket::read(const void* buf, size_t size, ssize_t* nread)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsSocket::read_fully(const void* buf, size_t size, ssize_t* nread)
|
int SrsSocket::read_fully(void* buf, size_t size, ssize_t* nread)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
ssize_t nb_read = st_read_fully(stfd, (void*)buf, size, recv_timeout);
|
ssize_t nb_read = st_read_fully(stfd, buf, size, recv_timeout);
|
||||||
if (nread) {
|
if (nread) {
|
||||||
*nread = nb_read;
|
*nread = nb_read;
|
||||||
}
|
}
|
||||||
|
@ -128,11 +128,11 @@ int SrsSocket::read_fully(const void* buf, size_t size, ssize_t* nread)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsSocket::write(const void* buf, size_t size, ssize_t* nwrite)
|
int SrsSocket::write(void* buf, size_t size, ssize_t* nwrite)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
ssize_t nb_write = st_write(stfd, (void*)buf, size, send_timeout);
|
ssize_t nb_write = st_write(stfd, buf, size, send_timeout);
|
||||||
if (nwrite) {
|
if (nwrite) {
|
||||||
*nwrite = nb_write;
|
*nwrite = nb_write;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,12 +60,12 @@ public:
|
||||||
/**
|
/**
|
||||||
* @param nread, the actual read bytes, ignore if NULL.
|
* @param nread, the actual read bytes, ignore if NULL.
|
||||||
*/
|
*/
|
||||||
virtual int read(const void* buf, size_t size, ssize_t* nread);
|
virtual int read(void* buf, size_t size, ssize_t* nread);
|
||||||
virtual int read_fully(const void* buf, size_t size, ssize_t* nread);
|
virtual int read_fully(void* buf, size_t size, ssize_t* nread);
|
||||||
/**
|
/**
|
||||||
* @param nwrite, the actual write bytes, ignore if NULL.
|
* @param nwrite, the actual write bytes, ignore if NULL.
|
||||||
*/
|
*/
|
||||||
virtual int write(const void* buf, size_t size, ssize_t* nwrite);
|
virtual int write(void* buf, size_t size, ssize_t* nwrite);
|
||||||
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite);
|
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ void SrsBuffer::erase(int size)
|
||||||
{
|
{
|
||||||
srs_assert(size > 0);
|
srs_assert(size > 0);
|
||||||
|
|
||||||
if (size == length()) {
|
if (size >= length()) {
|
||||||
data.clear();
|
data.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,15 +42,14 @@ public:
|
||||||
virtual ~ISrsBufferReader();
|
virtual ~ISrsBufferReader();
|
||||||
// for protocol/amf0/msg-codec
|
// for protocol/amf0/msg-codec
|
||||||
public:
|
public:
|
||||||
virtual int read(const void* buf, size_t size, ssize_t* nread) = 0;
|
virtual int read(void* buf, size_t size, ssize_t* nread) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the buffer provices bytes cache for protocol. generally,
|
* the buffer provices bytes cache for protocol. generally,
|
||||||
* protocol recv data from socket, put into buffer, decode to RTMP message.
|
* protocol recv data from socket, put into buffer, decode to RTMP message.
|
||||||
* protocol encode RTMP message to bytes, put into buffer, send to socket.
|
|
||||||
*/
|
*/
|
||||||
class SrsBuffer
|
class SrsBuffer
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::vector<char> data;
|
std::vector<char> data;
|
||||||
|
@ -59,8 +58,8 @@ public:
|
||||||
virtual ~SrsBuffer();
|
virtual ~SrsBuffer();
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* get the length of buffer.
|
* get the length of buffer. empty if zero.
|
||||||
* never negative, empty if zero.
|
* @remark assert length() is not negative.
|
||||||
*/
|
*/
|
||||||
virtual int length();
|
virtual int length();
|
||||||
/**
|
/**
|
||||||
|
@ -70,13 +69,15 @@ public:
|
||||||
virtual char* bytes();
|
virtual char* bytes();
|
||||||
/**
|
/**
|
||||||
* erase size of bytes from begin.
|
* erase size of bytes from begin.
|
||||||
* if size equals to length(), clear buffer.
|
* @param size to erase size of bytes.
|
||||||
* @param size
|
* clear if size greater than or equals to length()
|
||||||
|
* @remark assert size is positive.
|
||||||
*/
|
*/
|
||||||
virtual void erase(int size);
|
virtual void erase(int size);
|
||||||
/**
|
/**
|
||||||
* append specified bytes to buffer.
|
* append specified bytes to buffer.
|
||||||
* @param size the size of bytes, assert positive.
|
* @param size the size of bytes
|
||||||
|
* @remark assert size is positive.
|
||||||
*/
|
*/
|
||||||
virtual void append(const char* bytes, int size);
|
virtual void append(const char* bytes, int size);
|
||||||
public:
|
public:
|
||||||
|
@ -84,6 +85,8 @@ public:
|
||||||
* grow buffer to the required size, loop to read from skt to fill.
|
* grow buffer to the required size, loop to read from skt to fill.
|
||||||
* @param reader, read more bytes from reader to fill the buffer to required size.
|
* @param reader, read more bytes from reader to fill the buffer to required size.
|
||||||
* @param required_size, loop to fill to ensure buffer size to required.
|
* @param required_size, loop to fill to ensure buffer size to required.
|
||||||
|
* @return an int error code, error if required_size negative.
|
||||||
|
* @remark, we actually maybe read more than required_size, maybe 4k for example.
|
||||||
*/
|
*/
|
||||||
virtual int grow(ISrsBufferReader* reader, int required_size);
|
virtual int grow(ISrsBufferReader* reader, int required_size);
|
||||||
};
|
};
|
||||||
|
|
|
@ -78,11 +78,11 @@ int SimpleSocketStream::connect(const char* server_ip, int port)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ISrsBufferReader
|
// ISrsBufferReader
|
||||||
int SimpleSocketStream::read(const void* buf, size_t size, ssize_t* nread)
|
int SimpleSocketStream::read(void* buf, size_t size, ssize_t* nread)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
*nread = ::recv(fd, (void*)buf, size, 0);
|
*nread = ::recv(fd, buf, size, 0);
|
||||||
|
|
||||||
// On success a non-negative integer indicating the number of bytes actually read is returned
|
// On success a non-negative integer indicating the number of bytes actually read is returned
|
||||||
// (a value of 0 means the network connection is closed or end of file is reached).
|
// (a value of 0 means the network connection is closed or end of file is reached).
|
||||||
|
@ -160,7 +160,7 @@ bool SimpleSocketStream::is_never_timeout(int64_t timeout_us)
|
||||||
return timeout_us == (int64_t)ST_UTIME_NO_TIMEOUT;
|
return timeout_us == (int64_t)ST_UTIME_NO_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SimpleSocketStream::read_fully(const void* buf, size_t size, ssize_t* nread)
|
int SimpleSocketStream::read_fully(void* buf, size_t size, ssize_t* nread)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
@ -184,7 +184,7 @@ int SimpleSocketStream::read_fully(const void* buf, size_t size, ssize_t* nread)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SimpleSocketStream::write(const void* buf, size_t size, ssize_t* nwrite)
|
int SimpleSocketStream::write(void* buf, size_t size, ssize_t* nwrite)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ public:
|
||||||
virtual int connect(const char* server, int port);
|
virtual int connect(const char* server, int port);
|
||||||
// ISrsBufferReader
|
// ISrsBufferReader
|
||||||
public:
|
public:
|
||||||
virtual int read(const void* buf, size_t size, ssize_t* nread);
|
virtual int read(void* buf, size_t size, ssize_t* nread);
|
||||||
// ISrsProtocolReader
|
// ISrsProtocolReader
|
||||||
public:
|
public:
|
||||||
virtual void set_recv_timeout(int64_t timeout_us);
|
virtual void set_recv_timeout(int64_t timeout_us);
|
||||||
|
@ -67,8 +67,8 @@ public:
|
||||||
// ISrsProtocolReaderWriter
|
// ISrsProtocolReaderWriter
|
||||||
public:
|
public:
|
||||||
virtual bool is_never_timeout(int64_t timeout_us);
|
virtual bool is_never_timeout(int64_t timeout_us);
|
||||||
virtual int read_fully(const void* buf, size_t size, ssize_t* nread);
|
virtual int read_fully(void* buf, size_t size, ssize_t* nread);
|
||||||
virtual int write(const void* buf, size_t size, ssize_t* nwrite);
|
virtual int write(void* buf, size_t size, ssize_t* nwrite);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -78,8 +78,8 @@ public:
|
||||||
virtual bool is_never_timeout(int64_t timeout_us) = 0;
|
virtual bool is_never_timeout(int64_t timeout_us) = 0;
|
||||||
// for handshake.
|
// for handshake.
|
||||||
public:
|
public:
|
||||||
virtual int read_fully(const void* buf, size_t size, ssize_t* nread) = 0;
|
virtual int read_fully(void* buf, size_t size, ssize_t* nread) = 0;
|
||||||
virtual int write(const void* buf, size_t size, ssize_t* nwrite) = 0;
|
virtual int write(void* buf, size_t size, ssize_t* nwrite) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -108,6 +108,8 @@ file
|
||||||
..\utest\srs_utest.cpp,
|
..\utest\srs_utest.cpp,
|
||||||
..\utest\srs_utest_amf0.hpp,
|
..\utest\srs_utest_amf0.hpp,
|
||||||
..\utest\srs_utest_amf0.cpp,
|
..\utest\srs_utest_amf0.cpp,
|
||||||
|
..\utest\srs_utest_buffer.hpp,
|
||||||
|
..\utest\srs_utest_buffer.cpp,
|
||||||
..\utest\srs_utest_handshake.hpp,
|
..\utest\srs_utest_handshake.hpp,
|
||||||
..\utest\srs_utest_handshake.cpp,
|
..\utest\srs_utest_handshake.cpp,
|
||||||
research readonly separator,
|
research readonly separator,
|
||||||
|
|
|
@ -49,12 +49,12 @@ bool MockEmptyIO::is_never_timeout(int64_t /*timeout_us*/)
|
||||||
return true;
|
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;
|
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;
|
return ERROR_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ int MockEmptyIO::writev(const iovec */*iov*/, int /*iov_size*/, ssize_t* /*nwrit
|
||||||
return ERROR_SUCCESS;
|
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;
|
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.
|
// we add an empty macro for upp to show the smart tips.
|
||||||
#define VOID
|
#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>
|
#include <srs_protocol_io.hpp>
|
||||||
|
|
||||||
class MockEmptyIO : public ISrsProtocolReaderWriter
|
class MockEmptyIO : public ISrsProtocolReaderWriter
|
||||||
|
@ -46,8 +61,8 @@ public:
|
||||||
virtual bool is_never_timeout(int64_t timeout_us);
|
virtual bool is_never_timeout(int64_t timeout_us);
|
||||||
// for handshake.
|
// for handshake.
|
||||||
public:
|
public:
|
||||||
virtual int read_fully(const void* buf, size_t size, ssize_t* nread);
|
virtual int read_fully(void* buf, size_t size, ssize_t* nread);
|
||||||
virtual int write(const void* buf, size_t size, ssize_t* nwrite);
|
virtual int write(void* buf, size_t size, ssize_t* nwrite);
|
||||||
// for protocol
|
// for protocol
|
||||||
public:
|
public:
|
||||||
virtual void set_recv_timeout(int64_t timeout_us);
|
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);
|
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite);
|
||||||
// for protocol/amf0/msg-codec
|
// for protocol/amf0/msg-codec
|
||||||
public:
|
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
|
#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…
Reference in a new issue