visualtest/src/variator_random.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 10 Nov 2013 00:32:23 -0500
changeset 7924 fcb86d323770
permissions -rwxr-xr-x
Added Apoorv Upreti's GSoC2013 work: visual test suite.

See visualtest/README.txt for details.
icculus@7924
     1
/* See COPYING.txt for the full license governing this code. */
icculus@7924
     2
/**
icculus@7924
     3
 * \file variator_random.c
icculus@7924
     4
 *
icculus@7924
     5
 * Source file for the variator that tests the SUT with random variations to the
icculus@7924
     6
 * input parameters.
icculus@7924
     7
 */
icculus@7924
     8
icculus@7924
     9
#include <time.h>
icculus@7924
    10
#include <SDL_test.h>
icculus@7924
    11
#include "SDL_visualtest_random_variator.h"
icculus@7924
    12
icculus@7924
    13
int
icculus@7924
    14
SDLVisualTest_InitRandomVariator(SDLVisualTest_RandomVariator* variator,
icculus@7924
    15
                                 SDLVisualTest_SUTConfig* config, Uint64 seed)
icculus@7924
    16
{
icculus@7924
    17
    if(!variator)
icculus@7924
    18
    {
icculus@7924
    19
        SDLTest_LogError("variator argument cannot be NULL");
icculus@7924
    20
        return 0;
icculus@7924
    21
    }
icculus@7924
    22
    if(!config)
icculus@7924
    23
    {
icculus@7924
    24
        SDLTest_LogError("config argument cannot be NULL");
icculus@7924
    25
        return 0;
icculus@7924
    26
    }
icculus@7924
    27
icculus@7924
    28
    if(seed)
icculus@7924
    29
        SDLTest_FuzzerInit(seed);
icculus@7924
    30
    else
icculus@7924
    31
        SDLTest_FuzzerInit(time(NULL));
icculus@7924
    32
icculus@7924
    33
    variator->config = *config;
icculus@7924
    34
icculus@7924
    35
    if(!SDLVisualTest_InitVariation(&variator->variation, &variator->config))
icculus@7924
    36
    {
icculus@7924
    37
        SDLTest_LogError("SDLVisualTest_InitVariation() failed");
icculus@7924
    38
        return 0;
icculus@7924
    39
    }
icculus@7924
    40
icculus@7924
    41
    return 1;
icculus@7924
    42
}
icculus@7924
    43
icculus@7924
    44
char*
icculus@7924
    45
SDLVisualTest_GetNextRandomVariation(SDLVisualTest_RandomVariator* variator)
icculus@7924
    46
{
icculus@7924
    47
    SDLVisualTest_SUTOptionValue* vars;
icculus@7924
    48
    SDLVisualTest_SUTOption* options;
icculus@7924
    49
    int i;
icculus@7924
    50
    if(!variator)
icculus@7924
    51
    {
icculus@7924
    52
        SDLTest_LogError("variator argument cannot be NULL");
icculus@7924
    53
        return NULL;
icculus@7924
    54
    }
icculus@7924
    55
 
icculus@7924
    56
    /* to save typing */
icculus@7924
    57
    vars = variator->variation.vars;
icculus@7924
    58
    options = variator->config.options;
icculus@7924
    59
icculus@7924
    60
    /* generate a random variation */
icculus@7924
    61
    for(i = 0; i < variator->variation.num_vars; i++)
icculus@7924
    62
    {
icculus@7924
    63
        switch(options[i].type)
icculus@7924
    64
        {
icculus@7924
    65
            case SDL_SUT_OPTIONTYPE_BOOL:
icculus@7924
    66
                vars[i].bool_value = SDLTest_RandomIntegerInRange(0, 1) ? SDL_FALSE :
icculus@7924
    67
                                                                          SDL_TRUE;
icculus@7924
    68
            break;
icculus@7924
    69
icculus@7924
    70
            case SDL_SUT_OPTIONTYPE_ENUM:
icculus@7924
    71
            {
icculus@7924
    72
                int emx = 0;
icculus@7924
    73
                while(options[i].data.enum_values[emx])
icculus@7924
    74
                    emx++;
icculus@7924
    75
                vars[i].enumerated.index = SDLTest_RandomIntegerInRange(0, emx - 1);
icculus@7924
    76
            }
icculus@7924
    77
            break;
icculus@7924
    78
            
icculus@7924
    79
            case SDL_SUT_OPTIONTYPE_INT:
icculus@7924
    80
                vars[i].integer.value = SDLTest_RandomIntegerInRange(
icculus@7924
    81
                                        options[i].data.range.min,
icculus@7924
    82
                                        options[i].data.range.max);
icculus@7924
    83
            break;
icculus@7924
    84
icculus@7924
    85
            case SDL_SUT_OPTIONTYPE_STRING:
icculus@7924
    86
                // String values are left unchanged
icculus@7924
    87
            break;
icculus@7924
    88
        }
icculus@7924
    89
    }
icculus@7924
    90
icculus@7924
    91
    /* convert variation to an arguments string */
icculus@7924
    92
    if(!SDLVisualTest_MakeStrFromVariation(&variator->variation, &variator->config,
icculus@7924
    93
                                           variator->buffer, MAX_SUT_ARGS_LEN))
icculus@7924
    94
    {
icculus@7924
    95
        SDLTest_LogError("SDLVisualTest_MakeStrFromVariation() failed");
icculus@7924
    96
        return NULL;
icculus@7924
    97
    }
icculus@7924
    98
icculus@7924
    99
    return variator->buffer;
icculus@7924
   100
}
icculus@7924
   101
icculus@7924
   102
void SDLVisualTest_FreeRandomVariator(SDLVisualTest_RandomVariator* variator)
icculus@7924
   103
{
icculus@7924
   104
    if(!variator)
icculus@7924
   105
    {
icculus@7924
   106
        SDLTest_LogError("variator argument cannot be NULL");
icculus@7924
   107
        return;
icculus@7924
   108
    }
icculus@7924
   109
    SDL_free(variator->variation.vars);
icculus@7924
   110
    variator->variation.vars = NULL;
icculus@7924
   111
}