src/test/SDL_test_assert.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 04 Jun 2014 10:56:56 -0700
changeset 8820 0e935d5b193a
parent 8149 681eb46b8ac4
child 8830 2d74f0c59cc7
permissions -rw-r--r--
Added annotations to help code analysis tools

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