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