Files
test/source/blender/blenlib/tests/BLI_array_state_test.cc
Bastien Montagne dd168a35c5 Refactor: Replace MEM_cnew with a type-aware template version of MEM_callocN.
The general idea is to keep the 'old', C-style MEM_callocN signature, and slowly
replace most of its usages with the new, C++-style type-safer template version.

* `MEM_cnew<T>` allocation version is renamed to `MEM_callocN<T>`.
* `MEM_cnew_array<T>` allocation version is renamed to `MEM_calloc_arrayN<T>`.
* `MEM_cnew<T>` duplicate version is renamed to `MEM_dupallocN<T>`.

Similar templates type-safe version of `MEM_mallocN` will be added soon
as well.

Following discussions in !134452.

NOTE: For now static type checking in `MEM_callocN` and related are slightly
different for Windows MSVC. This compiler seems to consider structs using the
`DNA_DEFINE_CXX_METHODS` macro as non-trivial (likely because their default
copy constructors are deleted). So using checks on trivially
constructible/destructible instead on this compiler/system.

Pull Request: https://projects.blender.org/blender/blender/pulls/134771
2025-03-05 16:35:09 +01:00

62 lines
1.9 KiB
C++

/* SPDX-FileCopyrightText: 2024 Blender Authors
*
* SPDX-License-Identifier: Apache-2.0 */
#include "testing/testing.h"
#include "BLI_array_state.hh"
namespace blender::tests {
TEST(array_state, Empty)
{
ArrayState<int> state{};
EXPECT_TRUE(state.is_empty());
EXPECT_TRUE(state.same_as({}, nullptr));
EXPECT_FALSE(state.same_as(VArray<int>::ForSpan({3, 4}), nullptr));
}
TEST(array_state, NoSharing)
{
ArrayState<int> state{VArray<int>::ForSpan({1, 2, 3}), nullptr};
EXPECT_FALSE(state.is_empty());
EXPECT_TRUE(state.same_as(VArray<int>::ForSpan({1, 2, 3}), nullptr));
EXPECT_FALSE(state.same_as(VArray<int>::ForSpan({1, 2, 4}), nullptr));
EXPECT_FALSE(state.same_as(VArray<int>::ForSpan({1, 2, 3, 4}), nullptr));
}
TEST(array_state, WithSharing)
{
int *data = MEM_calloc_arrayN<int>(3, __func__);
data[0] = 0;
data[1] = 10;
data[2] = 20;
ImplicitSharingPtr sharing_info{implicit_sharing::info_for_mem_free(data)};
ArrayState<int> state{VArray<int>::ForSpan({data, 3}), sharing_info.get()};
EXPECT_FALSE(state.is_empty());
EXPECT_TRUE(state.same_as(VArray<int>::ForSpan({data, 3}), sharing_info.get()));
EXPECT_TRUE(state.same_as(VArray<int>::ForSpan({0, 10, 20}), nullptr));
EXPECT_FALSE(state.same_as(VArray<int>::ForSpan({0, 1, 2}), nullptr));
}
TEST(array_state, DifferentSharingInfoButSameData)
{
int *data1 = MEM_calloc_arrayN<int>(3, __func__);
data1[0] = 0;
data1[1] = 10;
data1[2] = 20;
ImplicitSharingPtr sharing_info1{implicit_sharing::info_for_mem_free(data1)};
int *data2 = MEM_calloc_arrayN<int>(3, __func__);
data2[0] = 0;
data2[1] = 10;
data2[2] = 20;
ImplicitSharingPtr sharing_info2{implicit_sharing::info_for_mem_free(data2)};
ArrayState<int> state{VArray<int>::ForSpan({data1, 3}), sharing_info1.get()};
EXPECT_TRUE(state.same_as(VArray<int>::ForSpan({data2, 3}), sharing_info2.get()));
}
} // namespace blender::tests