xref: /aosp_15_r20/external/libyuv/unit_test/unit_test.h (revision 4e366538070a3a6c5c163c31b791eab742e1657a)
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 #include <stddef.h>  // For NULL
15 #ifdef _WIN32
16 #include <windows.h>
17 #else
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 = NULL
81 
82 #define align_buffer_page_end_16(var, size)                                 \
83   uint8_t* var##_mem =                                                      \
84       reinterpret_cast<uint8_t*>(malloc(((size)*2 + 4095 + 63) & ~4095));   \
85   uint16_t* var = reinterpret_cast<uint16_t*>(                              \
86       (intptr_t)(var##_mem + (((size)*2 + 4095 + 63) & ~4095) - (size)*2) & \
87       ~63)
88 
89 #define free_aligned_buffer_page_end_16(var) \
90   free(var##_mem);                           \
91   var = NULL
92 
93 #ifdef WIN32
get_time()94 static inline double get_time() {
95   LARGE_INTEGER t, f;
96   QueryPerformanceCounter(&t);
97   QueryPerformanceFrequency(&f);
98   return static_cast<double>(t.QuadPart) / static_cast<double>(f.QuadPart);
99 }
100 #else
get_time()101 static inline double get_time() {
102   struct timeval t;
103   struct timezone tzp;
104   gettimeofday(&t, &tzp);
105   return t.tv_sec + t.tv_usec * 1e-6;
106 }
107 #endif
108 
109 #ifndef SIMD_ALIGNED
110 #if defined(_MSC_VER) && !defined(__CLR_VER)
111 #define SIMD_ALIGNED(var) __declspec(align(16)) var
112 #elif defined(__GNUC__) && !defined(__pnacl__)
113 #define SIMD_ALIGNED(var) var __attribute__((aligned(16)))
114 #else
115 #define SIMD_ALIGNED(var) var
116 #endif
117 #endif
118 
119 extern unsigned int fastrand_seed;
fastrand()120 inline int fastrand() {
121   fastrand_seed = fastrand_seed * 214013u + 2531011u;
122   return static_cast<int>((fastrand_seed >> 16) & 0xffff);
123 }
124 
125 // ubsan fails if dst is unaligned unless we use uint8
MemRandomize(uint8_t * dst,int64_t len)126 static inline void MemRandomize(uint8_t* dst, int64_t len) {
127   int64_t i;
128   for (i = 0; i < len - 1; i += 2) {
129     int r = fastrand();
130     dst[0] = static_cast<uint8_t>(r);
131     dst[1] = static_cast<uint8_t>(r >> 8);
132     dst += 2;
133   }
134   for (; i < len; ++i) {
135     *dst++ = fastrand();
136   }
137 }
138 
139 class LibYUVColorTest : public ::testing::Test {
140  protected:
141   LibYUVColorTest();
142 
143   int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
144   int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
145   int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
146   int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
147   int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
148   int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
149 };
150 
151 class LibYUVConvertTest : public ::testing::Test {
152  protected:
153   LibYUVConvertTest();
154 
155   int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
156   int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
157   int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
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 LibYUVScaleTest : public ::testing::Test {
164  protected:
165   LibYUVScaleTest();
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_div1280_;  // Total pixels to benchmark / 1280.
171   int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
172   int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
173 };
174 
175 class LibYUVRotateTest : public ::testing::Test {
176  protected:
177   LibYUVRotateTest();
178 
179   int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
180   int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
181   int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
182   int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
183   int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
184   int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
185 };
186 
187 class LibYUVPlanarTest : public ::testing::Test {
188  protected:
189   LibYUVPlanarTest();
190 
191   int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
192   int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
193   int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
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 class LibYUVBaseTest : public ::testing::Test {
200  protected:
201   LibYUVBaseTest();
202 
203   int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
204   int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
205   int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
206   int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
207   int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
208   int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
209 };
210 
211 class LibYUVCompareTest : public ::testing::Test {
212  protected:
213   LibYUVCompareTest();
214 
215   int benchmark_iterations_;  // Default 1. Use 1000 for benchmarking.
216   int benchmark_width_;       // Default 1280.  Use 640 for benchmarking VGA.
217   int benchmark_height_;      // Default 720.  Use 360 for benchmarking VGA.
218   int benchmark_pixels_div1280_;  // Total pixels to benchmark / 1280.
219   int disable_cpu_flags_;         // Default 1.  Use -1 for benchmarking.
220   int benchmark_cpu_info_;        // Default -1.  Use 1 to disable SIMD.
221 };
222 
223 #endif  // UNIT_TEST_UNIT_TEST_H_  NOLINT
224