🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.0 KiB

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