test/testfile.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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