testfile.c File Reference
`#include <stdlib.h>`
`#include <unistd.h>`
`#include "SDL.h"`
`#include <stdio.h>`
Include dependency graph for testfile.c:

Go to the source code of this file.

## Macros

#define | _CRT_NONSTDC_NO_WARNINGS
#define | FBASENAME1 "sdldata1" /* this file will be created during tests */
#define | FBASENAME2 "sdldata2" /* this file should not exist before starting test */
#define | NULL ((void *)0)
#define | RWOP_ERR_QUIT(x) rwops_error_quit( __LINE__, (x) )

## Functions

static void | cleanup (void)
static void | rwops_error_quit (unsigned line, SDL_RWops *rwops)
int | main (int argc, char *argv[])

## Macro Definition Documentation

## _CRT_NONSTDC_NO_WARNINGS

#define _CRT_NONSTDC_NO_WARNINGS

Definition at line 16 of file testfile.c.

## FBASENAME1

#define FBASENAME1 "sdldata1" /* this file will be created during tests */

Definition at line 35 of file testfile.c.

Referenced by cleanup(), and main().

## FBASENAME2

#define FBASENAME2 "sdldata2" /* this file should not exist before starting test */

Definition at line 36 of file testfile.c.

Referenced by cleanup(), and main().

## NULL

#define NULL ((void *)0)

Definition at line 40 of file testfile.c.

Referenced by main().

## RWOP_ERR_QUIT

#define RWOP_ERR_QUIT | ( |  | x | ) | rwops_error_quit( __LINE__, (x) )

Definition at line 61 of file testfile.c.

Referenced by main().

## Function Documentation

## cleanup()

static void cleanup | ( | void |  | ) |

static

Definition at line 44 of file testfile.c.

References FBASENAME1, and FBASENAME2.

Referenced by main(), rwops_error_quit(), and video_getSetWindowData().

{
unlink(FBASENAME1);
unlink(FBASENAME2);
}
FBASENAME1
#define FBASENAME1
Definition: testfile.c:35
FBASENAME2
#define FBASENAME2
Definition: testfile.c:36

## main()

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

Definition at line 66 of file testfile.c.

References cleanup(), SDL_RWops::close, FBASENAME1, FBASENAME2, NULL, SDL_RWops::read, RW_SEEK_CUR, RW_SEEK_END, RW_SEEK_SET, RWOP_ERR_QUIT, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, SDL_memcmp, SDL_RWFromFile, SDL_RWops::seek, and SDL_RWops::write.

{
SDL_RWops *rwops = NULL;
char test_buf[30];
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
cleanup();
/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
rwops = SDL_RWFromFile(NULL, NULL);
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(NULL, "ab+");
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile("something", "");
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile("something", NULL);
if (rwops)
RWOP_ERR_QUIT(rwops);
SDL_Log("test1 OK\n");
/* test 2 : check that inexistent file is not successfully opened/created when required */
/* modes : r, r+ imply that file MUST exist
modes : a, a+, w, w+ checks that it succeeds (file may not exists)
*/
rwops = SDL_RWFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(FBASENAME2, "wb");
if (!rwops)
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "wb+");
if (!rwops)
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "ab");
if (!rwops)
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "ab+");
if (!rwops)
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
SDL_Log("test2 OK\n");
/* test 3 : creation, writing , reading, seeking,
test : w mode, r mode, w+ mode
*/
rwops = SDL_RWFromFile(FBASENAME1, "wb"); /* write only */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops, "1234567890", 10, 1))
RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops, "1234567890", 1, 10))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops, "1234567", 1, 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops); /* we are in write only mode */
rwops->close(rwops);
rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exists */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->read(rwops, test_buf, 1, 7))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "1234567", 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 10, 100))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
if (2 != rwops->read(rwops, test_buf, 10, 3))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->write(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops); /* readonly mode */
rwops->close(rwops);
/* test 3: same with w+ mode */
rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops, "1234567890", 10, 1))
RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops, "1234567890", 1, 10))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops, "1234567", 1, 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (1 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->read(rwops, test_buf, 1, 7))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "1234567", 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 10, 100))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
if (2 != rwops->read(rwops, test_buf, 10, 3))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
SDL_Log("test3 OK\n");
/* test 4: same in r+ mode */
rwops = SDL_RWFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops, "1234567890", 10, 1))
RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops, "1234567890", 1, 10))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops, "1234567", 1, 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (1 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->read(rwops, test_buf, 1, 7))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "1234567", 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 10, 100))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
if (2 != rwops->read(rwops, test_buf, 10, 3))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
SDL_Log("test4 OK\n");
/* test5 : append mode */
rwops = SDL_RWFromFile(FBASENAME1, "ab+"); /* write + read + append */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops, "1234567890", 10, 1))
RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops, "1234567890", 1, 10))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops, "1234567", 1, 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (1 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->read(rwops, test_buf, 1, 7))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "1234567", 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 10, 100))
RWOP_ERR_QUIT(rwops);
if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (3 != rwops->read(rwops, test_buf, 10, 3))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
SDL_Log("test5 OK\n");
cleanup();
return 0; /* all ok */
}
SDL_RWops
Definition: SDL_rwops.h:52
NULL
#define NULL
Definition: testfile.c:40
SDL_RWops::write
size_t(* write)(struct SDL_RWops *context, const void *ptr, size_t size, size_t num)
Definition: SDL_rwops.h:83
RW_SEEK_END
#define RW_SEEK_END
Definition: SDL_rwops.h:176
SDL_RWops::seek
Sint64(* seek)(struct SDL_RWops *context, Sint64 offset, int whence)
Definition: SDL_rwops.h:65
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_RWFromFile
#define SDL_RWFromFile
Definition: SDL_dynapi_overrides.h:351
SDL_RWops::close
int(* close)(struct SDL_RWops *context)
Definition: SDL_rwops.h:91
SDL_memcmp
#define SDL_memcmp
Definition: SDL_dynapi_overrides.h:389
SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
RWOP_ERR_QUIT
#define RWOP_ERR_QUIT(x)
Definition: testfile.c:61
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_RWops::read
size_t(* read)(struct SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
Definition: SDL_rwops.h:74
RW_SEEK_SET
#define RW_SEEK_SET
Definition: SDL_rwops.h:174
FBASENAME1
#define FBASENAME1
Definition: testfile.c:35
RW_SEEK_CUR
#define RW_SEEK_CUR
Definition: SDL_rwops.h:175
FBASENAME2
#define FBASENAME2
Definition: testfile.c:36
cleanup
static void cleanup(void)
Definition: testfile.c:44
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66

## rwops_error_quit()

static void rwops_error_quit | ( | unsigned | line,
|  | SDL_RWops * | rwops
| ) |  |

static

Definition at line 51 of file testfile.c.

References cleanup(), SDL_RWops::close, SDL_LOG_CATEGORY_APPLICATION, and SDL_LogError.

{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
if (rwops) {
rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
}
cleanup();
exit(1); /* quit with rwops error (test failed) */
}
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_RWops::close
int(* close)(struct SDL_RWops *context)
Definition: SDL_rwops.h:91
cleanup
static void cleanup(void)
Definition: testfile.c:44
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66