cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

testfile.c (9494B)


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