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