Add log, assert and harness (partial) to test lib
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Fri, 30 Nov 2012 23:25:34 -0800
changeset 67172acd95060548
parent 6716 1616f6b3738c
child 6718 918ba414168b
Add log, assert and harness (partial) to test lib
include/SDL_log.h
include/SDL_test.h
include/SDL_test_assert.h
include/SDL_test_harness.h
include/SDL_test_log.h
src/SDL_log.c
src/test/SDL_test_assert.c
src/test/SDL_test_harness.c
src/test/SDL_test_log.c
     1.1 --- a/include/SDL_log.h	Thu Nov 29 15:24:56 2012 -0500
     1.2 +++ b/include/SDL_log.h	Fri Nov 30 23:25:34 2012 -0800
     1.3 @@ -59,8 +59,9 @@
     1.4   *  \brief The predefined log categories
     1.5   *
     1.6   *  By default the application category is enabled at the INFO level,
     1.7 - *  the assert category is enabled at the WARN level, and all other
     1.8 - *  categories are enabled at the CRITICAL level.
     1.9 + *  the assert category is enabled at the WARN level, test is enabled
    1.10 + *  at the VERBOSE level and all other categories are enabled at the 
    1.11 + *  CRITICAL level.
    1.12   */
    1.13  enum
    1.14  {
    1.15 @@ -72,6 +73,7 @@
    1.16      SDL_LOG_CATEGORY_VIDEO,
    1.17      SDL_LOG_CATEGORY_RENDER,
    1.18      SDL_LOG_CATEGORY_INPUT,
    1.19 +    SDL_LOG_CATEGORY_TEST,
    1.20  
    1.21      /* Reserved for future SDL library use */
    1.22      SDL_LOG_CATEGORY_RESERVED1,
     2.1 --- a/include/SDL_test.h	Thu Nov 29 15:24:56 2012 -0500
     2.2 +++ b/include/SDL_test.h	Fri Nov 30 23:25:34 2012 -0800
     2.3 @@ -36,6 +36,9 @@
     2.4  #include "SDL_test_fuzzer.h"
     2.5  #include "SDL_test_crc32.h"
     2.6  #include "SDL_test_md5.h"
     2.7 +#include "SDL_test_log.h"
     2.8 +#include "SDL_test_assert.h"
     2.9 +#include "SDL_test_harness.h"
    2.10  
    2.11  #include "begin_code.h"
    2.12  /* Set up for C function definitions, even when using C++ */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/include/SDL_test_assert.h	Fri Nov 30 23:25:34 2012 -0800
     3.3 @@ -0,0 +1,83 @@
     3.4 +/*
     3.5 +  Simple DirectMedia Layer
     3.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     3.7 +
     3.8 +  This software is provided 'as-is', without any express or implied
     3.9 +  warranty.  In no event will the authors be held liable for any damages
    3.10 +  arising from the use of this software.
    3.11 +
    3.12 +  Permission is granted to anyone to use this software for any purpose,
    3.13 +  including commercial applications, and to alter it and redistribute it
    3.14 +  freely, subject to the following restrictions:
    3.15 +
    3.16 +  1. The origin of this software must not be misrepresented; you must not
    3.17 +     claim that you wrote the original software. If you use this software
    3.18 +     in a product, an acknowledgment in the product documentation would be
    3.19 +     appreciated but is not required.
    3.20 +  2. Altered source versions must be plainly marked as such, and must not be
    3.21 +     misrepresented as being the original software.
    3.22 +  3. This notice may not be removed or altered from any source distribution.
    3.23 +*/
    3.24 +
    3.25 +/**
    3.26 + *  \file SDL_test_assert.h
    3.27 + *  
    3.28 + *  Include file for SDL test framework.
    3.29 + *
    3.30 + *  This code is a part of the SDL2_test library, not the main SDL library.
    3.31 + */
    3.32 +
    3.33 +/* 
    3.34 + *
    3.35 + * Assert API for test code and test cases
    3.36 + *
    3.37 + */
    3.38 +
    3.39 +#ifndef _SDL_test_assert_h
    3.40 +#define _SDL_test_assert_h
    3.41 +
    3.42 +#include "begin_code.h"
    3.43 +/* Set up for C function definitions, even when using C++ */
    3.44 +#ifdef __cplusplus
    3.45 +/* *INDENT-OFF* */
    3.46 +extern "C" {
    3.47 +/* *INDENT-ON* */
    3.48 +#endif
    3.49 +
    3.50 +/**
    3.51 + * \brief Assert that logs and break execution flow on failures.
    3.52 + *
    3.53 + * \param assertCondition Evaluated condition or variable to assert; fail (==0) or pass (!=0).
    3.54 + * \param assertDescription Message to log with the assert describing it.
    3.55 + */
    3.56 +void SDLTest_Assert(int assertCondition, char *assertDescription);
    3.57 +
    3.58 +/**
    3.59 + * \brief Assert for test cases that logs but does not break execution flow on failures.
    3.60 + *
    3.61 + * \param assertCondition Evaluated condition or variable to assert; fail (==0) or pass (!=0).
    3.62 + * \param assertDescription Message to log with the assert describing it.
    3.63 + */
    3.64 +void SDLTest_AssertCheck(int assertCondition, char *assertDescription);
    3.65 +
    3.66 +/**
    3.67 + * \brief Resets the assert summary counters to zero.
    3.68 + */
    3.69 +void SDLTest_ResetAssertSummary();
    3.70 +
    3.71 +/**
    3.72 + * \brief Logs summary of all assertions (total, pass, fail) since last reset as INFO or ERROR.
    3.73 + *
    3.74 + */
    3.75 +void SDLTest_LogAssertSummary();
    3.76 +
    3.77 +#ifdef __cplusplus
    3.78 +/* *INDENT-OFF* */
    3.79 +}
    3.80 +/* *INDENT-ON* */
    3.81 +#endif
    3.82 +#include "close_code.h"
    3.83 +
    3.84 +#endif /* _SDL_test_assert_h */
    3.85 +
    3.86 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/include/SDL_test_harness.h	Fri Nov 30 23:25:34 2012 -0800
     4.3 @@ -0,0 +1,116 @@
     4.4 +/*
     4.5 +  Simple DirectMedia Layer
     4.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4.7 +
     4.8 +  This software is provided 'as-is', without any express or implied
     4.9 +  warranty.  In no event will the authors be held liable for any damages
    4.10 +  arising from the use of this software.
    4.11 +
    4.12 +  Permission is granted to anyone to use this software for any purpose,
    4.13 +  including commercial applications, and to alter it and redistribute it
    4.14 +  freely, subject to the following restrictions:
    4.15 +
    4.16 +  1. The origin of this software must not be misrepresented; you must not
    4.17 +     claim that you wrote the original software. If you use this software
    4.18 +     in a product, an acknowledgment in the product documentation would be
    4.19 +     appreciated but is not required.
    4.20 +  2. Altered source versions must be plainly marked as such, and must not be
    4.21 +     misrepresented as being the original software.
    4.22 +  3. This notice may not be removed or altered from any source distribution.
    4.23 +*/
    4.24 +
    4.25 +/**
    4.26 + *  \file SDL_test_harness.h
    4.27 + *  
    4.28 + *  Include file for SDL test framework.
    4.29 + *
    4.30 + *  This code is a part of the SDL2_test library, not the main SDL library.
    4.31 + */
    4.32 +
    4.33 +/*
    4.34 +  Defines types for test case definitions and the test execution harness API.
    4.35 +  
    4.36 +  Based on original GSOC code by Markus Kauppila <markus.kauppila@gmail.com>
    4.37 +*/
    4.38 +
    4.39 +#ifndef _SDL_test_harness_h
    4.40 +#define _SDL_test_harness_h
    4.41 +
    4.42 +#include "begin_code.h"
    4.43 +/* Set up for C function definitions, even when using C++ */
    4.44 +#ifdef __cplusplus
    4.45 +/* *INDENT-OFF* */
    4.46 +extern "C" {
    4.47 +/* *INDENT-ON* */
    4.48 +#endif
    4.49 +
    4.50 +
    4.51 +//! Definitions for test case structures
    4.52 +#define TEST_ENABLED  1
    4.53 +#define TEST_DISABLED 0
    4.54 +
    4.55 +//! Definitions of assert results
    4.56 +#define ASSERT_PASS		1
    4.57 +#define ASSERT_FAIL		0
    4.58 +
    4.59 +//! Definition of all the possible test return values of the test case method
    4.60 +#define TEST_ABORTED		-1
    4.61 +#define TEST_COMPLETED		 0
    4.62 +#define TEST_SKIPPED		 1
    4.63 +
    4.64 +//! Definition of all the possible test results for the harness
    4.65 +#define TEST_RESULT_PASSED			0
    4.66 +#define TEST_RESULT_FAILED			1
    4.67 +#define TEST_RESULT_NO_ASSERT			2
    4.68 +#define TEST_RESULT_SKIPPED			3
    4.69 +#define TEST_RESULT_KILLED			4
    4.70 +#define TEST_RESULT_SETUP_FAILURE		5
    4.71 +
    4.72 +//!< Function pointer to a test case setup function (run before every test)
    4.73 +typedef void (*SDLTest_TestCaseSetUpFp)(void *arg);
    4.74 +
    4.75 +//!< Function pointer to a test case function
    4.76 +typedef void (*SDLTest_TestCaseFp)(void *arg);
    4.77 +
    4.78 +//!< Function pointer to a test case teardown function (run after every test)
    4.79 +typedef void  (*SDLTest_TestCaseTearDownFp)(void *arg);
    4.80 +
    4.81 +/**
    4.82 + * Holds information about a single test case.
    4.83 + */
    4.84 +typedef struct SDLTest_TestCaseReference {
    4.85 +	/*!< Func2Stress */
    4.86 +	SDLTest_TestCaseFp testCase;
    4.87 +	/*!< Short name (or function name) "Func2Stress" */
    4.88 +	char *name;
    4.89 +	/*!< Long name or full description "This test pushes func2() to the limit." */
    4.90 +	char *description;
    4.91 +	/*!< Set to TEST_ENABLED or TEST_DISABLED (test won't be run) */
    4.92 +	int enabled;
    4.93 +} SDLTest_TestCaseReference;
    4.94 +
    4.95 +/**
    4.96 + * Holds information about a test suite (multiple test cases).
    4.97 + */
    4.98 +typedef struct TestSuiteReference {
    4.99 +	/*!< "PlatformSuite" */
   4.100 +	char *name;
   4.101 +	/*!< The function that is run before each test. NULL skips. */
   4.102 +	SDLTest_TestCaseSetUpFp testSetUp;
   4.103 +	/*!< The test cases that are run as part of the suite. Last item should be NULL. */
   4.104 +	const SDLTest_TestCaseReference **testCases;
   4.105 +	/*!< The function that is run after each test. NULL skips. */
   4.106 +	SDLTest_TestCaseTearDownFp testTearDown;
   4.107 +} TestSuiteReference;
   4.108 +
   4.109 +/* Ends C function definitions when using C++ */
   4.110 +#ifdef __cplusplus
   4.111 +/* *INDENT-OFF* */
   4.112 +}
   4.113 +/* *INDENT-ON* */
   4.114 +#endif
   4.115 +#include "close_code.h"
   4.116 +
   4.117 +#endif /* _SDL_test_harness_h */
   4.118 +
   4.119 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/include/SDL_test_log.h	Fri Nov 30 23:25:34 2012 -0800
     5.3 @@ -0,0 +1,71 @@
     5.4 +/*
     5.5 +  Simple DirectMedia Layer
     5.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     5.7 +
     5.8 +  This software is provided 'as-is', without any express or implied
     5.9 +  warranty.  In no event will the authors be held liable for any damages
    5.10 +  arising from the use of this software.
    5.11 +
    5.12 +  Permission is granted to anyone to use this software for any purpose,
    5.13 +  including commercial applications, and to alter it and redistribute it
    5.14 +  freely, subject to the following restrictions:
    5.15 +
    5.16 +  1. The origin of this software must not be misrepresented; you must not
    5.17 +     claim that you wrote the original software. If you use this software
    5.18 +     in a product, an acknowledgment in the product documentation would be
    5.19 +     appreciated but is not required.
    5.20 +  2. Altered source versions must be plainly marked as such, and must not be
    5.21 +     misrepresented as being the original software.
    5.22 +  3. This notice may not be removed or altered from any source distribution.
    5.23 +*/
    5.24 +
    5.25 +/**
    5.26 + *  \file SDL_test_log.h
    5.27 + *  
    5.28 + *  Include file for SDL test framework.
    5.29 + *
    5.30 + *  This code is a part of the SDL2_test library, not the main SDL library.
    5.31 + */
    5.32 +
    5.33 +/* 
    5.34 + *
    5.35 + *  Wrapper to log in the TEST category
    5.36 + *
    5.37 + */
    5.38 +
    5.39 +#ifndef _SDL_test_log_h
    5.40 +#define _SDL_test_log_h
    5.41 +
    5.42 +#include "begin_code.h"
    5.43 +/* Set up for C function definitions, even when using C++ */
    5.44 +#ifdef __cplusplus
    5.45 +/* *INDENT-OFF* */
    5.46 +extern "C" {
    5.47 +/* *INDENT-ON* */
    5.48 +#endif
    5.49 +
    5.50 +/**
    5.51 + * \brief Prints given message with a timestamp in the TEST category and INFO priority.
    5.52 + *
    5.53 + * \param fmt Message to be logged
    5.54 + */
    5.55 +void SDLTest_Log(char *fmt, ...);
    5.56 +
    5.57 +/**
    5.58 + * \brief Prints given message with a timestamp in the TEST category and the ERROR priority.
    5.59 + *
    5.60 + * \param fmt Message to be logged
    5.61 + */
    5.62 +void SDLTest_LogError(char *fmt, ...);
    5.63 +
    5.64 +/* Ends C function definitions when using C++ */
    5.65 +#ifdef __cplusplus
    5.66 +/* *INDENT-OFF* */
    5.67 +}
    5.68 +/* *INDENT-ON* */
    5.69 +#endif
    5.70 +#include "close_code.h"
    5.71 +
    5.72 +#endif /* _SDL_test_log_h */
    5.73 +
    5.74 +/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/SDL_log.c	Thu Nov 29 15:24:56 2012 -0500
     6.2 +++ b/src/SDL_log.c	Fri Nov 30 23:25:34 2012 -0800
     6.3 @@ -37,6 +37,7 @@
     6.4  #define DEFAULT_PRIORITY                SDL_LOG_PRIORITY_CRITICAL
     6.5  #define DEFAULT_ASSERT_PRIORITY         SDL_LOG_PRIORITY_WARN
     6.6  #define DEFAULT_APPLICATION_PRIORITY    SDL_LOG_PRIORITY_INFO
     6.7 +#define DEFAULT_TEST_PRIORITY           SDL_LOG_PRIORITY_VERBOSE
     6.8  
     6.9  typedef struct SDL_LogLevel
    6.10  {
    6.11 @@ -54,6 +55,7 @@
    6.12  static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
    6.13  static SDL_LogPriority SDL_assert_priority = DEFAULT_ASSERT_PRIORITY;
    6.14  static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
    6.15 +static SDL_LogPriority SDL_test_priority = DEFAULT_TEST_PRIORITY;
    6.16  static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
    6.17  static void *SDL_log_userdata = NULL;
    6.18  
    6.19 @@ -135,7 +137,9 @@
    6.20          }
    6.21      }
    6.22  
    6.23 -    if (category == SDL_LOG_CATEGORY_APPLICATION) {
    6.24 +    if (category == SDL_LOG_CATEGORY_TEST) {
    6.25 +        return SDL_test_priority;    
    6.26 +    } else if (category == SDL_LOG_CATEGORY_APPLICATION) {
    6.27          return SDL_application_priority;
    6.28      } else if (category == SDL_LOG_CATEGORY_ASSERT) {
    6.29          return SDL_assert_priority;
    6.30 @@ -158,6 +162,7 @@
    6.31      SDL_default_priority = DEFAULT_PRIORITY;
    6.32      SDL_assert_priority = DEFAULT_ASSERT_PRIORITY;
    6.33      SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
    6.34 +    SDL_test_priority = DEFAULT_TEST_PRIORITY;
    6.35  }
    6.36  
    6.37  void
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/test/SDL_test_assert.c	Fri Nov 30 23:25:34 2012 -0800
     7.3 @@ -0,0 +1,96 @@
     7.4 +/*
     7.5 +  Simple DirectMedia Layer
     7.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     7.7 +
     7.8 +  This software is provided 'as-is', without any express or implied
     7.9 +  warranty.  In no event will the authors be held liable for any damages
    7.10 +  arising from the use of this software.
    7.11 +
    7.12 +  Permission is granted to anyone to use this software for any purpose,
    7.13 +  including commercial applications, and to alter it and redistribute it
    7.14 +  freely, subject to the following restrictions:
    7.15 +
    7.16 +  1. The origin of this software must not be misrepresented; you must not
    7.17 +     claim that you wrote the original software. If you use this software
    7.18 +     in a product, an acknowledgment in the product documentation would be
    7.19 +     appreciated but is not required.
    7.20 +  2. Altered source versions must be plainly marked as such, and must not be
    7.21 +     misrepresented as being the original software.
    7.22 +  3. This notice may not be removed or altered from any source distribution.
    7.23 +*/
    7.24 +
    7.25 +/*
    7.26 +
    7.27 + Used by the test framework and test cases. 
    7.28 +
    7.29 +*/
    7.30 +
    7.31 +#include "SDL_config.h"
    7.32 +
    7.33 +#include "SDL_test.h"
    7.34 +
    7.35 +/*! \brief counts the failed asserts */
    7.36 +static Uint32 SDLTest_testAssertsFailed = 0;
    7.37 +
    7.38 +/*! \brief counts the passed asserts */
    7.39 +static Uint32 SDLTest_testAssertsPassed = 0;
    7.40 +
    7.41 +/* Assert check message format */
    7.42 +const char *SDLTest_AssertCheckFmt = "Assert %s: %s";
    7.43 +
    7.44 +/* Assert summary message format */
    7.45 +const char *SDLTest_AssertSummaryFmt = "Assert Summary: Total=%d Passed=%d Failed=%d";
    7.46 +
    7.47 +/*
    7.48 + *  Assert that logs and break execution flow on failures (i.e. for harness errors).
    7.49 + */
    7.50 +void SDLTest_Assert(int assertCondition, char *assertDescription)
    7.51 +{
    7.52 +	SDLTest_AssertCheck(assertCondition, assertDescription);
    7.53 +	SDL_assert((assertCondition));
    7.54 +}
    7.55 +
    7.56 +/*
    7.57 + * Assert that logs but does not break execution flow on failures (i.e. for test cases).
    7.58 + */
    7.59 +void SDLTest_AssertCheck(int assertCondition, char *assertDescription)
    7.60 +{
    7.61 +	char *fmt = (char *)SDLTest_AssertCheckFmt;
    7.62 +	if (assertCondition)
    7.63 +	{
    7.64 +		SDLTest_testAssertsPassed++;
    7.65 +		SDLTest_Log(fmt, "Passed", assertDescription);
    7.66 +	} 
    7.67 +	else 
    7.68 +	{
    7.69 +		SDLTest_testAssertsFailed++;
    7.70 +		SDLTest_LogError(fmt, "Failed", assertDescription);
    7.71 +	}
    7.72 +}
    7.73 +
    7.74 +/*
    7.75 + * Resets the assert summary counters to zero.
    7.76 + */
    7.77 +void SDLTest_ResetAssertSummary()
    7.78 +{
    7.79 +	SDLTest_testAssertsPassed = 0;
    7.80 +	SDLTest_testAssertsFailed = 0;
    7.81 +}
    7.82 +
    7.83 +/*
    7.84 + * Logs summary of all assertions (total, pass, fail) since last reset 
    7.85 + * as INFO (failed==0) or ERROR (failed > 0).
    7.86 + */
    7.87 +void SDLTest_LogAssertSummary()
    7.88 +{
    7.89 +	char *fmt = (char *)SDLTest_AssertSummaryFmt;
    7.90 +	Uint32 totalAsserts = SDLTest_testAssertsPassed + SDLTest_testAssertsFailed;
    7.91 +	if (SDLTest_testAssertsFailed == 0)
    7.92 +	{
    7.93 +		SDLTest_Log(fmt, totalAsserts, SDLTest_testAssertsPassed, SDLTest_testAssertsFailed);
    7.94 +	} 
    7.95 +	else 
    7.96 +	{
    7.97 +		SDLTest_LogError(fmt, totalAsserts, SDLTest_testAssertsPassed, SDLTest_testAssertsFailed);
    7.98 +	}
    7.99 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/test/SDL_test_harness.c	Fri Nov 30 23:25:34 2012 -0800
     8.3 @@ -0,0 +1,142 @@
     8.4 +/*
     8.5 +  Simple DirectMedia Layer
     8.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     8.7 +
     8.8 +  This software is provided 'as-is', without any express or implied
     8.9 +  warranty.  In no event will the authors be held liable for any damages
    8.10 +  arising from the use of this software.
    8.11 +
    8.12 +  Permission is granted to anyone to use this software for any purpose,
    8.13 +  including commercial applications, and to alter it and redistribute it
    8.14 +  freely, subject to the following restrictions:
    8.15 +
    8.16 +  1. The origin of this software must not be misrepresented; you must not
    8.17 +     claim that you wrote the original software. If you use this software
    8.18 +     in a product, an acknowledgment in the product documentation would be
    8.19 +     appreciated but is not required.
    8.20 +  2. Altered source versions must be plainly marked as such, and must not be
    8.21 +     misrepresented as being the original software.
    8.22 +  3. This notice may not be removed or altered from any source distribution.
    8.23 +*/
    8.24 +
    8.25 +#include "SDL_config.h"
    8.26 +
    8.27 +#include "SDL_test.h"
    8.28 +
    8.29 +// TODO: port over harness
    8.30 +
    8.31 +/**
    8.32 + * Generates a random run seed string for the harness. The generated seed
    8.33 + * will contain alphanumeric characters (0-9A-Z).
    8.34 + *
    8.35 + * Note: The returned string needs to be deallocated by the caller.
    8.36 + *
    8.37 + * \param length The length of the seed string to generate
    8.38 + *
    8.39 + * \returns The generated seed string
    8.40 + */
    8.41 +char *
    8.42 +SDLTest_GenerateRunSeed(const int length)
    8.43 +{
    8.44 +	char *seed = NULL;
    8.45 +	SDLTest_RandomContext randomContext;
    8.46 +	int counter;
    8.47 +
    8.48 +	// Sanity check input
    8.49 +	if (length <= 0) {
    8.50 +		SDLTest_LogError("The length of the harness seed must be >0.");
    8.51 +		return NULL;
    8.52 +	}
    8.53 +
    8.54 +	// Allocate output buffer
    8.55 +	seed = (char *)SDL_malloc((length + 1) * sizeof(char));
    8.56 +	if (seed == NULL) {
    8.57 +		SDLTest_LogError("SDL_malloc for run seed output buffer failed.");
    8.58 +		return NULL;
    8.59 +	}
    8.60 +
    8.61 +	// Generate a random string of alphanumeric characters
    8.62 +	SDLTest_RandomInitTime(&randomContext);
    8.63 +	for (counter = 0; counter < length - 1; ++counter) {
    8.64 +		unsigned int number = SDLTest_Random(&randomContext);
    8.65 +		char ch = (char) (number % (91 - 48)) + 48;
    8.66 +		if (ch >= 58 && ch <= 64) {
    8.67 +			ch = 65;
    8.68 +		}
    8.69 +		seed[counter] = ch;
    8.70 +	}
    8.71 +	seed[counter] = '\0';
    8.72 +
    8.73 +	return seed;
    8.74 +}
    8.75 +
    8.76 +/**
    8.77 + * Generates an execution key for the fuzzer.
    8.78 + *
    8.79 + * \param runSeed		The run seed to use
    8.80 + * \param suiteName		The name of the test suite
    8.81 + * \param testName		The name of the test
    8.82 + * \param iteration		The iteration count
    8.83 + *
    8.84 + * \returns The generated execution key to initialize the fuzzer with.
    8.85 + *
    8.86 + */
    8.87 +Uint64
    8.88 +SDLTest_GenerateExecKey(char *runSeed, char *suiteName, char *testName, int iteration)
    8.89 +{
    8.90 +	SDLTest_Md5Context md5Context;
    8.91 +	Uint64 *keys;
    8.92 +	char iterationString[16];
    8.93 +	Uint32 runSeedLength;
    8.94 +	Uint32 suiteNameLength;
    8.95 +	Uint32 testNameLength;
    8.96 +	Uint32 iterationStringLength;
    8.97 +	Uint32 entireStringLength;
    8.98 +	char *buffer;
    8.99 +
   8.100 +	if (runSeed == NULL || strlen(runSeed)==0) {
   8.101 +		SDLTest_LogError("Invalid runSeed string.");
   8.102 +		return -1;
   8.103 +	}
   8.104 +
   8.105 +	if (suiteName == NULL || strlen(suiteName)==0) {
   8.106 +		SDLTest_LogError("Invalid suiteName string.");
   8.107 +		return -1;
   8.108 +	}
   8.109 +
   8.110 +	if (testName == NULL || strlen(testName)==0) {
   8.111 +		SDLTest_LogError("Invalid testName string.");
   8.112 +		return -1;
   8.113 +	}
   8.114 +
   8.115 +	if (iteration <= 0) {
   8.116 +		SDLTest_LogError("Invalid iteration count.");
   8.117 +		return -1;
   8.118 +	}
   8.119 +
   8.120 +	// Convert iteration number into a string
   8.121 +	memset(iterationString, 0, sizeof(iterationString));
   8.122 +	SDL_snprintf(iterationString, sizeof(iterationString) - 1, "%d", iteration);
   8.123 +
   8.124 +	// Combine the parameters into single string
   8.125 +	runSeedLength = strlen(runSeed);
   8.126 +	suiteNameLength = strlen(suiteName);
   8.127 +	testNameLength = strlen(testName);
   8.128 +	iterationStringLength = strlen(iterationString);
   8.129 +	entireStringLength  = runSeedLength + suiteNameLength + testNameLength + iterationStringLength + 1;
   8.130 +	buffer = (char *)SDL_malloc(entireStringLength);
   8.131 +	if (buffer == NULL) {
   8.132 +		SDLTest_LogError("SDL_malloc failed to allocate buffer for execKey generation.");
   8.133 +		return 0;
   8.134 +	}
   8.135 +	SDL_snprintf(buffer, entireStringLength, "%s%s%s%d", runSeed, suiteName, testName, iteration);
   8.136 +
   8.137 +	// Hash string and use half of the digest as 64bit exec key
   8.138 +	SDLTest_Md5Init(&md5Context);
   8.139 +	SDLTest_Md5Update(&md5Context, (unsigned char *)buffer, entireStringLength);
   8.140 +	SDLTest_Md5Final(&md5Context);
   8.141 +	SDL_free(buffer);
   8.142 +	keys = (Uint64 *)md5Context.digest;
   8.143 +
   8.144 +	return keys[0];
   8.145 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/test/SDL_test_log.c	Fri Nov 30 23:25:34 2012 -0800
     9.3 @@ -0,0 +1,100 @@
     9.4 +/*
     9.5 +  Simple DirectMedia Layer
     9.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     9.7 +
     9.8 +  This software is provided 'as-is', without any express or implied
     9.9 +  warranty.  In no event will the authors be held liable for any damages
    9.10 +  arising from the use of this software.
    9.11 +
    9.12 +  Permission is granted to anyone to use this software for any purpose,
    9.13 +  including commercial applications, and to alter it and redistribute it
    9.14 +  freely, subject to the following restrictions:
    9.15 +
    9.16 +  1. The origin of this software must not be misrepresented; you must not
    9.17 +     claim that you wrote the original software. If you use this software
    9.18 +     in a product, an acknowledgment in the product documentation would be
    9.19 +     appreciated but is not required.
    9.20 +  2. Altered source versions must be plainly marked as such, and must not be
    9.21 +     misrepresented as being the original software.
    9.22 +  3. This notice may not be removed or altered from any source distribution.
    9.23 +*/
    9.24 +
    9.25 +/*
    9.26 +
    9.27 + Used by the test framework and test cases. 
    9.28 +
    9.29 +*/
    9.30 +
    9.31 +#include "SDL_config.h"
    9.32 +
    9.33 +#include <stdarg.h> /* va_list */
    9.34 +#include <time.h>
    9.35 +
    9.36 +#include "SDL_test.h"
    9.37 +
    9.38 +/* 
    9.39 + * Note: Maximum size of SDLTest log message is less than SDLs limit 
    9.40 + * to ensure we can fit additional information such as the timestamp. 
    9.41 + */
    9.42 +#define SDLTEST_MAX_LOGMESSAGE_LENGTH	3584
    9.43 +
    9.44 +/*!
    9.45 + * Converts unix timestamp to its ascii representation in localtime
    9.46 + *
    9.47 + * Note: Uses a static buffer internally, so the return value
    9.48 + * isn't valid after the next call of this function. If you
    9.49 + * want to retain the return value, make a copy of it.
    9.50 + *
    9.51 + * \param timestamp A Timestamp, i.e. time(0)
    9.52 + *
    9.53 + * \return Ascii representation of the timestamp in localtime
    9.54 + */
    9.55 +char *SDLTest_TimestampToString(const time_t timestamp) 
    9.56 +{
    9.57 +	time_t copy;
    9.58 +	static char buffer[256];
    9.59 +	struct tm *local;
    9.60 +
    9.61 +	memset(buffer, 0, sizeof(buffer));\
    9.62 +	copy = timestamp;
    9.63 +	local = localtime(&copy);
    9.64 +	strftime(buffer, sizeof(buffer), "%a %Y-%m-%d %H:%M:%S %Z", local);
    9.65 +
    9.66 +	return buffer;
    9.67 +}
    9.68 +
    9.69 +/*
    9.70 + * Prints given message with a timestamp in the TEST category and INFO priority.
    9.71 + */
    9.72 +void SDLTest_Log(char *fmt, ...)
    9.73 +{
    9.74 +	va_list list;
    9.75 +	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    9.76 +
    9.77 +	// Print log message into a buffer
    9.78 +	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    9.79 +	va_start(list, fmt);
    9.80 +	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
    9.81 +	va_end(list);
    9.82 +
    9.83 +	// Log with timestamp and newline
    9.84 +	SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO, "%s: %s\n", SDLTest_TimestampToString(time(0)), logMessage);
    9.85 +}
    9.86 +
    9.87 +/*
    9.88 + * Prints given message with a timestamp in the TEST category and the ERROR priority.
    9.89 + */
    9.90 +void SDLTest_LogError(char *fmt, ...)
    9.91 +{
    9.92 +	va_list list;
    9.93 +	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    9.94 +
    9.95 +	// Print log message into a buffer
    9.96 +	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    9.97 +	va_start(list, fmt);
    9.98 +	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
    9.99 +	va_end(list);
   9.100 +
   9.101 +	// Log with timestamp and newline
   9.102 +	SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR, "%s: %s\n", SDLTest_TimestampToString(time(0)), logMessage);
   9.103 +}