-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathBufferTest.cc
104 lines (72 loc) · 1.8 KB
/
BufferTest.cc
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "gtest/gtest.h"
#include "util/Buffer.h"
using namespace ananas;
TEST(buffer, push) {
Buffer buf;
size_t ret = buf.PushData("hello", 5);
EXPECT_EQ(ret, 5);
ret = buf.PushData("world\n", 6);
EXPECT_EQ(ret, 6);
}
TEST(buffer, peek) {
Buffer buf;
{
buf.PushData("hello ", 6);
buf.PushData("world\n", 6);
}
char tmp[12];
size_t ret = buf.PeekDataAt(tmp, 5, 0);
EXPECT_EQ(ret, 5);
EXPECT_EQ(tmp[0], 'h');
EXPECT_EQ(tmp[4], 'o');
ret = buf.PeekDataAt(tmp, 2, 6);
EXPECT_EQ(ret, 2);
EXPECT_EQ(tmp[0], 'w');
EXPECT_EQ(tmp[1], 'o');
EXPECT_EQ(buf.ReadableSize(), 12);
}
TEST(buffer, pop) {
Buffer buf;
{
buf.PushData("hello ", 6);
buf.PushData("world\n", 6);
}
size_t cap = buf.Capacity();
char tmp[12];
size_t ret = buf.PopData(tmp, 6);
EXPECT_EQ(ret, 6);
EXPECT_EQ(tmp[0], 'h');
EXPECT_EQ(tmp[5], ' ');
EXPECT_EQ(buf.ReadableSize(), 6);
ret = buf.PopData(tmp, 6);
EXPECT_EQ(ret, 6);
EXPECT_EQ(tmp[0], 'w');
EXPECT_EQ(tmp[5], '\n');
EXPECT_TRUE(buf.IsEmpty());
EXPECT_EQ(buf.Capacity(), cap); // pop does not change capacity
}
TEST(buffer, shrink) {
Buffer buf;
{
buf.PushData("hello ", 6);
buf.PushData("world\n", 6);
}
EXPECT_NE(buf.Capacity(), 12);
buf.Shrink();
EXPECT_EQ(buf.Capacity(), 16);
buf.PushData("abcd", 4);
EXPECT_EQ(buf.Capacity(), 16);
char tmp[16];
buf.PopData(tmp, sizeof tmp);
EXPECT_EQ(buf.Capacity(), 16);
}
TEST(buffer, push_pop) {
Buffer buf;
buf.PushData("hello ", 6);
char tmp[8];
size_t ret = buf.PopData(tmp, 5);
EXPECT_EQ(ret, 5);
EXPECT_EQ(buf.Capacity(), Buffer::kDefaultSize);
buf.Shrink();
EXPECT_EQ(buf.Capacity(), 1);
}