|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | Swoole | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | This source file is subject to version 2.0 of the Apache license, | |
| 6 | + | that is bundled with this package in the file LICENSE, and is | |
| 7 | + | available through the world-wide-web at the following url: | |
| 8 | + | http://www.apache.org/licenses/LICENSE-2.0.html | |
| 9 | + | If you did not receive a copy of the Apache2.0 license and are unable| |
| 10 | + | to obtain it through the world-wide-web, please send a note to | |
| 11 | + | [email protected] so we can mail you a copy immediately. | |
| 12 | + +----------------------------------------------------------------------+ |
| 13 | + | @link https://www.swoole.com/ | |
| 14 | + |
| 15 | + | @license https://github.com/swoole/swoole-src/blob/master/LICENSE | |
| 16 | + | @author Tianfeng Han <[email protected]> | |
| 17 | + +----------------------------------------------------------------------+ |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "test_core.h" |
| 21 | +#include "swoole_memory.h" |
| 22 | +#include "swoole_buffer.h" |
| 23 | + |
| 24 | +using namespace std; |
| 25 | +using namespace swoole; |
| 26 | + |
| 27 | +TEST(buffer, append_iov) { |
| 28 | + Buffer buf(1024); |
| 29 | + |
| 30 | + int iovcnt = 4; |
| 31 | + iovec v[iovcnt]; |
| 32 | + |
| 33 | + v[0].iov_len = swoole_rand(99, 4095); |
| 34 | + v[1].iov_len = swoole_rand(99, 4095); |
| 35 | + v[2].iov_len = swoole_rand(99, 4095); |
| 36 | + v[3].iov_len = swoole_rand(99, 4095); |
| 37 | + |
| 38 | + unique_ptr<char> s1(new char[v[0].iov_len]); |
| 39 | + unique_ptr<char> s2(new char[v[1].iov_len]); |
| 40 | + unique_ptr<char> s3(new char[v[2].iov_len]); |
| 41 | + unique_ptr<char> s4(new char[v[3].iov_len]); |
| 42 | + |
| 43 | + v[0].iov_base = s1.get(); |
| 44 | + v[1].iov_base = s2.get(); |
| 45 | + v[2].iov_base = s3.get(); |
| 46 | + v[3].iov_base = s4.get(); |
| 47 | + |
| 48 | + memset(v[0].iov_base, 'A', v[0].iov_len); |
| 49 | + memset(v[1].iov_base, 'B', v[1].iov_len); |
| 50 | + memset(v[2].iov_base, 'C', v[2].iov_len); |
| 51 | + memset(v[3].iov_base, 'D', v[3].iov_len); |
| 52 | + |
| 53 | + buf.append(v, iovcnt, 0); |
| 54 | + |
| 55 | + ASSERT_EQ(buf.length(), v[0].iov_len + v[1].iov_len + v[2].iov_len+ v[3].iov_len); |
| 56 | + |
| 57 | + String str(buf.length()); |
| 58 | + |
| 59 | + while(!buf.empty()) { |
| 60 | + auto chunk = buf.front(); |
| 61 | + str.append(chunk->value.ptr, chunk->length); |
| 62 | + buf.pop(); |
| 63 | + } |
| 64 | + |
| 65 | + size_t offset = 0; |
| 66 | + |
| 67 | + SW_LOOP_N (iovcnt) { |
| 68 | + ASSERT_EQ(memcmp(str.str + offset, v[i].iov_base, v[i].iov_len), 0); |
| 69 | + offset += v[i].iov_len; |
| 70 | + } |
| 71 | +} |
0 commit comments