Blender V5.3
bake_items_serialize_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
6
8#include "BKE_geometry_set.hh"
9#include "BKE_gtest_base.hh"
10#include "BKE_node.hh"
11
14
15#include <sstream>
16#include <string>
17
19
21
22static std::optional<BakeValues> roundtrip_bake_values(const BakeValues &bake_values)
23{
24 MemoryBlobWriter blob_writer{"test"};
25 BlobWriteSharing blob_write_sharing;
26 std::ostringstream stream;
27 serialize_bake(bake_values, blob_writer, blob_write_sharing, stream);
28
30 for (const auto &item : blob_writer.get_stream_by_name().items()) {
31 blobs.add_new(item.key, item.value.stream->str());
32 }
33
34 MemoryBlobReader blob_reader;
35 for (auto item : blobs.items()) {
36 std::string &blob = item.value;
37 blob_reader.add(item.key,
38 Span<std::byte>(reinterpret_cast<std::byte *>(blob.data()), blob.size()));
39 }
40
41 BlobReadSharing blob_read_sharing;
42 std::istringstream read_stream{stream.str()};
43 return deserialize_bake(read_stream, blob_reader, blob_read_sharing);
44}
45
46TEST_F(BakeItemsSerializeTest, nested_bundle_with_geometry_and_bundle_lists)
47{
49 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
50
51 Vector<GeometrySet> geometries;
52 geometries.append(GeometrySet());
53 geometries.append(GeometrySet());
54 bundle.add(*nodes::BundleKey::from_str("geometries"),
58
59 Vector<std::string> strings;
60 strings.append("cloth");
61 strings.append("simulation");
62 bundle.add(*nodes::BundleKey::from_str("strings"),
66
67 Vector<nodes::BundlePtr> child_bundles;
68 child_bundles.append(nodes::Bundle::create());
69 child_bundles.append(nodes::Bundle::create());
70 bundle.add(*nodes::BundleKey::from_str("bundles"),
73 std::move(child_bundles)))});
74
76 items.add_new(0, BakeValues::Item{SocketValueVariant::from(std::move(bundle_ptr))});
77
78 const std::optional<BakeValues> bake_values = roundtrip_bake_values(
79 BakeValues(std::move(items)));
80 ASSERT_TRUE(bake_values);
81 const BakeValues::Item *item = bake_values->values_by_id().lookup_ptr(0);
82 ASSERT_NE(item, nullptr);
83
84 nodes::BundlePtr restored_bundle = *item->value.get_if<nodes::BundlePtr>();
85 ASSERT_TRUE(restored_bundle);
86
87 std::optional<nodes::GListPtr> restored_geometries = restored_bundle->lookup<nodes::GListPtr>(
88 *nodes::BundleKey::from_str("geometries"));
89 ASSERT_TRUE(restored_geometries);
90 ASSERT_TRUE(*restored_geometries);
91 EXPECT_TRUE((*restored_geometries)->cpp_type().is<GeometrySet>());
92 EXPECT_EQ((*restored_geometries)->size(), 2);
93
94 std::optional<nodes::GListPtr> restored_strings = restored_bundle->lookup<nodes::GListPtr>(
95 *nodes::BundleKey::from_str("strings"));
96 ASSERT_TRUE(restored_strings);
97 ASSERT_TRUE(*restored_strings);
98 EXPECT_TRUE((*restored_strings)->cpp_type().is<std::string>());
99 EXPECT_EQ((*restored_strings)->size(), 2);
100
101 std::optional<nodes::GListPtr> restored_bundles = restored_bundle->lookup<nodes::GListPtr>(
102 *nodes::BundleKey::from_str("bundles"));
103 ASSERT_TRUE(restored_bundles);
104 ASSERT_TRUE(*restored_bundles);
105 EXPECT_TRUE((*restored_bundles)->cpp_type().is<nodes::BundlePtr>());
106 EXPECT_EQ((*restored_bundles)->size(), 2);
107}
108
109} // namespace blender::bke::bake::tests
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
ItemIterator items() const &
Definition BLI_map.hh:902
void append(const T &value)
static SocketValueVariant from(T &&value)
void add(StringRef name, Span< std::byte > blob)
const Map< std::string, OutputStream > & get_stream_by_name() const
static std::optional< BundleKey > from_str(StringRef key)
bool add(BundleKey key, const BundleItemValue &value)
const BundleItemValue * lookup(BundleKey key) const
static GListPtr from_container(ContainerT &&container)
#define EXPECT_TRUE(result)
TEST_F(BakeItemsSerializeTest, nested_bundle_with_geometry_and_bundle_lists)
static std::optional< BakeValues > roundtrip_bake_values(const BakeValues &bake_values)
void serialize_bake(const BakeValues &bake_values, BlobWriter &blob_writer, BlobWriteSharing &blob_sharing, std::ostream &r_stream)
std::optional< BakeValues > deserialize_bake(std::istream &stream, const BlobReader &blob_reader, const BlobReadSharing &blob_sharing)
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
ImplicitSharingPtr< Bundle > BundlePtr
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)