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

224 lines
6.0 KiB

  1. /* See COPYING.txt for the full license governing this code. */
  2. /**
  3. * \file variator_common.c
  4. *
  5. * Source file for some common functionality used by variators.
  6. */
  7. #include <SDL_test.h>
  8. #include "SDL_visualtest_variator_common.h"
  9. int
  10. SDLVisualTest_NextValue(SDLVisualTest_SUTOptionValue* var,
  11. SDLVisualTest_SUTOption* opt)
  12. {
  13. if(!var)
  14. {
  15. SDLTest_LogError("var argument cannot be NULL");
  16. return -1;
  17. }
  18. if(!opt)
  19. {
  20. SDLTest_LogError("opt argument cannot be NULL");
  21. return -1;
  22. }
  23. switch(opt->type)
  24. {
  25. case SDL_SUT_OPTIONTYPE_BOOL:
  26. if(var->bool_value)
  27. {
  28. var->bool_value = SDL_FALSE;
  29. return 1;
  30. }
  31. else
  32. {
  33. var->bool_value = SDL_TRUE;
  34. return 0;
  35. }
  36. break;
  37. case SDL_SUT_OPTIONTYPE_ENUM:
  38. var->enumerated.index++;
  39. if(!opt->data.enum_values[var->enumerated.index])
  40. {
  41. var->enumerated.index = 0;
  42. return 1;
  43. }
  44. return 0;
  45. break;
  46. case SDL_SUT_OPTIONTYPE_INT:
  47. {
  48. int increment = (opt->data.range.max - opt->data.range.min) /
  49. SDL_SUT_INTEGER_OPTION_TEST_STEPS;
  50. /* prevents infinite loop when rounding */
  51. if(increment == 0)
  52. increment = 1;
  53. var->integer.value += increment;
  54. if(var->integer.value > opt->data.range.max)
  55. {
  56. var->integer.value = opt->data.range.min;
  57. return 1;
  58. }
  59. return 0;
  60. }
  61. break;
  62. case SDL_SUT_OPTIONTYPE_STRING:
  63. return 1;
  64. break;
  65. }
  66. return -1;
  67. }
  68. int
  69. SDLVisualTest_MakeStrFromVariation(SDLVisualTest_Variation* variation,
  70. SDLVisualTest_SUTConfig* config,
  71. char* buffer, int size)
  72. {
  73. int i, index;
  74. SDLVisualTest_SUTOptionValue* vars;
  75. SDLVisualTest_SUTOption* options;
  76. if(!variation)
  77. {
  78. SDLTest_LogError("variation argument cannot be NULL");
  79. return 0;
  80. }
  81. if(!config)
  82. {
  83. SDLTest_LogError("config argument cannot be NULL");
  84. return 0;
  85. }
  86. if(!buffer)
  87. {
  88. SDLTest_LogError("buffer argument cannot be NULL");
  89. return 0;
  90. }
  91. if(size <= 0)
  92. {
  93. SDLTest_LogError("size argument should be positive");
  94. return 0;
  95. }
  96. index = 0;
  97. buffer[0] = '\0';
  98. options = config->options;
  99. vars = variation->vars;
  100. for(i = 0; i < variation->num_vars; i++)
  101. {
  102. int n, enum_index;
  103. if(index >= size - 1)
  104. {
  105. SDLTest_LogError("String did not fit in buffer size");
  106. return 0;
  107. }
  108. switch(options[i].type)
  109. {
  110. case SDL_SUT_OPTIONTYPE_BOOL:
  111. if(vars[i].bool_value)
  112. {
  113. n = SDL_snprintf(buffer + index, size - index, "%s ",
  114. options[i].name);
  115. if(n <= 0)
  116. {
  117. SDLTest_LogError("SDL_snprintf() failed");
  118. return 0;
  119. }
  120. index += n;
  121. }
  122. break;
  123. case SDL_SUT_OPTIONTYPE_ENUM:
  124. if(vars[i].enumerated.on)
  125. {
  126. enum_index = vars[i].enumerated.index;
  127. n = SDL_snprintf(buffer + index, size - index, "%s %s ",
  128. options[i].name, options[i].data.enum_values[enum_index]);
  129. index += n;
  130. }
  131. break;
  132. case SDL_SUT_OPTIONTYPE_INT:
  133. if(vars[i].integer.on)
  134. {
  135. n = SDL_snprintf(buffer + index, size - index, "%s %d ",
  136. options[i].name, vars[i].integer.value);
  137. index += n;
  138. }
  139. break;
  140. case SDL_SUT_OPTIONTYPE_STRING:
  141. if(vars[i].string.on)
  142. {
  143. n = SDL_snprintf(buffer + index, size - index, "%s %s ",
  144. options[i].name, vars[i].string.value);
  145. index += n;
  146. }
  147. break;
  148. }
  149. }
  150. return 1;
  151. }
  152. int
  153. SDLVisualTest_InitVariation(SDLVisualTest_Variation* variation,
  154. SDLVisualTest_SUTConfig* config)
  155. {
  156. int i;
  157. SDLVisualTest_SUTOptionValue* vars;
  158. SDLVisualTest_SUTOption* options;
  159. if(!variation)
  160. {
  161. SDLTest_LogError("variation argument cannot be NULL");
  162. return 0;
  163. }
  164. if(!config)
  165. {
  166. SDLTest_LogError("config argument cannot be NULL");
  167. return 0;
  168. }
  169. /* initialize the first variation */
  170. if(config->num_options <= 0)
  171. {
  172. SDLTest_LogError("config->num_options must be positive");
  173. return 0;
  174. }
  175. variation->vars = (SDLVisualTest_SUTOptionValue*)SDL_malloc(config->num_options *
  176. sizeof(SDLVisualTest_SUTOptionValue));
  177. if(!variation->vars)
  178. {
  179. SDLTest_LogError("malloc() failed");
  180. return 0;
  181. }
  182. variation->num_vars = config->num_options;
  183. vars = variation->vars;
  184. options = config->options;
  185. for(i = 0; i < variation->num_vars; i++)
  186. {
  187. switch(options[i].type)
  188. {
  189. case SDL_SUT_OPTIONTYPE_BOOL:
  190. vars[i].bool_value = SDL_FALSE;
  191. break;
  192. case SDL_SUT_OPTIONTYPE_ENUM:
  193. vars[i].enumerated.on = SDL_TRUE;
  194. vars[i].enumerated.index = 0;
  195. break;
  196. case SDL_SUT_OPTIONTYPE_INT:
  197. {
  198. vars[i].integer.on = SDL_TRUE;
  199. vars[i].integer.value = options[i].data.range.min;
  200. }
  201. break;
  202. case SDL_SUT_OPTIONTYPE_STRING:
  203. vars[i].string.on = SDL_TRUE;
  204. vars[i].string.value = options[i].name;
  205. break;
  206. }
  207. }
  208. return 1;
  209. }