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/time.h>
18 #include <sys/resource.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, int src_height,
58 int dst_width, int dst_height) {
59 if (src_width > kMaxWidth || src_height > kMaxHeight ||
60 dst_width > kMaxWidth || dst_height > kMaxHeight) {
61 printf("Warning - size too large to test. Skipping\n");
62 return false;
63 }
64 return true;
65 }
66
67 #define align_buffer_page_end(var, size) \
68 uint8* var; \
69 uint8* var##_mem; \
70 var##_mem = reinterpret_cast<uint8*>(malloc(((size) + 4095 + 63) & ~4095)); \
71 var = (uint8*)((intptr_t)(var##_mem + (((size) + 4095 + 63) & ~4095) - \
72 (size)) & ~63);
73
74 #define free_aligned_buffer_page_end(var) \
75 free(var##_mem); \
76 var = 0;
77
78 #ifdef WIN32
get_time()79 static inline double get_time() {
80 LARGE_INTEGER t, f;
81 QueryPerformanceCounter(&t);
82 QueryPerformanceFrequency(&f);
83 return static_cast<double>(t.QuadPart) / static_cast<double>(f.QuadPart);
84 }
85 #else
get_time()86 static inline double get_time() {
87 struct timeval t;
88 struct timezone tzp;
89 gettimeofday(&t, &tzp);
90 return t.tv_sec + t.tv_usec * 1e-6;
91 }
92 #endif
93
94 #ifndef SIMD_ALIGNED
95 #if defined(_MSC_VER) && !defined(__CLR_VER)
96 #define SIMD_ALIGNED(var) __declspec(align(16)) var
97 #elif defined(__GNUC__) && !defined(__pnacl__)
98 #define SIMD_ALIGNED(var) var __attribute__((aligned(16)))
99 #else
100 #define SIMD_ALIGNED(var) var
101 #endif
102 #endif
103
104 extern unsigned int fastrand_seed;
fastrand()105 inline int fastrand() {
106 fastrand_seed = fastrand_seed * 214013u + 2531011u;
107 return static_cast<int>((fastrand_seed >> 16) & 0xffff);
108 }
109
MemRandomize(uint8 * dst,int64 len)110 static inline void MemRandomize(uint8* dst, int64 len) {
111 int64 i;
112 for (i = 0; i < len - 1; i += 2) {
113 *reinterpret_cast<uint16*>(dst) = fastrand();
114 dst += 2;
115 }
116 for (; i < len; ++i) {
117 *dst++ = fastrand();
118 }
119 }
120
121 class LibYUVColorTest : public ::testing::Test {
122 protected:
123 LibYUVColorTest();
124
125 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
126 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
127 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
128 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
129 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
130 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
131 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
132 };
133
134 class LibYUVConvertTest : public ::testing::Test {
135 protected:
136 LibYUVConvertTest();
137
138 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
139 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
140 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
141 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
142 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
143 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
144 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
145 };
146
147 class LibYUVScaleTest : public ::testing::Test {
148 protected:
149 LibYUVScaleTest();
150
151 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
152 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
153 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
154 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
155 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
156 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
157 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
158 };
159
160 class LibYUVRotateTest : public ::testing::Test {
161 protected:
162 LibYUVRotateTest();
163
164 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
165 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
166 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
167 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
168 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
169 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
170 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
171 };
172
173 class LibYUVPlanarTest : public ::testing::Test {
174 protected:
175 LibYUVPlanarTest();
176
177 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
178 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
179 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
180 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
181 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
182 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
183 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
184 };
185
186 class LibYUVBaseTest : public ::testing::Test {
187 protected:
188 LibYUVBaseTest();
189
190 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
191 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
192 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
193 int benchmark_pixels_div256_; // Total pixels to benchmark / 256.
194 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
195 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
196 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
197 };
198
199 #endif // UNIT_TEST_UNIT_TEST_H_ NOLINT
200