SDL  2.0
SDL_test_assert.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 /*
23 
24  Used by the test framework and test cases.
25 
26 */
27 
28 #include "SDL_config.h"
29 
30 #include "SDL_test.h"
31 
32 /* Assert check message format */
33 #define SDLTEST_ASSERT_CHECK_FORMAT "Assert '%s': %s"
34 
35 /* Assert summary message format */
36 #define SDLTEST_ASSERT_SUMMARY_FORMAT "Assert Summary: Total=%d Passed=%d Failed=%d"
37 
38 /* ! \brief counts the failed asserts */
40 
41 /* ! \brief counts the passed asserts */
43 
44 /*
45  * Assert that logs and break execution flow on failures (i.e. for harness errors).
46  */
47 void SDLTest_Assert(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...)
48 {
49  va_list list;
50  char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
51 
52  /* Print assert description into a buffer */
54  va_start(list, assertDescription);
55  SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
56  va_end(list);
57 
58  /* Log, then assert and break on failure */
59  SDL_assert((SDLTest_AssertCheck(assertCondition, "%s", logMessage)));
60 }
61 
62 /*
63  * Assert that logs but does not break execution flow on failures (i.e. for test cases).
64  */
65 int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...)
66 {
67  va_list list;
68  char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
69 
70  /* Print assert description into a buffer */
72  va_start(list, assertDescription);
73  SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
74  va_end(list);
75 
76  /* Log pass or fail message */
77  if (assertCondition == ASSERT_FAIL)
78  {
80  SDLTest_LogError(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Failed");
81  }
82  else
83  {
85  SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Passed");
86  }
87 
88  return assertCondition;
89 }
90 
91 /*
92  * Explicitly passing Assert that logs (i.e. for test cases).
93  */
94 void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription, ...)
95 {
96  va_list list;
97  char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
98 
99  /* Print assert description into a buffer */
101  va_start(list, assertDescription);
102  SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
103  va_end(list);
104 
105  /* Log pass message */
107  SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, "Pass");
108 }
109 
110 /*
111  * Resets the assert summary counters to zero.
112  */
114 {
117 }
118 
119 /*
120  * Logs summary of all assertions (total, pass, fail) since last reset
121  * as INFO (failed==0) or ERROR (failed > 0).
122  */
124 {
126  if (SDLTest_AssertsFailed == 0)
127  {
129  }
130  else
131  {
133  }
134 }
135 
136 /*
137  * Converts the current assert state into a test result
138  */
140 {
141  if (SDLTest_AssertsFailed > 0) {
142  return TEST_RESULT_FAILED;
143  } else {
144  if (SDLTest_AssertsPassed > 0) {
145  return TEST_RESULT_PASSED;
146  } else {
147  return TEST_RESULT_NO_ASSERT;
148  }
149  }
150 }
151 
152 /* vi: set ts=4 sw=4 expandtab: */
TEST_RESULT_FAILED
#define TEST_RESULT_FAILED
Definition: SDL_test_harness.h:58
SDL_memset
#define SDL_memset
Definition: SDL_dynapi_overrides.h:386
SDL_PRINTF_FORMAT_STRING
#define SDL_PRINTF_FORMAT_STRING
Definition: SDL_stdinc.h:300
SDL_test.h
SDLTest_AssertsPassed
static Uint32 SDLTest_AssertsPassed
Definition: SDL_test_assert.c:42
SDLTest_Log
void SDLTest_Log(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
Prints given message with a timestamp in the TEST category and INFO priority.
Definition: SDL_test_log.c:85
SDLTEST_ASSERT_CHECK_FORMAT
#define SDLTEST_ASSERT_CHECK_FORMAT
Definition: SDL_test_assert.c:33
SDLTEST_MAX_LOGMESSAGE_LENGTH
#define SDLTEST_MAX_LOGMESSAGE_LENGTH
Definition: SDL_test.h:59
TEST_RESULT_PASSED
#define TEST_RESULT_PASSED
Definition: SDL_test_harness.h:57
SDLTest_AssertsFailed
static Uint32 SDLTest_AssertsFailed
Definition: SDL_test_assert.c:39
SDLTest_Assert
void SDLTest_Assert(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription,...)
Assert that logs and break execution flow on failures.
Definition: SDL_test_assert.c:47
SDLTest_LogAssertSummary
void SDLTest_LogAssertSummary()
Logs summary of all assertions (total, pass, fail) since last reset as INFO or ERROR.
Definition: SDL_test_assert.c:123
ASSERT_FAIL
#define ASSERT_FAIL
Fails the assert.
Definition: SDL_test_assert.h:48
SDL_vsnprintf
#define SDL_vsnprintf
Definition: SDL_dynapi_overrides.h:421
SDLTest_ResetAssertSummary
void SDLTest_ResetAssertSummary()
Resets the assert summary counters to zero.
Definition: SDL_test_assert.c:113
SDLTEST_ASSERT_SUMMARY_FORMAT
#define SDLTEST_ASSERT_SUMMARY_FORMAT
Definition: SDL_test_assert.c:36
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
SDLTest_AssertSummaryToTestResult
int SDLTest_AssertSummaryToTestResult()
Converts the current assert summary state to a test result.
Definition: SDL_test_assert.c:139
SDLTest_LogError
void SDLTest_LogError(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
Prints given message with a timestamp in the TEST category and the ERROR priority.
Definition: SDL_test_log.c:103
TEST_RESULT_NO_ASSERT
#define TEST_RESULT_NO_ASSERT
Definition: SDL_test_harness.h:59
SDLTest_AssertCheck
int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription,...)
Assert for test cases that logs but does not break execution flow on failures. Updates assertion coun...
Definition: SDL_test_assert.c:65
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDLTest_AssertPass
void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription,...)
Explicitly pass without checking an assertion condition. Updates assertion counter.
Definition: SDL_test_assert.c:94