test/testfile.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Jan 2011 18:04:05 -0800
changeset 5062 e8916fe9cfc8
parent 4884 27ab20a36eba
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed bug #925

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