cscg22-gearboy

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

variator_common.c (6175B)


      1/* See COPYING.txt for the full license governing this code. */
      2/**
      3 * \file variator_common.c
      4 *
      5 * Source file for some common functionality used by variators.
      6 */
      7
      8#include <SDL_test.h>
      9#include "SDL_visualtest_variator_common.h"
     10
     11int
     12SDLVisualTest_NextValue(SDLVisualTest_SUTOptionValue* var,
     13                        SDLVisualTest_SUTOption* opt)
     14{
     15    if(!var)
     16    {
     17        SDLTest_LogError("var argument cannot be NULL");
     18        return -1;
     19    }
     20    if(!opt)
     21    {
     22        SDLTest_LogError("opt argument cannot be NULL");
     23        return -1;
     24    }
     25
     26    switch(opt->type)
     27    {
     28        case SDL_SUT_OPTIONTYPE_BOOL:
     29            if(var->bool_value)
     30            {
     31                var->bool_value = SDL_FALSE;
     32                return 1;
     33            }
     34            else
     35            {
     36                var->bool_value = SDL_TRUE;
     37                return 0;
     38            }
     39        break;
     40
     41        case SDL_SUT_OPTIONTYPE_ENUM:
     42            var->enumerated.index++;
     43            if(!opt->data.enum_values[var->enumerated.index])
     44            {
     45                var->enumerated.index = 0;
     46                return 1;
     47            }
     48            return 0;
     49        break;
     50
     51        case SDL_SUT_OPTIONTYPE_INT:
     52        {
     53            int increment = (opt->data.range.max - opt->data.range.min) /
     54                            SDL_SUT_INTEGER_OPTION_TEST_STEPS;
     55            /* prevents infinite loop when rounding */
     56            if(increment == 0)
     57                increment = 1;
     58            var->integer.value += increment;
     59            if(var->integer.value > opt->data.range.max)
     60            {
     61                var->integer.value = opt->data.range.min;
     62                return 1;
     63            }
     64            return 0;
     65        }
     66        break;
     67
     68        case SDL_SUT_OPTIONTYPE_STRING:
     69            return 1;
     70        break;
     71    }
     72    return -1;
     73}
     74
     75int
     76SDLVisualTest_MakeStrFromVariation(SDLVisualTest_Variation* variation,
     77                                   SDLVisualTest_SUTConfig* config,
     78                                   char* buffer, int size)
     79{
     80    int i, index;
     81    SDLVisualTest_SUTOptionValue* vars;
     82    SDLVisualTest_SUTOption* options;
     83    if(!variation)
     84    {
     85        SDLTest_LogError("variation argument cannot be NULL");
     86        return 0;
     87    }
     88    if(!config)
     89    {
     90        SDLTest_LogError("config argument cannot be NULL");
     91        return 0;
     92    }
     93    if(!buffer)
     94    {
     95        SDLTest_LogError("buffer argument cannot be NULL");
     96        return 0;
     97    }
     98    if(size <= 0)
     99    {
    100        SDLTest_LogError("size argument should be positive");
    101        return 0;
    102    }
    103
    104    index = 0;
    105    buffer[0] = '\0';
    106    options = config->options;
    107    vars = variation->vars;
    108    for(i = 0; i < variation->num_vars; i++)
    109    {
    110        int n, enum_index;
    111        if(index >= size - 1)
    112        {
    113            SDLTest_LogError("String did not fit in buffer size");
    114            return 0;
    115        }
    116        switch(options[i].type)
    117        {
    118            case SDL_SUT_OPTIONTYPE_BOOL:
    119                if(vars[i].bool_value)
    120                {
    121                    n = SDL_snprintf(buffer + index, size - index, "%s ",
    122                                     options[i].name);
    123                    if(n <= 0)
    124                    {
    125                        SDLTest_LogError("SDL_snprintf() failed");
    126                        return 0;
    127                    }
    128                    index += n;
    129                }
    130            break;
    131
    132            case SDL_SUT_OPTIONTYPE_ENUM:
    133                if(vars[i].enumerated.on)
    134                {
    135                    enum_index = vars[i].enumerated.index;
    136                    n = SDL_snprintf(buffer + index, size - index, "%s %s ",
    137                        options[i].name, options[i].data.enum_values[enum_index]);
    138                    index += n;
    139                }
    140            break;
    141
    142            case SDL_SUT_OPTIONTYPE_INT:
    143                if(vars[i].integer.on)
    144                {
    145                    n = SDL_snprintf(buffer + index, size - index, "%s %d ",
    146                                     options[i].name, vars[i].integer.value);
    147                    index += n;
    148                }
    149            break;
    150
    151            case SDL_SUT_OPTIONTYPE_STRING:
    152                if(vars[i].string.on)
    153                {
    154                    n = SDL_snprintf(buffer + index, size - index, "%s %s ",
    155                                     options[i].name, vars[i].string.value);
    156                    index += n;
    157                }
    158            break;
    159        }
    160    }
    161    return 1;
    162}
    163
    164int
    165SDLVisualTest_InitVariation(SDLVisualTest_Variation* variation,
    166                            SDLVisualTest_SUTConfig* config)
    167{
    168    int i;
    169    SDLVisualTest_SUTOptionValue* vars;
    170    SDLVisualTest_SUTOption* options;
    171    if(!variation)
    172    {
    173        SDLTest_LogError("variation argument cannot be NULL");
    174        return 0;
    175    }
    176    if(!config)
    177    {
    178        SDLTest_LogError("config argument cannot be NULL");
    179        return 0;
    180    }
    181
    182    /* initialize the first variation */
    183    if(config->num_options <= 0)
    184    {
    185        SDLTest_LogError("config->num_options must be positive");
    186        return 0;
    187    }
    188    variation->vars = (SDLVisualTest_SUTOptionValue*)SDL_malloc(config->num_options *
    189                     sizeof(SDLVisualTest_SUTOptionValue));
    190    if(!variation->vars)
    191    {
    192        SDLTest_LogError("malloc() failed");
    193        return 0;
    194    }
    195    variation->num_vars = config->num_options;
    196    vars = variation->vars;
    197    options = config->options;
    198    for(i = 0; i < variation->num_vars; i++)
    199    {
    200        switch(options[i].type)
    201        {
    202            case SDL_SUT_OPTIONTYPE_BOOL:
    203                vars[i].bool_value = SDL_FALSE;
    204            break;
    205
    206            case SDL_SUT_OPTIONTYPE_ENUM:
    207                vars[i].enumerated.on = SDL_TRUE;
    208                vars[i].enumerated.index = 0;
    209            break;
    210
    211            case SDL_SUT_OPTIONTYPE_INT:
    212            {
    213                vars[i].integer.on = SDL_TRUE;
    214                vars[i].integer.value = options[i].data.range.min;
    215            }
    216            break;
    217
    218            case SDL_SUT_OPTIONTYPE_STRING:
    219                vars[i].string.on = SDL_TRUE;
    220                vars[i].string.value = options[i].name;
    221            break;
    222        }
    223    }
    224    return 1;
    225}