正在显示
15 个修改的文件
包含
218 行增加
和
37 行删除
| @@ -516,7 +516,7 @@ if [ $SRS_LIBRTMP = YES ]; then | @@ -516,7 +516,7 @@ if [ $SRS_LIBRTMP = YES ]; then | ||
| 516 | fi | 516 | fi |
| 517 | # | 517 | # |
| 518 | # utest, the unit-test cases of srs, base on gtest1.6 | 518 | # utest, the unit-test cases of srs, base on gtest1.6 |
| 519 | -MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_handshake") | 519 | +MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_handshake" "srs_utest_buffer") |
| 520 | ModuleLibIncs=(${SRS_OBJS} ${LibSTRoot}) | 520 | ModuleLibIncs=(${SRS_OBJS} ${LibSTRoot}) |
| 521 | ModuleLibFiles=(${LibSTfile} ${LibHttpParserfile} ${LibSSLfile}) | 521 | ModuleLibFiles=(${LibSTfile} ${LibHttpParserfile} ${LibSSLfile}) |
| 522 | MODULE_DEPENDS=("CORE" "KERNEL" "RTMP" "APP") | 522 | MODULE_DEPENDS=("CORE" "KERNEL" "RTMP" "APP") |
| @@ -328,7 +328,7 @@ SrsHttpHandler* SrsHttpHandler::res_body(stringstream& ss, string body) | @@ -328,7 +328,7 @@ SrsHttpHandler* SrsHttpHandler::res_body(stringstream& ss, string body) | ||
| 328 | 328 | ||
| 329 | int SrsHttpHandler::res_flush(SrsSocket* skt, stringstream& ss) | 329 | int SrsHttpHandler::res_flush(SrsSocket* skt, stringstream& ss) |
| 330 | { | 330 | { |
| 331 | - return skt->write(ss.str().c_str(), ss.str().length(), NULL); | 331 | + return skt->write((void*)ss.str().c_str(), ss.str().length(), NULL); |
| 332 | } | 332 | } |
| 333 | 333 | ||
| 334 | int SrsHttpHandler::res_options(SrsSocket* skt) | 334 | int SrsHttpHandler::res_options(SrsSocket* skt) |
| @@ -83,7 +83,7 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res) | @@ -83,7 +83,7 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res) | ||
| 83 | SrsSocket skt(stfd); | 83 | SrsSocket skt(stfd); |
| 84 | 84 | ||
| 85 | std::string data = ss.str(); | 85 | std::string data = ss.str(); |
| 86 | - if ((ret = skt.write(data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) { | 86 | + if ((ret = skt.write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) { |
| 87 | // disconnect when error. | 87 | // disconnect when error. |
| 88 | disconnect(); | 88 | disconnect(); |
| 89 | 89 |
| @@ -72,11 +72,11 @@ int64_t SrsSocket::get_send_bytes() | @@ -72,11 +72,11 @@ int64_t SrsSocket::get_send_bytes() | ||
| 72 | return send_bytes; | 72 | return send_bytes; |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | -int SrsSocket::read(const void* buf, size_t size, ssize_t* nread) | 75 | +int SrsSocket::read(void* buf, size_t size, ssize_t* nread) |
| 76 | { | 76 | { |
| 77 | int ret = ERROR_SUCCESS; | 77 | int ret = ERROR_SUCCESS; |
| 78 | 78 | ||
| 79 | - ssize_t nb_read = st_read(stfd, (void*)buf, size, recv_timeout); | 79 | + ssize_t nb_read = st_read(stfd, buf, size, recv_timeout); |
| 80 | if (nread) { | 80 | if (nread) { |
| 81 | *nread = nb_read; | 81 | *nread = nb_read; |
| 82 | } | 82 | } |
| @@ -100,11 +100,11 @@ int SrsSocket::read(const void* buf, size_t size, ssize_t* nread) | @@ -100,11 +100,11 @@ int SrsSocket::read(const void* buf, size_t size, ssize_t* nread) | ||
| 100 | return ret; | 100 | return ret; |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | -int SrsSocket::read_fully(const void* buf, size_t size, ssize_t* nread) | 103 | +int SrsSocket::read_fully(void* buf, size_t size, ssize_t* nread) |
| 104 | { | 104 | { |
| 105 | int ret = ERROR_SUCCESS; | 105 | int ret = ERROR_SUCCESS; |
| 106 | 106 | ||
| 107 | - ssize_t nb_read = st_read_fully(stfd, (void*)buf, size, recv_timeout); | 107 | + ssize_t nb_read = st_read_fully(stfd, buf, size, recv_timeout); |
| 108 | if (nread) { | 108 | if (nread) { |
| 109 | *nread = nb_read; | 109 | *nread = nb_read; |
| 110 | } | 110 | } |
| @@ -128,11 +128,11 @@ int SrsSocket::read_fully(const void* buf, size_t size, ssize_t* nread) | @@ -128,11 +128,11 @@ int SrsSocket::read_fully(const void* buf, size_t size, ssize_t* nread) | ||
| 128 | return ret; | 128 | return ret; |
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | -int SrsSocket::write(const void* buf, size_t size, ssize_t* nwrite) | 131 | +int SrsSocket::write(void* buf, size_t size, ssize_t* nwrite) |
| 132 | { | 132 | { |
| 133 | int ret = ERROR_SUCCESS; | 133 | int ret = ERROR_SUCCESS; |
| 134 | 134 | ||
| 135 | - ssize_t nb_write = st_write(stfd, (void*)buf, size, send_timeout); | 135 | + ssize_t nb_write = st_write(stfd, buf, size, send_timeout); |
| 136 | if (nwrite) { | 136 | if (nwrite) { |
| 137 | *nwrite = nb_write; | 137 | *nwrite = nb_write; |
| 138 | } | 138 | } |
| @@ -60,12 +60,12 @@ public: | @@ -60,12 +60,12 @@ public: | ||
| 60 | /** | 60 | /** |
| 61 | * @param nread, the actual read bytes, ignore if NULL. | 61 | * @param nread, the actual read bytes, ignore if NULL. |
| 62 | */ | 62 | */ |
| 63 | - virtual int read(const void* buf, size_t size, ssize_t* nread); | ||
| 64 | - virtual int read_fully(const void* buf, size_t size, ssize_t* nread); | 63 | + virtual int read(void* buf, size_t size, ssize_t* nread); |
| 64 | + virtual int read_fully(void* buf, size_t size, ssize_t* nread); | ||
| 65 | /** | 65 | /** |
| 66 | * @param nwrite, the actual write bytes, ignore if NULL. | 66 | * @param nwrite, the actual write bytes, ignore if NULL. |
| 67 | */ | 67 | */ |
| 68 | - virtual int write(const void* buf, size_t size, ssize_t* nwrite); | 68 | + virtual int write(void* buf, size_t size, ssize_t* nwrite); |
| 69 | virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite); | 69 | virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite); |
| 70 | }; | 70 | }; |
| 71 | 71 |
| @@ -42,15 +42,14 @@ public: | @@ -42,15 +42,14 @@ public: | ||
| 42 | virtual ~ISrsBufferReader(); | 42 | virtual ~ISrsBufferReader(); |
| 43 | // for protocol/amf0/msg-codec | 43 | // for protocol/amf0/msg-codec |
| 44 | public: | 44 | public: |
| 45 | - virtual int read(const void* buf, size_t size, ssize_t* nread) = 0; | 45 | + virtual int read(void* buf, size_t size, ssize_t* nread) = 0; |
| 46 | }; | 46 | }; |
| 47 | 47 | ||
| 48 | /** | 48 | /** |
| 49 | * the buffer provices bytes cache for protocol. generally, | 49 | * the buffer provices bytes cache for protocol. generally, |
| 50 | * protocol recv data from socket, put into buffer, decode to RTMP message. | 50 | * protocol recv data from socket, put into buffer, decode to RTMP message. |
| 51 | -* protocol encode RTMP message to bytes, put into buffer, send to socket. | ||
| 52 | */ | 51 | */ |
| 53 | - class SrsBuffer | 52 | +class SrsBuffer |
| 54 | { | 53 | { |
| 55 | private: | 54 | private: |
| 56 | std::vector<char> data; | 55 | std::vector<char> data; |
| @@ -59,8 +58,8 @@ public: | @@ -59,8 +58,8 @@ public: | ||
| 59 | virtual ~SrsBuffer(); | 58 | virtual ~SrsBuffer(); |
| 60 | public: | 59 | public: |
| 61 | /** | 60 | /** |
| 62 | - * get the length of buffer. | ||
| 63 | - * never negative, empty if zero. | 61 | + * get the length of buffer. empty if zero. |
| 62 | + * @remark assert length() is not negative. | ||
| 64 | */ | 63 | */ |
| 65 | virtual int length(); | 64 | virtual int length(); |
| 66 | /** | 65 | /** |
| @@ -70,20 +69,24 @@ public: | @@ -70,20 +69,24 @@ public: | ||
| 70 | virtual char* bytes(); | 69 | virtual char* bytes(); |
| 71 | /** | 70 | /** |
| 72 | * erase size of bytes from begin. | 71 | * erase size of bytes from begin. |
| 73 | - * if size equals to length(), clear buffer. | ||
| 74 | - * @param size | 72 | + * @param size to erase size of bytes. |
| 73 | + * clear if size greater than or equals to length() | ||
| 74 | + * @remark assert size is positive. | ||
| 75 | */ | 75 | */ |
| 76 | virtual void erase(int size); | 76 | virtual void erase(int size); |
| 77 | /** | 77 | /** |
| 78 | * append specified bytes to buffer. | 78 | * append specified bytes to buffer. |
| 79 | - * @param size the size of bytes, assert positive. | 79 | + * @param size the size of bytes |
| 80 | + * @remark assert size is positive. | ||
| 80 | */ | 81 | */ |
| 81 | virtual void append(const char* bytes, int size); | 82 | virtual void append(const char* bytes, int size); |
| 82 | public: | 83 | public: |
| 83 | /** | 84 | /** |
| 84 | * grow buffer to the required size, loop to read from skt to fill. | 85 | * grow buffer to the required size, loop to read from skt to fill. |
| 85 | * @param reader, read more bytes from reader to fill the buffer to required size. | 86 | * @param reader, read more bytes from reader to fill the buffer to required size. |
| 86 | - * @param required_size, loop to fill to ensure buffer size to required. | 87 | + * @param required_size, loop to fill to ensure buffer size to required. |
| 88 | + * @return an int error code, error if required_size negative. | ||
| 89 | + * @remark, we actually maybe read more than required_size, maybe 4k for example. | ||
| 87 | */ | 90 | */ |
| 88 | virtual int grow(ISrsBufferReader* reader, int required_size); | 91 | virtual int grow(ISrsBufferReader* reader, int required_size); |
| 89 | }; | 92 | }; |
| @@ -78,11 +78,11 @@ int SimpleSocketStream::connect(const char* server_ip, int port) | @@ -78,11 +78,11 @@ int SimpleSocketStream::connect(const char* server_ip, int port) | ||
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | // ISrsBufferReader | 80 | // ISrsBufferReader |
| 81 | -int SimpleSocketStream::read(const void* buf, size_t size, ssize_t* nread) | 81 | +int SimpleSocketStream::read(void* buf, size_t size, ssize_t* nread) |
| 82 | { | 82 | { |
| 83 | int ret = ERROR_SUCCESS; | 83 | int ret = ERROR_SUCCESS; |
| 84 | 84 | ||
| 85 | - *nread = ::recv(fd, (void*)buf, size, 0); | 85 | + *nread = ::recv(fd, buf, size, 0); |
| 86 | 86 | ||
| 87 | // On success a non-negative integer indicating the number of bytes actually read is returned | 87 | // On success a non-negative integer indicating the number of bytes actually read is returned |
| 88 | // (a value of 0 means the network connection is closed or end of file is reached). | 88 | // (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) | @@ -160,7 +160,7 @@ bool SimpleSocketStream::is_never_timeout(int64_t timeout_us) | ||
| 160 | return timeout_us == (int64_t)ST_UTIME_NO_TIMEOUT; | 160 | return timeout_us == (int64_t)ST_UTIME_NO_TIMEOUT; |
| 161 | } | 161 | } |
| 162 | 162 | ||
| 163 | -int SimpleSocketStream::read_fully(const void* buf, size_t size, ssize_t* nread) | 163 | +int SimpleSocketStream::read_fully(void* buf, size_t size, ssize_t* nread) |
| 164 | { | 164 | { |
| 165 | int ret = ERROR_SUCCESS; | 165 | int ret = ERROR_SUCCESS; |
| 166 | 166 | ||
| @@ -184,7 +184,7 @@ int SimpleSocketStream::read_fully(const void* buf, size_t size, ssize_t* nread) | @@ -184,7 +184,7 @@ int SimpleSocketStream::read_fully(const void* buf, size_t size, ssize_t* nread) | ||
| 184 | return ret; | 184 | return ret; |
| 185 | } | 185 | } |
| 186 | 186 | ||
| 187 | -int SimpleSocketStream::write(const void* buf, size_t size, ssize_t* nwrite) | 187 | +int SimpleSocketStream::write(void* buf, size_t size, ssize_t* nwrite) |
| 188 | { | 188 | { |
| 189 | int ret = ERROR_SUCCESS; | 189 | int ret = ERROR_SUCCESS; |
| 190 | 190 |
| @@ -52,7 +52,7 @@ public: | @@ -52,7 +52,7 @@ public: | ||
| 52 | virtual int connect(const char* server, int port); | 52 | virtual int connect(const char* server, int port); |
| 53 | // ISrsBufferReader | 53 | // ISrsBufferReader |
| 54 | public: | 54 | public: |
| 55 | - virtual int read(const void* buf, size_t size, ssize_t* nread); | 55 | + virtual int read(void* buf, size_t size, ssize_t* nread); |
| 56 | // ISrsProtocolReader | 56 | // ISrsProtocolReader |
| 57 | public: | 57 | public: |
| 58 | virtual void set_recv_timeout(int64_t timeout_us); | 58 | virtual void set_recv_timeout(int64_t timeout_us); |
| @@ -67,8 +67,8 @@ public: | @@ -67,8 +67,8 @@ public: | ||
| 67 | // ISrsProtocolReaderWriter | 67 | // ISrsProtocolReaderWriter |
| 68 | public: | 68 | public: |
| 69 | virtual bool is_never_timeout(int64_t timeout_us); | 69 | virtual bool is_never_timeout(int64_t timeout_us); |
| 70 | - virtual int read_fully(const void* buf, size_t size, ssize_t* nread); | ||
| 71 | - virtual int write(const void* buf, size_t size, ssize_t* nwrite); | 70 | + virtual int read_fully(void* buf, size_t size, ssize_t* nread); |
| 71 | + virtual int write(void* buf, size_t size, ssize_t* nwrite); | ||
| 72 | }; | 72 | }; |
| 73 | 73 | ||
| 74 | #endif | 74 | #endif |
| @@ -78,8 +78,8 @@ public: | @@ -78,8 +78,8 @@ public: | ||
| 78 | virtual bool is_never_timeout(int64_t timeout_us) = 0; | 78 | virtual bool is_never_timeout(int64_t timeout_us) = 0; |
| 79 | // for handshake. | 79 | // for handshake. |
| 80 | public: | 80 | public: |
| 81 | - virtual int read_fully(const void* buf, size_t size, ssize_t* nread) = 0; | ||
| 82 | - virtual int write(const void* buf, size_t size, ssize_t* nwrite) = 0; | 81 | + virtual int read_fully(void* buf, size_t size, ssize_t* nread) = 0; |
| 82 | + virtual int write(void* buf, size_t size, ssize_t* nwrite) = 0; | ||
| 83 | }; | 83 | }; |
| 84 | 84 | ||
| 85 | #endif | 85 | #endif |
| @@ -108,6 +108,8 @@ file | @@ -108,6 +108,8 @@ file | ||
| 108 | ..\utest\srs_utest.cpp, | 108 | ..\utest\srs_utest.cpp, |
| 109 | ..\utest\srs_utest_amf0.hpp, | 109 | ..\utest\srs_utest_amf0.hpp, |
| 110 | ..\utest\srs_utest_amf0.cpp, | 110 | ..\utest\srs_utest_amf0.cpp, |
| 111 | + ..\utest\srs_utest_buffer.hpp, | ||
| 112 | + ..\utest\srs_utest_buffer.cpp, | ||
| 111 | ..\utest\srs_utest_handshake.hpp, | 113 | ..\utest\srs_utest_handshake.hpp, |
| 112 | ..\utest\srs_utest_handshake.cpp, | 114 | ..\utest\srs_utest_handshake.cpp, |
| 113 | research readonly separator, | 115 | research readonly separator, |
| @@ -49,12 +49,12 @@ bool MockEmptyIO::is_never_timeout(int64_t /*timeout_us*/) | @@ -49,12 +49,12 @@ bool MockEmptyIO::is_never_timeout(int64_t /*timeout_us*/) | ||
| 49 | return true; | 49 | return true; |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | -int MockEmptyIO::read_fully(const void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/) | 52 | +int MockEmptyIO::read_fully(void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/) |
| 53 | { | 53 | { |
| 54 | return ERROR_SUCCESS; | 54 | return ERROR_SUCCESS; |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | -int MockEmptyIO::write(const void* /*buf*/, size_t /*size*/, ssize_t* /*nwrite*/) | 57 | +int MockEmptyIO::write(void* /*buf*/, size_t /*size*/, ssize_t* /*nwrite*/) |
| 58 | { | 58 | { |
| 59 | return ERROR_SUCCESS; | 59 | return ERROR_SUCCESS; |
| 60 | } | 60 | } |
| @@ -92,7 +92,7 @@ int MockEmptyIO::writev(const iovec */*iov*/, int /*iov_size*/, ssize_t* /*nwrit | @@ -92,7 +92,7 @@ int MockEmptyIO::writev(const iovec */*iov*/, int /*iov_size*/, ssize_t* /*nwrit | ||
| 92 | return ERROR_SUCCESS; | 92 | return ERROR_SUCCESS; |
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | -int MockEmptyIO::read(const void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/) | 95 | +int MockEmptyIO::read(void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/) |
| 96 | { | 96 | { |
| 97 | return ERROR_SUCCESS; | 97 | return ERROR_SUCCESS; |
| 98 | } | 98 | } |
| @@ -34,6 +34,21 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -34,6 +34,21 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 34 | // we add an empty macro for upp to show the smart tips. | 34 | // we add an empty macro for upp to show the smart tips. |
| 35 | #define VOID | 35 | #define VOID |
| 36 | 36 | ||
| 37 | +// the asserts of gtest: | ||
| 38 | +// * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual | ||
| 39 | +// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 | ||
| 40 | +// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 | ||
| 41 | +// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 | ||
| 42 | +// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 | ||
| 43 | +// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 | ||
| 44 | +// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 | ||
| 45 | +// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 | ||
| 46 | +// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case | ||
| 47 | +// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case | ||
| 48 | +// * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual): Tests that two float values are almost equal. | ||
| 49 | +// * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual): Tests that two double values are almost equal. | ||
| 50 | +// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): Tests that v1 and v2 are within the given distance to each other. | ||
| 51 | + | ||
| 37 | #include <srs_protocol_io.hpp> | 52 | #include <srs_protocol_io.hpp> |
| 38 | 53 | ||
| 39 | class MockEmptyIO : public ISrsProtocolReaderWriter | 54 | class MockEmptyIO : public ISrsProtocolReaderWriter |
| @@ -46,8 +61,8 @@ public: | @@ -46,8 +61,8 @@ public: | ||
| 46 | virtual bool is_never_timeout(int64_t timeout_us); | 61 | virtual bool is_never_timeout(int64_t timeout_us); |
| 47 | // for handshake. | 62 | // for handshake. |
| 48 | public: | 63 | public: |
| 49 | - virtual int read_fully(const void* buf, size_t size, ssize_t* nread); | ||
| 50 | - virtual int write(const void* buf, size_t size, ssize_t* nwrite); | 64 | + virtual int read_fully(void* buf, size_t size, ssize_t* nread); |
| 65 | + virtual int write(void* buf, size_t size, ssize_t* nwrite); | ||
| 51 | // for protocol | 66 | // for protocol |
| 52 | public: | 67 | public: |
| 53 | virtual void set_recv_timeout(int64_t timeout_us); | 68 | virtual void set_recv_timeout(int64_t timeout_us); |
| @@ -61,7 +76,7 @@ public: | @@ -61,7 +76,7 @@ public: | ||
| 61 | virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite); | 76 | virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite); |
| 62 | // for protocol/amf0/msg-codec | 77 | // for protocol/amf0/msg-codec |
| 63 | public: | 78 | public: |
| 64 | - virtual int read(const void* buf, size_t size, ssize_t* nread); | 79 | + virtual int read(void* buf, size_t size, ssize_t* nread); |
| 65 | }; | 80 | }; |
| 66 | 81 | ||
| 67 | #endif | 82 | #endif |
trunk/src/utest/srs_utest_buffer.cpp
0 → 100644
| 1 | +/* | ||
| 2 | +The MIT License (MIT) | ||
| 3 | + | ||
| 4 | +Copyright (c) 2013-2014 winlin | ||
| 5 | + | ||
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| 7 | +this software and associated documentation files (the "Software"), to deal in | ||
| 8 | +the Software without restriction, including without limitation the rights to | ||
| 9 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| 10 | +the Software, and to permit persons to whom the Software is furnished to do so, | ||
| 11 | +subject to the following conditions: | ||
| 12 | + | ||
| 13 | +The above copyright notice and this permission notice shall be included in all | ||
| 14 | +copies or substantial portions of the Software. | ||
| 15 | + | ||
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| 18 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| 19 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| 20 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 21 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 22 | +*/ | ||
| 23 | +#include <srs_utest_buffer.hpp> | ||
| 24 | + | ||
| 25 | +#include <srs_kernel_error.hpp> | ||
| 26 | + | ||
| 27 | +MockBufferReader::MockBufferReader(const char* data) | ||
| 28 | +{ | ||
| 29 | + str = data; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +MockBufferReader::~MockBufferReader() | ||
| 33 | +{ | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +int MockBufferReader::read(void* buf, size_t size, ssize_t* nread) | ||
| 37 | +{ | ||
| 38 | + int len = srs_min(str.length(), size); | ||
| 39 | + | ||
| 40 | + memcpy(buf, str.data(), len); | ||
| 41 | + | ||
| 42 | + if (nread) { | ||
| 43 | + *nread = len; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + return ERROR_SUCCESS; | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +VOID TEST(BufferTest, DefaultObject) | ||
| 50 | +{ | ||
| 51 | + SrsBuffer b; | ||
| 52 | + | ||
| 53 | + EXPECT_EQ(0, b.length()); | ||
| 54 | + EXPECT_EQ(NULL, b.bytes()); | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +VOID TEST(BufferTest, AppendBytes) | ||
| 58 | +{ | ||
| 59 | + SrsBuffer b; | ||
| 60 | + | ||
| 61 | + char winlin[] = "winlin"; | ||
| 62 | + b.append(winlin, strlen(winlin)); | ||
| 63 | + EXPECT_EQ((int)strlen(winlin), b.length()); | ||
| 64 | + ASSERT_TRUE(NULL != b.bytes()); | ||
| 65 | + EXPECT_EQ('w', b.bytes()[0]); | ||
| 66 | + EXPECT_EQ('n', b.bytes()[5]); | ||
| 67 | + | ||
| 68 | + b.append(winlin, strlen(winlin)); | ||
| 69 | + EXPECT_EQ(2 * (int)strlen(winlin), b.length()); | ||
| 70 | + ASSERT_TRUE(NULL != b.bytes()); | ||
| 71 | + EXPECT_EQ('w', b.bytes()[0]); | ||
| 72 | + EXPECT_EQ('n', b.bytes()[5]); | ||
| 73 | + EXPECT_EQ('w', b.bytes()[6]); | ||
| 74 | + EXPECT_EQ('n', b.bytes()[11]); | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +VOID TEST(BufferTest, EraseBytes) | ||
| 78 | +{ | ||
| 79 | + SrsBuffer b; | ||
| 80 | + | ||
| 81 | + char winlin[] = "winlin"; | ||
| 82 | + b.append(winlin, strlen(winlin)); | ||
| 83 | + b.erase(b.length()); | ||
| 84 | + EXPECT_EQ(0, b.length()); | ||
| 85 | + | ||
| 86 | + b.append(winlin, strlen(winlin)); | ||
| 87 | + b.erase(1); | ||
| 88 | + EXPECT_EQ(5, b.length()); | ||
| 89 | + EXPECT_EQ('i', b.bytes()[0]); | ||
| 90 | + EXPECT_EQ('n', b.bytes()[4]); | ||
| 91 | + b.erase(2); | ||
| 92 | + EXPECT_EQ(3, b.length()); | ||
| 93 | + EXPECT_EQ('l', b.bytes()[0]); | ||
| 94 | + EXPECT_EQ('n', b.bytes()[2]); | ||
| 95 | + b.erase(3); | ||
| 96 | + EXPECT_EQ(0, b.length()); | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +VOID TEST(BufferTest, Grow) | ||
| 100 | +{ | ||
| 101 | + SrsBuffer b; | ||
| 102 | + MockBufferReader r("winlin"); | ||
| 103 | + | ||
| 104 | + b.grow(&r, 1); | ||
| 105 | + EXPECT_EQ(6, b.length()); | ||
| 106 | + EXPECT_EQ('w', b.bytes()[0]); | ||
| 107 | + | ||
| 108 | + b.grow(&r, 3); | ||
| 109 | + EXPECT_EQ(6, b.length()); | ||
| 110 | + EXPECT_EQ('n', b.bytes()[2]); | ||
| 111 | + | ||
| 112 | + b.grow(&r, 100); | ||
| 113 | + EXPECT_EQ(102, b.length()); | ||
| 114 | + EXPECT_EQ('l', b.bytes()[99]); | ||
| 115 | +} |
trunk/src/utest/srs_utest_buffer.hpp
0 → 100644
| 1 | +/* | ||
| 2 | +The MIT License (MIT) | ||
| 3 | + | ||
| 4 | +Copyright (c) 2013-2014 winlin | ||
| 5 | + | ||
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| 7 | +this software and associated documentation files (the "Software"), to deal in | ||
| 8 | +the Software without restriction, including without limitation the rights to | ||
| 9 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| 10 | +the Software, and to permit persons to whom the Software is furnished to do so, | ||
| 11 | +subject to the following conditions: | ||
| 12 | + | ||
| 13 | +The above copyright notice and this permission notice shall be included in all | ||
| 14 | +copies or substantial portions of the Software. | ||
| 15 | + | ||
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| 18 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| 19 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| 20 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 21 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 22 | +*/ | ||
| 23 | + | ||
| 24 | +#ifndef SRS_UTEST_BUFFER_HPP | ||
| 25 | +#define SRS_UTEST_BUFFER_HPP | ||
| 26 | + | ||
| 27 | +/* | ||
| 28 | +#include <srs_utest_buffer.hpp> | ||
| 29 | +*/ | ||
| 30 | +#include <srs_utest.hpp> | ||
| 31 | + | ||
| 32 | +#include <string> | ||
| 33 | +#include <srs_kernel_buffer.hpp> | ||
| 34 | + | ||
| 35 | +class MockBufferReader: public ISrsBufferReader | ||
| 36 | +{ | ||
| 37 | +private: | ||
| 38 | + std::string str; | ||
| 39 | +public: | ||
| 40 | + MockBufferReader(const char* data); | ||
| 41 | + virtual ~MockBufferReader(); | ||
| 42 | +public: | ||
| 43 | + virtual int read(void* buf, size_t size, ssize_t* nread); | ||
| 44 | +}; | ||
| 45 | + | ||
| 46 | +#endif |
-
请 注册 或 登录 后发表评论