Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_NodeOperation_test.cc « tests « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 94e9fdeedb121184284a594cc8b1363fac01aaf1 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * 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.
 *
 * Copyright 2021, Blender Foundation.
 */

#include "testing/testing.h"

#include "COM_ConstantOperation.h"

namespace blender::compositor::tests {

class NonHashedOperation : public NodeOperation {
 public:
  NonHashedOperation(int id)
  {
    set_id(id);
    addOutputSocket(DataType::Value);
    setWidth(2);
    setHeight(3);
  }
};

class NonHashedConstantOperation : public ConstantOperation {
  float constant_;

 public:
  NonHashedConstantOperation(int id)
  {
    set_id(id);
    addOutputSocket(DataType::Value);
    setWidth(2);
    setHeight(3);
    constant_ = 1.0f;
  }

  const float *get_constant_elem() override
  {
    return &constant_;
  }

  void set_constant(float value)
  {
    constant_ = value;
  }
};

class HashedOperation : public NodeOperation {
 private:
  int param1;
  float param2;

 public:
  HashedOperation(NodeOperation &input, int width, int height)
  {
    addInputSocket(DataType::Value);
    addOutputSocket(DataType::Color);
    setWidth(width);
    setHeight(height);
    param1 = 2;
    param2 = 7.0f;

    getInputSocket(0)->setLink(input.getOutputSocket());
  }

  void set_param1(int value)
  {
    param1 = value;
  }

  void hash_output_params() override
  {
    hash_params(param1, param2);
  }
};

static void test_non_equal_hashes_compare(NodeOperationHash &h1,
                                          NodeOperationHash &h2,
                                          NodeOperationHash &h3)
{
  if (h1 < h2) {
    if (h3 < h1) {
      EXPECT_TRUE(h3 < h2);
    }
    else if (h3 < h2) {
      EXPECT_TRUE(h1 < h3);
    }
    else {
      EXPECT_TRUE(h1 < h3);
      EXPECT_TRUE(h2 < h3);
    }
  }
  else {
    EXPECT_TRUE(h2 < h1);
  }
}

TEST(NodeOperation, generate_hash)
{
  /* Constant input. */
  {
    NonHashedConstantOperation input_op1(1);
    input_op1.set_constant(1.0f);
    EXPECT_EQ(input_op1.generate_hash(), std::nullopt);

    HashedOperation op1(input_op1, 6, 4);
    std::optional<NodeOperationHash> hash1_opt = op1.generate_hash();
    EXPECT_NE(hash1_opt, std::nullopt);
    NodeOperationHash hash1 = *hash1_opt;

    NonHashedConstantOperation input_op2(1);
    input_op2.set_constant(1.0f);
    HashedOperation op2(input_op2, 6, 4);
    NodeOperationHash hash2 = *op2.generate_hash();
    EXPECT_EQ(hash1, hash2);

    input_op2.set_constant(3.0f);
    hash2 = *op2.generate_hash();
    EXPECT_NE(hash1, hash2);
  }

  /* Non constant input. */
  {
    NonHashedOperation input_op(1);
    EXPECT_EQ(input_op.generate_hash(), std::nullopt);

    HashedOperation op1(input_op, 6, 4);
    HashedOperation op2(input_op, 6, 4);
    NodeOperationHash hash1 = *op1.generate_hash();
    NodeOperationHash hash2 = *op2.generate_hash();
    EXPECT_EQ(hash1, hash2);
    op1.set_param1(-1);
    hash1 = *op1.generate_hash();
    EXPECT_NE(hash1, hash2);

    HashedOperation op3(input_op, 11, 14);
    NodeOperationHash hash3 = *op3.generate_hash();
    EXPECT_NE(hash2, hash3);
    EXPECT_NE(hash1, hash3);

    test_non_equal_hashes_compare(hash1, hash2, hash3);
    test_non_equal_hashes_compare(hash3, hash2, hash1);
    test_non_equal_hashes_compare(hash2, hash3, hash1);
    test_non_equal_hashes_compare(hash3, hash1, hash2);

    NonHashedOperation input_op2(2);
    HashedOperation op4(input_op2, 11, 14);
    NodeOperationHash hash4 = *op4.generate_hash();
    EXPECT_NE(hash3, hash4);

    input_op2.set_id(1);
    hash4 = *op4.generate_hash();
    EXPECT_EQ(hash3, hash4);
  }
}

}  // namespace blender::compositor::tests