test/testfile.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 18 Feb 2015 21:31:21 +0100
changeset 9356 e87d6e1e812a
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Removed redundant include statements from test programs.

The needed header files are already included with SDL.h. Still including them in
the test programs is confusing because it somehow suggests they would be needed.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
slouken@1453
    26
slouken@1895
    27
#include <stdio.h>
slouken@1453
    28
slouken@1453
    29
/* WARNING ! those 2 files will be destroyed by this test program */
slouken@2765
    30
slouken@2765
    31
#ifdef __IPHONEOS__
slouken@7191
    32
#define FBASENAME1  "../Documents/sdldata1" /* this file will be created during tests */
slouken@2765
    33
#define FBASENAME2  "../Documents/sdldata2"     /* this file should not exist before starting test */
slouken@2765
    34
#else
slouken@7191
    35
#define FBASENAME1  "sdldata1"      /* this file will be created during tests */
slouken@7191
    36
#define FBASENAME2  "sdldata2"      /* this file should not exist before starting test */
slouken@2765
    37
#endif
slouken@1453
    38
slouken@1453
    39
#ifndef NULL
slouken@1453
    40
#define NULL ((void *)0)
slouken@1895
    41
#endif
slouken@1453
    42
slouken@1895
    43
static void
slouken@1895
    44
cleanup(void)
slouken@1895
    45
{
slouken@1895
    46
    unlink(FBASENAME1);
slouken@1895
    47
    unlink(FBASENAME2);
slouken@1453
    48
}
slouken@1453
    49
slouken@1895
    50
static void
slouken@1895
    51
rwops_error_quit(unsigned line, SDL_RWops * rwops)
slouken@1895
    52
{
aschiffler@7639
    53
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
slouken@1895
    54
    if (rwops) {
slouken@1895
    55
        rwops->close(rwops);    /* This calls SDL_FreeRW(rwops); */
slouken@1895
    56
    }
slouken@1895
    57
    cleanup();
slouken@1895
    58
    exit(1);                    /* quit with rwops error (test failed) */
slouken@1453
    59
}
slouken@1453
    60
slouken@7191
    61
#define RWOP_ERR_QUIT(x)    rwops_error_quit( __LINE__, (x) )
slouken@1453
    62
slouken@1453
    63
slouken@1453
    64
slouken@1895
    65
int
slouken@1895
    66
main(int argc, char *argv[])
slouken@1453
    67
{
slouken@1895
    68
    SDL_RWops *rwops = NULL;
slouken@1895
    69
    char test_buf[30];
slouken@1895
    70
aschiffler@7639
    71
    /* Enable standard application logging */
aschiffler@7639
    72
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    73
slouken@1895
    74
    cleanup();
slouken@1453
    75
slouken@1453
    76
/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
slouken@1895
    77
slouken@1895
    78
    rwops = SDL_RWFromFile(NULL, NULL);
slouken@1895
    79
    if (rwops)
slouken@1895
    80
        RWOP_ERR_QUIT(rwops);
slouken@1895
    81
    rwops = SDL_RWFromFile(NULL, "ab+");
slouken@1895
    82
    if (rwops)
slouken@1895
    83
        RWOP_ERR_QUIT(rwops);
slouken@1895
    84
    rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
slouken@1895
    85
    if (rwops)
slouken@1895
    86
        RWOP_ERR_QUIT(rwops);
slouken@1895
    87
    rwops = SDL_RWFromFile("something", "");
slouken@1895
    88
    if (rwops)
slouken@1895
    89
        RWOP_ERR_QUIT(rwops);
slouken@1895
    90
    rwops = SDL_RWFromFile("something", NULL);
slouken@1895
    91
    if (rwops)
slouken@1895
    92
        RWOP_ERR_QUIT(rwops);
aschiffler@7639
    93
    SDL_Log("test1 OK\n");
slouken@1453
    94
philipp@7538
    95
/* test 2 : check that inexistent file is not successfully opened/created when required */
philipp@7538
    96
/* modes : r, r+ imply that file MUST exist
slouken@1453
    97
   modes : a, a+, w, w+ checks that it succeeds (file may not exists)
slouken@7191
    98
slouken@1453
    99
 */
slouken@1895
   100
    rwops = SDL_RWFromFile(FBASENAME2, "rb");   /* this file doesn't exist that call must fail */
slouken@1895
   101
    if (rwops)
slouken@1895
   102
        RWOP_ERR_QUIT(rwops);
slouken@1895
   103
    rwops = SDL_RWFromFile(FBASENAME2, "rb+");  /* this file doesn't exist that call must fail */
slouken@1895
   104
    if (rwops)
slouken@1895
   105
        RWOP_ERR_QUIT(rwops);
slouken@1895
   106
    rwops = SDL_RWFromFile(FBASENAME2, "wb");
slouken@1895
   107
    if (!rwops)
slouken@1895
   108
        RWOP_ERR_QUIT(rwops);
slouken@1895
   109
    rwops->close(rwops);
slouken@1895
   110
    unlink(FBASENAME2);
slouken@1895
   111
    rwops = SDL_RWFromFile(FBASENAME2, "wb+");
slouken@1895
   112
    if (!rwops)
slouken@1895
   113
        RWOP_ERR_QUIT(rwops);
slouken@1895
   114
    rwops->close(rwops);
slouken@1895
   115
    unlink(FBASENAME2);
slouken@1895
   116
    rwops = SDL_RWFromFile(FBASENAME2, "ab");
slouken@1895
   117
    if (!rwops)
slouken@1895
   118
        RWOP_ERR_QUIT(rwops);
slouken@1895
   119
    rwops->close(rwops);
slouken@1895
   120
    unlink(FBASENAME2);
slouken@1895
   121
    rwops = SDL_RWFromFile(FBASENAME2, "ab+");
slouken@1895
   122
    if (!rwops)
slouken@1895
   123
        RWOP_ERR_QUIT(rwops);
slouken@1895
   124
    rwops->close(rwops);
slouken@1895
   125
    unlink(FBASENAME2);
aschiffler@7639
   126
    SDL_Log("test2 OK\n");
slouken@1453
   127
slouken@7191
   128
/* test 3 : creation, writing , reading, seeking,
slouken@7191
   129
            test : w mode, r mode, w+ mode
slouken@1453
   130
 */
slouken@1895
   131
    rwops = SDL_RWFromFile(FBASENAME1, "wb");   /* write only */
slouken@1895
   132
    if (!rwops)
slouken@1895
   133
        RWOP_ERR_QUIT(rwops);
slouken@1895
   134
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   135
        RWOP_ERR_QUIT(rwops);
slouken@1895
   136
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   137
        RWOP_ERR_QUIT(rwops);
slouken@1895
   138
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   139
        RWOP_ERR_QUIT(rwops);
slouken@1895
   140
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   141
        RWOP_ERR_QUIT(rwops);
slouken@1895
   142
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   143
        RWOP_ERR_QUIT(rwops);   /* we are in write only mode */
slouken@1895
   144
    rwops->close(rwops);
slouken@1453
   145
slouken@1895
   146
    rwops = SDL_RWFromFile(FBASENAME1, "rb");   /* read mode, file must exists */
slouken@1895
   147
    if (!rwops)
slouken@1895
   148
        RWOP_ERR_QUIT(rwops);
slouken@1895
   149
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   150
        RWOP_ERR_QUIT(rwops);
slouken@1895
   151
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   152
        RWOP_ERR_QUIT(rwops);
slouken@1895
   153
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   154
        RWOP_ERR_QUIT(rwops);
slouken@1895
   155
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   156
        RWOP_ERR_QUIT(rwops);
slouken@1895
   157
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   158
        RWOP_ERR_QUIT(rwops);
slouken@1895
   159
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   160
        RWOP_ERR_QUIT(rwops);
slouken@1895
   161
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   162
        RWOP_ERR_QUIT(rwops);
slouken@1895
   163
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   164
        RWOP_ERR_QUIT(rwops);
slouken@1895
   165
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   166
        RWOP_ERR_QUIT(rwops);
slouken@1895
   167
    if (0 != rwops->write(rwops, test_buf, 1, 1))
slouken@1895
   168
        RWOP_ERR_QUIT(rwops);   /* readonly mode */
slouken@1895
   169
    rwops->close(rwops);
slouken@1453
   170
slouken@1453
   171
/* test 3: same with w+ mode */
slouken@1895
   172
    rwops = SDL_RWFromFile(FBASENAME1, "wb+");  /* write + read + truncation */
slouken@1895
   173
    if (!rwops)
slouken@1895
   174
        RWOP_ERR_QUIT(rwops);
slouken@1895
   175
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   176
        RWOP_ERR_QUIT(rwops);
slouken@1895
   177
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   178
        RWOP_ERR_QUIT(rwops);
slouken@1895
   179
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   180
        RWOP_ERR_QUIT(rwops);
slouken@1895
   181
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   182
        RWOP_ERR_QUIT(rwops);
slouken@1895
   183
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   184
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
slouken@1895
   185
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   186
        RWOP_ERR_QUIT(rwops);
slouken@1895
   187
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   188
        RWOP_ERR_QUIT(rwops);
slouken@1895
   189
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   190
        RWOP_ERR_QUIT(rwops);
slouken@1895
   191
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   192
        RWOP_ERR_QUIT(rwops);
slouken@1895
   193
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   194
        RWOP_ERR_QUIT(rwops);
slouken@1895
   195
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   196
        RWOP_ERR_QUIT(rwops);
slouken@1895
   197
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   198
        RWOP_ERR_QUIT(rwops);
slouken@1895
   199
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   200
        RWOP_ERR_QUIT(rwops);
slouken@1895
   201
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   202
        RWOP_ERR_QUIT(rwops);
slouken@1895
   203
    rwops->close(rwops);
aschiffler@7639
   204
    SDL_Log("test3 OK\n");
slouken@1453
   205
slouken@1453
   206
/* test 4: same in r+ mode */
slouken@1895
   207
    rwops = SDL_RWFromFile(FBASENAME1, "rb+");  /* write + read + file must exists, no truncation */
slouken@1895
   208
    if (!rwops)
slouken@1895
   209
        RWOP_ERR_QUIT(rwops);
slouken@1895
   210
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   211
        RWOP_ERR_QUIT(rwops);
slouken@1895
   212
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   213
        RWOP_ERR_QUIT(rwops);
slouken@1895
   214
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   215
        RWOP_ERR_QUIT(rwops);
slouken@1895
   216
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   217
        RWOP_ERR_QUIT(rwops);
slouken@1895
   218
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   219
        RWOP_ERR_QUIT(rwops);   /* we are in read/write mode */
slouken@1895
   220
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   221
        RWOP_ERR_QUIT(rwops);
slouken@1895
   222
    if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   223
        RWOP_ERR_QUIT(rwops);
slouken@1895
   224
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   225
        RWOP_ERR_QUIT(rwops);
slouken@1895
   226
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   227
        RWOP_ERR_QUIT(rwops);
slouken@1895
   228
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   229
        RWOP_ERR_QUIT(rwops);
slouken@1895
   230
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   231
        RWOP_ERR_QUIT(rwops);
slouken@1895
   232
    if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   233
        RWOP_ERR_QUIT(rwops);
slouken@1895
   234
    if (2 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   235
        RWOP_ERR_QUIT(rwops);
slouken@1895
   236
    if (SDL_memcmp(test_buf, "12345678901234567890", 20))
slouken@1895
   237
        RWOP_ERR_QUIT(rwops);
slouken@1895
   238
    rwops->close(rwops);
aschiffler@7639
   239
    SDL_Log("test4 OK\n");
slouken@1453
   240
slouken@1453
   241
/* test5 : append mode */
slouken@1895
   242
    rwops = SDL_RWFromFile(FBASENAME1, "ab+");  /* write + read + append */
slouken@1895
   243
    if (!rwops)
slouken@1895
   244
        RWOP_ERR_QUIT(rwops);
slouken@1895
   245
    if (1 != rwops->write(rwops, "1234567890", 10, 1))
slouken@1895
   246
        RWOP_ERR_QUIT(rwops);
slouken@1895
   247
    if (10 != rwops->write(rwops, "1234567890", 1, 10))
slouken@1895
   248
        RWOP_ERR_QUIT(rwops);
slouken@1895
   249
    if (7 != rwops->write(rwops, "1234567", 1, 7))
slouken@1895
   250
        RWOP_ERR_QUIT(rwops);
slouken@1895
   251
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   252
        RWOP_ERR_QUIT(rwops);
slouken@1895
   253
slouken@1895
   254
    if (1 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   255
        RWOP_ERR_QUIT(rwops);
slouken@1895
   256
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   257
        RWOP_ERR_QUIT(rwops);
slouken@1895
   258
slouken@1895
   259
    if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
slouken@1895
   260
        RWOP_ERR_QUIT(rwops);
slouken@1895
   261
    if (7 != rwops->read(rwops, test_buf, 1, 7))
slouken@1895
   262
        RWOP_ERR_QUIT(rwops);
slouken@1895
   263
    if (SDL_memcmp(test_buf, "1234567", 7))
slouken@1895
   264
        RWOP_ERR_QUIT(rwops);
slouken@1895
   265
    if (0 != rwops->read(rwops, test_buf, 1, 1))
slouken@1895
   266
        RWOP_ERR_QUIT(rwops);
slouken@1895
   267
    if (0 != rwops->read(rwops, test_buf, 10, 100))
slouken@1895
   268
        RWOP_ERR_QUIT(rwops);
slouken@1895
   269
slouken@1895
   270
    if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
slouken@1895
   271
        RWOP_ERR_QUIT(rwops);
slouken@1895
   272
slouken@1895
   273
    if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
slouken@1895
   274
        RWOP_ERR_QUIT(rwops);
slouken@1895
   275
    if (3 != rwops->read(rwops, test_buf, 10, 3))
slouken@1895
   276
        RWOP_ERR_QUIT(rwops);
slouken@1895
   277
    if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
slouken@1895
   278
        RWOP_ERR_QUIT(rwops);
slouken@1895
   279
    rwops->close(rwops);
aschiffler@7639
   280
    SDL_Log("test5 OK\n");
slouken@1895
   281
    cleanup();
slouken@1895
   282
    return 0;                   /* all ok */
slouken@1453
   283
}