diff options
Diffstat (limited to 'vespalib/src/tests/util')
5 files changed, 186 insertions, 0 deletions
diff --git a/vespalib/src/tests/util/file_area_freelist/CMakeLists.txt b/vespalib/src/tests/util/file_area_freelist/CMakeLists.txt new file mode 100644 index 00000000000..a0961adbba8 --- /dev/null +++ b/vespalib/src/tests/util/file_area_freelist/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_file_area_freelist_test_app TEST + SOURCES + file_area_freelist_test.cpp + DEPENDS + vespalib + GTest::GTest +) +vespa_add_test(NAME vespalib_file_area_freelist_test_app COMMAND vespalib_file_area_freelist_test_app) diff --git a/vespalib/src/tests/util/file_area_freelist/file_area_freelist_test.cpp b/vespalib/src/tests/util/file_area_freelist/file_area_freelist_test.cpp new file mode 100644 index 00000000000..aecad4e12cf --- /dev/null +++ b/vespalib/src/tests/util/file_area_freelist/file_area_freelist_test.cpp @@ -0,0 +1,73 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/vespalib/util/file_area_freelist.h> +#include <vespa/vespalib/gtest/gtest.h> + +using vespalib::alloc::FileAreaFreeList; + +class FileAreaFreeListTest : public ::testing::Test +{ +protected: + FileAreaFreeList _freelist; + static constexpr auto bad_offset = FileAreaFreeList::bad_offset; + +public: + FileAreaFreeListTest(); + ~FileAreaFreeListTest(); +}; + +FileAreaFreeListTest::FileAreaFreeListTest() + : _freelist() +{ +} + +FileAreaFreeListTest::~FileAreaFreeListTest() = default; + + +TEST_F(FileAreaFreeListTest, empty_freelist_is_ok) +{ + EXPECT_EQ(bad_offset, _freelist.alloc(1)); +} + +TEST_F(FileAreaFreeListTest, can_reuse_free_area) +{ + _freelist.free(4, 1); + EXPECT_EQ(4, _freelist.alloc(1)); + EXPECT_EQ(bad_offset, _freelist.alloc(1)); +} + +TEST_F(FileAreaFreeListTest, merge_area_with_next_area) +{ + _freelist.free(5, 1); + _freelist.free(4, 1); + EXPECT_EQ(4, _freelist.alloc(2)); + EXPECT_EQ(bad_offset, _freelist.alloc(1)); +} + +TEST_F(FileAreaFreeListTest, merge_area_with_previous_area) +{ + _freelist.free(3, 1); + _freelist.free(4, 1); + EXPECT_EQ(3, _freelist.alloc(2)); + EXPECT_EQ(bad_offset, _freelist.alloc(1)); +} + +TEST_F(FileAreaFreeListTest, merge_area_with_previous_and_next_area) +{ + _freelist.free(5, 1); + _freelist.free(3, 1); + _freelist.free(4, 1); + EXPECT_EQ(3, _freelist.alloc(3)); + EXPECT_EQ(bad_offset, _freelist.alloc(1)); +} + +TEST_F(FileAreaFreeListTest, can_use_part_of_free_area) +{ + _freelist.free(4, 2); + EXPECT_EQ(4, _freelist.alloc(1)); + EXPECT_EQ(5, _freelist.alloc(1)); + EXPECT_EQ(bad_offset, _freelist.alloc(1)); +} + + +GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/tests/util/mmap_file_allocator/.gitignore b/vespalib/src/tests/util/mmap_file_allocator/.gitignore new file mode 100644 index 00000000000..a18e9aac589 --- /dev/null +++ b/vespalib/src/tests/util/mmap_file_allocator/.gitignore @@ -0,0 +1 @@ +/mmap-file-allocator-factory-dir diff --git a/vespalib/src/tests/util/mmap_file_allocator/CMakeLists.txt b/vespalib/src/tests/util/mmap_file_allocator/CMakeLists.txt new file mode 100644 index 00000000000..00ce5f52fd7 --- /dev/null +++ b/vespalib/src/tests/util/mmap_file_allocator/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_mmap_file_allocator_test_app TEST + SOURCES + mmap_file_allocator_test.cpp + DEPENDS + vespalib + GTest::GTest +) +vespa_add_test(NAME vespalib_mmap_file_allocator_test_app COMMAND vespalib_mmap_file_allocator_test_app) diff --git a/vespalib/src/tests/util/mmap_file_allocator/mmap_file_allocator_test.cpp b/vespalib/src/tests/util/mmap_file_allocator/mmap_file_allocator_test.cpp new file mode 100644 index 00000000000..0d6e7718b86 --- /dev/null +++ b/vespalib/src/tests/util/mmap_file_allocator/mmap_file_allocator_test.cpp @@ -0,0 +1,94 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/vespalib/util/mmap_file_allocator.h> +#include <vespa/vespalib/gtest/gtest.h> +#include <sys/mman.h> + +using vespalib::alloc::MemoryAllocator; +using vespalib::alloc::MmapFileAllocator; + +namespace { + +vespalib::string basedir("mmap-file-allocator-dir"); +vespalib::string hello("hello"); + +struct MyAlloc +{ + const MemoryAllocator& allocator; + void* data; + size_t size; + + MyAlloc(MemoryAllocator& allocator_in, MemoryAllocator::PtrAndSize buf) + : allocator(allocator_in), + data(buf.first), + size(buf.second) + { + } + + ~MyAlloc() + { + allocator.free(data, size); + } + + MemoryAllocator::PtrAndSize asPair() const noexcept { return std::make_pair(data, size); } +}; + +} + +class MmapFileAllocatorTest : public ::testing::Test +{ +protected: + MmapFileAllocator _allocator; + +public: + MmapFileAllocatorTest(); + ~MmapFileAllocatorTest(); +}; + +MmapFileAllocatorTest::MmapFileAllocatorTest() + : _allocator(basedir) +{ +} + +MmapFileAllocatorTest::~MmapFileAllocatorTest() = default; + +TEST_F(MmapFileAllocatorTest, zero_sized_allocation_is_handled) +{ + MyAlloc buf(_allocator, _allocator.alloc(0)); + EXPECT_EQ(nullptr, buf.data); + EXPECT_EQ(0u, buf.size); +} + +TEST_F(MmapFileAllocatorTest, mmap_file_allocator_works) +{ + MyAlloc buf(_allocator, _allocator.alloc(4)); + EXPECT_LE(4u, buf.size); + EXPECT_TRUE(buf.data != nullptr); + memcpy(buf.data, "1st", 4); + MyAlloc buf2(_allocator, _allocator.alloc(5)); + EXPECT_LE(5u, buf2.size); + EXPECT_TRUE(buf2.data != nullptr); + EXPECT_TRUE(buf.data != buf2.data); + memcpy(buf2.data, "fine", 5); + EXPECT_EQ(0u, _allocator.resize_inplace(buf.asPair(), 5)); + EXPECT_EQ(0u, _allocator.resize_inplace(buf.asPair(), 3)); + EXPECT_NE(0u, _allocator.get_end_offset()); + int result = msync(buf.data, buf.size, MS_SYNC); + EXPECT_EQ(0, result); + result = msync(buf2.data, buf2.size, MS_SYNC); + EXPECT_EQ(0, result); +} + +TEST_F(MmapFileAllocatorTest, reuse_file_offset_works) +{ + { + MyAlloc buf(_allocator, _allocator.alloc(hello.size() + 1)); + memcpy(buf.data, hello.c_str(), hello.size() + 1); + } + { + MyAlloc buf(_allocator, _allocator.alloc(hello.size() + 1)); + EXPECT_EQ(0, memcmp(buf.data, hello.c_str(), hello.size() + 1)); + } +} + +GTEST_MAIN_RUN_ALL_TESTS() |