cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

testautomation_events.c (6161B)


      1/**
      2 * Events test suite
      3 */
      4
      5#include <stdio.h>
      6
      7#include "SDL.h"
      8#include "SDL_test.h"
      9
     10/* ================= Test Case Implementation ================== */
     11
     12/* Test case functions */
     13
     14/* Flag indicating if the userdata should be checked */
     15int _userdataCheck = 0;
     16
     17/* Userdata value to check */
     18int _userdataValue = 0;
     19
     20/* Flag indicating that the filter was called */
     21int _eventFilterCalled = 0;
     22
     23/* Userdata values for event */
     24int _userdataValue1 = 1;
     25int _userdataValue2 = 2;
     26
     27/* Event filter that sets some flags and optionally checks userdata */
     28int _events_sampleNullEventFilter(void *userdata, SDL_Event *event)
     29{
     30   _eventFilterCalled = 1;
     31
     32   if (_userdataCheck != 0) {
     33       SDLTest_AssertCheck(userdata != NULL, "Check userdata pointer, expected: non-NULL, got: %s", (userdata != NULL) ? "non-NULL" : "NULL");
     34       if (userdata != NULL) {
     35          SDLTest_AssertCheck(*(int *)userdata == _userdataValue, "Check userdata value, expected: %i, got: %i", _userdataValue, *(int *)userdata);
     36       }
     37   }
     38
     39   return 0;
     40}
     41
     42/**
     43 * @brief Test pumping and peeking events.
     44 *
     45 * @sa http://wiki.libsdl.org/moin.cgi/SDL_PumpEvents
     46 * @sa http://wiki.libsdl.org/moin.cgi/SDL_PollEvent
     47 */
     48int
     49events_pushPumpAndPollUserevent(void *arg)
     50{
     51   SDL_Event event1;
     52   SDL_Event event2;
     53   int result;
     54
     55   /* Create user event */
     56   event1.type = SDL_USEREVENT;
     57   event1.user.code = SDLTest_RandomSint32();
     58   event1.user.data1 = (void *)&_userdataValue1;
     59   event1.user.data2 = (void *)&_userdataValue2;
     60
     61   /* Push a user event onto the queue and force queue update */
     62   SDL_PushEvent(&event1);
     63   SDLTest_AssertPass("Call to SDL_PushEvent()");
     64   SDL_PumpEvents();
     65   SDLTest_AssertPass("Call to SDL_PumpEvents()");
     66
     67   /* Poll for user event */
     68   result = SDL_PollEvent(&event2);
     69   SDLTest_AssertPass("Call to SDL_PollEvent()");
     70   SDLTest_AssertCheck(result == 1, "Check result from SDL_PollEvent, expected: 1, got: %d", result);
     71
     72   return TEST_COMPLETED;
     73}
     74
     75
     76/**
     77 * @brief Adds and deletes an event watch function with NULL userdata
     78 *
     79 * @sa http://wiki.libsdl.org/moin.cgi/SDL_AddEventWatch
     80 * @sa http://wiki.libsdl.org/moin.cgi/SDL_DelEventWatch
     81 *
     82 */
     83int
     84events_addDelEventWatch(void *arg)
     85{
     86   SDL_Event event;
     87
     88   /* Create user event */
     89   event.type = SDL_USEREVENT;
     90   event.user.code = SDLTest_RandomSint32();;
     91   event.user.data1 = (void *)&_userdataValue1;
     92   event.user.data2 = (void *)&_userdataValue2;
     93
     94   /* Disable userdata check */
     95   _userdataCheck = 0;
     96
     97   /* Reset event filter call tracker */
     98   _eventFilterCalled = 0;
     99
    100   /* Add watch */
    101   SDL_AddEventWatch(_events_sampleNullEventFilter, NULL);
    102   SDLTest_AssertPass("Call to SDL_AddEventWatch()");
    103
    104   /* Push a user event onto the queue and force queue update */
    105   SDL_PushEvent(&event);
    106   SDLTest_AssertPass("Call to SDL_PushEvent()");
    107   SDL_PumpEvents();
    108   SDLTest_AssertPass("Call to SDL_PumpEvents()");
    109   SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
    110
    111   /* Delete watch */
    112   SDL_DelEventWatch(_events_sampleNullEventFilter, NULL);
    113   SDLTest_AssertPass("Call to SDL_DelEventWatch()");
    114
    115   /* Push a user event onto the queue and force queue update */
    116   _eventFilterCalled = 0;
    117   SDL_PushEvent(&event);
    118   SDLTest_AssertPass("Call to SDL_PushEvent()");
    119   SDL_PumpEvents();
    120   SDLTest_AssertPass("Call to SDL_PumpEvents()");
    121   SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
    122
    123   return TEST_COMPLETED;
    124}
    125
    126/**
    127 * @brief Adds and deletes an event watch function with userdata
    128 *
    129 * @sa http://wiki.libsdl.org/moin.cgi/SDL_AddEventWatch
    130 * @sa http://wiki.libsdl.org/moin.cgi/SDL_DelEventWatch
    131 *
    132 */
    133int
    134events_addDelEventWatchWithUserdata(void *arg)
    135{
    136   SDL_Event event;
    137
    138   /* Create user event */
    139   event.type = SDL_USEREVENT;
    140   event.user.code = SDLTest_RandomSint32();;
    141   event.user.data1 = (void *)&_userdataValue1;
    142   event.user.data2 = (void *)&_userdataValue2;
    143
    144   /* Enable userdata check and set a value to check */
    145   _userdataCheck = 1;
    146   _userdataValue = SDLTest_RandomIntegerInRange(-1024, 1024);
    147
    148   /* Reset event filter call tracker */
    149   _eventFilterCalled = 0;
    150
    151   /* Add watch */
    152   SDL_AddEventWatch(_events_sampleNullEventFilter, (void *)&_userdataValue);
    153   SDLTest_AssertPass("Call to SDL_AddEventWatch()");
    154
    155   /* Push a user event onto the queue and force queue update */
    156   SDL_PushEvent(&event);
    157   SDLTest_AssertPass("Call to SDL_PushEvent()");
    158   SDL_PumpEvents();
    159   SDLTest_AssertPass("Call to SDL_PumpEvents()");
    160   SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
    161
    162   /* Delete watch */
    163   SDL_DelEventWatch(_events_sampleNullEventFilter, (void *)&_userdataValue);
    164   SDLTest_AssertPass("Call to SDL_DelEventWatch()");
    165
    166   /* Push a user event onto the queue and force queue update */
    167   _eventFilterCalled = 0;
    168   SDL_PushEvent(&event);
    169   SDLTest_AssertPass("Call to SDL_PushEvent()");
    170   SDL_PumpEvents();
    171   SDLTest_AssertPass("Call to SDL_PumpEvents()");
    172   SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
    173
    174   return TEST_COMPLETED;
    175}
    176
    177
    178/* ================= Test References ================== */
    179
    180/* Events test cases */
    181static const SDLTest_TestCaseReference eventsTest1 =
    182        { (SDLTest_TestCaseFp)events_pushPumpAndPollUserevent, "events_pushPumpAndPollUserevent", "Pushes, pumps and polls a user event", TEST_ENABLED };
    183
    184static const SDLTest_TestCaseReference eventsTest2 =
    185        { (SDLTest_TestCaseFp)events_addDelEventWatch, "events_addDelEventWatch", "Adds and deletes an event watch function with NULL userdata", TEST_ENABLED };
    186
    187static const SDLTest_TestCaseReference eventsTest3 =
    188        { (SDLTest_TestCaseFp)events_addDelEventWatchWithUserdata, "events_addDelEventWatchWithUserdata", "Adds and deletes an event watch function with userdata", TEST_ENABLED };
    189
    190/* Sequence of Events test cases */
    191static const SDLTest_TestCaseReference *eventsTests[] =  {
    192    &eventsTest1, &eventsTest2, &eventsTest3, NULL
    193};
    194
    195/* Events test suite (global) */
    196SDLTest_TestSuiteReference eventsTestSuite = {
    197    "Events",
    198    NULL,
    199    eventsTests,
    200    NULL
    201};