visualtest/src/variator_random.c
changeset 7924 fcb86d323770
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/visualtest/src/variator_random.c	Sun Nov 10 00:32:23 2013 -0500
     1.3 @@ -0,0 +1,111 @@
     1.4 +/* See COPYING.txt for the full license governing this code. */
     1.5 +/**
     1.6 + * \file variator_random.c
     1.7 + *
     1.8 + * Source file for the variator that tests the SUT with random variations to the
     1.9 + * input parameters.
    1.10 + */
    1.11 +
    1.12 +#include <time.h>
    1.13 +#include <SDL_test.h>
    1.14 +#include "SDL_visualtest_random_variator.h"
    1.15 +
    1.16 +int
    1.17 +SDLVisualTest_InitRandomVariator(SDLVisualTest_RandomVariator* variator,
    1.18 +                                 SDLVisualTest_SUTConfig* config, Uint64 seed)
    1.19 +{
    1.20 +    if(!variator)
    1.21 +    {
    1.22 +        SDLTest_LogError("variator argument cannot be NULL");
    1.23 +        return 0;
    1.24 +    }
    1.25 +    if(!config)
    1.26 +    {
    1.27 +        SDLTest_LogError("config argument cannot be NULL");
    1.28 +        return 0;
    1.29 +    }
    1.30 +
    1.31 +    if(seed)
    1.32 +        SDLTest_FuzzerInit(seed);
    1.33 +    else
    1.34 +        SDLTest_FuzzerInit(time(NULL));
    1.35 +
    1.36 +    variator->config = *config;
    1.37 +
    1.38 +    if(!SDLVisualTest_InitVariation(&variator->variation, &variator->config))
    1.39 +    {
    1.40 +        SDLTest_LogError("SDLVisualTest_InitVariation() failed");
    1.41 +        return 0;
    1.42 +    }
    1.43 +
    1.44 +    return 1;
    1.45 +}
    1.46 +
    1.47 +char*
    1.48 +SDLVisualTest_GetNextRandomVariation(SDLVisualTest_RandomVariator* variator)
    1.49 +{
    1.50 +    SDLVisualTest_SUTOptionValue* vars;
    1.51 +    SDLVisualTest_SUTOption* options;
    1.52 +    int i;
    1.53 +    if(!variator)
    1.54 +    {
    1.55 +        SDLTest_LogError("variator argument cannot be NULL");
    1.56 +        return NULL;
    1.57 +    }
    1.58 + 
    1.59 +    /* to save typing */
    1.60 +    vars = variator->variation.vars;
    1.61 +    options = variator->config.options;
    1.62 +
    1.63 +    /* generate a random variation */
    1.64 +    for(i = 0; i < variator->variation.num_vars; i++)
    1.65 +    {
    1.66 +        switch(options[i].type)
    1.67 +        {
    1.68 +            case SDL_SUT_OPTIONTYPE_BOOL:
    1.69 +                vars[i].bool_value = SDLTest_RandomIntegerInRange(0, 1) ? SDL_FALSE :
    1.70 +                                                                          SDL_TRUE;
    1.71 +            break;
    1.72 +
    1.73 +            case SDL_SUT_OPTIONTYPE_ENUM:
    1.74 +            {
    1.75 +                int emx = 0;
    1.76 +                while(options[i].data.enum_values[emx])
    1.77 +                    emx++;
    1.78 +                vars[i].enumerated.index = SDLTest_RandomIntegerInRange(0, emx - 1);
    1.79 +            }
    1.80 +            break;
    1.81 +            
    1.82 +            case SDL_SUT_OPTIONTYPE_INT:
    1.83 +                vars[i].integer.value = SDLTest_RandomIntegerInRange(
    1.84 +                                        options[i].data.range.min,
    1.85 +                                        options[i].data.range.max);
    1.86 +            break;
    1.87 +
    1.88 +            case SDL_SUT_OPTIONTYPE_STRING:
    1.89 +                // String values are left unchanged
    1.90 +            break;
    1.91 +        }
    1.92 +    }
    1.93 +
    1.94 +    /* convert variation to an arguments string */
    1.95 +    if(!SDLVisualTest_MakeStrFromVariation(&variator->variation, &variator->config,
    1.96 +                                           variator->buffer, MAX_SUT_ARGS_LEN))
    1.97 +    {
    1.98 +        SDLTest_LogError("SDLVisualTest_MakeStrFromVariation() failed");
    1.99 +        return NULL;
   1.100 +    }
   1.101 +
   1.102 +    return variator->buffer;
   1.103 +}
   1.104 +
   1.105 +void SDLVisualTest_FreeRandomVariator(SDLVisualTest_RandomVariator* variator)
   1.106 +{
   1.107 +    if(!variator)
   1.108 +    {
   1.109 +        SDLTest_LogError("variator argument cannot be NULL");
   1.110 +        return;
   1.111 +    }
   1.112 +    SDL_free(variator->variation.vars);
   1.113 +    variator->variation.vars = NULL;
   1.114 +}
   1.115 \ No newline at end of file