diff --git a/src/io/generic/SDL_asyncio_generic.c b/src/io/generic/SDL_asyncio_generic.c index 4c2a56202db58..1194e4e83a3b1 100644 --- a/src/io/generic/SDL_asyncio_generic.c +++ b/src/io/generic/SDL_asyncio_generic.c @@ -87,7 +87,7 @@ static void SynchronousIO(SDL_AsyncIOTask *task) } else { const bool writing = (task->type == SDL_ASYNCIO_TASK_WRITE); task->result_size = (Uint64) (writing ? SDL_WriteIO(io, ptr, size) : SDL_ReadIO(io, ptr, size)); - if (task->result_size == task->requested_size) { + if (task->result_size <= task->requested_size) { task->result = SDL_ASYNCIO_COMPLETE; } else { if (writing) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 40156a6ef9487..7955013099653 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,7 +37,7 @@ add_library(sdltests_utils OBJECT ) target_link_libraries(sdltests_utils PRIVATE SDL3::Headers) -file(GLOB RESOURCE_FILES *.bmp *.wav *.csv *.hex moose.dat utf8.txt) +file(GLOB RESOURCE_FILES *.bmp *.wav *.csv *.hex moose.dat utf8.txt small.txt) option(SDLTEST_TRACKMEM "Run tests with --trackmem" OFF) diff --git a/test/small.txt b/test/small.txt new file mode 100644 index 0000000000000..9daeafb9864cf --- /dev/null +++ b/test/small.txt @@ -0,0 +1 @@ +test diff --git a/test/testasyncio.c b/test/testasyncio.c index 5a9f719c64011..5f4d8d589ad99 100644 --- a/test/testasyncio.c +++ b/test/testasyncio.c @@ -19,11 +19,13 @@ static SDL_Renderer *renderer = NULL; static SDL_Texture *texture = NULL; static SDL_AsyncIOQueue *queue = NULL; static SDLTest_CommonState *state = NULL; +static char READ_BUFFER[4096] = { }; SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { const char *base = NULL; SDL_AsyncIO *asyncio = NULL; + SDL_AsyncIO *read_asyncio = NULL; char **bmps = NULL; int bmpcount = 0; int i; @@ -103,6 +105,24 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) } } + { + char* path =NULL; + if (SDL_asprintf(&path, "%s%s", base, "small.txt") < 0) { + SDL_free(path); + SDL_Log("Failed!"); + return SDL_APP_FAILURE; + } + + read_asyncio = SDL_AsyncIOFromFile(path, "r"); + SDL_free(path); + + /* buffer should be bigger than the file contents */ + if (SDL_ReadAsyncIO(read_asyncio, READ_BUFFER, 0, 4096, queue, "small.txt") == false) { + SDL_Log("Failed!"); + return SDL_APP_FAILURE; + } + } + SDL_free(bmps); SDL_Log("Opening asyncio.tmp..."); @@ -130,7 +150,7 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) return SDLTest_CommonEventMainCallbacks(state, event); } -static void async_io_task_complete(const SDL_AsyncIOOutcome *outcome) +static SDL_AppResult async_io_task_complete(const SDL_AsyncIOOutcome *outcome) { const char *fname = (const char *) outcome->userdata; const char *resultstr = "[unknown result]"; @@ -146,7 +166,14 @@ static void async_io_task_complete(const SDL_AsyncIOOutcome *outcome) SDL_Log("File '%s' async results: %s", fname, resultstr); if (SDL_strncmp(fname, "asyncio.tmp", 11) == 0) { - return; + return SDL_APP_SUCCESS; + } + + if (SDL_strncmp(fname, "small.txt", 9) == 0) { + if (outcome->result != SDL_ASYNCIO_COMPLETE) { + return SDL_APP_FAILURE; + } + return SDL_APP_SUCCESS; } if (outcome->result == SDL_ASYNCIO_COMPLETE) { @@ -164,13 +191,18 @@ static void async_io_task_complete(const SDL_AsyncIOOutcome *outcome) SDL_free(outcome->userdata); SDL_free(outcome->buffer); + return SDL_APP_SUCCESS; } SDL_AppResult SDL_AppIterate(void *appstate) { + SDL_AppResult result; SDL_AsyncIOOutcome outcome; if (SDL_GetAsyncIOResult(queue, &outcome)) { - async_io_task_complete(&outcome); + result = async_io_task_complete(&outcome); + if (result != SDL_APP_SUCCESS) { + return result; + } } SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);