Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1732 Rework bump allocator tests
Browse files Browse the repository at this point in the history
Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Jan 18, 2023
1 parent 45eb158 commit 67fe3c1
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 69 deletions.
2 changes: 1 addition & 1 deletion iceoryx_dust/source/posix_wrapper/named_pipe.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2020 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021, 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/memory/include/iox/bump_allocator.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/memory/source/bump_allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/test/moduletests/test_cxx_function.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2020 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
176 changes: 116 additions & 60 deletions iceoryx_hoofs/test/moduletests/test_memory_bump_allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
#include "iox/bump_allocator.hpp"
#include "test.hpp"

#include <limits>

namespace
{
using namespace testing;
Expand All @@ -28,7 +30,8 @@ class BumpAllocator_Test : public Test
void SetUp() override
{
// NOLINTNEXTLINE(hicpp-no-malloc, cppcoreguidelines-no-malloc) required to test allocation
memory = malloc(memorySize);
memory = malloc(MEMORY_SIZE);
ASSERT_THAT(memory, Ne(nullptr));
}

void TearDown() override
Expand All @@ -38,105 +41,158 @@ class BumpAllocator_Test : public Test
}

static constexpr uint64_t MEMORY_ALIGNMENT{8};
static constexpr uint64_t MEMORY_SIZE{10016};

void* memory{nullptr};
size_t memorySize = 10016;
};

TEST_F(BumpAllocator_Test, allocateOneSmallElement)
{
::testing::Test::RecordProperty("TEST_ID", "f689e95c-5743-4370-93f0-8a23b909c75a");
iox::BumpAllocator sut(memory, memorySize);
auto allocationResult = sut.allocate(sizeof(int), MEMORY_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
int* bla = static_cast<int*>(allocationResult.value());
*bla = 123;
EXPECT_THAT(*bla, Eq(123));
}

TEST_F(BumpAllocator_Test, allocateEverythingWithSingleElement)
TEST_F(BumpAllocator_Test, AllocateFailsWithZeroSize)
{
::testing::Test::RecordProperty("TEST_ID", "f2e1085b-08fe-4b08-b022-0385b5a53fca");
iox::BumpAllocator sut(memory, memorySize);
auto allocationResult = sut.allocate(memorySize, 1);
ASSERT_FALSE(allocationResult.has_error());
int* bla = static_cast<int*>(allocationResult.value());
*bla = 123;
EXPECT_THAT(*bla, Eq(123));
}
::testing::Test::RecordProperty("TEST_ID", "17caa50c-94bf-4a1d-a1ec-dfda563caa0b");
iox::BumpAllocator sut(memory, MEMORY_SIZE);

TEST_F(BumpAllocator_Test, allocateEverythingWithMultipleElements)
{
::testing::Test::RecordProperty("TEST_ID", "21d0fa61-54f9-41a0-8e53-e3448784497b");
iox::BumpAllocator sut(memory, memorySize);
for (size_t i = 0; i < memorySize; i += 32)
{
auto allocationResult = sut.allocate(32, 1);
ASSERT_FALSE(allocationResult.has_error());
auto* bla = static_cast<size_t*>(allocationResult.value());
*bla = i;
EXPECT_THAT(*bla, Eq(i));
}
auto allocationResult = sut.allocate(0, MEMORY_ALIGNMENT);
ASSERT_TRUE(allocationResult.has_error());
EXPECT_THAT(allocationResult.get_error(), Eq(iox::BumpAllocatorError::REQUESTED_ZERO_SIZED_MEMORY));
}

TEST_F(BumpAllocator_Test, allocateTooMuchSingleElement)
TEST_F(BumpAllocator_Test, OverallocationFails)
{
::testing::Test::RecordProperty("TEST_ID", "9deed5c0-19d8-4469-a5c3-f185d4d881f1");
iox::BumpAllocator sut(memory, memorySize);
auto allocationResult = sut.allocate(memorySize + 1, MEMORY_ALIGNMENT);
iox::BumpAllocator sut(memory, MEMORY_SIZE);

auto allocationResult = sut.allocate(MEMORY_SIZE + 1, MEMORY_ALIGNMENT);
ASSERT_TRUE(allocationResult.has_error());
EXPECT_THAT(allocationResult.get_error(), Eq(iox::BumpAllocatorError::OUT_OF_MEMORY));
}

TEST_F(BumpAllocator_Test, allocateTooMuchMultipleElement)
TEST_F(BumpAllocator_Test, OverallocationAfterMultipleCallsFails)
{
::testing::Test::RecordProperty("TEST_ID", "435151e8-cc34-41ce-8115-5c179716a60a");
iox::BumpAllocator sut(memory, memorySize);
for (size_t i = 0; i < memorySize; i += 32)
constexpr uint64_t MEMORY_CHUNK_SIZE{32};
iox::BumpAllocator sut(memory, MEMORY_SIZE);
for (uint64_t i{0}; i < MEMORY_SIZE; i += MEMORY_CHUNK_SIZE)
{
ASSERT_FALSE(sut.allocate(32, 1).has_error());
ASSERT_FALSE(sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_ALIGNMENT).has_error());
}

auto allocationResult = sut.allocate(1, MEMORY_ALIGNMENT);
ASSERT_TRUE(allocationResult.has_error());
EXPECT_THAT(allocationResult.get_error(), Eq(iox::BumpAllocatorError::OUT_OF_MEMORY));
}

TEST_F(BumpAllocator_Test, allocateAndAlignment)
TEST_F(BumpAllocator_Test, AllocationIsCorrectlyAligned)
{
::testing::Test::RecordProperty("TEST_ID", "4252ddcc-05d4-499f-ad7c-30bffb420e08");
iox::BumpAllocator sut(memory, memorySize);
auto allocationResult = sut.allocate(5, MEMORY_ALIGNMENT);
constexpr uint64_t MEMORY_CHUNK_SIZE{sizeof(int)};
constexpr uint64_t MEMORY_CHUNK_ALIGNMENT{alignof(int)};
iox::BumpAllocator sut(memory, MEMORY_SIZE);

auto allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
auto* bla = static_cast<uint8_t*>(allocationResult.value());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p = reinterpret_cast<uintptr_t>(allocationResult.value());
EXPECT_THAT(p % MEMORY_CHUNK_ALIGNMENT, Eq(0));

allocationResult = sut.allocate(5, MEMORY_ALIGNMENT);
allocationResult = sut.allocate(2 * MEMORY_CHUNK_SIZE, 2 * MEMORY_CHUNK_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
auto* bla2 = static_cast<uint8_t*>(allocationResult.value());
EXPECT_THAT(bla2 - bla, Eq(8U));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
p = reinterpret_cast<uintptr_t>(allocationResult.value());
EXPECT_THAT(p % (2 * MEMORY_CHUNK_ALIGNMENT), Eq(0));
}

TEST_F(BumpAllocator_Test, allocateElementOfSizeZero)
TEST_F(BumpAllocator_Test, AllocateSmallMemoryChunkAndStoreDataWorks)
{
::testing::Test::RecordProperty("TEST_ID", "17caa50c-94bf-4a1d-a1ec-dfda563caa0b");
iox::BumpAllocator sut(memory, memorySize);
::testing::Test::RecordProperty("TEST_ID", "f689e95c-5743-4370-93f0-8a23b909c75a");
iox::BumpAllocator sut(memory, MEMORY_SIZE);
auto allocationResult = sut.allocate(sizeof(int), alignof(int));
ASSERT_FALSE(allocationResult.has_error());

auto allocationResult = sut.allocate(0, MEMORY_ALIGNMENT);
ASSERT_TRUE(allocationResult.has_error());
EXPECT_THAT(allocationResult.get_error(), Eq(iox::BumpAllocatorError::REQUESTED_ZERO_SIZED_MEMORY));
int* bla = static_cast<int*>(allocationResult.value());
*bla = std::numeric_limits<int>::min();
EXPECT_THAT(*bla, Eq(std::numeric_limits<int>::min()));
}

TEST_F(BumpAllocator_Test, allocateAfterDeallocateWorks)
TEST_F(BumpAllocator_Test, AllocateCompleteMemoryAndStoreDataWorks)
{
::testing::Test::RecordProperty("TEST_ID", "f2e1085b-08fe-4b08-b022-0385b5a53fca");
iox::BumpAllocator sut(memory, sizeof(int));
auto allocationResult = sut.allocate(sizeof(int), alignof(int));
ASSERT_FALSE(allocationResult.has_error());

int* bla = static_cast<int*>(allocationResult.value());
*bla = std::numeric_limits<int>::max();
EXPECT_THAT(*bla, Eq(std::numeric_limits<int>::max()));
}

TEST_F(BumpAllocator_Test, AllocateCompleteMemoryWithEquallySizedChunksWorks)
{
::testing::Test::RecordProperty("TEST_ID", "21d0fa61-54f9-41a0-8e53-e3448784497b");
constexpr uint64_t MEMORY_CHUNK_SIZE{32};
constexpr uint64_t MEMORY_CHUNK_ALIGNMENT{32};
iox::BumpAllocator sut(memory, MEMORY_SIZE);

auto allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p0 = reinterpret_cast<uintptr_t>(allocationResult.value());

for (uint64_t i{MEMORY_CHUNK_SIZE}; i < MEMORY_SIZE; i += MEMORY_CHUNK_SIZE)
{
allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p1 = reinterpret_cast<uintptr_t>(allocationResult.value());
EXPECT_THAT(p1 - p0, Eq(MEMORY_CHUNK_SIZE));
p0 = p1;
}
}

TEST_F(BumpAllocator_Test, AllocateCompleteMemoryWithDifferentSizedChunksWorks)
{
::testing::Test::RecordProperty("TEST_ID", "c8079bb7-1de6-45a6-92af-a50aa59d7481");
constexpr uint64_t MEMORY_CHUNK_SIZE{64};
constexpr uint64_t MEMORY_CHUNK_ALIGNMENT{64};
iox::BumpAllocator sut(memory, MEMORY_SIZE);

auto allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p0 = reinterpret_cast<uintptr_t>(allocationResult.value());

for (uint64_t i{MEMORY_CHUNK_SIZE}; i < MEMORY_SIZE - MEMORY_CHUNK_SIZE; i += MEMORY_CHUNK_SIZE)
{
allocationResult = sut.allocate(MEMORY_CHUNK_SIZE, MEMORY_CHUNK_ALIGNMENT);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p1 = reinterpret_cast<uintptr_t>(allocationResult.value());
EXPECT_THAT(p1 - p0, Eq(MEMORY_CHUNK_SIZE));
p0 = p1;
}

allocationResult = sut.allocate(MEMORY_CHUNK_SIZE / 2, MEMORY_CHUNK_ALIGNMENT / 2);
ASSERT_FALSE(allocationResult.has_error());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) required for testing
auto p1 = reinterpret_cast<uintptr_t>(allocationResult.value());
EXPECT_THAT(p1 - p0, Eq(MEMORY_CHUNK_SIZE));
}

TEST_F(BumpAllocator_Test, AllocateAfterDeallocateWorks)
{
::testing::Test::RecordProperty("TEST_ID", "323fc1af-481f-4732-b7d3-fa32da389cef");
iox::BumpAllocator sut(memory, memorySize);
ASSERT_FALSE(sut.allocate(memorySize, 1).has_error());
iox::BumpAllocator sut(memory, MEMORY_SIZE);
auto allocationResult = sut.allocate(sizeof(int), alignof(int));
ASSERT_FALSE(allocationResult.has_error());
int* bla = static_cast<int*>(allocationResult.value());
*bla = std::numeric_limits<int>::max();

sut.deallocate();

auto allocationResult = sut.allocate(memorySize, 1);
allocationResult = sut.allocate(sizeof(int), alignof(int));
ASSERT_FALSE(allocationResult.has_error());
int* bla = static_cast<int*>(allocationResult.value());
bla = static_cast<int*>(allocationResult.value());
EXPECT_THAT(bla, Eq(memory));

*bla = 1990;
EXPECT_THAT(*bla, Eq(1990));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 67fe3c1

Please sign in to comment.