xref: /aosp_15_r20/external/libjpeg-turbo/gtest/tjbench-gtest-wrapper.cpp (revision dfc6aa5c1cfd4bc4e2018dc74aa96e29ee49c6da)
1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker  * Copyright 2020 The Chromium Authors. All Rights Reserved.
3*dfc6aa5cSAndroid Build Coastguard Worker  *
4*dfc6aa5cSAndroid Build Coastguard Worker  * This software is provided 'as-is', without any express or implied
5*dfc6aa5cSAndroid Build Coastguard Worker  * warranty.  In no event will the authors be held liable for any damages
6*dfc6aa5cSAndroid Build Coastguard Worker  * arising from the use of this software.
7*dfc6aa5cSAndroid Build Coastguard Worker  *
8*dfc6aa5cSAndroid Build Coastguard Worker  * Permission is granted to anyone to use this software for any purpose,
9*dfc6aa5cSAndroid Build Coastguard Worker  * including commercial applications, and to alter it and redistribute it
10*dfc6aa5cSAndroid Build Coastguard Worker  * freely, subject to the following restrictions:
11*dfc6aa5cSAndroid Build Coastguard Worker  *
12*dfc6aa5cSAndroid Build Coastguard Worker  * 1. The origin of this software must not be misrepresented; you must not
13*dfc6aa5cSAndroid Build Coastguard Worker  *    claim that you wrote the original software. If you use this software
14*dfc6aa5cSAndroid Build Coastguard Worker  *    in a product, an acknowledgment in the product documentation would be
15*dfc6aa5cSAndroid Build Coastguard Worker  *    appreciated but is not required.
16*dfc6aa5cSAndroid Build Coastguard Worker  * 2. Altered source versions must be plainly marked as such, and must not be
17*dfc6aa5cSAndroid Build Coastguard Worker  *    misrepresented as being the original software.
18*dfc6aa5cSAndroid Build Coastguard Worker  * 3. This notice may not be removed or altered from any source distribution.
19*dfc6aa5cSAndroid Build Coastguard Worker  */
20*dfc6aa5cSAndroid Build Coastguard Worker 
21*dfc6aa5cSAndroid Build Coastguard Worker #include "base/files/file.h"
22*dfc6aa5cSAndroid Build Coastguard Worker #include "base/files/file_util.h"
23*dfc6aa5cSAndroid Build Coastguard Worker #include "base/path_service.h"
24*dfc6aa5cSAndroid Build Coastguard Worker #include "gtest-utils.h"
25*dfc6aa5cSAndroid Build Coastguard Worker 
26*dfc6aa5cSAndroid Build Coastguard Worker #include <gtest/gtest.h>
27*dfc6aa5cSAndroid Build Coastguard Worker #include <string>
28*dfc6aa5cSAndroid Build Coastguard Worker 
29*dfc6aa5cSAndroid Build Coastguard Worker extern "C" int tjbench(int argc, char *argv[]);
30*dfc6aa5cSAndroid Build Coastguard Worker 
31*dfc6aa5cSAndroid Build Coastguard Worker // Test image files and their expected MD5 sums.
32*dfc6aa5cSAndroid Build Coastguard Worker const static std::vector<std::pair<const std::string,
33*dfc6aa5cSAndroid Build Coastguard Worker                                    const std::string>> IMAGE_MD5_BASELINE = {
34*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_GRAY_Q95_8x8.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
35*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_420_Q95_8x8.ppm", "847fceab15c5b7b911cb986cf0f71de3" },
36*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_422_Q95_8x8.ppm", "d83dacd9fc73b0a6f10c09acad64eb1e" },
37*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_444_Q95_8x8.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" },
38*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_GRAY_Q95_16x16.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
39*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_420_Q95_16x16.ppm", "ca45552a93687e078f7137cc4126a7b0" },
40*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_422_Q95_16x16.ppm", "35077fb610d72dd743b1eb0cbcfe10fb" },
41*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_444_Q95_16x16.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" },
42*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_GRAY_Q95_32x32.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
43*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_420_Q95_32x32.ppm", "d8676f1d6b68df358353bba9844f4a00" },
44*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_422_Q95_32x32.ppm", "e6902ed8a449ecc0f0d6f2bf945f65f7" },
45*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_444_Q95_32x32.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" },
46*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_GRAY_Q95_64x64.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
47*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_420_Q95_64x64.ppm", "4e4c1a3d7ea4bace4f868bcbe83b7050" },
48*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_422_Q95_64x64.ppm", "2b4502a8f316cedbde1da7bce3d2231e" },
49*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_444_Q95_64x64.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" },
50*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_GRAY_Q95_128x128.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
51*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_420_Q95_128x128.ppm", "f24c3429c52265832beab9df72a0ceae" },
52*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_422_Q95_128x128.ppm", "f0b5617d578f5e13c8eee215d64d4877" },
53*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tile_444_Q95_128x128.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" }
54*dfc6aa5cSAndroid Build Coastguard Worker };
55*dfc6aa5cSAndroid Build Coastguard Worker 
56*dfc6aa5cSAndroid Build Coastguard Worker class TJBenchTest : public
57*dfc6aa5cSAndroid Build Coastguard Worker   ::testing::TestWithParam<std::pair<const std::string, const std::string>> {
58*dfc6aa5cSAndroid Build Coastguard Worker 
59*dfc6aa5cSAndroid Build Coastguard Worker  protected:
60*dfc6aa5cSAndroid Build Coastguard Worker 
SetUpTestSuite()61*dfc6aa5cSAndroid Build Coastguard Worker   static void SetUpTestSuite() {
62*dfc6aa5cSAndroid Build Coastguard Worker     base::FilePath resource_path;
63*dfc6aa5cSAndroid Build Coastguard Worker     ASSERT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &resource_path));
64*dfc6aa5cSAndroid Build Coastguard Worker     resource_path = resource_path.AppendASCII("third_party");
65*dfc6aa5cSAndroid Build Coastguard Worker     resource_path = resource_path.AppendASCII("libjpeg_turbo");
66*dfc6aa5cSAndroid Build Coastguard Worker     resource_path = resource_path.AppendASCII("testimages");
67*dfc6aa5cSAndroid Build Coastguard Worker     resource_path = resource_path.AppendASCII("testorig.ppm");
68*dfc6aa5cSAndroid Build Coastguard Worker     ASSERT_TRUE(base::PathExists(resource_path));
69*dfc6aa5cSAndroid Build Coastguard Worker 
70*dfc6aa5cSAndroid Build Coastguard Worker     base::FilePath target_path(GetTargetDirectory());
71*dfc6aa5cSAndroid Build Coastguard Worker     target_path = target_path.AppendASCII("testout_tile.ppm");
72*dfc6aa5cSAndroid Build Coastguard Worker 
73*dfc6aa5cSAndroid Build Coastguard Worker     ASSERT_TRUE(base::CopyFile(resource_path, target_path));
74*dfc6aa5cSAndroid Build Coastguard Worker 
75*dfc6aa5cSAndroid Build Coastguard Worker     std::string prog_name = "tjbench";
76*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg1 = target_path.MaybeAsASCII();
77*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg2 = "95";
78*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg3 = "-rgb";
79*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg4 = "-quiet";
80*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg5 = "-tile";
81*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg6 = "-benchtime";
82*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg7 = "0.01";
83*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg8 = "-warmup";
84*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg9 = "0";
85*dfc6aa5cSAndroid Build Coastguard Worker     char *command_line[] = { &prog_name[0],
86*dfc6aa5cSAndroid Build Coastguard Worker                              &arg1[0], &arg2[0], &arg3[0], &arg4[0], &arg5[0],
87*dfc6aa5cSAndroid Build Coastguard Worker                              &arg6[0], &arg7[0], &arg8[0], &arg9[0],
88*dfc6aa5cSAndroid Build Coastguard Worker                            };
89*dfc6aa5cSAndroid Build Coastguard Worker     // Generate test image tiles.
90*dfc6aa5cSAndroid Build Coastguard Worker     EXPECT_EQ(tjbench(10, command_line), 0);
91*dfc6aa5cSAndroid Build Coastguard Worker   }
92*dfc6aa5cSAndroid Build Coastguard Worker 
93*dfc6aa5cSAndroid Build Coastguard Worker };
94*dfc6aa5cSAndroid Build Coastguard Worker 
TEST_P(TJBenchTest,TestTileBaseline)95*dfc6aa5cSAndroid Build Coastguard Worker TEST_P(TJBenchTest, TestTileBaseline) {
96*dfc6aa5cSAndroid Build Coastguard Worker   // Construct path for test image file.
97*dfc6aa5cSAndroid Build Coastguard Worker   base::FilePath test_image_path(GetTargetDirectory());
98*dfc6aa5cSAndroid Build Coastguard Worker   test_image_path = test_image_path.AppendASCII(std::get<0>(GetParam()));
99*dfc6aa5cSAndroid Build Coastguard Worker   // Read test image as string and compute MD5 sum.
100*dfc6aa5cSAndroid Build Coastguard Worker   std::string test_image_data;
101*dfc6aa5cSAndroid Build Coastguard Worker   ASSERT_TRUE(base::ReadFileToString(test_image_path, &test_image_data));
102*dfc6aa5cSAndroid Build Coastguard Worker   const std::string md5 = base::MD5String(test_image_data);
103*dfc6aa5cSAndroid Build Coastguard Worker   // Compare expected MD5 sum against that of test image.
104*dfc6aa5cSAndroid Build Coastguard Worker   EXPECT_EQ(std::get<1>(GetParam()), md5);
105*dfc6aa5cSAndroid Build Coastguard Worker }
106*dfc6aa5cSAndroid Build Coastguard Worker 
107*dfc6aa5cSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(TestTileBaseline,
108*dfc6aa5cSAndroid Build Coastguard Worker                          TJBenchTest,
109*dfc6aa5cSAndroid Build Coastguard Worker                          ::testing::ValuesIn(IMAGE_MD5_BASELINE));
110*dfc6aa5cSAndroid Build Coastguard Worker 
111*dfc6aa5cSAndroid Build Coastguard Worker // Test image files and their expected MD5 sums.
112*dfc6aa5cSAndroid Build Coastguard Worker const static std::vector<std::pair<const std::string,
113*dfc6aa5cSAndroid Build Coastguard Worker                                    const std::string>> IMAGE_MD5_MERGED = {
114*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_420_Q95_8x8.ppm", "bc25320e1f4c31ce2e610e43e9fd173c" },
115*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_422_Q95_8x8.ppm", "828941d7f41cd6283abd6beffb7fd51d" },
116*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_420_Q95_16x16.ppm", "75ffdf14602258c5c189522af57fa605" },
117*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_422_Q95_16x16.ppm", "e877ae1324c4a280b95376f7f018172f" },
118*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_420_Q95_32x32.ppm", "75ffdf14602258c5c189522af57fa605" },
119*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_422_Q95_32x32.ppm", "e877ae1324c4a280b95376f7f018172f" },
120*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_420_Q95_64x64.ppm", "75ffdf14602258c5c189522af57fa605" },
121*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_422_Q95_64x64.ppm", "e877ae1324c4a280b95376f7f018172f" },
122*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_420_Q95_128x128.ppm", "75ffdf14602258c5c189522af57fa605" },
123*dfc6aa5cSAndroid Build Coastguard Worker   { "testout_tilem_422_Q95_128x128.ppm", "e877ae1324c4a280b95376f7f018172f" }
124*dfc6aa5cSAndroid Build Coastguard Worker };
125*dfc6aa5cSAndroid Build Coastguard Worker 
126*dfc6aa5cSAndroid Build Coastguard Worker class TJBenchTestMerged : public
127*dfc6aa5cSAndroid Build Coastguard Worker   ::testing::TestWithParam<std::pair<const std::string, const std::string>> {
128*dfc6aa5cSAndroid Build Coastguard Worker 
129*dfc6aa5cSAndroid Build Coastguard Worker  protected:
130*dfc6aa5cSAndroid Build Coastguard Worker 
SetUpTestSuite()131*dfc6aa5cSAndroid Build Coastguard Worker   static void SetUpTestSuite() {
132*dfc6aa5cSAndroid Build Coastguard Worker     base::FilePath resource_path;
133*dfc6aa5cSAndroid Build Coastguard Worker     ASSERT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &resource_path));
134*dfc6aa5cSAndroid Build Coastguard Worker     resource_path = resource_path.AppendASCII("third_party");
135*dfc6aa5cSAndroid Build Coastguard Worker     resource_path = resource_path.AppendASCII("libjpeg_turbo");
136*dfc6aa5cSAndroid Build Coastguard Worker     resource_path = resource_path.AppendASCII("testimages");
137*dfc6aa5cSAndroid Build Coastguard Worker     resource_path = resource_path.AppendASCII("testorig.ppm");
138*dfc6aa5cSAndroid Build Coastguard Worker     ASSERT_TRUE(base::PathExists(resource_path));
139*dfc6aa5cSAndroid Build Coastguard Worker 
140*dfc6aa5cSAndroid Build Coastguard Worker     base::FilePath target_path(GetTargetDirectory());
141*dfc6aa5cSAndroid Build Coastguard Worker     target_path = target_path.AppendASCII("testout_tilem.ppm");
142*dfc6aa5cSAndroid Build Coastguard Worker 
143*dfc6aa5cSAndroid Build Coastguard Worker     ASSERT_TRUE(base::CopyFile(resource_path, target_path));
144*dfc6aa5cSAndroid Build Coastguard Worker 
145*dfc6aa5cSAndroid Build Coastguard Worker     std::string prog_name = "tjbench";
146*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg1 = target_path.MaybeAsASCII();
147*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg2 = "95";
148*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg3 = "-rgb";
149*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg4 = "-fastupsample";
150*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg5 = "-quiet";
151*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg6 = "-tile";
152*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg7 = "-benchtime";
153*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg8 = "0.01";
154*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg9 = "-warmup";
155*dfc6aa5cSAndroid Build Coastguard Worker     std::string arg10 = "0";
156*dfc6aa5cSAndroid Build Coastguard Worker     char *command_line[] = { &prog_name[0],
157*dfc6aa5cSAndroid Build Coastguard Worker                              &arg1[0], &arg2[0], &arg3[0], &arg4[0], &arg5[0],
158*dfc6aa5cSAndroid Build Coastguard Worker                              &arg6[0], &arg7[0], &arg8[0], &arg9[0], &arg10[0]
159*dfc6aa5cSAndroid Build Coastguard Worker                            };
160*dfc6aa5cSAndroid Build Coastguard Worker     // Generate test image output tiles.
161*dfc6aa5cSAndroid Build Coastguard Worker     EXPECT_EQ(tjbench(11, command_line), 0);
162*dfc6aa5cSAndroid Build Coastguard Worker   }
163*dfc6aa5cSAndroid Build Coastguard Worker 
164*dfc6aa5cSAndroid Build Coastguard Worker };
165*dfc6aa5cSAndroid Build Coastguard Worker 
TEST_P(TJBenchTestMerged,TestTileMerged)166*dfc6aa5cSAndroid Build Coastguard Worker TEST_P(TJBenchTestMerged, TestTileMerged) {
167*dfc6aa5cSAndroid Build Coastguard Worker   // Construct path for test image file.
168*dfc6aa5cSAndroid Build Coastguard Worker   base::FilePath test_image_path(GetTargetDirectory());
169*dfc6aa5cSAndroid Build Coastguard Worker   test_image_path = test_image_path.AppendASCII(std::get<0>(GetParam()));
170*dfc6aa5cSAndroid Build Coastguard Worker   // Read test image as string and compute MD5 sum.
171*dfc6aa5cSAndroid Build Coastguard Worker   std::string test_image_data;
172*dfc6aa5cSAndroid Build Coastguard Worker   ASSERT_TRUE(base::ReadFileToString(test_image_path, &test_image_data));
173*dfc6aa5cSAndroid Build Coastguard Worker   const std::string md5 = base::MD5String(test_image_data);
174*dfc6aa5cSAndroid Build Coastguard Worker   // Compare expected MD5 sum against that of test image.
175*dfc6aa5cSAndroid Build Coastguard Worker   EXPECT_EQ(std::get<1>(GetParam()), md5);
176*dfc6aa5cSAndroid Build Coastguard Worker }
177*dfc6aa5cSAndroid Build Coastguard Worker 
178*dfc6aa5cSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(TestTileMerged,
179*dfc6aa5cSAndroid Build Coastguard Worker                          TJBenchTestMerged,
180*dfc6aa5cSAndroid Build Coastguard Worker                          ::testing::ValuesIn(IMAGE_MD5_MERGED));
181