summaryrefslogtreecommitdiff
path: root/gtest/tjbench-gtest-wrapper.cpp
blob: ec4c850a139b339df1e0aeca50c280c115c29a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright 2020 The Chromium Authors. All Rights Reserved.
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/hash/md5.h"
#include "base/path_service.h"

#include <gtest/gtest.h>
#include <string>

extern "C" int tjbench(int argc, char *argv[]);

static std::string GetTargetDirectory() {
#if defined(ANDROID)
  return "/sdcard";
#else
  base::FilePath path;
  base::PathService::Get(base::DIR_CURRENT, &path);
  return path.MaybeAsASCII();
#endif
}

// Test image files and their expected MD5 sums.
const static std::vector<std::pair<const std::string,
                                   const std::string>> IMAGE_MD5_BASELINE = {
  { "testout_tile_GRAY_Q95_8x8.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
  { "testout_tile_420_Q95_8x8.ppm", "847fceab15c5b7b911cb986cf0f71de3" },
  { "testout_tile_422_Q95_8x8.ppm", "d83dacd9fc73b0a6f10c09acad64eb1e" },
  { "testout_tile_444_Q95_8x8.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" },
  { "testout_tile_GRAY_Q95_16x16.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
  { "testout_tile_420_Q95_16x16.ppm", "ca45552a93687e078f7137cc4126a7b0" },
  { "testout_tile_422_Q95_16x16.ppm", "35077fb610d72dd743b1eb0cbcfe10fb" },
  { "testout_tile_444_Q95_16x16.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" },
  { "testout_tile_GRAY_Q95_32x32.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
  { "testout_tile_420_Q95_32x32.ppm", "d8676f1d6b68df358353bba9844f4a00" },
  { "testout_tile_422_Q95_32x32.ppm", "e6902ed8a449ecc0f0d6f2bf945f65f7" },
  { "testout_tile_444_Q95_32x32.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" },
  { "testout_tile_GRAY_Q95_64x64.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
  { "testout_tile_420_Q95_64x64.ppm", "4e4c1a3d7ea4bace4f868bcbe83b7050" },
  { "testout_tile_422_Q95_64x64.ppm", "2b4502a8f316cedbde1da7bce3d2231e" },
  { "testout_tile_444_Q95_64x64.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" },
  { "testout_tile_GRAY_Q95_128x128.ppm", "89d3ca21213d9d864b50b4e4e7de4ca6" },
  { "testout_tile_420_Q95_128x128.ppm", "f24c3429c52265832beab9df72a0ceae" },
  { "testout_tile_422_Q95_128x128.ppm", "f0b5617d578f5e13c8eee215d64d4877" },
  { "testout_tile_444_Q95_128x128.ppm", "7964e41e67cfb8d0a587c0aa4798f9c3" }
};

class TJBenchTest : public
  ::testing::TestWithParam<std::pair<const std::string, const std::string>> {

 protected:

  static void SetUpTestSuite() {
    base::FilePath resource_path;
    ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &resource_path));
    resource_path = resource_path.AppendASCII("third_party");
    resource_path = resource_path.AppendASCII("libjpeg_turbo");
    resource_path = resource_path.AppendASCII("testimages");
    resource_path = resource_path.AppendASCII("testorig.ppm");
    ASSERT_TRUE(base::PathExists(resource_path));

    base::FilePath target_path(FILE_PATH_LITERAL(GetTargetDirectory()));
    target_path = target_path.AppendASCII("testout_tile.ppm");

    ASSERT_TRUE(base::CopyFile(resource_path, target_path));

    std::string prog_name = "tjbench";
    std::string arg1 = target_path.MaybeAsASCII();
    std::string arg2 = "95";
    std::string arg3 = "-rgb";
    std::string arg4 = "-quiet";
    std::string arg5 = "-tile";
    std::string arg6 = "-benchtime";
    std::string arg7 = "0.01";
    std::string arg8 = "-warmup";
    std::string arg9 = "0";
    char *command_line[] = { &prog_name[0],
                             &arg1[0], &arg2[0], &arg3[0], &arg4[0], &arg5[0],
                             &arg6[0], &arg7[0], &arg8[0], &arg9[0],
                           };
    // Generate test image tiles.
    EXPECT_EQ(tjbench(10, command_line), 0);
  }

};

TEST_P(TJBenchTest, TestTileBaseline) {
  // Construct path for test image file.
  base::FilePath test_image_path(FILE_PATH_LITERAL(GetTargetDirectory()));
  test_image_path = test_image_path.AppendASCII(std::get<0>(GetParam()));
  // Read test image as string and compute MD5 sum.
  std::string test_image_data;
  ASSERT_TRUE(base::ReadFileToString(test_image_path, &test_image_data));
  const std::string md5 = base::MD5String(test_image_data);
  // Compare expected MD5 sum against that of test image.
  EXPECT_EQ(std::get<1>(GetParam()), md5);
}

INSTANTIATE_TEST_SUITE_P(TestTileBaseline,
                         TJBenchTest,
                         ::testing::ValuesIn(IMAGE_MD5_BASELINE));

// Test image files and their expected MD5 sums.
const static std::vector<std::pair<const std::string,
                                   const std::string>> IMAGE_MD5_MERGED = {
  { "testout_tilem_420_Q95_8x8.ppm", "bc25320e1f4c31ce2e610e43e9fd173c" },
  { "testout_tilem_422_Q95_8x8.ppm", "828941d7f41cd6283abd6beffb7fd51d" },
  { "testout_tilem_420_Q95_16x16.ppm", "75ffdf14602258c5c189522af57fa605" },
  { "testout_tilem_422_Q95_16x16.ppm", "e877ae1324c4a280b95376f7f018172f" },
  { "testout_tilem_420_Q95_32x32.ppm", "75ffdf14602258c5c189522af57fa605" },
  { "testout_tilem_422_Q95_32x32.ppm", "e877ae1324c4a280b95376f7f018172f" },
  { "testout_tilem_420_Q95_64x64.ppm", "75ffdf14602258c5c189522af57fa605" },
  { "testout_tilem_422_Q95_64x64.ppm", "e877ae1324c4a280b95376f7f018172f" },
  { "testout_tilem_420_Q95_128x128.ppm", "75ffdf14602258c5c189522af57fa605" },
  { "testout_tilem_422_Q95_128x128.ppm", "e877ae1324c4a280b95376f7f018172f" }
};

class TJBenchTestMerged : public
  ::testing::TestWithParam<std::pair<const std::string, const std::string>> {

 protected:

  static void SetUpTestSuite() {
    base::FilePath resource_path;
    ASSERT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &resource_path));
    resource_path = resource_path.AppendASCII("third_party");
    resource_path = resource_path.AppendASCII("libjpeg_turbo");
    resource_path = resource_path.AppendASCII("testimages");
    resource_path = resource_path.AppendASCII("testorig.ppm");
    ASSERT_TRUE(base::PathExists(resource_path));

    base::FilePath target_path(FILE_PATH_LITERAL(GetTargetDirectory()));
    target_path = target_path.AppendASCII("testout_tilem.ppm");

    ASSERT_TRUE(base::CopyFile(resource_path, target_path));

    std::string prog_name = "tjbench";
    std::string arg1 = target_path.MaybeAsASCII();
    std::string arg2 = "95";
    std::string arg3 = "-rgb";
    std::string arg4 = "-fastupsample";
    std::string arg5 = "-quiet";
    std::string arg6 = "-tile";
    std::string arg7 = "-benchtime";
    std::string arg8 = "0.01";
    std::string arg9 = "-warmup";
    std::string arg10 = "0";
    char *command_line[] = { &prog_name[0],
                             &arg1[0], &arg2[0], &arg3[0], &arg4[0], &arg5[0],
                             &arg6[0], &arg7[0], &arg8[0], &arg9[0], &arg10[0]
                           };
    // Generate test image output tiles.
    EXPECT_EQ(tjbench(11, command_line), 0);
  }

};

TEST_P(TJBenchTestMerged, TestTileMerged) {
  // Construct path for test image file.
  base::FilePath test_image_path(FILE_PATH_LITERAL(GetTargetDirectory()));
  test_image_path = test_image_path.AppendASCII(std::get<0>(GetParam()));
  // Read test image as string and compute MD5 sum.
  std::string test_image_data;
  ASSERT_TRUE(base::ReadFileToString(test_image_path, &test_image_data));
  const std::string md5 = base::MD5String(test_image_data);
  // Compare expected MD5 sum against that of test image.
  EXPECT_EQ(std::get<1>(GetParam()), md5);
}

INSTANTIATE_TEST_SUITE_P(TestTileMerged,
                         TJBenchTestMerged,
                         ::testing::ValuesIn(IMAGE_MD5_MERGED));