visualtest/src/variator_common.c
changeset 7924 fcb86d323770
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/visualtest/src/variator_common.c	Sun Nov 10 00:32:23 2013 -0500
     1.3 @@ -0,0 +1,225 @@
     1.4 +/* See COPYING.txt for the full license governing this code. */
     1.5 +/**
     1.6 + * \file variator_common.c
     1.7 + *
     1.8 + * Source file for some common functionality used by variators.
     1.9 + */
    1.10 +
    1.11 +#include <SDL_test.h>
    1.12 +#include "SDL_visualtest_variator_common.h"
    1.13 +
    1.14 +int
    1.15 +SDLVisualTest_NextValue(SDLVisualTest_SUTOptionValue* var,
    1.16 +                        SDLVisualTest_SUTOption* opt)
    1.17 +{
    1.18 +    if(!var)
    1.19 +    {
    1.20 +        SDLTest_LogError("var argument cannot be NULL");
    1.21 +        return -1;
    1.22 +    }
    1.23 +    if(!opt)
    1.24 +    {
    1.25 +        SDLTest_LogError("opt argument cannot be NULL");
    1.26 +        return -1;
    1.27 +    }
    1.28 +
    1.29 +    switch(opt->type)
    1.30 +    {
    1.31 +        case SDL_SUT_OPTIONTYPE_BOOL:
    1.32 +            if(var->bool_value)
    1.33 +            {
    1.34 +                var->bool_value = SDL_FALSE;
    1.35 +                return 1;
    1.36 +            }
    1.37 +            else
    1.38 +            {
    1.39 +                var->bool_value = SDL_TRUE;
    1.40 +                return 0;
    1.41 +            }
    1.42 +        break;
    1.43 +
    1.44 +        case SDL_SUT_OPTIONTYPE_ENUM:
    1.45 +            var->enumerated.index++;
    1.46 +            if(!opt->data.enum_values[var->enumerated.index])
    1.47 +            {
    1.48 +                var->enumerated.index = 0;
    1.49 +                return 1;
    1.50 +            }
    1.51 +            return 0;
    1.52 +        break;
    1.53 +
    1.54 +        case SDL_SUT_OPTIONTYPE_INT:
    1.55 +        {
    1.56 +            int increment = (opt->data.range.max - opt->data.range.min) /
    1.57 +                            SDL_SUT_INTEGER_OPTION_TEST_STEPS;
    1.58 +            /* prevents infinite loop when rounding */
    1.59 +            if(increment == 0)
    1.60 +                increment = 1;
    1.61 +            var->integer.value += increment;
    1.62 +            if(var->integer.value > opt->data.range.max)
    1.63 +            {
    1.64 +                var->integer.value = opt->data.range.min;
    1.65 +                return 1;
    1.66 +            }
    1.67 +            return 0;
    1.68 +        }
    1.69 +        break;
    1.70 +
    1.71 +        case SDL_SUT_OPTIONTYPE_STRING:
    1.72 +            return 1;
    1.73 +        break;
    1.74 +    }
    1.75 +    return -1;
    1.76 +}
    1.77 +
    1.78 +int
    1.79 +SDLVisualTest_MakeStrFromVariation(SDLVisualTest_Variation* variation,
    1.80 +                                   SDLVisualTest_SUTConfig* config,
    1.81 +                                   char* buffer, int size)
    1.82 +{
    1.83 +    int i, index;
    1.84 +    SDLVisualTest_SUTOptionValue* vars;
    1.85 +    SDLVisualTest_SUTOption* options;
    1.86 +    if(!variation)
    1.87 +    {
    1.88 +        SDLTest_LogError("variation argument cannot be NULL");
    1.89 +        return 0;
    1.90 +    }
    1.91 +    if(!config)
    1.92 +    {
    1.93 +        SDLTest_LogError("config argument cannot be NULL");
    1.94 +        return 0;
    1.95 +    }
    1.96 +    if(!buffer)
    1.97 +    {
    1.98 +        SDLTest_LogError("buffer argument cannot be NULL");
    1.99 +        return 0;
   1.100 +    }
   1.101 +    if(size <= 0)
   1.102 +    {
   1.103 +        SDLTest_LogError("size argument should be positive");
   1.104 +        return 0;
   1.105 +    }
   1.106 +
   1.107 +    index = 0;
   1.108 +    buffer[0] = '\0';
   1.109 +    options = config->options;
   1.110 +    vars = variation->vars;
   1.111 +    for(i = 0; i < variation->num_vars; i++)
   1.112 +    {
   1.113 +        int n, enum_index;
   1.114 +        if(index >= size - 1)
   1.115 +        {
   1.116 +            SDLTest_LogError("String did not fit in buffer size");
   1.117 +            return 0;
   1.118 +        }
   1.119 +        switch(options[i].type)
   1.120 +        {
   1.121 +            case SDL_SUT_OPTIONTYPE_BOOL:
   1.122 +                if(vars[i].bool_value)
   1.123 +                {
   1.124 +                    n = SDL_snprintf(buffer + index, size - index, "%s ",
   1.125 +                                     options[i].name);
   1.126 +                    if(n <= 0)
   1.127 +                    {
   1.128 +                        SDLTest_LogError("SDL_snprintf() failed");
   1.129 +                        return 0;
   1.130 +                    }
   1.131 +                    index += n;
   1.132 +                }
   1.133 +            break;
   1.134 +
   1.135 +            case SDL_SUT_OPTIONTYPE_ENUM:
   1.136 +                if(vars[i].enumerated.on)
   1.137 +                {
   1.138 +                    enum_index = vars[i].enumerated.index;
   1.139 +                    n = SDL_snprintf(buffer + index, size - index, "%s %s ",
   1.140 +                        options[i].name, options[i].data.enum_values[enum_index]);
   1.141 +                    index += n;
   1.142 +                }
   1.143 +            break;
   1.144 +
   1.145 +            case SDL_SUT_OPTIONTYPE_INT:
   1.146 +                if(vars[i].integer.on)
   1.147 +                {
   1.148 +                    n = SDL_snprintf(buffer + index, size - index, "%s %d ",
   1.149 +                                     options[i].name, vars[i].integer.value);
   1.150 +                    index += n;
   1.151 +                }
   1.152 +            break;
   1.153 +
   1.154 +            case SDL_SUT_OPTIONTYPE_STRING:
   1.155 +                if(vars[i].string.on)
   1.156 +                {
   1.157 +                    n = SDL_snprintf(buffer + index, size - index, "%s %s ",
   1.158 +                                     options[i].name, vars[i].string.value);
   1.159 +                    index += n;
   1.160 +                }
   1.161 +            break;
   1.162 +        }
   1.163 +    }
   1.164 +    return 1;
   1.165 +}
   1.166 +
   1.167 +int
   1.168 +SDLVisualTest_InitVariation(SDLVisualTest_Variation* variation,
   1.169 +                            SDLVisualTest_SUTConfig* config)
   1.170 +{
   1.171 +    int i;
   1.172 +    SDLVisualTest_SUTOptionValue* vars;
   1.173 +    SDLVisualTest_SUTOption* options;
   1.174 +    if(!variation)
   1.175 +    {
   1.176 +        SDLTest_LogError("variation argument cannot be NULL");
   1.177 +        return 0;
   1.178 +    }
   1.179 +    if(!config)
   1.180 +    {
   1.181 +        SDLTest_LogError("config argument cannot be NULL");
   1.182 +        return 0;
   1.183 +    }
   1.184 +
   1.185 +    /* initialize the first variation */
   1.186 +    if(config->num_options <= 0)
   1.187 +    {
   1.188 +        SDLTest_LogError("config->num_options must be positive");
   1.189 +        return 0;
   1.190 +    }
   1.191 +    variation->vars = (SDLVisualTest_SUTOptionValue*)SDL_malloc(config->num_options *
   1.192 +                     sizeof(SDLVisualTest_SUTOptionValue));
   1.193 +    if(!variation->vars)
   1.194 +    {
   1.195 +        SDLTest_LogError("malloc() failed");
   1.196 +        return 0;
   1.197 +    }
   1.198 +    variation->num_vars = config->num_options;
   1.199 +    vars = variation->vars;
   1.200 +    options = config->options;
   1.201 +    for(i = 0; i < variation->num_vars; i++)
   1.202 +    {
   1.203 +        switch(options[i].type)
   1.204 +        {
   1.205 +            case SDL_SUT_OPTIONTYPE_BOOL:
   1.206 +                vars[i].bool_value = SDL_FALSE;
   1.207 +            break;
   1.208 +
   1.209 +            case SDL_SUT_OPTIONTYPE_ENUM:
   1.210 +                vars[i].enumerated.on = SDL_TRUE;
   1.211 +                vars[i].enumerated.index = 0;
   1.212 +            break;
   1.213 +
   1.214 +            case SDL_SUT_OPTIONTYPE_INT:
   1.215 +            {
   1.216 +                vars[i].integer.on = SDL_TRUE;
   1.217 +                vars[i].integer.value = options[i].data.range.min;
   1.218 +            }
   1.219 +            break;
   1.220 +
   1.221 +            case SDL_SUT_OPTIONTYPE_STRING:
   1.222 +                vars[i].string.on = SDL_TRUE;
   1.223 +                vars[i].string.value = options[i].name;
   1.224 +            break;
   1.225 +        }
   1.226 +    }
   1.227 +    return 1;
   1.228 +}
   1.229 \ No newline at end of file