From 4a4503ca2294e8e9273aca9face3ab7814989137 Mon Sep 17 00:00:00 2001 From: Andreas Schiffler Date: Mon, 14 Jan 2013 08:14:53 -0800 Subject: [PATCH] Added timer test suite --- .../testautomation_vs2010.vcxproj | 1 + .../testautomation_vs2012.vcxproj | 1 + test/Makefile.in | 3 +- test/testautomation_suites.h | 2 + test/testautomation_timer.c | 202 ++++++++++++++++++ 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 test/testautomation_timer.c diff --git a/VisualC/tests/testautomation/testautomation_vs2010.vcxproj b/VisualC/tests/testautomation/testautomation_vs2010.vcxproj index ab4bed2be..4c7acf808 100644 --- a/VisualC/tests/testautomation/testautomation_vs2010.vcxproj +++ b/VisualC/tests/testautomation/testautomation_vs2010.vcxproj @@ -189,6 +189,7 @@ + diff --git a/VisualC/tests/testautomation/testautomation_vs2012.vcxproj b/VisualC/tests/testautomation/testautomation_vs2012.vcxproj index 8b9f939c7..9a831220e 100644 --- a/VisualC/tests/testautomation/testautomation_vs2012.vcxproj +++ b/VisualC/tests/testautomation/testautomation_vs2012.vcxproj @@ -193,6 +193,7 @@ + diff --git a/test/Makefile.in b/test/Makefile.in index 463a1271d..07f11d140 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -80,7 +80,8 @@ testautomation$(EXE): $(srcdir)/testautomation.c \ $(srcdir)/testautomation_keyboard.c \ $(srcdir)/testautomation_video.c \ $(srcdir)/testautomation_syswm.c \ - $(srcdir)/testautomation_mouse.c + $(srcdir)/testautomation_mouse.c \ + $(srcdir)/testautomation_timer.c $(CC) -o $@ $^ $(CFLAGS) -lSDL2_test $(LIBS) testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c diff --git a/test/testautomation_suites.h b/test/testautomation_suites.h index af09d1c1c..cea10d40b 100644 --- a/test/testautomation_suites.h +++ b/test/testautomation_suites.h @@ -21,6 +21,7 @@ extern SDLTest_TestSuiteReference surfaceTestSuite; extern SDLTest_TestSuiteReference syswmTestSuite; extern SDLTest_TestSuiteReference videoTestSuite; extern SDLTest_TestSuiteReference mouseTestSuite; +extern SDLTest_TestSuiteReference timerTestSuite; // All test suites SDLTest_TestSuiteReference *testSuites[] = { @@ -36,6 +37,7 @@ SDLTest_TestSuiteReference *testSuites[] = { &syswmTestSuite, &videoTestSuite, &mouseTestSuite, + &timerTestSuite, NULL }; diff --git a/test/testautomation_timer.c b/test/testautomation_timer.c new file mode 100644 index 000000000..17f2f65e1 --- /dev/null +++ b/test/testautomation_timer.c @@ -0,0 +1,202 @@ +/** + * Timer test suite + */ + +#include + +#include "SDL.h" +#include "SDL_test.h" + +/* Flag indicating if the param should be checked */ +int _paramCheck = 0; + +/* Userdata value to check */ +int _paramValue = 0; + +/* Flag indicating that the callback was called */ +int _timerCallbackCalled = 0; + +/* Fixture */ + +void +_timerSetUp(void *arg) +{ + /* Start SDL timer subsystem */ + int ret = SDL_InitSubSystem( SDL_INIT_TIMER ); + SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_TIMER)"); + SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_TIMER)"); + if (ret != 0) { + SDLTest_LogError("%s", SDL_GetError()); + } +} + +/* Test case functions */ + +/** + * @brief Call to SDL_GetPerformanceCounter + */ +int +timer_getPerformanceCounter(void *arg) +{ + Uint64 result; + + result = SDL_GetPerformanceCounter(); + SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()"); + SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result); + + return TEST_COMPLETED; +} + +/** + * @brief Call to SDL_GetPerformanceFrequency + */ +int +timer_getPerformanceFrequency(void *arg) +{ + Uint64 result; + + result = SDL_GetPerformanceFrequency(); + SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()"); + SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result); + + return TEST_COMPLETED; +} + +/** + * @brief Call to SDL_Delay and SDL_GetTicks + */ +int +timer_delayAndGetTicks(void *arg) +{ + const Uint32 testDelay = 100; + const Uint32 marginOfError = 25; + Uint32 result; + Uint32 result2; + Uint32 difference; + + /* Zero delay */ + SDL_Delay(0); + SDLTest_AssertPass("Call to SDL_Delay(0)"); + + /* Non-zero delay */ + SDL_Delay(1); + SDLTest_AssertPass("Call to SDL_Delay(1)"); + + SDL_Delay(SDLTest_RandomIntegerInRange(5, 15)); + SDLTest_AssertPass("Call to SDL_Delay()"); + + /* Get ticks count - should be non-zero by now */ + result = SDL_GetTicks(); + SDLTest_AssertPass("Call to SDL_GetTicks()"); + SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %d", result); + + /* Delay a bit longer and mesure ticks and verify difference */ + SDL_Delay(testDelay); + SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay); + result2 = SDL_GetTicks(); + SDLTest_AssertPass("Call to SDL_GetTicks()"); + SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %d", result2); + difference = result2 - result; + SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %d", testDelay - marginOfError, difference); + SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %d", testDelay + marginOfError, difference); + + return TEST_COMPLETED; +} + +/* Test callback */ +Uint32 _timerTestCallback(Uint32 interval, void *param) +{ + _timerCallbackCalled = 1; + + if (_paramCheck != 0) { + SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL"); + if (param != NULL) { + SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param); + } + } + + return 0; + return interval; +} + +/** + * @brief Call to SDL_AddTimer and SDL_RemoveTimer + */ +int +timer_addRemoveTimer(void *arg) +{ + SDL_TimerID id; + SDL_bool result; + int param; + + /* Reset state */ + _paramCheck = 0; + _timerCallbackCalled = 0; + + /* Set timer with a long delay */ + id = SDL_AddTimer(10000, _timerTestCallback, NULL); + SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)"); + SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id); + + /* Remove timer again and check that callback was not called */ + result = SDL_RemoveTimer(id); + SDLTest_AssertPass("Call to SDL_RemoveTimer()"); + SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result); + SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled); + + /* Try to temove timer again (should be a NOOP) */ + result = SDL_RemoveTimer(id); + SDLTest_AssertPass("Call to SDL_RemoveTimer()"); + SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result); + + /* Reset state */ + param = SDLTest_RandomIntegerInRange(-1024, 1024); + _paramCheck = 1; + _paramValue = param; + _timerCallbackCalled = 0; + + /* Set timer with a short delay */ + id = SDL_AddTimer(10, _timerTestCallback, (void *)¶m); + SDLTest_AssertPass("Call to SDL_AddTimer(10, param)"); + SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id); + + /* Wait to let timer trigger callback */ + SDL_Delay(100); + SDLTest_AssertPass("Call to SDL_Delay(100)"); + + /* Remove timer again and check that callback was called */ + result = SDL_RemoveTimer(id); + SDLTest_AssertPass("Call to SDL_RemoveTimer()"); + SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result); + SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled); + + return TEST_COMPLETED; +} + +/* ================= Test References ================== */ + +/* Timer test cases */ +static const SDLTest_TestCaseReference timerTest1 = + { (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED }; + +static const SDLTest_TestCaseReference timerTest2 = + { (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED }; + +static const SDLTest_TestCaseReference timerTest3 = + { (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED }; + +static const SDLTest_TestCaseReference timerTest4 = + { (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED }; + +/* Sequence of Timer test cases */ +static const SDLTest_TestCaseReference *timerTests[] = { + &timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL +}; + +/* Timer test suite (global) */ +SDLTest_TestSuiteReference timerTestSuite = { + "Timer", + _timerSetUp, + timerTests, + NULL +};