]>
git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/io/test_common.cc
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
18 #include "arrow/io/test_common.h"
22 #include <fstream> // IWYU pragma: keep
31 #include "arrow/buffer.h"
32 #include "arrow/io/file.h"
33 #include "arrow/io/memory.h"
34 #include "arrow/memory_pool.h"
35 #include "arrow/testing/gtest_util.h"
40 void AssertFileContents(const std::string
& path
, const std::string
& contents
) {
41 ASSERT_OK_AND_ASSIGN(auto rf
, ReadableFile::Open(path
));
42 ASSERT_OK_AND_ASSIGN(int64_t size
, rf
->GetSize());
43 ASSERT_EQ(size
, contents
.size());
45 ASSERT_OK_AND_ASSIGN(auto actual_data
, rf
->Read(size
));
46 ASSERT_TRUE(actual_data
->Equals(Buffer(contents
)));
49 bool FileExists(const std::string
& path
) { return std::ifstream(path
.c_str()).good(); }
52 static void InvalidParamHandler(const wchar_t* expr
, const wchar_t* func
,
53 const wchar_t* source_file
, unsigned int source_line
,
55 wprintf(L
"Invalid parameter in function '%s'. Source: '%s' line %d expression '%s'\n",
56 func
, source_file
, source_line
, expr
);
60 bool FileIsClosed(int fd
) {
62 // Disables default behavior on wrong params which causes the application to crash
63 // https://msdn.microsoft.com/en-us/library/ksazx244.aspx
64 _set_invalid_parameter_handler(InvalidParamHandler
);
66 // Disables possible assertion alert box on invalid input arguments
67 _CrtSetReportMode(_CRT_ASSERT
, 0);
69 int new_fd
= _dup(fd
);
71 return errno
== EBADF
;
76 if (-1 != fcntl(fd
, F_GETFD
)) {
79 return errno
== EBADF
;
83 Status
ZeroMemoryMap(MemoryMappedFile
* file
) {
84 constexpr int64_t kBufferSize
= 512;
85 static constexpr uint8_t kZeroBytes
[kBufferSize
] = {0};
87 RETURN_NOT_OK(file
->Seek(0));
89 ARROW_ASSIGN_OR_RAISE(int64_t file_size
, file
->GetSize());
92 while (position
< file_size
) {
93 chunksize
= std::min(kBufferSize
, file_size
- position
);
94 RETURN_NOT_OK(file
->Write(kZeroBytes
, chunksize
));
95 position
+= chunksize
;
100 void MemoryMapFixture::TearDown() {
101 for (auto path
: tmp_files_
) {
102 ARROW_UNUSED(std::remove(path
.c_str()));
106 void MemoryMapFixture::CreateFile(const std::string
& path
, int64_t size
) {
107 ASSERT_OK(MemoryMappedFile::Create(path
, size
));
108 tmp_files_
.push_back(path
);
111 Result
<std::shared_ptr
<MemoryMappedFile
>> MemoryMapFixture::InitMemoryMap(
112 int64_t size
, const std::string
& path
) {
113 ARROW_ASSIGN_OR_RAISE(auto mmap
, MemoryMappedFile::Create(path
, size
));
114 tmp_files_
.push_back(path
);
118 void MemoryMapFixture::AppendFile(const std::string
& path
) { tmp_files_
.push_back(path
); }