winlin

merge buffer to kernel utest

@@ -517,8 +517,7 @@ fi @@ -517,8 +517,7 @@ 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"
520 - "srs_utest_buffer" "srs_utest_protocol" "srs_utest_kernel"  
521 - "srs_utest_core") 520 + "srs_utest_protocol" "srs_utest_kernel" "srs_utest_core")
522 ModuleLibIncs=(${SRS_OBJS} ${LibSTRoot}) 521 ModuleLibIncs=(${SRS_OBJS} ${LibSTRoot})
523 ModuleLibFiles=(${LibSTfile} ${LibHttpParserfile} ${LibSSLfile}) 522 ModuleLibFiles=(${LibSTfile} ${LibHttpParserfile} ${LibSSLfile})
524 MODULE_DEPENDS=("CORE" "KERNEL" "RTMP" "APP") 523 MODULE_DEPENDS=("CORE" "KERNEL" "RTMP" "APP")
@@ -114,8 +114,6 @@ file @@ -114,8 +114,6 @@ file
114 ..\utest\srs_utest.cpp, 114 ..\utest\srs_utest.cpp,
115 ..\utest\srs_utest_amf0.hpp, 115 ..\utest\srs_utest_amf0.hpp,
116 ..\utest\srs_utest_amf0.cpp, 116 ..\utest\srs_utest_amf0.cpp,
117 - ..\utest\srs_utest_buffer.hpp,  
118 - ..\utest\srs_utest_buffer.cpp,  
119 ..\utest\srs_utest_core.hpp, 117 ..\utest\srs_utest_core.hpp,
120 ..\utest\srs_utest_core.cpp, 118 ..\utest\srs_utest_core.cpp,
121 ..\utest\srs_utest_handshake.hpp, 119 ..\utest\srs_utest_handshake.hpp,
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 -#include <srs_kernel_utility.hpp>  
27 -  
28 -MockBufferReader::MockBufferReader(const char* data)  
29 -{  
30 - str = data;  
31 -}  
32 -  
33 -MockBufferReader::~MockBufferReader()  
34 -{  
35 -}  
36 -  
37 -int MockBufferReader::read(void* buf, size_t size, ssize_t* nread)  
38 -{  
39 - int len = srs_min(str.length(), size);  
40 -  
41 - memcpy(buf, str.data(), len);  
42 -  
43 - if (nread) {  
44 - *nread = len;  
45 - }  
46 -  
47 - return ERROR_SUCCESS;  
48 -}  
49 -  
50 -VOID TEST(BufferTest, DefaultObject)  
51 -{  
52 - SrsBuffer b;  
53 -  
54 - EXPECT_EQ(0, b.length());  
55 - EXPECT_EQ(NULL, b.bytes());  
56 -}  
57 -  
58 -VOID TEST(BufferTest, AppendBytes)  
59 -{  
60 - SrsBuffer b;  
61 -  
62 - char winlin[] = "winlin";  
63 - b.append(winlin, strlen(winlin));  
64 - EXPECT_EQ((int)strlen(winlin), b.length());  
65 - ASSERT_TRUE(NULL != b.bytes());  
66 - EXPECT_EQ('w', b.bytes()[0]);  
67 - EXPECT_EQ('n', b.bytes()[5]);  
68 -  
69 - b.append(winlin, strlen(winlin));  
70 - EXPECT_EQ(2 * (int)strlen(winlin), b.length());  
71 - ASSERT_TRUE(NULL != b.bytes());  
72 - EXPECT_EQ('w', b.bytes()[0]);  
73 - EXPECT_EQ('n', b.bytes()[5]);  
74 - EXPECT_EQ('w', b.bytes()[6]);  
75 - EXPECT_EQ('n', b.bytes()[11]);  
76 -}  
77 -  
78 -VOID TEST(BufferTest, EraseBytes)  
79 -{  
80 - SrsBuffer b;  
81 -  
82 - b.erase(0);  
83 - b.erase(-1);  
84 - EXPECT_EQ(0, b.length());  
85 -  
86 - char winlin[] = "winlin";  
87 - b.append(winlin, strlen(winlin));  
88 - b.erase(b.length());  
89 - EXPECT_EQ(0, b.length());  
90 -  
91 - b.erase(0);  
92 - b.erase(-1);  
93 - EXPECT_EQ(0, b.length());  
94 -  
95 - b.append(winlin, strlen(winlin));  
96 - b.erase(1);  
97 - EXPECT_EQ(5, b.length());  
98 - EXPECT_EQ('i', b.bytes()[0]);  
99 - EXPECT_EQ('n', b.bytes()[4]);  
100 -  
101 - b.erase(2);  
102 - EXPECT_EQ(3, b.length());  
103 - EXPECT_EQ('l', b.bytes()[0]);  
104 - EXPECT_EQ('n', b.bytes()[2]);  
105 -  
106 - b.erase(0);  
107 - b.erase(-1);  
108 - EXPECT_EQ(3, b.length());  
109 -  
110 - b.erase(3);  
111 - EXPECT_EQ(0, b.length());  
112 -}  
113 -  
114 -VOID TEST(BufferTest, Grow)  
115 -{  
116 - SrsBuffer b;  
117 - MockBufferReader r("winlin");  
118 -  
119 - b.grow(&r, 1);  
120 - EXPECT_EQ(6, b.length());  
121 - EXPECT_EQ('w', b.bytes()[0]);  
122 -  
123 - b.grow(&r, 3);  
124 - EXPECT_EQ(6, b.length());  
125 - EXPECT_EQ('n', b.bytes()[2]);  
126 -  
127 - b.grow(&r, 100);  
128 - EXPECT_EQ(102, b.length());  
129 - EXPECT_EQ('l', b.bytes()[99]);  
130 -}  
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  
@@ -176,6 +176,110 @@ void MockSrsFileReader::mock_reset_offset() @@ -176,6 +176,110 @@ void MockSrsFileReader::mock_reset_offset()
176 offset = 0; 176 offset = 0;
177 } 177 }
178 178
  179 +MockBufferReader::MockBufferReader(const char* data)
  180 +{
  181 + str = data;
  182 +}
  183 +
  184 +MockBufferReader::~MockBufferReader()
  185 +{
  186 +}
  187 +
  188 +int MockBufferReader::read(void* buf, size_t size, ssize_t* nread)
  189 +{
  190 + int len = srs_min(str.length(), size);
  191 +
  192 + memcpy(buf, str.data(), len);
  193 +
  194 + if (nread) {
  195 + *nread = len;
  196 + }
  197 +
  198 + return ERROR_SUCCESS;
  199 +}
  200 +
  201 +VOID TEST(BufferTest, DefaultObject)
  202 +{
  203 + SrsBuffer b;
  204 +
  205 + EXPECT_EQ(0, b.length());
  206 + EXPECT_EQ(NULL, b.bytes());
  207 +}
  208 +
  209 +VOID TEST(BufferTest, AppendBytes)
  210 +{
  211 + SrsBuffer b;
  212 +
  213 + char winlin[] = "winlin";
  214 + b.append(winlin, strlen(winlin));
  215 + EXPECT_EQ((int)strlen(winlin), b.length());
  216 + ASSERT_TRUE(NULL != b.bytes());
  217 + EXPECT_EQ('w', b.bytes()[0]);
  218 + EXPECT_EQ('n', b.bytes()[5]);
  219 +
  220 + b.append(winlin, strlen(winlin));
  221 + EXPECT_EQ(2 * (int)strlen(winlin), b.length());
  222 + ASSERT_TRUE(NULL != b.bytes());
  223 + EXPECT_EQ('w', b.bytes()[0]);
  224 + EXPECT_EQ('n', b.bytes()[5]);
  225 + EXPECT_EQ('w', b.bytes()[6]);
  226 + EXPECT_EQ('n', b.bytes()[11]);
  227 +}
  228 +
  229 +VOID TEST(BufferTest, EraseBytes)
  230 +{
  231 + SrsBuffer b;
  232 +
  233 + b.erase(0);
  234 + b.erase(-1);
  235 + EXPECT_EQ(0, b.length());
  236 +
  237 + char winlin[] = "winlin";
  238 + b.append(winlin, strlen(winlin));
  239 + b.erase(b.length());
  240 + EXPECT_EQ(0, b.length());
  241 +
  242 + b.erase(0);
  243 + b.erase(-1);
  244 + EXPECT_EQ(0, b.length());
  245 +
  246 + b.append(winlin, strlen(winlin));
  247 + b.erase(1);
  248 + EXPECT_EQ(5, b.length());
  249 + EXPECT_EQ('i', b.bytes()[0]);
  250 + EXPECT_EQ('n', b.bytes()[4]);
  251 +
  252 + b.erase(2);
  253 + EXPECT_EQ(3, b.length());
  254 + EXPECT_EQ('l', b.bytes()[0]);
  255 + EXPECT_EQ('n', b.bytes()[2]);
  256 +
  257 + b.erase(0);
  258 + b.erase(-1);
  259 + EXPECT_EQ(3, b.length());
  260 +
  261 + b.erase(3);
  262 + EXPECT_EQ(0, b.length());
  263 +}
  264 +
  265 +VOID TEST(BufferTest, Grow)
  266 +{
  267 + SrsBuffer b;
  268 + MockBufferReader r("winlin");
  269 +
  270 + b.grow(&r, 1);
  271 + EXPECT_EQ(6, b.length());
  272 + EXPECT_EQ('w', b.bytes()[0]);
  273 +
  274 + b.grow(&r, 3);
  275 + EXPECT_EQ(6, b.length());
  276 + EXPECT_EQ('n', b.bytes()[2]);
  277 +
  278 + b.grow(&r, 100);
  279 + EXPECT_EQ(102, b.length());
  280 + EXPECT_EQ('l', b.bytes()[99]);
  281 +}
  282 +
179 /** 283 /**
180 * test the codec, 284 * test the codec,
181 * whether H.264 keyframe 285 * whether H.264 keyframe
@@ -31,6 +31,18 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,6 +31,18 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 31
32 #include <string> 32 #include <string>
33 #include <srs_kernel_file.hpp> 33 #include <srs_kernel_file.hpp>
  34 +#include <srs_kernel_buffer.hpp>
  35 +
  36 +class MockBufferReader: public ISrsBufferReader
  37 +{
  38 +private:
  39 + std::string str;
  40 +public:
  41 + MockBufferReader(const char* data);
  42 + virtual ~MockBufferReader();
  43 +public:
  44 + virtual int read(void* buf, size_t size, ssize_t* nread);
  45 +};
34 46
35 class MockSrsFileWriter : public SrsFileWriter 47 class MockSrsFileWriter : public SrsFileWriter
36 { 48 {