|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "velox/dwio/common/FileSink.h" |
| 18 | + |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +namespace facebook::velox::dwio::common { |
| 22 | + |
| 23 | +class MemorySinkTest : public testing::Test { |
| 24 | + protected: |
| 25 | + static void SetUpTestCase() { |
| 26 | + memory::MemoryManager::testingSetInstance({}); |
| 27 | + } |
| 28 | + |
| 29 | + std::shared_ptr<velox::memory::MemoryPool> pool_{ |
| 30 | + memory::memoryManager()->addLeafPool()}; |
| 31 | +}; |
| 32 | + |
| 33 | +TEST_F(MemorySinkTest, create) { |
| 34 | + std::string chars("abcdefghijklmnopqrst"); |
| 35 | + std::vector<DataBuffer<char>> buffers; |
| 36 | + |
| 37 | + // Add 'abcdefghij' to first buffer |
| 38 | + buffers.emplace_back(*pool_); |
| 39 | + buffers.back().append(0, chars.data(), 10); |
| 40 | + |
| 41 | + // Add 'klmnopqrst' to second buffer |
| 42 | + buffers.emplace_back(*pool_); |
| 43 | + buffers.back().append(0, chars.data() + 10, 10); |
| 44 | + |
| 45 | + ASSERT_EQ(buffers.size(), 2); |
| 46 | + |
| 47 | + auto memorySink = std::make_unique<MemorySink>( |
| 48 | + 1024, dwio::common::FileSink::Options{.pool = pool_.get()}); |
| 49 | + |
| 50 | + ASSERT_TRUE(memorySink->isBuffered()); |
| 51 | + // Write data to MemorySink. |
| 52 | + memorySink->write(buffers); |
| 53 | + ASSERT_EQ(memorySink->size(), chars.length()); |
| 54 | + ASSERT_EQ(memorySink->data(), chars); |
| 55 | + memorySink->close(); |
| 56 | +} |
| 57 | +} // namespace facebook::velox::dwio::common |
0 commit comments