From ff820496422da1b71352e6e55d68541fc6d6e2b3 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sat, 27 Jun 2020 20:58:24 +0200 Subject: Functions: add MutableAttributesRef data structure This will be used to reference the content of a CustomData structure in C++ code, that does not need to know who owns the data but only works with it. --- tests/gtests/functions/CMakeLists.txt | 1 + tests/gtests/functions/FN_attributes_ref_test.cc | 114 +++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tests/gtests/functions/FN_attributes_ref_test.cc (limited to 'tests') diff --git a/tests/gtests/functions/CMakeLists.txt b/tests/gtests/functions/CMakeLists.txt index e52a616bf61..1246bbd5599 100644 --- a/tests/gtests/functions/CMakeLists.txt +++ b/tests/gtests/functions/CMakeLists.txt @@ -37,6 +37,7 @@ if(WITH_BUILDINFO) endif() BLENDER_TEST(FN_array_spans "bf_blenlib;bf_functions;${BUILDINFO}") +BLENDER_TEST(FN_attributes_ref "bf_blenlib;bf_functions;${BUILDINFO}") BLENDER_TEST(FN_cpp_type "bf_blenlib;bf_functions;${BUILDINFO}") BLENDER_TEST(FN_generic_vector_array "bf_blenlib;bf_functions;${BUILDINFO}") BLENDER_TEST(FN_multi_function "bf_blenlib;bf_functions;${BUILDINFO}") diff --git a/tests/gtests/functions/FN_attributes_ref_test.cc b/tests/gtests/functions/FN_attributes_ref_test.cc new file mode 100644 index 00000000000..111190564e5 --- /dev/null +++ b/tests/gtests/functions/FN_attributes_ref_test.cc @@ -0,0 +1,114 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "BLI_float3.hh" +#include "FN_attributes_ref.hh" +#include "FN_cpp_types.hh" + +#include "testing/testing.h" + +namespace blender { +namespace fn { + +TEST(attributes_info, BuildEmpty) +{ + AttributesInfoBuilder info_builder; + AttributesInfo info{info_builder}; + + EXPECT_EQ(info.size(), 0); +} + +TEST(attributes_info, AddSameNameTwice) +{ + AttributesInfoBuilder info_builder; + info_builder.add("A", 4); + info_builder.add("A", 5); + AttributesInfo info{info_builder}; + EXPECT_EQ(info.size(), 1); + EXPECT_TRUE(info.has_attribute("A", CPPType::get())); + EXPECT_FALSE(info.has_attribute("B", CPPType::get())); + EXPECT_FALSE(info.has_attribute("A", CPPType::get())); + EXPECT_EQ(info.default_of("A"), 4); + EXPECT_EQ(info.name_of(0), "A"); + EXPECT_EQ(info.index_range().start(), 0); + EXPECT_EQ(info.index_range().one_after_last(), 1); +} + +TEST(attributes_info, BuildWithDefaultString) +{ + AttributesInfoBuilder info_builder; + info_builder.add("A", CPPType::get()); + AttributesInfo info{info_builder}; + EXPECT_EQ(info.default_of("A"), ""); +} + +TEST(attributes_info, BuildWithGivenDefault) +{ + AttributesInfoBuilder info_builder; + info_builder.add("A", "hello world"); + AttributesInfo info{info_builder}; + const void *default_value = info.default_of("A"); + EXPECT_EQ(*(const std::string *)default_value, "hello world"); + EXPECT_EQ(info.type_of("A"), CPPType::get()); +} + +TEST(mutable_attributes_ref, ComplexTest) +{ + AttributesInfoBuilder info_builder; + info_builder.add("Position", {0, 0, 10}); + info_builder.add("ID", 0); + info_builder.add("Size", 0.5f); + info_builder.add("Name", ""); + AttributesInfo info{info_builder}; + + uint amount = 5; + Array positions(amount); + Array ids(amount, 0); + Array sizes(amount); + Array names(amount); + + Array buffers = {positions.data(), ids.data(), sizes.data(), names.data()}; + MutableAttributesRef attributes{info, buffers, IndexRange(1, 3)}; + EXPECT_EQ(attributes.size(), 3); + EXPECT_EQ(attributes.info().size(), 4); + EXPECT_EQ(attributes.get("Position").buffer(), positions.data() + 1); + EXPECT_EQ(attributes.get("ID").buffer(), ids.data() + 1); + EXPECT_EQ(attributes.get("Size").buffer(), sizes.data() + 1); + EXPECT_EQ(attributes.get("Name").buffer(), names.data() + 1); + + EXPECT_EQ(attributes.get("ID").size(), 3); + EXPECT_EQ(attributes.get("ID").size(), 3); + + EXPECT_EQ(ids[2], 0); + MutableSpan ids_span = attributes.get("ID"); + ids_span[1] = 42; + EXPECT_EQ(ids[2], 42); + + EXPECT_FALSE(attributes.try_get("not existant").has_value()); + EXPECT_FALSE(attributes.try_get("Position").has_value()); + EXPECT_TRUE(attributes.try_get("Position").has_value()); + EXPECT_FALSE(attributes.try_get("not existant", CPPType::get()).has_value()); + EXPECT_FALSE(attributes.try_get("Position", CPPType::get()).has_value()); + EXPECT_TRUE(attributes.try_get("Position", CPPType::get()).has_value()); + + MutableAttributesRef sliced = attributes.slice(IndexRange(1, 2)); + EXPECT_EQ(sliced.size(), 2); + sliced.get("ID")[0] = 100; + EXPECT_EQ(ids[2], 100); +} + +} // namespace fn +} // namespace blender -- cgit v1.2.3