Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion score/containers/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# *******************************************************************************

Check failure on line 1 in score/containers/BUILD

View workflow job for this annotation

GitHub Actions / Restricted file changes

score/containers/BUILD is in a restricted path
# Copyright (c) 2025 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
Expand Down Expand Up @@ -38,7 +38,6 @@
"@googletest//:gtest_main",
"@score_baselibs//score/containers/test:allocator_test_type_helpers",
"@score_baselibs//score/containers/test:container_test_types",
"@score_baselibs//score/containers/test:custom_allocator_mock",
"@score_baselibs//score/language/futurecpp:futurecpp_test_support",
"@score_baselibs//score/memory/shared:types",
"@score_baselibs//score/memory/shared/fake:fake_memory_resources",
Expand All @@ -59,6 +58,7 @@
"@googletest//:gtest_main",
"@score_baselibs//score/containers/test:allocator_test_type_helpers",
"@score_baselibs//score/containers/test:container_test_types",
"@score_baselibs//score/containers/test:mockable_allocator",
"@score_baselibs//score/language/futurecpp:futurecpp_test_support",
"@score_baselibs//score/memory/shared:types",
"@score_baselibs//score/memory/shared/fake:fake_memory_resources",
Expand Down
4 changes: 2 additions & 2 deletions score/containers/dynamic_array_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************************

Check failure on line 1 in score/containers/dynamic_array_test.cpp

View workflow job for this annotation

GitHub Actions / Restricted file changes

score/containers/dynamic_array_test.cpp is in a restricted path
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand All @@ -14,7 +14,6 @@

#include "score/containers/test/allocator_test_type_helpers.h"
#include "score/containers/test/container_test_types.h"
#include "score/containers/test/custom_allocator_mock.h"

#include "score/memory/shared/fake/my_memory_resource.h"
#include "score/memory/shared/polymorphic_offset_ptr_allocator.h"
Expand Down Expand Up @@ -341,7 +340,8 @@
};

// Since a std::exception is thrown by std::allocator_traits<Allocator>::allocate(), rather than by an AMP
// assertion / precondition, we capture this using the gtest framework instead of SCORE_LANGUAGE_FUTURECPP_ASSERT_CONTRACT_VIOLATED.
// assertion / precondition, we capture this using the gtest framework instead of
// SCORE_LANGUAGE_FUTURECPP_ASSERT_CONTRACT_VIOLATED.
EXPECT_THROW(initialise_dynamic_array(), std::exception);
}

Expand Down
1 change: 0 additions & 1 deletion score/containers/non_relocatable_vector.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************************

Check failure on line 1 in score/containers/non_relocatable_vector.h

View workflow job for this annotation

GitHub Actions / Restricted file changes

score/containers/non_relocatable_vector.h is in a restricted path
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand All @@ -18,7 +18,6 @@

#include <cstddef>
#include <cstring>
#include <iostream>
#include <iterator>
#include <memory>
#include <type_traits>
Expand Down
135 changes: 135 additions & 0 deletions score/containers/non_relocatable_vector_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************************

Check failure on line 1 in score/containers/non_relocatable_vector_test.cpp

View workflow job for this annotation

GitHub Actions / Restricted file changes

score/containers/non_relocatable_vector_test.cpp is in a restricted path
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand All @@ -14,6 +14,8 @@

#include "score/containers/test/allocator_test_type_helpers.h"
#include "score/containers/test/container_test_types.h"
#include "score/containers/test/mockable_allocator.h"
#include "score/containers/test/mockable_pointer_mock_guard.h"

#include "score/memory/shared/fake/my_memory_resource.h"
#include "score/memory/shared/polymorphic_offset_ptr_allocator.h"
Expand All @@ -28,6 +30,7 @@
#include <cstdint>
#include <limits>
#include <memory>
#include <vector>

namespace score::containers
{
Expand Down Expand Up @@ -237,4 +240,136 @@
}
}

class NonRelocatableVectorPointerSpyFixture : public ::testing::Test
{
void SetUp() override {}
void TearDown() override {}

protected:
using ElementType = std::uint32_t;
using Allocator = typename test::MockableAllocator<ElementType>;

NonRelocatableVectorPointerSpyFixture& GivenANonRelocatableVectorContainingNumberOfElements(
const std::size_t number_of_elements)
{
unit_ = std::make_unique<NonRelocatableVector<ElementType, Allocator>>(number_of_elements);
for (std::size_t i = 0; i < number_of_elements; ++i)
{
score::cpp::ignore = this->unit_->emplace_back(i);
}
return *this;
}

score::memory::shared::test::MyMemoryResource memory_resource_{};
std::unique_ptr<NonRelocatableVector<ElementType, Allocator>> unit_{nullptr};
Comment on lines +263 to +264
};

using NonRelocatableVectorPointerInteractionFixture = NonRelocatableVectorPointerSpyFixture;
using testing::_;

TEST_F(NonRelocatableVectorPointerInteractionFixture, DataDereferencesBeginAndEnd)
{
// Given a NonRelocatableVector which has been filled with elements
GivenANonRelocatableVectorContainingNumberOfElements(kNonZeroNumberElements);

// and a registered pointer-spy mock,
testing::StrictMock<test::PointerSpyMock<ElementType>> pointer_spy;
std::vector<ElementType*> arrow_args;

ElementType* data_ptr = nullptr;
{
test::MockablePointerMockGuard<ElementType> guard{&pointer_spy};

// expect, that the pointer to the start of the data is advanced to the last element (size - 1) ...
EXPECT_CALL(pointer_spy, PlusAssign(static_cast<std::ptrdiff_t>(kNonZeroNumberElements - 1)));
// ... and two pointer dereferences take place
EXPECT_CALL(pointer_spy, Arrow(_)).Times(2).WillRepeatedly(testing::Invoke([&arrow_args](ElementType* ptr) {
// capturing which addresses are dereferenced.
arrow_args.push_back(ptr);
}));

// when calling data() on the NonRelocatableVector
data_ptr = unit_->data();
EXPECT_NE(data_ptr, nullptr);
}
// Guard destructor clears the spy - destruction proceeds with normal pointer behavior!

// Then two pointer-dereferences happened
ASSERT_EQ(arrow_args.size(), 2U);
// where the 1st deref comes from GetLastElement (pointer advanced to last element)
EXPECT_EQ(arrow_args[0], data_ptr + (kNonZeroNumberElements - 1));
// and the 2nd deref call comes from GetFirstElement (pointer to first element)
EXPECT_EQ(arrow_args[1], data_ptr);
}

TEST_F(NonRelocatableVectorPointerInteractionFixture, BeginDereferencesBeginAndEnd)
{
// Given a NonRelocatableVector which has been filled with elements
GivenANonRelocatableVectorContainingNumberOfElements(kNonZeroNumberElements);

// and a registered pointer-spy mock,
testing::StrictMock<test::PointerSpyMock<ElementType>> pointer_spy;
std::vector<ElementType*> arrow_args;

ElementType* begin_it = nullptr;
{
test::MockablePointerMockGuard<ElementType> guard{&pointer_spy};

// expect, that the pointer to the start of the data is advanced to the last element (size - 1) ...
EXPECT_CALL(pointer_spy, PlusAssign(static_cast<std::ptrdiff_t>(kNonZeroNumberElements - 1)));
// ... and two pointer dereferences take place
EXPECT_CALL(pointer_spy, Arrow(_)).Times(2).WillRepeatedly(testing::Invoke([&arrow_args](ElementType* ptr) {
// capturing which addresses are dereferenced.
arrow_args.push_back(ptr);
}));

// when calling begin() on the NonRelocatableVector
begin_it = unit_->begin();
EXPECT_NE(begin_it, nullptr);
}
// Guard destructor clears the spy - destruction proceeds with normal pointer behavior!

// Then two pointer-dereferences happened
ASSERT_EQ(arrow_args.size(), 2U);
// where the 1st deref comes from GetLastElement (pointer advanced to last element)
EXPECT_EQ(arrow_args[0], begin_it + (kNonZeroNumberElements - 1));
// and the 2nd deref call comes from GetFirstElement (pointer to first element)
EXPECT_EQ(arrow_args[1], begin_it);
}

TEST_F(NonRelocatableVectorPointerInteractionFixture, EndDereferencesBeginAndEnd)
{
// Given a NonRelocatableVector which has been filled with elements
GivenANonRelocatableVectorContainingNumberOfElements(kNonZeroNumberElements);

// and a registered pointer-spy mock,
testing::StrictMock<test::PointerSpyMock<ElementType>> pointer_spy;
std::vector<ElementType*> arrow_args;

ElementType* end_it = nullptr;
{
test::MockablePointerMockGuard<ElementType> guard{&pointer_spy};

// expect, that the pointer to the start of the data is advanced to the last element (size - 1) ...
EXPECT_CALL(pointer_spy, PlusAssign(static_cast<std::ptrdiff_t>(kNonZeroNumberElements - 1)));
// ... and two pointer dereferences take place
EXPECT_CALL(pointer_spy, Arrow(_)).Times(2).WillRepeatedly(testing::Invoke([&arrow_args](ElementType* ptr) {
// capturing which addresses are dereferenced.
arrow_args.push_back(ptr);
}));

// when calling end() on the NonRelocatableVector
end_it = unit_->end();
EXPECT_NE(end_it, nullptr);
}
// Guard destructor clears the spy - destruction proceeds with normal pointer behavior!

// Then two pointer-dereferences happened
ASSERT_EQ(arrow_args.size(), 2U);
// where the 1st deref comes from GetFirstElement (pointer to first element)
EXPECT_EQ(arrow_args[0], end_it - kNonZeroNumberElements);
// and the 2nd deref call comes from GetLastElement (pointer advanced to last element)
EXPECT_EQ(arrow_args[1], end_it - 1);
}

} // namespace score::containers
18 changes: 13 additions & 5 deletions score/containers/test/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# *******************************************************************************

Check failure on line 1 in score/containers/test/BUILD

View workflow job for this annotation

GitHub Actions / Restricted file changes

score/containers/test/BUILD is in a restricted path
# Copyright (c) 2025 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
Expand All @@ -17,18 +17,26 @@
load("@score_baselibs//score/language/safecpp:toolchain_features.bzl", "COMPILER_WARNING_FEATURES")

cc_library(
name = "custom_allocator_mock",
name = "mockable_allocator",
testonly = True,
srcs = ["custom_allocator_mock.cpp"],
hdrs = ["custom_allocator_mock.h"],
srcs = [
"mockable_allocator.cpp",
"mockable_pointer.cpp",
"mockable_pointer_mock_guard.cpp",
"pointer_spy_mock.cpp",
],
hdrs = [
"mockable_allocator.h",
"mockable_pointer.h",
"mockable_pointer_mock_guard.h",
"pointer_spy_mock.h",
],
features = COMPILER_WARNING_FEATURES,
visibility = [
"@score_baselibs//score/containers:__subpackages__",
],
deps = [
"@googletest//:gtest_main",
"@score_baselibs//score/language/futurecpp",
"@score_baselibs//score/memory/shared:offset_ptr",
],
)

Expand Down
86 changes: 0 additions & 86 deletions score/containers/test/custom_allocator_mock.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/********************************************************************************

Check failure on line 1 in score/containers/test/mockable_allocator.cpp

View workflow job for this annotation

GitHub Actions / Restricted file changes

score/containers/test/mockable_allocator.cpp is in a restricted path
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
Expand All @@ -10,4 +10,4 @@
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include "score/containers/test/custom_allocator_mock.h"
#include "score/containers/test/mockable_allocator.h"
Loading
Loading