test/testfile.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 2765 f55c87ae336b
child 4884 27ab20a36eba
permissions -rw-r--r--
Added testresample.c
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@2765
    17
slouken@2765
    18
#ifdef __IPHONEOS__
slouken@2765
    19
#define FBASENAME1	"../Documents/sdldata1" /* this file will be created during tests */
slouken@2765
    20
#define FBASENAME2  "../Documents/sdldata2"     /* this file should not exist before starting test */
slouken@2765
    21
#else
slouken@1895
    22
#define FBASENAME1	"sdldata1"      /* this file will be created during tests */
slouken@2765
    23
#define FBASENAME2	"sdldata2"      /* this file should not exist before starting test */
slouken@2765
    24
#endif
slouken@1453
    25
slouken@1453
    26
#ifndef NULL
slouken@1453
    27
#define NULL ((void *)0)
slouken@1895
    28
#endif
slouken@1453
    29
slouken@1895
    30
static void
slouken@1895
    31
cleanup(void)
slouken@1895
    32
{
slouken@1453
    33
slouken@1895
    34
    unlink(FBASENAME1);
slouken@1895
    35
    unlink(FBASENAME2);
slouken@1453
    36
}
slouken@1453
    37
slouken@1895
    38
static void
slouken@1895
    39
rwops_error_quit(unsigned line, SDL_RWops * rwops)
slouken@1895
    40
{
slouken@1895
    41
slouken@1895
    42
    printf("testfile.c(%d): failed\n", line);
slouken@1895
    43
    if (rwops) {
slouken@1895
    44
        rwops->close(rwops);    /* This calls SDL_FreeRW(rwops); */
slouken@1895
    45
    }
slouken@1895
    46
    cleanup();
slouken@1895
    47
    exit(1);                    /* quit with rwops error (test failed) */
slouken@1453
    48
}
slouken@1453
    49
slouken@1453
    50
#define RWOP_ERR_QUIT(x)	rwops_error_quit( __LINE__, (x) )
slouken@1453
    51
slouken@1453
    52
slouken@1453
    53
slouken@1895
    54
int
slouken@1895
    55
main(int argc, char *argv[])
slouken@1453
    56
{
slouken@1895
    57
    SDL_RWops *rwops = NULL;
slouken@1895
    58
    char test_buf[30];
slouken@1895
    59
slouken@1895
    60
    cleanup();
slouken@1453
    61
slouken@1453
    62
/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
slouken@1895
    63
slouken@1895
    64
    rwops = SDL_RWFromFile(NULL, NULL);
slouken@1895
    65
    if (rwops)
slouken@1895
    66
        RWOP_ERR_QUIT(rwops);
slouken@1895
    67
    rwops = SDL_RWFromFile(NULL, "ab+");
slouken@1895
    68
    if (rwops)
slouken@1895
    69
        RWOP_ERR_QUIT(rwops);
slouken@1895
    70
    rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
slouken@1895
    71
    if (rwops)
slouken@1895
    72
        RWOP_ERR_QUIT(rwops);
slouken@1895
    73
    rwops = SDL_RWFromFile("something", "");
slouken@1895
    74
    if (rwops)
slouken@1895
    75
        RWOP_ERR_QUIT(rwops);
slouken@1895
    76
    rwops = SDL_RWFromFile("something", NULL);
slouken@1895
    77
    if (rwops)
slouken@1895
    78
        RWOP_ERR_QUIT(rwops);
slouken@1895
    79
    printf("test1 OK\n");
slouken@1453
    80
slouken@1453
    81
/* test 2 : check that inexistant file is not successfully opened/created when required */
slouken@1453
    82
/* modes : r, r+ implie that file MUST exist 
slouken@1453
    83
   modes : a, a+, w, w+ checks that it succeeds (file may not exists)
slouken@1453
    84
   
slouken@1453
    85
 */
slouken@1895
    86
    rwops = SDL_RWFromFile(FBASENAME2, "rb");   /* this file doesn't exist that call must fail */
slouken@1895
    87
    if (rwops)
slouken@1895
    88
        RWOP_ERR_QUIT(rwops);
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, "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, "wb+");
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
    rwops = SDL_RWFromFile(FBASENAME2, "ab+");
slouken@1895
   108
    if (!rwops)
slouken@1895
   109
        RWOP_ERR_QUIT(rwops);
slouken@1895
   110
    rwops->close(rwops);
slouken@1895
   111
    unlink(FBASENAME2);
slouken@1895
   112
    printf("test2 OK\n");
slouken@1453
   113
slouken@1453
   114
/* test 3 : creation, writing , reading, seeking, 
slouken@1453
   115
	        test : w mode, r mode, w+ mode
slouken@1453
   116
 */
slouken@1895
   117
    rwops = SDL_RWFromFile(FBASENAME1, "wb");   /* write only */
slouken@1895
   118
    if (!rwops)
slouken@1895
   119
        RWOP_ERR_QUIT(rwops);
slouken@1895
   120
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   121
        RWOP_ERR_QUIT(rwops);
slouken@1895
   122
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   123
        RWOP_ERR_QUIT(rwops);
slouken@1895
   124
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   125
        RWOP_ERR_QUIT(rwops);
slouken@1895
   126
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   127
        RWOP_ERR_QUIT(rwops);
slouken@1895
   128
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   129
        RWOP_ERR_QUIT(rwops);   /* we are in write only mode */
slouken@1895
   130
    rwops->close(rwops);
slouken@1453
   131
slouken@1895
   132
    rwops = SDL_RWFromFile(FBASENAME1, "rb");   /* read mode, file must exists */
slouken@1895
   133
    if (!rwops)
slouken@1895
   134
        RWOP_ERR_QUIT(rwops);
slouken@1895
   135
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   136
        RWOP_ERR_QUIT(rwops);
slouken@1895
   137
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   138
        RWOP_ERR_QUIT(rwops);
slouken@1895
   139
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   140
        RWOP_ERR_QUIT(rwops);
slouken@1895
   141
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   142
        RWOP_ERR_QUIT(rwops);
slouken@1895
   143
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   144
        RWOP_ERR_QUIT(rwops);
slouken@1895
   145
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   146
        RWOP_ERR_QUIT(rwops);
slouken@1895
   147
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   148
        RWOP_ERR_QUIT(rwops);
slouken@1895
   149
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   150
        RWOP_ERR_QUIT(rwops);
slouken@1895
   151
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   152
        RWOP_ERR_QUIT(rwops);
slouken@1895
   153
    if (0 != rwops->write(rwops, test_buf, 1, 1))
slouken@1895
   154
        RWOP_ERR_QUIT(rwops);   /* readonly mode */
slouken@1895
   155
    rwops->close(rwops);
slouken@1453
   156
slouken@1453
   157
/* test 3: same with w+ mode */
slouken@1895
   158
    rwops = SDL_RWFromFile(FBASENAME1, "wb+");  /* write + read + truncation */
slouken@1895
   159
    if (!rwops)
slouken@1895
   160
        RWOP_ERR_QUIT(rwops);
slouken@1895
   161
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   162
        RWOP_ERR_QUIT(rwops);
slouken@1895
   163
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   164
        RWOP_ERR_QUIT(rwops);
slouken@1895
   165
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   166
        RWOP_ERR_QUIT(rwops);
slouken@1895
   167
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   168
        RWOP_ERR_QUIT(rwops);
slouken@1895
   169
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   170
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
slouken@1895
   171
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   172
        RWOP_ERR_QUIT(rwops);
slouken@1895
   173
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   174
        RWOP_ERR_QUIT(rwops);
slouken@1895
   175
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   176
        RWOP_ERR_QUIT(rwops);
slouken@1895
   177
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   178
        RWOP_ERR_QUIT(rwops);
slouken@1895
   179
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   180
        RWOP_ERR_QUIT(rwops);
slouken@1895
   181
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   182
        RWOP_ERR_QUIT(rwops);
slouken@1895
   183
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   184
        RWOP_ERR_QUIT(rwops);
slouken@1895
   185
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   186
        RWOP_ERR_QUIT(rwops);
slouken@1895
   187
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   188
        RWOP_ERR_QUIT(rwops);
slouken@1895
   189
    rwops->close(rwops);
slouken@1895
   190
    printf("test3 OK\n");
slouken@1453
   191
slouken@1453
   192
/* test 4: same in r+ mode */
slouken@1895
   193
    rwops = SDL_RWFromFile(FBASENAME1, "rb+");  /* write + read + file must exists, no truncation */
slouken@1895
   194
    if (!rwops)
slouken@1895
   195
        RWOP_ERR_QUIT(rwops);
slouken@1895
   196
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   197
        RWOP_ERR_QUIT(rwops);
slouken@1895
   198
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   199
        RWOP_ERR_QUIT(rwops);
slouken@1895
   200
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   201
        RWOP_ERR_QUIT(rwops);
slouken@1895
   202
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   203
        RWOP_ERR_QUIT(rwops);
slouken@1895
   204
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   205
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
slouken@1895
   206
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   207
        RWOP_ERR_QUIT(rwops);
slouken@1895
   208
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   209
        RWOP_ERR_QUIT(rwops);
slouken@1895
   210
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   211
        RWOP_ERR_QUIT(rwops);
slouken@1895
   212
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   213
        RWOP_ERR_QUIT(rwops);
slouken@1895
   214
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   215
        RWOP_ERR_QUIT(rwops);
slouken@1895
   216
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   217
        RWOP_ERR_QUIT(rwops);
slouken@1895
   218
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   219
        RWOP_ERR_QUIT(rwops);
slouken@1895
   220
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   221
        RWOP_ERR_QUIT(rwops);
slouken@1895
   222
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   223
        RWOP_ERR_QUIT(rwops);
slouken@1895
   224
    rwops->close(rwops);
slouken@1895
   225
    printf("test4 OK\n");
slouken@1453
   226
slouken@1453
   227
/* test5 : append mode */
slouken@1895
   228
    rwops = SDL_RWFromFile(FBASENAME1, "ab+");  /* write + read + append */
slouken@1895
   229
    if (!rwops)
slouken@1895
   230
        RWOP_ERR_QUIT(rwops);
slouken@1895
   231
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   232
        RWOP_ERR_QUIT(rwops);
slouken@1895
   233
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   234
        RWOP_ERR_QUIT(rwops);
slouken@1895
   235
    if (7 != rwops->write(rwops, "1234567", 1, 7))
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 (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   241
        RWOP_ERR_QUIT(rwops);
slouken@1895
   242
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   243
        RWOP_ERR_QUIT(rwops);
slouken@1895
   244
slouken@1895
   245
    if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   246
        RWOP_ERR_QUIT(rwops);
slouken@1895
   247
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   248
        RWOP_ERR_QUIT(rwops);
slouken@1895
   249
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   250
        RWOP_ERR_QUIT(rwops);
slouken@1895
   251
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   252
        RWOP_ERR_QUIT(rwops);
slouken@1895
   253
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   254
        RWOP_ERR_QUIT(rwops);
slouken@1895
   255
slouken@1895
   256
    if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   257
        RWOP_ERR_QUIT(rwops);
slouken@1895
   258
slouken@1895
   259
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   260
        RWOP_ERR_QUIT(rwops);
slouken@1895
   261
    if (3 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   262
        RWOP_ERR_QUIT(rwops);
slouken@1895
   263
    if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
slouken@1895
   264
        RWOP_ERR_QUIT(rwops);
slouken@1895
   265
    rwops->close(rwops);
slouken@1895
   266
    printf("test5 OK\n");
slouken@1895
   267
    cleanup();
slouken@1895
   268
    return 0;                   /* all ok */
slouken@1453
   269
}