This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/compositor/tests/COM_NodeOperation_test.cc
Campbell Barton c434782e3a File headers: SPDX License migration
Use a shorter/simpler license convention, stops the header taking so
much space.

Follow the SPDX license specification: https://spdx.org/licenses

- C/C++/objc/objc++
- Python
- Shell Scripts
- CMake, GNUmakefile

While most of the source tree has been included

- `./extern/` was left out.
- `./intern/cycles` & `./intern/atomic` are also excluded because they
  use different header conventions.

doc/license/SPDX-license-identifiers.txt has been added to list SPDX all
used identifiers.

See P2788 for the script that automated these edits.

Reviewed By: brecht, mont29, sergey

Ref D14069
2022-02-11 09:14:36 +11:00

155 lines
3.5 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later
* 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);
add_output_socket(DataType::Value);
set_width(2);
set_height(3);
}
};
class NonHashedConstantOperation : public ConstantOperation {
float constant_;
public:
NonHashedConstantOperation(int id)
{
set_id(id);
add_output_socket(DataType::Value);
set_width(2);
set_height(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)
{
add_input_socket(DataType::Value);
add_output_socket(DataType::Color);
set_width(width);
set_height(height);
param1 = 2;
param2 = 7.0f;
get_input_socket(0)->set_link(input.get_output_socket());
}
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