visualtest/src/variator_exhaustive.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Jun 2018 17:07:05 -0700
changeset 12013 270e17c85d11
parent 7924 fcb86d323770
permissions -rw-r--r--
Don't crash on exit from SDLActivity if we don't have a singleton for some reason. (Thanks Rachel!)
icculus@7924
     1
/* See COPYING.txt for the full license governing this code. */
icculus@7924
     2
/**
icculus@7924
     3
 * \file variator_exhaustive.c
icculus@7924
     4
 *
icculus@7924
     5
 * Source file for the variator that tests the SUT with all the different
icculus@7924
     6
 * variations of input parameters that are valid.
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_sut_configparser.h"
icculus@7924
    12
#include "SDL_visualtest_exhaustive_variator.h"
icculus@7924
    13
icculus@7924
    14
static int
icculus@7924
    15
NextVariation(SDLVisualTest_Variation* variation,
icculus@7924
    16
              SDLVisualTest_SUTConfig* config)
icculus@7924
    17
{
icculus@7924
    18
    int i, carry;
icculus@7924
    19
    if(!variation)
icculus@7924
    20
    {
icculus@7924
    21
        SDLTest_LogError("variation argument cannot be NULL");
icculus@7924
    22
        return -1;
icculus@7924
    23
    }
icculus@7924
    24
    if(!config)
icculus@7924
    25
    {
icculus@7924
    26
        SDLTest_LogError("config argument cannot be NULL");
icculus@7924
    27
        return -1;
icculus@7924
    28
    }
icculus@7924
    29
icculus@7924
    30
    carry = 1;
icculus@7924
    31
    for(i = 0; i < variation->num_vars; i++)
icculus@7924
    32
    {
icculus@7924
    33
        carry = SDLVisualTest_NextValue(&variation->vars[i], &config->options[i]);
icculus@7924
    34
        if(carry != 1)
icculus@7924
    35
            break;
icculus@7924
    36
    }
icculus@7924
    37
icculus@7924
    38
    if(carry == 1) /* we're done, we've tried all possible variations */
icculus@7924
    39
        return 0;
icculus@7924
    40
    if(carry == 0)
icculus@7924
    41
        return 1;
icculus@7924
    42
    SDLTest_LogError("NextVariation() failed");
icculus@7924
    43
    return -1;
icculus@7924
    44
}
icculus@7924
    45
icculus@7924
    46
int
icculus@7924
    47
SDLVisualTest_InitExhaustiveVariator(SDLVisualTest_ExhaustiveVariator* variator,
icculus@7924
    48
                                     SDLVisualTest_SUTConfig* config)
icculus@7924
    49
{
icculus@7924
    50
    if(!variator)
icculus@7924
    51
    {
icculus@7924
    52
        SDLTest_LogError("variator argument cannot be NULL");
icculus@7924
    53
        return 0;
icculus@7924
    54
    }
icculus@7924
    55
    if(!config)
icculus@7924
    56
    {
icculus@7924
    57
        SDLTest_LogError("config argument cannot be NULL");
icculus@7924
    58
        return 0;
icculus@7924
    59
    }
icculus@7924
    60
icculus@7924
    61
    SDLTest_FuzzerInit(time(NULL));
icculus@7924
    62
icculus@7924
    63
    variator->config = *config;
icculus@7924
    64
    variator->variation.num_vars = 0;
icculus@7924
    65
    variator->variation.vars = NULL;
icculus@7924
    66
icculus@7924
    67
    return 1;
icculus@7924
    68
}
icculus@7924
    69
icculus@7924
    70
/* TODO: Right now variations where an option is not specified at all are not
icculus@7924
    71
   tested for. This can be implemented by switching the on attribute for integer,
icculus@7924
    72
   enum and string options to true and false. */
icculus@7924
    73
char*
icculus@7924
    74
SDLVisualTest_GetNextExhaustiveVariation(SDLVisualTest_ExhaustiveVariator* variator)
icculus@7924
    75
{
icculus@7924
    76
    int success;
icculus@7924
    77
    if(!variator)
icculus@7924
    78
    {
icculus@7924
    79
        SDLTest_LogError("variator argument cannot be NULL");
icculus@7924
    80
        return NULL;
icculus@7924
    81
    }
icculus@7924
    82
icculus@7924
    83
    if(!variator->variation.vars) /* the first time this function is called */
icculus@7924
    84
    {
icculus@7924
    85
        success = SDLVisualTest_InitVariation(&variator->variation,
icculus@7924
    86
                                              &variator->config);
icculus@7924
    87
        if(!success)
icculus@7924
    88
        {
icculus@7924
    89
            SDLTest_LogError("SDLVisualTest_InitVariation() failed");
icculus@7924
    90
            return NULL;
icculus@7924
    91
        }
icculus@7924
    92
        success = SDLVisualTest_MakeStrFromVariation(&variator->variation,
icculus@7924
    93
                  &variator->config, variator->buffer, MAX_SUT_ARGS_LEN);
icculus@7924
    94
        if(!success)
icculus@7924
    95
        {
icculus@7924
    96
            SDLTest_LogError("SDLVisualTest_MakeStrFromVariation() failed");
icculus@7924
    97
            return NULL;
icculus@7924
    98
        }
icculus@7924
    99
        return variator->buffer;
icculus@7924
   100
    }
icculus@7924
   101
    else
icculus@7924
   102
    {
icculus@7924
   103
        success = NextVariation(&variator->variation, &variator->config);
icculus@7924
   104
        if(success == 1)
icculus@7924
   105
        {
icculus@7924
   106
            success = SDLVisualTest_MakeStrFromVariation(&variator->variation,
icculus@7924
   107
                      &variator->config, variator->buffer, MAX_SUT_ARGS_LEN);
icculus@7924
   108
            if(!success)
icculus@7924
   109
            {
icculus@7924
   110
                SDLTest_LogError("SDLVisualTest_MakeStrFromVariation() failed");
icculus@7924
   111
                return NULL;
icculus@7924
   112
            }
icculus@7924
   113
            return variator->buffer;
icculus@7924
   114
        }
icculus@7924
   115
        else if(success == -1)
icculus@7924
   116
            SDLTest_LogError("NextVariation() failed.");
icculus@7924
   117
        return NULL;
icculus@7924
   118
    }
icculus@7924
   119
    return NULL;
icculus@7924
   120
}
icculus@7924
   121
icculus@7924
   122
void
icculus@7924
   123
SDLVisualTest_FreeExhaustiveVariator(SDLVisualTest_ExhaustiveVariator* variator)
icculus@7924
   124
{
icculus@7924
   125
    if(!variator)
icculus@7924
   126
    {
icculus@7924
   127
        SDLTest_LogError("variator argument cannot be NULL");
icculus@7924
   128
        return;
icculus@7924
   129
    }
icculus@7924
   130
    SDL_free(variator->variation.vars);
icculus@7924
   131
    variator->variation.vars = NULL;
icculus@7924
   132
}