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