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
MemRandomize(uint8_t * dst,int64_t len)114 static inline void MemRandomize(uint8_t* dst, int64_t len) {
115 int64_t i;
116 for (i = 0; i < len - 1; i += 2) {
117 *reinterpret_cast<uint16_t*>(dst) = fastrand();
118 dst += 2;
119 }
120 for (; i < len; ++i) {
121 *dst++ = fastrand();
122 }
123 }
124
125 class LibYUVColorTest : public ::testing::Test {
126 protected:
127 LibYUVColorTest();
128
129 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
130 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
131 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
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_div1280_; // Total pixels to benchmark / 1280.
145 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
146 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
147 };
148
149 class LibYUVScaleTest : public ::testing::Test {
150 protected:
151 LibYUVScaleTest();
152
153 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
154 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
155 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
156 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
157 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
158 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
159 };
160
161 class LibYUVRotateTest : public ::testing::Test {
162 protected:
163 LibYUVRotateTest();
164
165 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
166 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
167 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
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_div1280_; // Total pixels to benchmark / 1280.
181 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
182 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
183 };
184
185 class LibYUVBaseTest : public ::testing::Test {
186 protected:
187 LibYUVBaseTest();
188
189 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
190 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
191 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
192 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
193 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
194 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
195 };
196
197 class LibYUVCompareTest : public ::testing::Test {
198 protected:
199 LibYUVCompareTest();
200
201 int benchmark_iterations_; // Default 1. Use 1000 for benchmarking.
202 int benchmark_width_; // Default 1280. Use 640 for benchmarking VGA.
203 int benchmark_height_; // Default 720. Use 360 for benchmarking VGA.
204 int benchmark_pixels_div1280_; // Total pixels to benchmark / 1280.
205 int disable_cpu_flags_; // Default 1. Use -1 for benchmarking.
206 int benchmark_cpu_info_; // Default -1. Use 1 to disable SIMD.
207 };
208
209 #endif // UNIT_TEST_UNIT_TEST_H_ NOLINT
210