🛠️🐜 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.

133 lines
3.6 KiB

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