SDL_test_compare.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. Based on automated SDL_Surface tests originally written by Edgar Simo 'bobbens'.
  20. Rewritten for test lib by Andreas Schiffler.
  21. */
  22. #include <SDL3/SDL_test.h>
  23. #define FILENAME_SIZE 128
  24. /* Counter for _CompareSurface calls; used for filename creation when comparisons fail */
  25. static int _CompareSurfaceCount = 0;
  26. /* Compare surfaces */
  27. int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error)
  28. {
  29. int ret;
  30. int i, j;
  31. int dist;
  32. int sampleErrorX = 0, sampleErrorY = 0, sampleDist = 0;
  33. SDL_Color sampleReference = { 0, 0, 0, 0 };
  34. SDL_Color sampleActual = { 0, 0, 0, 0 };
  35. Uint8 R, G, B, A;
  36. Uint8 Rd, Gd, Bd, Ad;
  37. char imageFilename[FILENAME_SIZE];
  38. char referenceFilename[FILENAME_SIZE];
  39. /* Validate input surfaces */
  40. if (!surface) {
  41. SDLTest_LogError("Cannot compare NULL surface");
  42. return -1;
  43. }
  44. if (!referenceSurface) {
  45. SDLTest_LogError("Cannot compare NULL reference surface");
  46. return -1;
  47. }
  48. /* Make sure surface size is the same. */
  49. if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h)) {
  50. SDLTest_LogError("Expected %dx%d surface, got %dx%d", referenceSurface->w, referenceSurface->h, surface->w, surface->h);
  51. return -2;
  52. }
  53. /* Sanitize input value */
  54. if (allowable_error < 0) {
  55. allowable_error = 0;
  56. }
  57. SDL_LockSurface(surface);
  58. SDL_LockSurface(referenceSurface);
  59. ret = 0;
  60. /* Compare image - should be same format. */
  61. for (j = 0; j < surface->h; j++) {
  62. for (i = 0; i < surface->w; i++) {
  63. int temp;
  64. temp = SDL_ReadSurfacePixel(surface, i, j, &R, &G, &B, &A);
  65. if (!temp) {
  66. SDLTest_LogError("Failed to retrieve pixel (%d,%d): %s", i, j, SDL_GetError());
  67. ret++;
  68. continue;
  69. }
  70. temp = SDL_ReadSurfacePixel(referenceSurface, i, j, &Rd, &Gd, &Bd, &Ad);
  71. if (!temp) {
  72. SDLTest_LogError("Failed to retrieve reference pixel (%d,%d): %s", i, j, SDL_GetError());
  73. ret++;
  74. continue;
  75. }
  76. dist = 0;
  77. dist += (R - Rd) * (R - Rd);
  78. dist += (G - Gd) * (G - Gd);
  79. dist += (B - Bd) * (B - Bd);
  80. /* Allow some difference in blending accuracy */
  81. if (dist > allowable_error) {
  82. ret++;
  83. if (ret == 1) {
  84. sampleErrorX = i;
  85. sampleErrorY = j;
  86. sampleDist = dist;
  87. sampleReference.r = Rd;
  88. sampleReference.g = Gd;
  89. sampleReference.b = Bd;
  90. sampleReference.a = Ad;
  91. sampleActual.r = R;
  92. sampleActual.g = G;
  93. sampleActual.b = B;
  94. sampleActual.a = A;
  95. }
  96. }
  97. }
  98. }
  99. SDL_UnlockSurface(surface);
  100. SDL_UnlockSurface(referenceSurface);
  101. /* Save test image and reference for analysis on failures */
  102. _CompareSurfaceCount++;
  103. if (ret != 0) {
  104. SDLTest_LogError("Comparison of pixels with allowable error of %i failed %i times.", allowable_error, ret);
  105. SDLTest_LogError("Reference surface format: %s", SDL_GetPixelFormatName(referenceSurface->format));
  106. SDLTest_LogError("Actual surface format: %s", SDL_GetPixelFormatName(surface->format));
  107. SDLTest_LogError("First detected occurrence at position %i,%i with a squared RGB-difference of %i.", sampleErrorX, sampleErrorY, sampleDist);
  108. SDLTest_LogError("Reference pixel: R=%u G=%u B=%u A=%u", sampleReference.r, sampleReference.g, sampleReference.b, sampleReference.a);
  109. SDLTest_LogError("Actual pixel : R=%u G=%u B=%u A=%u", sampleActual.r, sampleActual.g, sampleActual.b, sampleActual.a);
  110. (void)SDL_snprintf(imageFilename, FILENAME_SIZE - 1, "CompareSurfaces%04d_TestOutput.bmp", _CompareSurfaceCount);
  111. SDL_SaveBMP(surface, imageFilename);
  112. (void)SDL_snprintf(referenceFilename, FILENAME_SIZE - 1, "CompareSurfaces%04d_Reference.bmp", _CompareSurfaceCount);
  113. SDL_SaveBMP(referenceSurface, referenceFilename);
  114. SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
  115. }
  116. return ret;
  117. }
  118. int SDLTest_CompareMemory(const void *actual, size_t size_actual, const void *reference, size_t size_reference) {
  119. #define WIDTH 16
  120. const size_t size_max = SDL_max(size_actual, size_reference);
  121. size_t i;
  122. struct {
  123. const char *header;
  124. const Uint8 *data;
  125. size_t size;
  126. } columns[] = {
  127. {
  128. "actual",
  129. actual,
  130. size_actual,
  131. },
  132. {
  133. "reference",
  134. reference,
  135. size_reference,
  136. },
  137. };
  138. char line_buffer[16 + SDL_arraysize(columns) * (4 * WIDTH + 1) + (SDL_arraysize(columns) - 1) * 2 + 1];
  139. SDLTest_AssertCheck(size_actual == size_reference, "Sizes of memory blocks must be equal (actual=%" SDL_PRIu64 " expected=%" SDL_PRIu64 ")", (Uint64)size_actual, (Uint64)size_reference);
  140. if (size_actual == size_reference) {
  141. int equals;
  142. equals = SDL_memcmp(actual, reference, size_max) == 0;
  143. SDLTest_AssertCheck(equals, "Memory blocks contain the same data");
  144. if (equals) {
  145. return 0;
  146. }
  147. }
  148. SDL_memset(line_buffer, ' ', sizeof(line_buffer));
  149. line_buffer[sizeof(line_buffer) - 1] = '\0';
  150. for (i = 0; i < SDL_arraysize(columns); i++) {
  151. SDL_memcpy(line_buffer + 16 + 1 + i * (4 * WIDTH + 3), columns[i].header, SDL_strlen(columns[i].header));
  152. }
  153. SDLTest_LogError("%s", line_buffer);
  154. for (i = 0; i < size_max; i += WIDTH) {
  155. size_t pos = 0;
  156. size_t col;
  157. pos += SDL_snprintf(line_buffer + pos, SDL_arraysize(line_buffer) - pos, "%016" SDL_PRIx64 , (Uint64)i);
  158. for (col = 0; col < SDL_arraysize(columns); col++) {
  159. size_t j;
  160. for (j = 0; j < WIDTH; j++) {
  161. if (i + j < columns[col].size) {
  162. pos += SDL_snprintf(line_buffer + pos, SDL_arraysize(line_buffer) - pos, " %02x", columns[col].data[i + j]);
  163. } else {
  164. pos += SDL_snprintf(line_buffer + pos, SDL_arraysize(line_buffer) - pos, " ");
  165. }
  166. }
  167. pos += SDL_snprintf(line_buffer + pos, SDL_arraysize(line_buffer) - pos, " ");
  168. for (j = 0; j < WIDTH; j++) {
  169. char c = ' ';
  170. if (i + j < columns[col].size) {
  171. c = columns[col].data[i + j];
  172. if (!SDL_isprint(c)) {
  173. c = '.';
  174. }
  175. }
  176. pos += SDL_snprintf(line_buffer + pos, SDL_arraysize(line_buffer) - pos, "%c", c);
  177. }
  178. if (col < SDL_arraysize(columns) - 1) {
  179. pos += SDL_snprintf(line_buffer + pos, SDL_arraysize(line_buffer), " |");
  180. }
  181. }
  182. SDLTest_LogError("%s", line_buffer);
  183. SDL_assert(pos == SDL_arraysize(line_buffer) - 1);
  184. }
  185. #undef WIDTH
  186. return 1;
  187. }