Created
June 11, 2014 19:09
-
-
Save Daft-Freak/7acd0ea3bf5af27ea3db to your computer and use it in GitHub Desktop.
sdl2 testfilesystem without sdl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (C) 1997-2014 Sam Lantinga <[email protected]> | |
This software is provided 'as-is', without any express or implied | |
warranty. In no event will the authors be held liable for any damages | |
arising from the use of this software. | |
Permission is granted to anyone to use this software for any purpose, | |
including commercial applications, and to alter it and redistribute it | |
freely. | |
*/ | |
/* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */ | |
/* quiet windows compiler warnings */ | |
#define _CRT_NONSTDC_NO_WARNINGS | |
#include <stdlib.h> | |
#ifndef _MSC_VER | |
#include <unistd.h> | |
#endif | |
#include <stdint.h> | |
#include <string.h> | |
/*#include "SDL.h" | |
//#include "SDL_endian.h"*/ | |
#include <stdio.h> | |
/* WARNING ! those 2 files will be destroyed by this test program */ | |
#ifdef __IPHONEOS__ | |
#define FBASENAME1 "../Documents/sdldata1" /* this file will be created during tests */ | |
#define FBASENAME2 "../Documents/sdldata2" /* this file should not exist before starting test */ | |
#else | |
#define FBASENAME1 "sdldata1" /* this file will be created during tests */ | |
#define FBASENAME2 "sdldata2" /* this file should not exist before starting test */ | |
#endif | |
#ifndef NULL | |
#define NULL ((void *)0) | |
#endif | |
static void | |
cleanup(void) | |
{ | |
unlink(FBASENAME1); | |
unlink(FBASENAME2); | |
} | |
static void | |
rwops_error_quit(unsigned line, FILE * rwops) | |
{ | |
/*printfError(printf_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);*/ | |
printf("testfile.c(%d): failed\n", line); | |
if (rwops) { | |
fclose(rwops); | |
} | |
cleanup(); | |
exit(1); /* quit with rwops error (test failed) */ | |
} | |
#define RWOP_ERR_QUIT(x) rwops_error_quit( __LINE__, (x) ) | |
static int64_t | |
seek_file(FILE * context, int64_t offset, int whence) | |
{ | |
if (fseek(context, offset, whence) == 0) { | |
return ftell(context); | |
} | |
return -1; | |
} | |
FILE * | |
open_file(const char *file, const char *mode) | |
{ | |
if(!file || !*file || !mode || !*mode) | |
return NULL; | |
return fopen(file, mode); | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
FILE *rwops = NULL; | |
char test_buf[30]; | |
/* Enable standard application logging */ | |
/*printfSetPriority(printf_CATEGORY_APPLICATION, printf_PRIORITY_INFO);*/ | |
cleanup(); | |
/* test 1 : basic argument test: all those calls to fopen should fail */ | |
rwops = open_file(NULL, NULL); | |
if (rwops) | |
RWOP_ERR_QUIT(rwops); | |
rwops = open_file(NULL, "ab+"); | |
if (rwops) | |
RWOP_ERR_QUIT(rwops); | |
rwops = open_file(NULL, "sldfkjsldkfj"); | |
if (rwops) | |
RWOP_ERR_QUIT(rwops); | |
rwops = open_file("something", ""); | |
if (rwops) | |
RWOP_ERR_QUIT(rwops); | |
rwops = open_file("something", NULL); | |
if (rwops) | |
RWOP_ERR_QUIT(rwops); | |
printf("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 = open_file(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */ | |
if (rwops) | |
RWOP_ERR_QUIT(rwops); | |
rwops = open_file(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */ | |
if (rwops) | |
RWOP_ERR_QUIT(rwops); | |
rwops = open_file(FBASENAME2, "wb"); | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
fclose(rwops); | |
unlink(FBASENAME2); | |
rwops = open_file(FBASENAME2, "wb+"); | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
fclose(rwops); | |
unlink(FBASENAME2); | |
rwops = open_file(FBASENAME2, "ab"); | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
fclose(rwops); | |
unlink(FBASENAME2); | |
rwops = open_file(FBASENAME2, "ab+"); | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
fclose(rwops); | |
unlink(FBASENAME2); | |
printf("test2 OK\n"); | |
/* test 3 : creation, writing , reading, seeking, | |
test : w mode, r mode, w+ mode | |
*/ | |
rwops = open_file(FBASENAME1, "wb"); /* write only */ | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
if (1 != fwrite("1234567890", 10, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (10 != fwrite("1234567890", 1, 10, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (7 != fwrite("1234567", 1, 7, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); /* we are in write only mode */ | |
fclose(rwops); | |
rwops = open_file(FBASENAME1, "rb"); /* read mode, file must exists */ | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (20 != seek_file(rwops, -7, SEEK_END)) | |
RWOP_ERR_QUIT(rwops); | |
if (7 != fread(test_buf, 1, 7, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (memcmp(test_buf, "1234567", 7)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 10, 100, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, -27, SEEK_CUR)) | |
RWOP_ERR_QUIT(rwops); | |
if (2 != fread(test_buf, 10, 3, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (memcmp(test_buf, "12345678901234567890", 20)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fwrite(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); /* readonly mode */ | |
fclose(rwops); | |
/* test 3: same with w+ mode */ | |
rwops = open_file(FBASENAME1, "wb+"); /* write + read + truncation */ | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
if (1 != fwrite("1234567890", 10, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (10 != fwrite("1234567890", 1, 10, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (7 != fwrite("1234567", 1, 7, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (1 != fread(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); /* we are in read/write mode */ | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (20 != seek_file(rwops, -7, SEEK_END)) | |
RWOP_ERR_QUIT(rwops); | |
if (7 != fread(test_buf, 1, 7, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (memcmp(test_buf, "1234567", 7)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 10, 100, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, -27, SEEK_CUR)) | |
RWOP_ERR_QUIT(rwops); | |
if (2 != fread(test_buf, 10, 3, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (memcmp(test_buf, "12345678901234567890", 20)) | |
RWOP_ERR_QUIT(rwops); | |
fclose(rwops); | |
printf("test3 OK\n"); | |
/* test 4: same in r+ mode */ | |
rwops = open_file(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */ | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
if (1 != fwrite("1234567890", 10, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (10 != fwrite( "1234567890", 1, 10, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (7 != fwrite( "1234567", 1, 7, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (1 != fread(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); /* we are in read/write mode */ | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (20 != seek_file(rwops, -7, SEEK_END)) | |
RWOP_ERR_QUIT(rwops); | |
if (7 != fread(test_buf, 1, 7, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (memcmp(test_buf, "1234567", 7)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 10, 100, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, -27, SEEK_CUR)) | |
RWOP_ERR_QUIT(rwops); | |
if (2 != fread(test_buf, 10, 3, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (memcmp(test_buf, "12345678901234567890", 20)) | |
RWOP_ERR_QUIT(rwops); | |
fclose(rwops); | |
printf("test4 OK\n"); | |
/* test5 : append mode */ | |
rwops = open_file(FBASENAME1, "ab+"); /* write + read + append */ | |
if (!rwops) | |
RWOP_ERR_QUIT(rwops); | |
if (1 != fwrite("1234567890", 10, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (10 != fwrite("1234567890", 1, 10, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (7 != fwrite("1234567", 1, 7, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (1 != fread(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (20 + 27 != seek_file(rwops, -7, SEEK_END)) | |
RWOP_ERR_QUIT(rwops); | |
if (7 != fread(test_buf, 1, 7, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (memcmp(test_buf, "1234567", 7)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 1, 1, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != fread(test_buf, 10, 100, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (27 != seek_file(rwops, -27, SEEK_CUR)) | |
RWOP_ERR_QUIT(rwops); | |
if (0 != seek_file(rwops, 0L, SEEK_SET)) | |
RWOP_ERR_QUIT(rwops); | |
if (3 != fread(test_buf, 10, 3, rwops)) | |
RWOP_ERR_QUIT(rwops); | |
if (memcmp(test_buf, "123456789012345678901234567123", 30)) | |
RWOP_ERR_QUIT(rwops); | |
fclose(rwops); | |
printf("test5 OK\n"); | |
cleanup(); | |
return 0; /* all ok */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment