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