test/testfile.c
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 20 Aug 2013 20:34:40 -0300
changeset 7663 53fe1b64eb2d
parent 7639 9406b7dd2f2d
child 8149 681eb46b8ac4
permissions -rw-r--r--
More non C89 compliant comments
slouken@5535
     1
/*
slouken@7517
     2
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@1453
    12
slouken@1453
    13
/* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */
slouken@1453
    14
gabomdq@7663
    15
/* quiet windows compiler warnings */
aschiffler@4884
    16
#define _CRT_NONSTDC_NO_WARNINGS
aschiffler@4884
    17
slouken@1484
    18
#include <stdlib.h>
icculus@2099
    19
icculus@2099
    20
#ifndef _MSC_VER
slouken@1843
    21
#include <unistd.h>
icculus@2099
    22
#endif
slouken@1453
    23
slouken@1453
    24
#include "SDL.h"
slouken@1453
    25
#include "SDL_endian.h"
slouken@1453
    26
slouken@1453
    27
slouken@1895
    28
#include <stdio.h>
slouken@1453
    29
slouken@1453
    30
/* WARNING ! those 2 files will be destroyed by this test program */
slouken@2765
    31
slouken@2765
    32
#ifdef __IPHONEOS__
slouken@7191
    33
#define FBASENAME1  "../Documents/sdldata1" /* this file will be created during tests */
slouken@2765
    34
#define FBASENAME2  "../Documents/sdldata2"     /* this file should not exist before starting test */
slouken@2765
    35
#else
slouken@7191
    36
#define FBASENAME1  "sdldata1"      /* this file will be created during tests */
slouken@7191
    37
#define FBASENAME2  "sdldata2"      /* this file should not exist before starting test */
slouken@2765
    38
#endif
slouken@1453
    39
slouken@1453
    40
#ifndef NULL
slouken@1453
    41
#define NULL ((void *)0)
slouken@1895
    42
#endif
slouken@1453
    43
slouken@1895
    44
static void
slouken@1895
    45
cleanup(void)
slouken@1895
    46
{
slouken@1895
    47
    unlink(FBASENAME1);
slouken@1895
    48
    unlink(FBASENAME2);
slouken@1453
    49
}
slouken@1453
    50
slouken@1895
    51
static void
slouken@1895
    52
rwops_error_quit(unsigned line, SDL_RWops * rwops)
slouken@1895
    53
{
aschiffler@7639
    54
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
slouken@1895
    55
    if (rwops) {
slouken@1895
    56
        rwops->close(rwops);    /* This calls SDL_FreeRW(rwops); */
slouken@1895
    57
    }
slouken@1895
    58
    cleanup();
slouken@1895
    59
    exit(1);                    /* quit with rwops error (test failed) */
slouken@1453
    60
}
slouken@1453
    61
slouken@7191
    62
#define RWOP_ERR_QUIT(x)    rwops_error_quit( __LINE__, (x) )
slouken@1453
    63
slouken@1453
    64
slouken@1453
    65
slouken@1895
    66
int
slouken@1895
    67
main(int argc, char *argv[])
slouken@1453
    68
{
slouken@1895
    69
    SDL_RWops *rwops = NULL;
slouken@1895
    70
    char test_buf[30];
slouken@1895
    71
aschiffler@7639
    72
    /* Enable standard application logging */
aschiffler@7639
    73
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    74
slouken@1895
    75
    cleanup();
slouken@1453
    76
slouken@1453
    77
/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
slouken@1895
    78
slouken@1895
    79
    rwops = SDL_RWFromFile(NULL, NULL);
slouken@1895
    80
    if (rwops)
slouken@1895
    81
        RWOP_ERR_QUIT(rwops);
slouken@1895
    82
    rwops = SDL_RWFromFile(NULL, "ab+");
slouken@1895
    83
    if (rwops)
slouken@1895
    84
        RWOP_ERR_QUIT(rwops);
slouken@1895
    85
    rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
slouken@1895
    86
    if (rwops)
slouken@1895
    87
        RWOP_ERR_QUIT(rwops);
slouken@1895
    88
    rwops = SDL_RWFromFile("something", "");
slouken@1895
    89
    if (rwops)
slouken@1895
    90
        RWOP_ERR_QUIT(rwops);
slouken@1895
    91
    rwops = SDL_RWFromFile("something", NULL);
slouken@1895
    92
    if (rwops)
slouken@1895
    93
        RWOP_ERR_QUIT(rwops);
aschiffler@7639
    94
    SDL_Log("test1 OK\n");
slouken@1453
    95
philipp@7538
    96
/* test 2 : check that inexistent file is not successfully opened/created when required */
philipp@7538
    97
/* modes : r, r+ imply that file MUST exist
slouken@1453
    98
   modes : a, a+, w, w+ checks that it succeeds (file may not exists)
slouken@7191
    99
slouken@1453
   100
 */
slouken@1895
   101
    rwops = SDL_RWFromFile(FBASENAME2, "rb");   /* this file doesn't exist that call must fail */
slouken@1895
   102
    if (rwops)
slouken@1895
   103
        RWOP_ERR_QUIT(rwops);
slouken@1895
   104
    rwops = SDL_RWFromFile(FBASENAME2, "rb+");  /* this file doesn't exist that call must fail */
slouken@1895
   105
    if (rwops)
slouken@1895
   106
        RWOP_ERR_QUIT(rwops);
slouken@1895
   107
    rwops = SDL_RWFromFile(FBASENAME2, "wb");
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
    rwops = SDL_RWFromFile(FBASENAME2, "wb+");
slouken@1895
   113
    if (!rwops)
slouken@1895
   114
        RWOP_ERR_QUIT(rwops);
slouken@1895
   115
    rwops->close(rwops);
slouken@1895
   116
    unlink(FBASENAME2);
slouken@1895
   117
    rwops = SDL_RWFromFile(FBASENAME2, "ab");
slouken@1895
   118
    if (!rwops)
slouken@1895
   119
        RWOP_ERR_QUIT(rwops);
slouken@1895
   120
    rwops->close(rwops);
slouken@1895
   121
    unlink(FBASENAME2);
slouken@1895
   122
    rwops = SDL_RWFromFile(FBASENAME2, "ab+");
slouken@1895
   123
    if (!rwops)
slouken@1895
   124
        RWOP_ERR_QUIT(rwops);
slouken@1895
   125
    rwops->close(rwops);
slouken@1895
   126
    unlink(FBASENAME2);
aschiffler@7639
   127
    SDL_Log("test2 OK\n");
slouken@1453
   128
slouken@7191
   129
/* test 3 : creation, writing , reading, seeking,
slouken@7191
   130
            test : w mode, r mode, w+ mode
slouken@1453
   131
 */
slouken@1895
   132
    rwops = SDL_RWFromFile(FBASENAME1, "wb");   /* write only */
slouken@1895
   133
    if (!rwops)
slouken@1895
   134
        RWOP_ERR_QUIT(rwops);
slouken@1895
   135
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   136
        RWOP_ERR_QUIT(rwops);
slouken@1895
   137
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   138
        RWOP_ERR_QUIT(rwops);
slouken@1895
   139
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   140
        RWOP_ERR_QUIT(rwops);
slouken@1895
   141
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
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);   /* we are in write only mode */
slouken@1895
   145
    rwops->close(rwops);
slouken@1453
   146
slouken@1895
   147
    rwops = SDL_RWFromFile(FBASENAME1, "rb");   /* read mode, file must exists */
slouken@1895
   148
    if (!rwops)
slouken@1895
   149
        RWOP_ERR_QUIT(rwops);
slouken@1895
   150
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   151
        RWOP_ERR_QUIT(rwops);
slouken@1895
   152
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   153
        RWOP_ERR_QUIT(rwops);
slouken@1895
   154
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   155
        RWOP_ERR_QUIT(rwops);
slouken@1895
   156
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   157
        RWOP_ERR_QUIT(rwops);
slouken@1895
   158
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   159
        RWOP_ERR_QUIT(rwops);
slouken@1895
   160
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   161
        RWOP_ERR_QUIT(rwops);
slouken@1895
   162
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   163
        RWOP_ERR_QUIT(rwops);
slouken@1895
   164
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   165
        RWOP_ERR_QUIT(rwops);
slouken@1895
   166
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   167
        RWOP_ERR_QUIT(rwops);
slouken@1895
   168
    if (0 != rwops->write(rwops, test_buf, 1, 1))
slouken@1895
   169
        RWOP_ERR_QUIT(rwops);   /* readonly mode */
slouken@1895
   170
    rwops->close(rwops);
slouken@1453
   171
slouken@1453
   172
/* test 3: same with w+ mode */
slouken@1895
   173
    rwops = SDL_RWFromFile(FBASENAME1, "wb+");  /* write + read + truncation */
slouken@1895
   174
    if (!rwops)
slouken@1895
   175
        RWOP_ERR_QUIT(rwops);
slouken@1895
   176
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   177
        RWOP_ERR_QUIT(rwops);
slouken@1895
   178
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   179
        RWOP_ERR_QUIT(rwops);
slouken@1895
   180
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   181
        RWOP_ERR_QUIT(rwops);
slouken@1895
   182
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   183
        RWOP_ERR_QUIT(rwops);
slouken@1895
   184
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   185
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
slouken@1895
   186
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   187
        RWOP_ERR_QUIT(rwops);
slouken@1895
   188
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   189
        RWOP_ERR_QUIT(rwops);
slouken@1895
   190
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   191
        RWOP_ERR_QUIT(rwops);
slouken@1895
   192
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   193
        RWOP_ERR_QUIT(rwops);
slouken@1895
   194
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   195
        RWOP_ERR_QUIT(rwops);
slouken@1895
   196
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   197
        RWOP_ERR_QUIT(rwops);
slouken@1895
   198
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   199
        RWOP_ERR_QUIT(rwops);
slouken@1895
   200
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   201
        RWOP_ERR_QUIT(rwops);
slouken@1895
   202
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   203
        RWOP_ERR_QUIT(rwops);
slouken@1895
   204
    rwops->close(rwops);
aschiffler@7639
   205
    SDL_Log("test3 OK\n");
slouken@1453
   206
slouken@1453
   207
/* test 4: same in r+ mode */
slouken@1895
   208
    rwops = SDL_RWFromFile(FBASENAME1, "rb+");  /* write + read + file must exists, no truncation */
slouken@1895
   209
    if (!rwops)
slouken@1895
   210
        RWOP_ERR_QUIT(rwops);
slouken@1895
   211
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   212
        RWOP_ERR_QUIT(rwops);
slouken@1895
   213
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   214
        RWOP_ERR_QUIT(rwops);
slouken@1895
   215
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   216
        RWOP_ERR_QUIT(rwops);
slouken@1895
   217
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   218
        RWOP_ERR_QUIT(rwops);
slouken@1895
   219
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   220
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
slouken@1895
   221
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   222
        RWOP_ERR_QUIT(rwops);
slouken@1895
   223
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   224
        RWOP_ERR_QUIT(rwops);
slouken@1895
   225
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   226
        RWOP_ERR_QUIT(rwops);
slouken@1895
   227
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   228
        RWOP_ERR_QUIT(rwops);
slouken@1895
   229
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   230
        RWOP_ERR_QUIT(rwops);
slouken@1895
   231
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   232
        RWOP_ERR_QUIT(rwops);
slouken@1895
   233
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   234
        RWOP_ERR_QUIT(rwops);
slouken@1895
   235
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   236
        RWOP_ERR_QUIT(rwops);
slouken@1895
   237
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   238
        RWOP_ERR_QUIT(rwops);
slouken@1895
   239
    rwops->close(rwops);
aschiffler@7639
   240
    SDL_Log("test4 OK\n");
slouken@1453
   241
slouken@1453
   242
/* test5 : append mode */
slouken@1895
   243
    rwops = SDL_RWFromFile(FBASENAME1, "ab+");  /* write + read + append */
slouken@1895
   244
    if (!rwops)
slouken@1895
   245
        RWOP_ERR_QUIT(rwops);
slouken@1895
   246
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   247
        RWOP_ERR_QUIT(rwops);
slouken@1895
   248
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   249
        RWOP_ERR_QUIT(rwops);
slouken@1895
   250
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   251
        RWOP_ERR_QUIT(rwops);
slouken@1895
   252
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   253
        RWOP_ERR_QUIT(rwops);
slouken@1895
   254
slouken@1895
   255
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   256
        RWOP_ERR_QUIT(rwops);
slouken@1895
   257
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   258
        RWOP_ERR_QUIT(rwops);
slouken@1895
   259
slouken@1895
   260
    if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   261
        RWOP_ERR_QUIT(rwops);
slouken@1895
   262
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   263
        RWOP_ERR_QUIT(rwops);
slouken@1895
   264
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   265
        RWOP_ERR_QUIT(rwops);
slouken@1895
   266
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   267
        RWOP_ERR_QUIT(rwops);
slouken@1895
   268
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   269
        RWOP_ERR_QUIT(rwops);
slouken@1895
   270
slouken@1895
   271
    if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   272
        RWOP_ERR_QUIT(rwops);
slouken@1895
   273
slouken@1895
   274
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   275
        RWOP_ERR_QUIT(rwops);
slouken@1895
   276
    if (3 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   277
        RWOP_ERR_QUIT(rwops);
slouken@1895
   278
    if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
slouken@1895
   279
        RWOP_ERR_QUIT(rwops);
slouken@1895
   280
    rwops->close(rwops);
aschiffler@7639
   281
    SDL_Log("test5 OK\n");
slouken@1895
   282
    cleanup();
slouken@1895
   283
    return 0;                   /* all ok */
slouken@1453
   284
}