test/testfile.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 15 Sep 2010 22:15:47 -0700
changeset 4884 27ab20a36eba
parent 2765 f55c87ae336b
child 5062 e8916fe9cfc8
permissions -rw-r--r--
- added directx include path to VS2008 solution
- updated shape vcproj and add it to VS2008 solution
- minor changes (i.e. typecasting) to get rid of compiler warnings in VS
slouken@1453
     1
slouken@1453
     2
/* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */
slouken@1453
     3
aschiffler@4884
     4
// quiet win32 compiler warnings
aschiffler@4884
     5
#define _CRT_NONSTDC_NO_WARNINGS
aschiffler@4884
     6
slouken@1484
     7
#include <stdlib.h>
icculus@2099
     8
icculus@2099
     9
#ifndef _MSC_VER
slouken@1843
    10
#include <unistd.h>
icculus@2099
    11
#endif
slouken@1453
    12
slouken@1453
    13
#include "SDL.h"
slouken@1453
    14
#include "SDL_endian.h"
slouken@1453
    15
slouken@1453
    16
slouken@1895
    17
#include <stdio.h>
slouken@1453
    18
slouken@1453
    19
/* WARNING ! those 2 files will be destroyed by this test program */
slouken@2765
    20
slouken@2765
    21
#ifdef __IPHONEOS__
slouken@2765
    22
#define FBASENAME1	"../Documents/sdldata1" /* this file will be created during tests */
slouken@2765
    23
#define FBASENAME2  "../Documents/sdldata2"     /* this file should not exist before starting test */
slouken@2765
    24
#else
slouken@1895
    25
#define FBASENAME1	"sdldata1"      /* this file will be created during tests */
slouken@2765
    26
#define FBASENAME2	"sdldata2"      /* this file should not exist before starting test */
slouken@2765
    27
#endif
slouken@1453
    28
slouken@1453
    29
#ifndef NULL
slouken@1453
    30
#define NULL ((void *)0)
slouken@1895
    31
#endif
slouken@1453
    32
slouken@1895
    33
static void
slouken@1895
    34
cleanup(void)
slouken@1895
    35
{
slouken@1453
    36
slouken@1895
    37
    unlink(FBASENAME1);
slouken@1895
    38
    unlink(FBASENAME2);
slouken@1453
    39
}
slouken@1453
    40
slouken@1895
    41
static void
slouken@1895
    42
rwops_error_quit(unsigned line, SDL_RWops * rwops)
slouken@1895
    43
{
slouken@1895
    44
slouken@1895
    45
    printf("testfile.c(%d): failed\n", line);
slouken@1895
    46
    if (rwops) {
slouken@1895
    47
        rwops->close(rwops);    /* This calls SDL_FreeRW(rwops); */
slouken@1895
    48
    }
slouken@1895
    49
    cleanup();
slouken@1895
    50
    exit(1);                    /* quit with rwops error (test failed) */
slouken@1453
    51
}
slouken@1453
    52
slouken@1453
    53
#define RWOP_ERR_QUIT(x)	rwops_error_quit( __LINE__, (x) )
slouken@1453
    54
slouken@1453
    55
slouken@1453
    56
slouken@1895
    57
int
slouken@1895
    58
main(int argc, char *argv[])
slouken@1453
    59
{
slouken@1895
    60
    SDL_RWops *rwops = NULL;
slouken@1895
    61
    char test_buf[30];
slouken@1895
    62
slouken@1895
    63
    cleanup();
slouken@1453
    64
slouken@1453
    65
/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
slouken@1895
    66
slouken@1895
    67
    rwops = SDL_RWFromFile(NULL, NULL);
slouken@1895
    68
    if (rwops)
slouken@1895
    69
        RWOP_ERR_QUIT(rwops);
slouken@1895
    70
    rwops = SDL_RWFromFile(NULL, "ab+");
slouken@1895
    71
    if (rwops)
slouken@1895
    72
        RWOP_ERR_QUIT(rwops);
slouken@1895
    73
    rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
slouken@1895
    74
    if (rwops)
slouken@1895
    75
        RWOP_ERR_QUIT(rwops);
slouken@1895
    76
    rwops = SDL_RWFromFile("something", "");
slouken@1895
    77
    if (rwops)
slouken@1895
    78
        RWOP_ERR_QUIT(rwops);
slouken@1895
    79
    rwops = SDL_RWFromFile("something", NULL);
slouken@1895
    80
    if (rwops)
slouken@1895
    81
        RWOP_ERR_QUIT(rwops);
slouken@1895
    82
    printf("test1 OK\n");
slouken@1453
    83
slouken@1453
    84
/* test 2 : check that inexistant file is not successfully opened/created when required */
slouken@1453
    85
/* modes : r, r+ implie that file MUST exist 
slouken@1453
    86
   modes : a, a+, w, w+ checks that it succeeds (file may not exists)
slouken@1453
    87
   
slouken@1453
    88
 */
slouken@1895
    89
    rwops = SDL_RWFromFile(FBASENAME2, "rb");   /* this file doesn't exist that call must fail */
slouken@1895
    90
    if (rwops)
slouken@1895
    91
        RWOP_ERR_QUIT(rwops);
slouken@1895
    92
    rwops = SDL_RWFromFile(FBASENAME2, "rb+");  /* this file doesn't exist that call must fail */
slouken@1895
    93
    if (rwops)
slouken@1895
    94
        RWOP_ERR_QUIT(rwops);
slouken@1895
    95
    rwops = SDL_RWFromFile(FBASENAME2, "wb");
slouken@1895
    96
    if (!rwops)
slouken@1895
    97
        RWOP_ERR_QUIT(rwops);
slouken@1895
    98
    rwops->close(rwops);
slouken@1895
    99
    unlink(FBASENAME2);
slouken@1895
   100
    rwops = SDL_RWFromFile(FBASENAME2, "wb+");
slouken@1895
   101
    if (!rwops)
slouken@1895
   102
        RWOP_ERR_QUIT(rwops);
slouken@1895
   103
    rwops->close(rwops);
slouken@1895
   104
    unlink(FBASENAME2);
slouken@1895
   105
    rwops = SDL_RWFromFile(FBASENAME2, "ab");
slouken@1895
   106
    if (!rwops)
slouken@1895
   107
        RWOP_ERR_QUIT(rwops);
slouken@1895
   108
    rwops->close(rwops);
slouken@1895
   109
    unlink(FBASENAME2);
slouken@1895
   110
    rwops = SDL_RWFromFile(FBASENAME2, "ab+");
slouken@1895
   111
    if (!rwops)
slouken@1895
   112
        RWOP_ERR_QUIT(rwops);
slouken@1895
   113
    rwops->close(rwops);
slouken@1895
   114
    unlink(FBASENAME2);
slouken@1895
   115
    printf("test2 OK\n");
slouken@1453
   116
slouken@1453
   117
/* test 3 : creation, writing , reading, seeking, 
slouken@1453
   118
	        test : w mode, r mode, w+ mode
slouken@1453
   119
 */
slouken@1895
   120
    rwops = SDL_RWFromFile(FBASENAME1, "wb");   /* write only */
slouken@1895
   121
    if (!rwops)
slouken@1895
   122
        RWOP_ERR_QUIT(rwops);
slouken@1895
   123
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   124
        RWOP_ERR_QUIT(rwops);
slouken@1895
   125
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   126
        RWOP_ERR_QUIT(rwops);
slouken@1895
   127
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   128
        RWOP_ERR_QUIT(rwops);
slouken@1895
   129
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   130
        RWOP_ERR_QUIT(rwops);
slouken@1895
   131
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   132
        RWOP_ERR_QUIT(rwops);   /* we are in write only mode */
slouken@1895
   133
    rwops->close(rwops);
slouken@1453
   134
slouken@1895
   135
    rwops = SDL_RWFromFile(FBASENAME1, "rb");   /* read mode, file must exists */
slouken@1895
   136
    if (!rwops)
slouken@1895
   137
        RWOP_ERR_QUIT(rwops);
slouken@1895
   138
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   139
        RWOP_ERR_QUIT(rwops);
slouken@1895
   140
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   141
        RWOP_ERR_QUIT(rwops);
slouken@1895
   142
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   143
        RWOP_ERR_QUIT(rwops);
slouken@1895
   144
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   145
        RWOP_ERR_QUIT(rwops);
slouken@1895
   146
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   147
        RWOP_ERR_QUIT(rwops);
slouken@1895
   148
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   149
        RWOP_ERR_QUIT(rwops);
slouken@1895
   150
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   151
        RWOP_ERR_QUIT(rwops);
slouken@1895
   152
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   153
        RWOP_ERR_QUIT(rwops);
slouken@1895
   154
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   155
        RWOP_ERR_QUIT(rwops);
slouken@1895
   156
    if (0 != rwops->write(rwops, test_buf, 1, 1))
slouken@1895
   157
        RWOP_ERR_QUIT(rwops);   /* readonly mode */
slouken@1895
   158
    rwops->close(rwops);
slouken@1453
   159
slouken@1453
   160
/* test 3: same with w+ mode */
slouken@1895
   161
    rwops = SDL_RWFromFile(FBASENAME1, "wb+");  /* write + read + truncation */
slouken@1895
   162
    if (!rwops)
slouken@1895
   163
        RWOP_ERR_QUIT(rwops);
slouken@1895
   164
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   165
        RWOP_ERR_QUIT(rwops);
slouken@1895
   166
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   167
        RWOP_ERR_QUIT(rwops);
slouken@1895
   168
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   169
        RWOP_ERR_QUIT(rwops);
slouken@1895
   170
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   171
        RWOP_ERR_QUIT(rwops);
slouken@1895
   172
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   173
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
slouken@1895
   174
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   175
        RWOP_ERR_QUIT(rwops);
slouken@1895
   176
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   177
        RWOP_ERR_QUIT(rwops);
slouken@1895
   178
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   179
        RWOP_ERR_QUIT(rwops);
slouken@1895
   180
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   181
        RWOP_ERR_QUIT(rwops);
slouken@1895
   182
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   183
        RWOP_ERR_QUIT(rwops);
slouken@1895
   184
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   185
        RWOP_ERR_QUIT(rwops);
slouken@1895
   186
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   187
        RWOP_ERR_QUIT(rwops);
slouken@1895
   188
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   189
        RWOP_ERR_QUIT(rwops);
slouken@1895
   190
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   191
        RWOP_ERR_QUIT(rwops);
slouken@1895
   192
    rwops->close(rwops);
slouken@1895
   193
    printf("test3 OK\n");
slouken@1453
   194
slouken@1453
   195
/* test 4: same in r+ mode */
slouken@1895
   196
    rwops = SDL_RWFromFile(FBASENAME1, "rb+");  /* write + read + file must exists, no truncation */
slouken@1895
   197
    if (!rwops)
slouken@1895
   198
        RWOP_ERR_QUIT(rwops);
slouken@1895
   199
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   200
        RWOP_ERR_QUIT(rwops);
slouken@1895
   201
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   202
        RWOP_ERR_QUIT(rwops);
slouken@1895
   203
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   204
        RWOP_ERR_QUIT(rwops);
slouken@1895
   205
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   206
        RWOP_ERR_QUIT(rwops);
slouken@1895
   207
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   208
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
slouken@1895
   209
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   210
        RWOP_ERR_QUIT(rwops);
slouken@1895
   211
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   212
        RWOP_ERR_QUIT(rwops);
slouken@1895
   213
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   214
        RWOP_ERR_QUIT(rwops);
slouken@1895
   215
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   216
        RWOP_ERR_QUIT(rwops);
slouken@1895
   217
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   218
        RWOP_ERR_QUIT(rwops);
slouken@1895
   219
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   220
        RWOP_ERR_QUIT(rwops);
slouken@1895
   221
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   222
        RWOP_ERR_QUIT(rwops);
slouken@1895
   223
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   224
        RWOP_ERR_QUIT(rwops);
slouken@1895
   225
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   226
        RWOP_ERR_QUIT(rwops);
slouken@1895
   227
    rwops->close(rwops);
slouken@1895
   228
    printf("test4 OK\n");
slouken@1453
   229
slouken@1453
   230
/* test5 : append mode */
slouken@1895
   231
    rwops = SDL_RWFromFile(FBASENAME1, "ab+");  /* write + read + append */
slouken@1895
   232
    if (!rwops)
slouken@1895
   233
        RWOP_ERR_QUIT(rwops);
slouken@1895
   234
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   235
        RWOP_ERR_QUIT(rwops);
slouken@1895
   236
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   237
        RWOP_ERR_QUIT(rwops);
slouken@1895
   238
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   239
        RWOP_ERR_QUIT(rwops);
slouken@1895
   240
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   241
        RWOP_ERR_QUIT(rwops);
slouken@1895
   242
slouken@1895
   243
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   244
        RWOP_ERR_QUIT(rwops);
slouken@1895
   245
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   246
        RWOP_ERR_QUIT(rwops);
slouken@1895
   247
slouken@1895
   248
    if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   249
        RWOP_ERR_QUIT(rwops);
slouken@1895
   250
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   251
        RWOP_ERR_QUIT(rwops);
slouken@1895
   252
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   253
        RWOP_ERR_QUIT(rwops);
slouken@1895
   254
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   255
        RWOP_ERR_QUIT(rwops);
slouken@1895
   256
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   257
        RWOP_ERR_QUIT(rwops);
slouken@1895
   258
slouken@1895
   259
    if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   260
        RWOP_ERR_QUIT(rwops);
slouken@1895
   261
slouken@1895
   262
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   263
        RWOP_ERR_QUIT(rwops);
slouken@1895
   264
    if (3 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   265
        RWOP_ERR_QUIT(rwops);
slouken@1895
   266
    if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
slouken@1895
   267
        RWOP_ERR_QUIT(rwops);
slouken@1895
   268
    rwops->close(rwops);
slouken@1895
   269
    printf("test5 OK\n");
slouken@1895
   270
    cleanup();
slouken@1895
   271
    return 0;                   /* all ok */
slouken@1453
   272
}