Add a unit test for MixEffect.
[movit] / mix_effect_test.cpp
1 // Unit tests for MixEffect.
2
3 #include "test_util.h"
4 #include "gtest/gtest.h"
5 #include "mix_effect.h"
6
7 TEST(MixEffectTest, FiftyFiftyMix) {
8         float data_a[] = {
9                 0.0f, 0.25f,
10                 0.75f, 1.0f,
11         };
12         float data_b[] = {
13                 1.0f, 0.5f,
14                 0.75f, 0.6f,
15         };
16         float expected_data[] = {
17                 0.5f, 0.375f,
18                 0.75f, 0.8f,
19         };
20         float out_data[4];
21         EffectChainTester tester(data_a, 2, 2, COLORSPACE_sRGB, GAMMA_LINEAR);
22         Effect *input1 = tester.get_chain()->last_added_effect();
23         Effect *input2 = tester.add_input(data_b, COLORSPACE_sRGB, GAMMA_LINEAR);
24
25         Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
26         ASSERT_TRUE(mix_effect->set_float("strength_first", 0.5f));
27         ASSERT_TRUE(mix_effect->set_float("strength_second", 0.5f));
28         tester.run(out_data, COLORSPACE_sRGB, GAMMA_LINEAR);
29
30         expect_equal(expected_data, out_data, 2, 2);
31 }
32
33 TEST(MixEffectTest, OnlyA) {
34         float data_a[] = {
35                 0.0f, 0.25f,
36                 0.75f, 1.0f,
37         };
38         float data_b[] = {
39                 1.0f, 0.5f,
40                 0.75f, 0.6f,
41         };
42         float out_data[4];
43         EffectChainTester tester(data_a, 2, 2, COLORSPACE_sRGB, GAMMA_LINEAR);
44         Effect *input1 = tester.get_chain()->last_added_effect();
45         Effect *input2 = tester.add_input(data_b, COLORSPACE_sRGB, GAMMA_LINEAR);
46
47         Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
48         ASSERT_TRUE(mix_effect->set_float("strength_first", 1.0f));
49         ASSERT_TRUE(mix_effect->set_float("strength_second", 0.0f));
50         tester.run(out_data, COLORSPACE_sRGB, GAMMA_LINEAR);
51
52         expect_equal(data_a, out_data, 2, 2);
53 }
54
55 TEST(MixEffectTest, DoesNotSumToOne) {
56         float data_a[] = {
57                 1.0f, 0.5f,
58                 0.75f, 1.0f,
59         };
60         float data_b[] = {
61                 1.0f, 0.25f,
62                 0.15f, 0.6f,
63         };
64         float expected_data[] = {
65                 0.0f, 0.25f,
66                 0.6f, 0.4f,
67         };
68         float out_data[4];
69         EffectChainTester tester(data_a, 2, 2, COLORSPACE_sRGB, GAMMA_LINEAR);
70         Effect *input1 = tester.get_chain()->last_added_effect();
71         Effect *input2 = tester.add_input(data_b, COLORSPACE_sRGB, GAMMA_LINEAR);
72
73         Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
74         ASSERT_TRUE(mix_effect->set_float("strength_first", 1.0f));
75         ASSERT_TRUE(mix_effect->set_float("strength_second", -1.0f));
76         tester.run(out_data, COLORSPACE_sRGB, GAMMA_LINEAR);
77
78         expect_equal(expected_data, out_data, 2, 2);
79 }
80
81 TEST(MixEffectTest, MixesLinearlyDespitesRGBInputsAndOutputs) {
82         float data_a[] = {
83                 0.0f, 0.25f,
84                 0.75f, 1.0f,
85         };
86         float data_b[] = {
87                 0.0f, 0.0f,
88                 0.0f, 0.0f,
89         };
90         float expected_data[] = {  // sRGB(0.5 * inv_sRGB(a)).
91                 0.00000f, 0.17349f,
92                 0.54807f, 0.73536f,
93         };
94         float out_data[4];
95         EffectChainTester tester(data_a, 2, 2, COLORSPACE_sRGB, GAMMA_sRGB);
96         Effect *input1 = tester.get_chain()->last_added_effect();
97         Effect *input2 = tester.add_input(data_b, COLORSPACE_sRGB, GAMMA_sRGB);
98
99         Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
100         ASSERT_TRUE(mix_effect->set_float("strength_first", 0.5f));
101         ASSERT_TRUE(mix_effect->set_float("strength_second", 0.5f));
102         tester.run(out_data, COLORSPACE_sRGB, GAMMA_sRGB);
103
104         expect_equal(expected_data, out_data, 2, 2);
105 }