1 /*
2 * Copyright 2011 The LibYuv Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #ifndef UNIT_TEST_UNIT_TEST_H_ // NOLINT
12 #define UNIT_TEST_UNIT_TEST_H_
13
14 #ifdef _WIN32
15 #include <windows.h>
16 #else
17 #include <sys/resource.h>
18 #include <sys/time.h>
19 #endif
20
21 #include <gtest/gtest.h>
22
23 #include "libyuv/basic_types.h"
24
25 #ifndef SIMD_ALIGNED
26 #if defined(_MSC_VER) && !defined(__CLR_VER)
27 #define SIMD_ALIGNED(var) __declspec(align(16)) var
28 #elif defined(__GNUC__) && !defined(__pnacl__)
29 #define SIMD_ALIGNED(var) var __attribute__((aligned(16)))
30 #else
31 #define SIMD_ALIGNED(var) var
32 #endif
33 #endif
34
Abs(int v)35 static __inline int Abs(int v) {
36 return v >= 0 ? v : -v;
37 }
38
FAbs(float v)39 static __inline float FAbs(float v) {
40 return v >= 0 ? v : -v;
41 }
42 #define OFFBY 0
43
44 // Scaling uses 16.16 fixed point to step thru the source image, so a
45 // maximum size of 32767.999 can be expressed. 32768 is valid because
46 // the step is 1 beyond the image but not used.
47 // Destination size is mainly constrained by valid scale step not the
48 // absolute size, so it may be possible to relax the destination size
49 // constraint.
50 // Source size is unconstrained for most specialized scalers. e.g.
51 // An image of 65536 scaled to half size would be valid. The test
52 // could be relaxed for special scale factors.
53 // If this test is removed, the scaling function should gracefully
54 // fail with a return code. The test could be changed to know that
55 // libyuv failed in a controlled way.
56
57 static const int kMaxWidth = 32768;
58 static const int kMaxHeight = 32768;
59
SizeValid(int src_width,int src_height,int dst_width,int dst_height)60 static inline bool SizeValid(int src_width,
61 int src_height,
62 int dst_width,
63 int dst_height) {
64 if (src_width > kMaxWidth || src_height > kMaxHeight ||
65 dst_width > kMaxWidth || dst_height > kMaxHeight) {
66 printf("Warning - size too large to test. Skipping\n");
67 return false;
68 }
69 return true;
70 }
71
72 #define align_buffer_page_end(var, size) \
73 uint8_t* var##_mem = \
74 reinterpret_cast<uint8_t*>(malloc(((size) + 4095 + 63) & ~4095)); \
75 uint8_t* var = reinterpret_cast<uint8_t*>( \
76 (intptr_t)(var##_mem + (((size) + 4095 + 63) & ~4095) - (size)) & ~63)
77
78 #define free_aligned_buffer_page_end(var) \
79 free(var##_mem); \
80 var = 0
81
82 #ifdef WIN32
get_time()83 static inline double get_time() {
84 LARGE_INTEGER t, f;
85 QueryPerformanceCounter(&t);
86 QueryPerformanceFrequency(&f);
87 return static_cast<double>(t.QuadPart) / static_cast<double>(f.QuadPart);
88 }
89 #else
get_time()90 static inline double get_time() {
91 struct timeval t;
92 struct timezone tzp;
93 gettimeofday(&t, &tzp);
94 return t.tv_sec + t.tv_usec * 1e-6;
95 }
96 #endif
97
98 #ifndef SIMD_ALIGNED
99 #if defined(_MSC_VER) && !defined(__CLR_VER)
100 #define SIMD_ALIGNED(var) __declspec(align(16)) var
101 #elif defined(__GNUC__) && !defined(__pnacl__)
102 #define SIMD_ALIGNED(var) var __attribute__((aligned(16)))
103 #else
104 #define SIMD_ALIGNED(var) var
105 #endif
106 #endif
107
108 extern unsigned int fastrand_seed;
fastrand()109 inline int fastrand() {
110 fastrand_seed = fastrand_seed * 214013u + 2531011u;
111 return static_cast<int>((fastrand_seed >> 16) & 0xffff);
112 }
113
114 // ubsan fails if dst is unaligned unless we use uint8
MemRandomize(uint8_t * dst,int64_t len)115 static inline void MemRandomize(uint8_t* dst, int64_t len) {
116 int64_t i;
117 for (i = 0; i < len - 1; i += 2) {
118 int r = fastrand();
119 dst[0] = static_cast<uint8_t>(r);
120 dst[1] = static_cast<uint8_t>(r >> 8);
121 dst += 2;
122 }
123 for (; i < len; ++i) {
124 *dst++ = fastrand();
125 }
126 }
127
128 class LibYUVColorTest : public ::testing::Test {
129 protected:
130 LibYUVColorTest();
131
132 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
133 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
134 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
135 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
136 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
137 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
138 };
139
140 class LibYUVConvertTest : public ::testing::Test {
141 protected:
142 LibYUVConvertTest();
143
144 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
145 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
146 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
147 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
148 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
149 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
150 };
151
152 class LibYUVScaleTest : public ::testing::Test {
153 protected:
154 LibYUVScaleTest();
155
156 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
157 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
158 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
159 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
160 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
161 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
162 };
163
164 class LibYUVRotateTest : public ::testing::Test {
165 protected:
166 LibYUVRotateTest();
167
168 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
169 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
170 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
171 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
172 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
173 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
174 };
175
176 class LibYUVPlanarTest : public ::testing::Test {
177 protected:
178 LibYUVPlanarTest();
179
180 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
181 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
182 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
183 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
184 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
185 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
186 };
187
188 class LibYUVBaseTest : public ::testing::Test {
189 protected:
190 LibYUVBaseTest();
191
192 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
193 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
194 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
195 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
196 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
197 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
198 };
199
200 class LibYUVCompareTest : public ::testing::Test {
201 protected:
202 LibYUVCompareTest();
203
204 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
205 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
206 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
207 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
208 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
209 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
210 };
211
212 #endif // UNIT_TEST_UNIT_TEST_H_ NOLINT
213