test/testfile.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Oct 2008 06:46:59 +0000
changeset 2765 f55c87ae336b
parent 2099 891ed60fcaa9
child 4884 27ab20a36eba
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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