visualtest/src/variator_common.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 21 Oct 2018 22:40:17 -0400
changeset 12345 50e1cca28b39
parent 7924 fcb86d323770
permissions -rw-r--r--
wasapi/win32: Sort initial device lists by device GUID.

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