1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/* Apache License, Version 2.0 */
#include "testing/testing.h"
#include "FN_cpp_type.hh"
#include "FN_field.hh"
#include "FN_multi_function_builder.hh"
namespace blender::fn::tests {
TEST(field, ConstantFunction)
{
Field constant_field = Field(CPPType::get<int>(),
std::make_shared<FieldFunction>(FieldFunction(
std::make_unique<CustomMF_Constant<int>>(10), {})),
0);
Array<int> result(4);
GMutableSpan result_generic(result.as_mutable_span());
evaluate_fields({constant_field}, IndexMask(IndexRange(4)), {result_generic});
EXPECT_EQ(result[0], 10);
EXPECT_EQ(result[1], 10);
EXPECT_EQ(result[2], 10);
EXPECT_EQ(result[3], 10);
}
class IndexFieldInput final : public FieldInput {
GVArrayPtr retrieve_data(IndexMask mask) const final
{
auto index_func = [](int i) { return i; };
return std::make_unique<
GVArray_For_EmbeddedVArray<int, VArray_For_Func<int, decltype(index_func)>>>(
mask.min_array_size(), mask.min_array_size(), index_func);
}
};
TEST(field, VArrayInput)
{
Field index_field = Field(CPPType::get<int>(), std::make_shared<IndexFieldInput>(), "Index");
Array<int> result_1(4);
GMutableSpan result_generic_1(result_1.as_mutable_span());
evaluate_fields({index_field}, IndexMask(IndexRange(4)), {result_generic_1});
EXPECT_EQ(result_1[0], 0);
EXPECT_EQ(result_1[1], 1);
EXPECT_EQ(result_1[2], 2);
EXPECT_EQ(result_1[3], 3);
/* Evaluate a second time, just to test that the first didn't break anything. */
Array<int> result_2(10);
GMutableSpan result_generic_2(result_2.as_mutable_span());
evaluate_fields({index_field}, {2, 4, 6, 8}, {result_generic_2});
EXPECT_EQ(result_2[2], 2);
EXPECT_EQ(result_2[4], 4);
EXPECT_EQ(result_2[6], 6);
EXPECT_EQ(result_2[8], 8);
}
TEST(field, VArrayInputMultipleOutputs)
{
std::shared_ptr<FieldInput> index_input = std::make_shared<IndexFieldInput>();
Field field_1 = Field(CPPType::get<int>(), index_input, "Index");
Field field_2 = Field(CPPType::get<int>(), index_input, "Index");
Array<int> result_1(10);
Array<int> result_2(10);
GMutableSpan result_generic_1(result_1.as_mutable_span());
GMutableSpan result_generic_2(result_2.as_mutable_span());
evaluate_fields({field_1, field_2}, {2, 4, 6, 8}, {result_generic_1, result_generic_2});
EXPECT_EQ(result_1[2], 2);
EXPECT_EQ(result_1[4], 4);
EXPECT_EQ(result_1[6], 6);
EXPECT_EQ(result_1[8], 8);
EXPECT_EQ(result_2[2], 2);
EXPECT_EQ(result_2[4], 4);
EXPECT_EQ(result_2[6], 6);
EXPECT_EQ(result_2[8], 8);
}
TEST(field, InputAndFunction)
{
Field index_field = Field(CPPType::get<int>(), std::make_shared<IndexFieldInput>(), "Index");
Field output_field = Field(CPPType::get<int>(),
std::make_shared<FieldFunction>(
FieldFunction(std::make_unique<CustomMF_SI_SI_SO<int, int, int>>(
"add", [](int a, int b) { return a + b; }),
{index_field, index_field})),
0);
std::shared_ptr<FieldInput> index_input = std::make_shared<IndexFieldInput>();
Field field_1 = Field(CPPType::get<int>(), index_input);
Field field_2 = Field(CPPType::get<int>(), index_input);
Array<int> result(10);
GMutableSpan result_generic(result.as_mutable_span());
evaluate_fields({output_field}, {2, 4, 6, 8}, {result_generic});
EXPECT_EQ(result[2], 4);
EXPECT_EQ(result[4], 8);
EXPECT_EQ(result[6], 12);
EXPECT_EQ(result[8], 16);
}
} // namespace blender::fn::tests
|