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
39 #define OFFBY 0
40
41 // Scaling uses 16.16 fixed point to step thru the source image, so a
42 // maximum size of 32767.999 can be expressed. 32768 is valid because
43 // the step is 1 beyond the image but not used.
44 // Destination size is mainly constrained by valid scale step not the
45 // absolute size, so it may be possible to relax the destination size
46 // constraint.
47 // Source size is unconstrained for most specialized scalers. e.g.
48 // An image of 65536 scaled to half size would be valid. The test
49 // could be relaxed for special scale factors.
50 // If this test is removed, the scaling function should gracefully
51 // fail with a return code. The test could be changed to know that
52 // libyuv failed in a controlled way.
53
54 static const int kMaxWidth = 32768;
55 static const int kMaxHeight = 32768;
56
SizeValid(int src_width,int src_height,int dst_width,int dst_height)57 static inline bool SizeValid(int src_width,
58 int src_height,
59 int dst_width,
60 int dst_height) {
61 if (src_width > kMaxWidth || src_height > kMaxHeight ||
62 dst_width > kMaxWidth || dst_height > kMaxHeight) {
63 printf("Warning - size too large to test. Skipping\n");
64 return false;
65 }
66 return true;
67 }
68
69 #define align_buffer_page_end(var, size) \
70 uint8* var; \
71 uint8* var##_mem; \
72 var##_mem = reinterpret_cast<uint8*>(malloc(((size) + 4095 + 63) & ~4095)); \
73 var = (uint8*)((intptr_t)(var##_mem + (((size) + 4095 + 63) & ~4095) - \
74 (size)) & \
75 ~63);
76
77 #define free_aligned_buffer_page_end(var) \
78 free(var##_mem); \
79 var = 0;
80
81 #ifdef WIN32
get_time()82 static inline double get_time() {
83 LARGE_INTEGER t, f;
84 QueryPerformanceCounter(&t);
85 QueryPerformanceFrequency(&f);
86 return static_cast<double>(t.QuadPart) / static_cast<double>(f.QuadPart);
87 }
88 #else
get_time()89 static inline double get_time() {
90 struct timeval t;
91 struct timezone tzp;
92 gettimeofday(&t, &tzp);
93 return t.tv_sec + t.tv_usec * 1e-6;
94 }
95 #endif
96
97 #ifndef SIMD_ALIGNED
98 #if defined(_MSC_VER) && !defined(__CLR_VER)
99 #define SIMD_ALIGNED(var) __declspec(align(16)) var
100 #elif defined(__GNUC__) && !defined(__pnacl__)
101 #define SIMD_ALIGNED(var) var __attribute__((aligned(16)))
102 #else
103 #define SIMD_ALIGNED(var) var
104 #endif
105 #endif
106
107 extern unsigned int fastrand_seed;
fastrand()108 inline int fastrand() {
109 fastrand_seed = fastrand_seed * 214013u + 2531011u;
110 return static_cast<int>((fastrand_seed >> 16) & 0xffff);
111 }
112
MemRandomize(uint8 * dst,int64 len)113 static inline void MemRandomize(uint8* dst, int64 len) {
114 int64 i;
115 for (i = 0; i < len - 1; i += 2) {
116 *reinterpret_cast<uint16*>(dst) = fastrand();
117 dst += 2;
118 }
119 for (; i < len; ++i) {
120 *dst++ = fastrand();
121 }
122 }
123
124 class LibYUVColorTest : public ::testing::Test {
125 protected:
126 LibYUVColorTest();
127
128 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
129 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
130 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
131 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
132 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
133 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
134 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
135 };
136
137 class LibYUVConvertTest : public ::testing::Test {
138 protected:
139 LibYUVConvertTest();
140
141 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
142 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
143 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
144 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
145 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
146 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
147 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
148 };
149
150 class LibYUVScaleTest : public ::testing::Test {
151 protected:
152 LibYUVScaleTest();
153
154 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
155 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
156 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
157 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
158 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
159 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
160 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
161 };
162
163 class LibYUVRotateTest : public ::testing::Test {
164 protected:
165 LibYUVRotateTest();
166
167 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
168 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
169 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
170 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
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_div256_; // Total pixels to benchmark / 256.
184 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
185 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
186 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
187 };
188
189 class LibYUVBaseTest : public ::testing::Test {
190 protected:
191 LibYUVBaseTest();
192
193 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
194 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
195 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
196 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
197 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
198 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
199 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
200 };
201
202 #endif // UNIT_TEST_UNIT_TEST_H_ NOLINT
203