forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer-test.cc
More file actions
239 lines (185 loc) · 6.62 KB
/
Copy pathbuffer-test.cc
File metadata and controls
239 lines (185 loc) · 6.62 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <gtest/gtest.h>
#include "arrow/buffer.h"
#include "arrow/memory_pool.h"
#include "arrow/status.h"
#include "arrow/test-util.h"
using std::string;
namespace arrow {
TEST(TestBuffer, IsMutableFlag) {
Buffer buf(nullptr, 0);
ASSERT_FALSE(buf.is_mutable());
MutableBuffer mbuf(nullptr, 0);
ASSERT_TRUE(mbuf.is_mutable());
PoolBuffer pbuf;
ASSERT_TRUE(pbuf.is_mutable());
}
TEST(TestBuffer, FromStdString) {
std::string val = "hello, world";
Buffer buf(val);
ASSERT_EQ(0, memcmp(buf.data(), val.c_str(), val.size()));
ASSERT_EQ(static_cast<int64_t>(val.size()), buf.size());
}
TEST(TestBuffer, FromStdStringWithMemory) {
std::string expected = "hello, world";
std::shared_ptr<Buffer> buf;
{
std::string temp = "hello, world";
ASSERT_OK(Buffer::FromString(temp, &buf));
ASSERT_EQ(0, memcmp(buf->data(), temp.c_str(), temp.size()));
ASSERT_EQ(static_cast<int64_t>(temp.size()), buf->size());
}
// Now temp goes out of scope and we check if created buffer
// is still valid to make sure it actually owns its space
ASSERT_EQ(0, memcmp(buf->data(), expected.c_str(), expected.size()));
ASSERT_EQ(static_cast<int64_t>(expected.size()), buf->size());
}
TEST(TestBuffer, Resize) {
PoolBuffer buf;
ASSERT_EQ(0, buf.size());
ASSERT_OK(buf.Resize(100));
ASSERT_EQ(100, buf.size());
ASSERT_OK(buf.Resize(200));
ASSERT_EQ(200, buf.size());
// Make it smaller, too
ASSERT_OK(buf.Resize(50, true));
ASSERT_EQ(50, buf.size());
// We have actually shrunken in size
// The spec requires that capacity is a multiple of 64
ASSERT_EQ(64, buf.capacity());
// Resize to a larger capacity again to test shrink_to_fit = false
ASSERT_OK(buf.Resize(100));
ASSERT_EQ(128, buf.capacity());
ASSERT_OK(buf.Resize(50, false));
ASSERT_EQ(128, buf.capacity());
}
TEST(TestBuffer, TypedResize) {
PoolBuffer buf;
ASSERT_EQ(0, buf.size());
ASSERT_OK(buf.TypedResize<double>(100));
ASSERT_EQ(800, buf.size());
ASSERT_OK(buf.TypedResize<double>(200));
ASSERT_EQ(1600, buf.size());
ASSERT_OK(buf.TypedResize<double>(50, true));
ASSERT_EQ(400, buf.size());
ASSERT_EQ(448, buf.capacity());
ASSERT_OK(buf.TypedResize<double>(100));
ASSERT_EQ(832, buf.capacity());
ASSERT_OK(buf.TypedResize<double>(50, false));
ASSERT_EQ(832, buf.capacity());
}
TEST(TestBuffer, ResizeOOM) {
// This test doesn't play nice with AddressSanitizer
#ifndef ADDRESS_SANITIZER
// realloc fails, even though there may be no explicit limit
PoolBuffer buf;
ASSERT_OK(buf.Resize(100));
int64_t to_alloc = std::numeric_limits<int64_t>::max();
ASSERT_RAISES(OutOfMemory, buf.Resize(to_alloc));
#endif
}
TEST(TestBuffer, EqualsWithSameContent) {
MemoryPool* pool = default_memory_pool();
const int32_t bufferSize = 128 * 1024;
uint8_t* rawBuffer1;
ASSERT_OK(pool->Allocate(bufferSize, &rawBuffer1));
memset(rawBuffer1, 12, bufferSize);
uint8_t* rawBuffer2;
ASSERT_OK(pool->Allocate(bufferSize, &rawBuffer2));
memset(rawBuffer2, 12, bufferSize);
uint8_t* rawBuffer3;
ASSERT_OK(pool->Allocate(bufferSize, &rawBuffer3));
memset(rawBuffer3, 3, bufferSize);
Buffer buffer1(rawBuffer1, bufferSize);
Buffer buffer2(rawBuffer2, bufferSize);
Buffer buffer3(rawBuffer3, bufferSize);
ASSERT_TRUE(buffer1.Equals(buffer2));
ASSERT_FALSE(buffer1.Equals(buffer3));
pool->Free(rawBuffer1, bufferSize);
pool->Free(rawBuffer2, bufferSize);
pool->Free(rawBuffer3, bufferSize);
}
TEST(TestBuffer, EqualsWithSameBuffer) {
MemoryPool* pool = default_memory_pool();
const int32_t bufferSize = 128 * 1024;
uint8_t* rawBuffer;
ASSERT_OK(pool->Allocate(bufferSize, &rawBuffer));
memset(rawBuffer, 111, bufferSize);
Buffer buffer1(rawBuffer, bufferSize);
Buffer buffer2(rawBuffer, bufferSize);
ASSERT_TRUE(buffer1.Equals(buffer2));
const int64_t nbytes = bufferSize / 2;
Buffer buffer3(rawBuffer, nbytes);
ASSERT_TRUE(buffer1.Equals(buffer3, nbytes));
ASSERT_FALSE(buffer1.Equals(buffer3, nbytes + 1));
pool->Free(rawBuffer, bufferSize);
}
TEST(TestBuffer, Copy) {
std::string data_str = "some data to copy";
auto data = reinterpret_cast<const uint8_t*>(data_str.c_str());
Buffer buf(data, data_str.size());
std::shared_ptr<Buffer> out;
ASSERT_OK(buf.Copy(5, 4, &out));
Buffer expected(data + 5, 4);
ASSERT_TRUE(out->Equals(expected));
}
TEST(TestBuffer, SliceBuffer) {
std::string data_str = "some data to slice";
auto data = reinterpret_cast<const uint8_t*>(data_str.c_str());
auto buf = std::make_shared<Buffer>(data, data_str.size());
std::shared_ptr<Buffer> out = SliceBuffer(buf, 5, 4);
Buffer expected(data + 5, 4);
ASSERT_TRUE(out->Equals(expected));
ASSERT_EQ(2, buf.use_count());
}
TEST(TestBuffer, SliceMutableBuffer) {
std::string data_str = "some data to slice";
auto data = reinterpret_cast<const uint8_t*>(data_str.c_str());
std::shared_ptr<Buffer> buffer;
ASSERT_OK(AllocateBuffer(default_memory_pool(), 50, &buffer));
memcpy(buffer->mutable_data(), data, data_str.size());
std::shared_ptr<Buffer> slice = SliceMutableBuffer(buffer, 5, 10);
ASSERT_TRUE(slice->is_mutable());
ASSERT_EQ(10, slice->size());
Buffer expected(data + 5, 10);
ASSERT_TRUE(slice->Equals(expected));
}
TEST(TestBufferBuilder, ResizeReserve) {
const std::string data = "some data";
auto data_ptr = data.c_str();
BufferBuilder builder;
ASSERT_OK(builder.Append(data_ptr, 9));
ASSERT_EQ(9, builder.length());
ASSERT_OK(builder.Resize(128));
ASSERT_EQ(128, builder.capacity());
// Do not shrink to fit
ASSERT_OK(builder.Resize(64, false));
ASSERT_EQ(128, builder.capacity());
// Shrink to fit
ASSERT_OK(builder.Resize(64));
ASSERT_EQ(64, builder.capacity());
// Reserve elements
ASSERT_OK(builder.Reserve(60));
ASSERT_EQ(128, builder.capacity());
}
} // namespace arrow