SDL_test_assert.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. Used by the test framework and test cases.
  20. */
  21. #include "SDL_config.h"
  22. #include "SDL_test.h"
  23. /* Assert check message format */
  24. #define SDLTEST_ASSERT_CHECK_FORMAT "Assert '%s': %s"
  25. /* Assert summary message format */
  26. #define SDLTEST_ASSERT_SUMMARY_FORMAT "Assert Summary: Total=%d Passed=%d Failed=%d"
  27. /* ! \brief counts the failed asserts */
  28. static int SDLTest_AssertsFailed = 0;
  29. /* ! \brief counts the passed asserts */
  30. static int SDLTest_AssertsPassed = 0;
  31. /*
  32. * Assert that logs and break execution flow on failures (i.e. for harness errors).
  33. */
  34. void SDLTest_Assert(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...)
  35. {
  36. va_list list;
  37. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  38. /* Print assert description into a buffer */
  39. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  40. va_start(list, assertDescription);
  41. SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
  42. va_end(list);
  43. /* Log, then assert and break on failure */
  44. SDL_assert((SDLTest_AssertCheck(assertCondition, "%s", logMessage)));
  45. }
  46. /*
  47. * Assert that logs but does not break execution flow on failures (i.e. for test cases).
  48. */
  49. int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...)
  50. {
  51. va_list list;
  52. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  53. /* Print assert description into a buffer */
  54. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  55. va_start(list, assertDescription);
  56. SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
  57. va_end(list);
  58. /* Log pass or fail message */
  59. if (assertCondition == ASSERT_FAIL)
  60. {
  61. SDLTest_AssertsFailed++;
  62. SDLTest_LogError(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Failed");
  63. }
  64. else
  65. {
  66. SDLTest_AssertsPassed++;
  67. SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Passed");
  68. }
  69. return assertCondition;
  70. }
  71. /*
  72. * Explicitly passing Assert that logs (i.e. for test cases).
  73. */
  74. void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...)
  75. {
  76. va_list list;
  77. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  78. /* Print assert description into a buffer */
  79. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  80. va_start(list, assertDescription);
  81. SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
  82. va_end(list);
  83. /* Log pass message */
  84. SDLTest_AssertsPassed++;
  85. SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Pass");
  86. }
  87. /*
  88. * Resets the assert summary counters to zero.
  89. */
  90. void SDLTest_ResetAssertSummary()
  91. {
  92. SDLTest_AssertsPassed = 0;
  93. SDLTest_AssertsFailed = 0;
  94. }
  95. /*
  96. * Logs summary of all assertions (total, pass, fail) since last reset
  97. * as INFO (failed==0) or ERROR (failed > 0).
  98. */
  99. void SDLTest_LogAssertSummary()
  100. {
  101. int totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed;
  102. if (SDLTest_AssertsFailed == 0)
  103. {
  104. SDLTest_Log(SDLTEST_ASSERT_SUMMARY_FORMAT, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
  105. }
  106. else
  107. {
  108. SDLTest_LogError(SDLTEST_ASSERT_SUMMARY_FORMAT, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
  109. }
  110. }
  111. /*
  112. * Converts the current assert state into a test result
  113. */
  114. int SDLTest_AssertSummaryToTestResult()
  115. {
  116. if (SDLTest_AssertsFailed > 0) {
  117. return TEST_RESULT_FAILED;
  118. } else {
  119. if (SDLTest_AssertsPassed > 0) {
  120. return TEST_RESULT_PASSED;
  121. } else {
  122. return TEST_RESULT_NO_ASSERT;
  123. }
  124. }
  125. }
  126. /* vi: set ts=4 sw=4 expandtab: */