2
0

unit_test.h 7.4 KB

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