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