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