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