test/testfile.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    23
cleanup (void)
slouken@1662
    24
{
slouken@1453
    25
slouken@1662
    26
    unlink (FBASENAME1);
slouken@1662
    27
    unlink (FBASENAME2);
slouken@1453
    28
}
slouken@1453
    29
slouken@1662
    30
static void
slouken@1662
    31
rwops_error_quit (unsigned line, SDL_RWops * rwops)
slouken@1662
    32
{
slouken@1662
    33
slouken@1662
    34
    printf ("testfile.c(%d): failed\n", line);
slouken@1662
    35
    if (rwops) {
slouken@1662
    36
        rwops->close (rwops);   /* This calls SDL_FreeRW(rwops); */
slouken@1662
    37
    }
slouken@1662
    38
    cleanup ();
slouken@1662
    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@1662
    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@1662
    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@1662
    56
    rwops = SDL_RWFromFile (NULL, NULL);
slouken@1662
    57
    if (rwops)
slouken@1662
    58
        RWOP_ERR_QUIT (rwops);
slouken@1662
    59
    rwops = SDL_RWFromFile (NULL, "ab+");
slouken@1662
    60
    if (rwops)
slouken@1662
    61
        RWOP_ERR_QUIT (rwops);
slouken@1662
    62
    rwops = SDL_RWFromFile (NULL, "sldfkjsldkfj");
slouken@1662
    63
    if (rwops)
slouken@1662
    64
        RWOP_ERR_QUIT (rwops);
slouken@1662
    65
    rwops = SDL_RWFromFile ("something", "");
slouken@1662
    66
    if (rwops)
slouken@1662
    67
        RWOP_ERR_QUIT (rwops);
slouken@1662
    68
    rwops = SDL_RWFromFile ("something", NULL);
slouken@1662
    69
    if (rwops)
slouken@1662
    70
        RWOP_ERR_QUIT (rwops);
slouken@1662
    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@1662
    78
    rwops = SDL_RWFromFile (FBASENAME2, "rb");  /* this file doesn't exist that call must fail */
slouken@1662
    79
    if (rwops)
slouken@1662
    80
        RWOP_ERR_QUIT (rwops);
slouken@1662
    81
    rwops = SDL_RWFromFile (FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
slouken@1662
    82
    if (rwops)
slouken@1662
    83
        RWOP_ERR_QUIT (rwops);
slouken@1662
    84
    rwops = SDL_RWFromFile (FBASENAME2, "wb");
slouken@1662
    85
    if (!rwops)
slouken@1662
    86
        RWOP_ERR_QUIT (rwops);
slouken@1662
    87
    rwops->close (rwops);
slouken@1662
    88
    unlink (FBASENAME2);
slouken@1662
    89
    rwops = SDL_RWFromFile (FBASENAME2, "wb+");
slouken@1662
    90
    if (!rwops)
slouken@1662
    91
        RWOP_ERR_QUIT (rwops);
slouken@1662
    92
    rwops->close (rwops);
slouken@1662
    93
    unlink (FBASENAME2);
slouken@1662
    94
    rwops = SDL_RWFromFile (FBASENAME2, "ab");
slouken@1662
    95
    if (!rwops)
slouken@1662
    96
        RWOP_ERR_QUIT (rwops);
slouken@1662
    97
    rwops->close (rwops);
slouken@1662
    98
    unlink (FBASENAME2);
slouken@1662
    99
    rwops = SDL_RWFromFile (FBASENAME2, "ab+");
slouken@1662
   100
    if (!rwops)
slouken@1662
   101
        RWOP_ERR_QUIT (rwops);
slouken@1662
   102
    rwops->close (rwops);
slouken@1662
   103
    unlink (FBASENAME2);
slouken@1662
   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@1662
   109
    rwops = SDL_RWFromFile (FBASENAME1, "wb");  /* write only */
slouken@1662
   110
    if (!rwops)
slouken@1662
   111
        RWOP_ERR_QUIT (rwops);
slouken@1662
   112
    if (1 != rwops->write (rwops, "1234567890", 10, 1))
slouken@1662
   113
        RWOP_ERR_QUIT (rwops);
slouken@1662
   114
    if (10 != rwops->write (rwops, "1234567890", 1, 10))
slouken@1662
   115
        RWOP_ERR_QUIT (rwops);
slouken@1662
   116
    if (7 != rwops->write (rwops, "1234567", 1, 7))
slouken@1662
   117
        RWOP_ERR_QUIT (rwops);
slouken@1662
   118
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   119
        RWOP_ERR_QUIT (rwops);
slouken@1662
   120
    if (0 != rwops->read (rwops, test_buf, 1, 1))
slouken@1662
   121
        RWOP_ERR_QUIT (rwops);  /* we are in write only mode */
slouken@1662
   122
    rwops->close (rwops);
slouken@1453
   123
slouken@1662
   124
    rwops = SDL_RWFromFile (FBASENAME1, "rb");  /* read mode, file must exists */
slouken@1662
   125
    if (!rwops)
slouken@1662
   126
        RWOP_ERR_QUIT (rwops);
slouken@1662
   127
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   128
        RWOP_ERR_QUIT (rwops);
slouken@1662
   129
    if (20 != rwops->seek (rwops, -7, RW_SEEK_END))
slouken@1662
   130
        RWOP_ERR_QUIT (rwops);
slouken@1662
   131
    if (7 != rwops->read (rwops, test_buf, 1, 7))
slouken@1662
   132
        RWOP_ERR_QUIT (rwops);
slouken@1662
   133
    if (SDL_memcmp (test_buf, "1234567", 7))
slouken@1662
   134
        RWOP_ERR_QUIT (rwops);
slouken@1662
   135
    if (0 != rwops->read (rwops, test_buf, 1, 1))
slouken@1662
   136
        RWOP_ERR_QUIT (rwops);
slouken@1662
   137
    if (0 != rwops->read (rwops, test_buf, 10, 100))
slouken@1662
   138
        RWOP_ERR_QUIT (rwops);
slouken@1662
   139
    if (0 != rwops->seek (rwops, -27, RW_SEEK_CUR))
slouken@1662
   140
        RWOP_ERR_QUIT (rwops);
slouken@1662
   141
    if (2 != rwops->read (rwops, test_buf, 10, 3))
slouken@1662
   142
        RWOP_ERR_QUIT (rwops);
slouken@1662
   143
    if (SDL_memcmp (test_buf, "12345678901234567890", 20))
slouken@1662
   144
        RWOP_ERR_QUIT (rwops);
slouken@1662
   145
    if (0 != rwops->write (rwops, test_buf, 1, 1))
slouken@1662
   146
        RWOP_ERR_QUIT (rwops);  /* readonly mode */
slouken@1662
   147
    rwops->close (rwops);
slouken@1453
   148
slouken@1453
   149
/* test 3: same with w+ mode */
slouken@1662
   150
    rwops = SDL_RWFromFile (FBASENAME1, "wb+"); /* write + read + truncation */
slouken@1662
   151
    if (!rwops)
slouken@1662
   152
        RWOP_ERR_QUIT (rwops);
slouken@1662
   153
    if (1 != rwops->write (rwops, "1234567890", 10, 1))
slouken@1662
   154
        RWOP_ERR_QUIT (rwops);
slouken@1662
   155
    if (10 != rwops->write (rwops, "1234567890", 1, 10))
slouken@1662
   156
        RWOP_ERR_QUIT (rwops);
slouken@1662
   157
    if (7 != rwops->write (rwops, "1234567", 1, 7))
slouken@1662
   158
        RWOP_ERR_QUIT (rwops);
slouken@1662
   159
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   160
        RWOP_ERR_QUIT (rwops);
slouken@1662
   161
    if (1 != rwops->read (rwops, test_buf, 1, 1))
slouken@1662
   162
        RWOP_ERR_QUIT (rwops);  /* we are in read/write mode */
slouken@1662
   163
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   164
        RWOP_ERR_QUIT (rwops);
slouken@1662
   165
    if (20 != rwops->seek (rwops, -7, RW_SEEK_END))
slouken@1662
   166
        RWOP_ERR_QUIT (rwops);
slouken@1662
   167
    if (7 != rwops->read (rwops, test_buf, 1, 7))
slouken@1662
   168
        RWOP_ERR_QUIT (rwops);
slouken@1662
   169
    if (SDL_memcmp (test_buf, "1234567", 7))
slouken@1662
   170
        RWOP_ERR_QUIT (rwops);
slouken@1662
   171
    if (0 != rwops->read (rwops, test_buf, 1, 1))
slouken@1662
   172
        RWOP_ERR_QUIT (rwops);
slouken@1662
   173
    if (0 != rwops->read (rwops, test_buf, 10, 100))
slouken@1662
   174
        RWOP_ERR_QUIT (rwops);
slouken@1662
   175
    if (0 != rwops->seek (rwops, -27, RW_SEEK_CUR))
slouken@1662
   176
        RWOP_ERR_QUIT (rwops);
slouken@1662
   177
    if (2 != rwops->read (rwops, test_buf, 10, 3))
slouken@1662
   178
        RWOP_ERR_QUIT (rwops);
slouken@1662
   179
    if (SDL_memcmp (test_buf, "12345678901234567890", 20))
slouken@1662
   180
        RWOP_ERR_QUIT (rwops);
slouken@1662
   181
    rwops->close (rwops);
slouken@1662
   182
    printf ("test3 OK\n");
slouken@1453
   183
slouken@1453
   184
/* test 4: same in r+ mode */
slouken@1662
   185
    rwops = SDL_RWFromFile (FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
slouken@1662
   186
    if (!rwops)
slouken@1662
   187
        RWOP_ERR_QUIT (rwops);
slouken@1662
   188
    if (1 != rwops->write (rwops, "1234567890", 10, 1))
slouken@1662
   189
        RWOP_ERR_QUIT (rwops);
slouken@1662
   190
    if (10 != rwops->write (rwops, "1234567890", 1, 10))
slouken@1662
   191
        RWOP_ERR_QUIT (rwops);
slouken@1662
   192
    if (7 != rwops->write (rwops, "1234567", 1, 7))
slouken@1662
   193
        RWOP_ERR_QUIT (rwops);
slouken@1662
   194
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   195
        RWOP_ERR_QUIT (rwops);
slouken@1662
   196
    if (1 != rwops->read (rwops, test_buf, 1, 1))
slouken@1662
   197
        RWOP_ERR_QUIT (rwops);  /* we are in read/write mode */
slouken@1662
   198
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   199
        RWOP_ERR_QUIT (rwops);
slouken@1662
   200
    if (20 != rwops->seek (rwops, -7, RW_SEEK_END))
slouken@1662
   201
        RWOP_ERR_QUIT (rwops);
slouken@1662
   202
    if (7 != rwops->read (rwops, test_buf, 1, 7))
slouken@1662
   203
        RWOP_ERR_QUIT (rwops);
slouken@1662
   204
    if (SDL_memcmp (test_buf, "1234567", 7))
slouken@1662
   205
        RWOP_ERR_QUIT (rwops);
slouken@1662
   206
    if (0 != rwops->read (rwops, test_buf, 1, 1))
slouken@1662
   207
        RWOP_ERR_QUIT (rwops);
slouken@1662
   208
    if (0 != rwops->read (rwops, test_buf, 10, 100))
slouken@1662
   209
        RWOP_ERR_QUIT (rwops);
slouken@1662
   210
    if (0 != rwops->seek (rwops, -27, RW_SEEK_CUR))
slouken@1662
   211
        RWOP_ERR_QUIT (rwops);
slouken@1662
   212
    if (2 != rwops->read (rwops, test_buf, 10, 3))
slouken@1662
   213
        RWOP_ERR_QUIT (rwops);
slouken@1662
   214
    if (SDL_memcmp (test_buf, "12345678901234567890", 20))
slouken@1662
   215
        RWOP_ERR_QUIT (rwops);
slouken@1662
   216
    rwops->close (rwops);
slouken@1662
   217
    printf ("test4 OK\n");
slouken@1453
   218
slouken@1453
   219
/* test5 : append mode */
slouken@1662
   220
    rwops = SDL_RWFromFile (FBASENAME1, "ab+"); /* write + read + append */
slouken@1662
   221
    if (!rwops)
slouken@1662
   222
        RWOP_ERR_QUIT (rwops);
slouken@1662
   223
    if (1 != rwops->write (rwops, "1234567890", 10, 1))
slouken@1662
   224
        RWOP_ERR_QUIT (rwops);
slouken@1662
   225
    if (10 != rwops->write (rwops, "1234567890", 1, 10))
slouken@1662
   226
        RWOP_ERR_QUIT (rwops);
slouken@1662
   227
    if (7 != rwops->write (rwops, "1234567", 1, 7))
slouken@1662
   228
        RWOP_ERR_QUIT (rwops);
slouken@1662
   229
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   230
        RWOP_ERR_QUIT (rwops);
slouken@1662
   231
slouken@1662
   232
    if (1 != rwops->read (rwops, test_buf, 1, 1))
slouken@1662
   233
        RWOP_ERR_QUIT (rwops);
slouken@1662
   234
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   235
        RWOP_ERR_QUIT (rwops);
slouken@1662
   236
slouken@1662
   237
    if (20 + 27 != rwops->seek (rwops, -7, RW_SEEK_END))
slouken@1662
   238
        RWOP_ERR_QUIT (rwops);
slouken@1662
   239
    if (7 != rwops->read (rwops, test_buf, 1, 7))
slouken@1662
   240
        RWOP_ERR_QUIT (rwops);
slouken@1662
   241
    if (SDL_memcmp (test_buf, "1234567", 7))
slouken@1662
   242
        RWOP_ERR_QUIT (rwops);
slouken@1662
   243
    if (0 != rwops->read (rwops, test_buf, 1, 1))
slouken@1662
   244
        RWOP_ERR_QUIT (rwops);
slouken@1662
   245
    if (0 != rwops->read (rwops, test_buf, 10, 100))
slouken@1662
   246
        RWOP_ERR_QUIT (rwops);
slouken@1662
   247
slouken@1662
   248
    if (27 != rwops->seek (rwops, -27, RW_SEEK_CUR))
slouken@1662
   249
        RWOP_ERR_QUIT (rwops);
slouken@1662
   250
slouken@1662
   251
    if (0 != rwops->seek (rwops, 0L, RW_SEEK_SET))
slouken@1662
   252
        RWOP_ERR_QUIT (rwops);
slouken@1662
   253
    if (3 != rwops->read (rwops, test_buf, 10, 3))
slouken@1662
   254
        RWOP_ERR_QUIT (rwops);
slouken@1662
   255
    if (SDL_memcmp (test_buf, "123456789012345678901234567123", 30))
slouken@1662
   256
        RWOP_ERR_QUIT (rwops);
slouken@1662
   257
    rwops->close (rwops);
slouken@1662
   258
    printf ("test5 OK\n");
slouken@1662
   259
    cleanup ();
slouken@1662
   260
    return 0;                   /* all ok */
slouken@1453
   261
}