Start on unit testing (adds a dependency on Google Test). Right now, we have a single...
[movit] / effect_chain_test.cpp
1 // Unit tests for EffectChain.
2
3 #include "effect_chain.h"
4 #include "flat_input.h"
5 #include "sandbox_effect.h"
6 #include "opengl.h"
7 #include "gtest/gtest.h"
8
9 #include <stdio.h>
10 #include <math.h>
11
12 #include <algorithm>
13
14 class EffectChainTester {
15 public:
16         EffectChainTester(const float *data, unsigned width, unsigned height, ColorSpace color_space, GammaCurve gamma_curve)
17                 : chain(width, height), width(width), height(height)
18         {
19                 ImageFormat format;
20                 format.color_space = color_space;
21                 format.gamma_curve = gamma_curve;
22         
23                 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
24                 input->set_pixel_data(data);
25                 chain.add_input(input);
26         }
27
28         EffectChain *get_chain() { return &chain; }
29
30         void run(float *out_data, ColorSpace color_space, GammaCurve gamma_curve)
31         {
32                 ImageFormat format;
33                 format.color_space = color_space;
34                 format.gamma_curve = gamma_curve;
35                 chain.add_output(format);
36                 chain.finalize();
37
38                 glViewport(0, 0, width, height);
39                 chain.render_to_screen();
40
41                 glReadPixels(0, 0, width, height, GL_RED, GL_FLOAT, out_data);
42
43                 // Flip upside-down to compensate for different origin.
44                 for (unsigned y = 0; y < height / 2; ++y) {
45                         unsigned flip_y = height - y - 1;
46                         for (unsigned x = 0; x < width; ++x) {
47                                 std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
48                         }
49                 }
50         }
51
52 private:
53         EffectChain chain;
54         unsigned width, height;
55 };
56
57 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height)
58 {
59         float largest_difference = -1.0f;
60         float squared_difference = 0.0f;
61
62         for (unsigned y = 0; y < height; ++y) {
63                 for (unsigned x = 0; x < width; ++x) {
64                         float diff = ref[y * width + x] - result[y * width + x];
65                         largest_difference = std::max(largest_difference, fabsf(diff));
66                         squared_difference += diff * diff;
67                 }
68         }
69
70         const float largest_difference_limit = 1.5 / 255.0;
71         const float rms_limit = 0.5 / 255.0;
72
73         EXPECT_LT(largest_difference, largest_difference_limit);
74
75         float rms = sqrt(squared_difference) / (width * height);
76         EXPECT_LT(rms, rms_limit);
77
78         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
79                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
80
81                 fprintf(stderr, "Reference:\n");
82                 for (unsigned y = 0; y < height; ++y) {
83                         for (unsigned x = 0; x < width; ++x) {
84                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
85                         }
86                         fprintf(stderr, "\n");
87                 }
88
89                 fprintf(stderr, "\nResult:\n");
90                 for (unsigned y = 0; y < height; ++y) {
91                         for (unsigned x = 0; x < width; ++x) {
92                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
93                         }
94                         fprintf(stderr, "\n");
95                 }
96         }
97 }
98
99 TEST(EffectChainTest, Identity) {
100         float data[] = {
101                 0.0f, 0.25f, 0.3f,
102                 0.75f, 1.0f, 1.0f,
103         };
104         float out_data[6];
105         EffectChainTester tester(data, 3, 2, COLORSPACE_sRGB, GAMMA_LINEAR);
106         tester.run(out_data, COLORSPACE_sRGB, GAMMA_LINEAR);
107
108         expect_equal(data, out_data, 3, 2);
109 }