2
0

testautomation_properties.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**
  2. * Properties test suite
  3. */
  4. #include <SDL3/SDL.h>
  5. #include <SDL3/SDL_test.h>
  6. #include "testautomation_suites.h"
  7. /* Test case functions */
  8. /**
  9. * Test basic functionality
  10. */
  11. static void SDLCALL count_properties(void *userdata, SDL_PropertiesID props, const char *name)
  12. {
  13. int *count = (int *)userdata;
  14. ++(*count);
  15. }
  16. static void SDLCALL count_foo_properties(void *userdata, SDL_PropertiesID props, const char *name)
  17. {
  18. int *count = (int *)userdata;
  19. if (SDL_strcmp(name, "foo") == 0) {
  20. ++(*count);
  21. }
  22. }
  23. static int properties_testBasic(void *arg)
  24. {
  25. SDL_PropertiesID props;
  26. char key[2], expected_value[2];
  27. SDL_PropertyType type;
  28. void *value;
  29. const char *value_string;
  30. Sint64 value_number;
  31. float value_float;
  32. SDL_bool value_bool;
  33. int i, result, count;
  34. props = SDL_CreateProperties();
  35. SDLTest_AssertPass("Call to SDL_CreateProperties()");
  36. SDLTest_AssertCheck(props != 0,
  37. "Verify props were created, got: %" SDL_PRIu32, props);
  38. for (i = 0; i < 10; ++i) {
  39. SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
  40. SDL_snprintf(expected_value, SDL_arraysize(expected_value), "%c", 'a' + i);
  41. result = SDL_SetProperty(props, key, expected_value);
  42. SDLTest_AssertPass("Call to SDL_SetProperty()");
  43. SDLTest_AssertCheck(result == 0,
  44. "Verify property value was set, got: %d", result);
  45. value = SDL_GetProperty(props, key, NULL);
  46. SDLTest_AssertPass("Call to SDL_GetProperty()");
  47. SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, expected_value) == 0,
  48. "Verify property value was set, got %s, expected %s", value ? (const char *)value : "NULL", expected_value);
  49. }
  50. count = 0;
  51. SDL_EnumerateProperties(props, count_properties, &count);
  52. SDLTest_AssertCheck(count == 10,
  53. "Verify property count, expected 10, got: %d", count);
  54. for (i = 0; i < 10; ++i) {
  55. SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
  56. result = SDL_SetProperty(props, key, NULL);
  57. SDLTest_AssertPass("Call to SDL_SetProperty(NULL)");
  58. SDLTest_AssertCheck(result == 0,
  59. "Verify property value was set, got: %d", result);
  60. value = SDL_GetProperty(props, key, NULL);
  61. SDLTest_AssertPass("Call to SDL_GetProperty()");
  62. SDLTest_AssertCheck(value == NULL,
  63. "Verify property value was set, got %s, expected NULL", (const char *)value);
  64. }
  65. count = 0;
  66. SDL_EnumerateProperties(props, count_properties, &count);
  67. SDLTest_AssertCheck(count == 0,
  68. "Verify property count, expected 0, got: %d", count);
  69. /* Check default values */
  70. value = SDL_GetProperty(props, "foo", (void *)0xabcd);
  71. SDLTest_AssertCheck(value == (void *)0xabcd,
  72. "Verify property, expected 0xabcd, got: %p", value);
  73. value_string = SDL_GetStringProperty(props, "foo", "abcd");
  74. SDLTest_AssertCheck(value_string && SDL_strcmp(value_string, "abcd") == 0,
  75. "Verify string property, expected abcd, got: %s", value_string);
  76. value_number = SDL_GetNumberProperty(props, "foo", 1234);
  77. SDLTest_AssertCheck(value_number == 1234,
  78. "Verify number property, expected 1234, got: %" SDL_PRIu64, value_number);
  79. value_float = SDL_GetFloatProperty(props, "foo", 1234.0f);
  80. SDLTest_AssertCheck(value_float == 1234.0f,
  81. "Verify float property, expected 1234, got: %f", value_float);
  82. value_bool = SDL_GetBooleanProperty(props, "foo", SDL_TRUE);
  83. SDLTest_AssertCheck(value_bool == SDL_TRUE,
  84. "Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
  85. /* Check data value */
  86. SDLTest_AssertPass("Call to SDL_SetProperty(\"foo\", 0x01)");
  87. SDL_SetProperty(props, "foo", (void *)0x01);
  88. type = SDL_GetPropertyType(props, "foo");
  89. SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_POINTER,
  90. "Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_POINTER, type);
  91. value = SDL_GetProperty(props, "foo", NULL);
  92. SDLTest_AssertCheck(value == (void *)0x01,
  93. "Verify property, expected 0x01, got: %p", value);
  94. value_string = SDL_GetStringProperty(props, "foo", NULL);
  95. SDLTest_AssertCheck(value_string == NULL,
  96. "Verify string property, expected NULL, got: %s", value_string);
  97. value_number = SDL_GetNumberProperty(props, "foo", 0);
  98. SDLTest_AssertCheck(value_number == 0,
  99. "Verify number property, expected 0, got: %" SDL_PRIu64, value_number);
  100. value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
  101. SDLTest_AssertCheck(value_float == 0.0f,
  102. "Verify float property, expected 0, got: %f", value_float);
  103. value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
  104. SDLTest_AssertCheck(value_bool == SDL_FALSE,
  105. "Verify boolean property, expected SDL_FALSE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
  106. /* Check string value */
  107. SDLTest_AssertPass("Call to SDL_SetStringProperty(\"foo\", \"bar\")");
  108. SDL_SetStringProperty(props, "foo", "bar");
  109. type = SDL_GetPropertyType(props, "foo");
  110. SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_STRING,
  111. "Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_STRING, type);
  112. value = SDL_GetProperty(props, "foo", NULL);
  113. SDLTest_AssertCheck(value == NULL,
  114. "Verify property, expected NULL, got: %p", value);
  115. value_string = SDL_GetStringProperty(props, "foo", NULL);
  116. SDLTest_AssertCheck(value_string != NULL && SDL_strcmp(value_string, "bar") == 0,
  117. "Verify string property, expected bar, got: %s", value_string);
  118. value_number = SDL_GetNumberProperty(props, "foo", 0);
  119. SDLTest_AssertCheck(value_number == 0,
  120. "Verify number property, expected 0, got: %" SDL_PRIu64, value_number);
  121. value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
  122. SDLTest_AssertCheck(value_float == 0.0f,
  123. "Verify float property, expected 0, got: %f", value_float);
  124. value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
  125. SDLTest_AssertCheck(value_bool == SDL_TRUE,
  126. "Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
  127. /* Check number value */
  128. SDLTest_AssertPass("Call to SDL_SetNumberProperty(\"foo\", 1)");
  129. SDL_SetNumberProperty(props, "foo", 1);
  130. type = SDL_GetPropertyType(props, "foo");
  131. SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_NUMBER,
  132. "Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_NUMBER, type);
  133. value = SDL_GetProperty(props, "foo", NULL);
  134. SDLTest_AssertCheck(value == NULL,
  135. "Verify property, expected NULL, got: %p", value);
  136. value_string = SDL_GetStringProperty(props, "foo", NULL);
  137. SDLTest_AssertCheck(value_string && SDL_strcmp(value_string, "1") == 0,
  138. "Verify string property, expected 1, got: %s", value_string);
  139. value_number = SDL_GetNumberProperty(props, "foo", 0);
  140. SDLTest_AssertCheck(value_number == 1,
  141. "Verify number property, expected 1, got: %" SDL_PRIu64, value_number);
  142. value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
  143. SDLTest_AssertCheck(value_float == 1.0f,
  144. "Verify float property, expected 1, got: %f", value_float);
  145. value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
  146. SDLTest_AssertCheck(value_bool == SDL_TRUE,
  147. "Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
  148. /* Check float value */
  149. SDLTest_AssertPass("Call to SDL_SetFloatProperty(\"foo\", 1)");
  150. SDL_SetFloatProperty(props, "foo", 1.75f);
  151. type = SDL_GetPropertyType(props, "foo");
  152. SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_FLOAT,
  153. "Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_FLOAT, type);
  154. value = SDL_GetProperty(props, "foo", NULL);
  155. SDLTest_AssertCheck(value == NULL,
  156. "Verify property, expected NULL, got: %p", value);
  157. value_string = SDL_GetStringProperty(props, "foo", NULL);
  158. SDLTest_AssertCheck(value_string && SDL_strcmp(value_string, "1.750000") == 0,
  159. "Verify string property, expected 1.750000, got: %s", value_string);
  160. value_number = SDL_GetNumberProperty(props, "foo", 0);
  161. SDLTest_AssertCheck(value_number == 2,
  162. "Verify number property, expected 2, got: %" SDL_PRIu64, value_number);
  163. value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
  164. SDLTest_AssertCheck(value_float == 1.75f,
  165. "Verify float property, expected 1.75, got: %f", value_float);
  166. value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
  167. SDLTest_AssertCheck(value_bool == SDL_TRUE,
  168. "Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
  169. /* Check boolean value */
  170. SDLTest_AssertPass("Call to SDL_SetBooleanProperty(\"foo\", SDL_TRUE)");
  171. SDL_SetBooleanProperty(props, "foo", 3); /* Note we're testing non-true/false value here */
  172. type = SDL_GetPropertyType(props, "foo");
  173. SDLTest_AssertCheck(type == SDL_PROPERTY_TYPE_BOOLEAN,
  174. "Verify property type, expected %d, got: %d", SDL_PROPERTY_TYPE_BOOLEAN, type);
  175. value = SDL_GetProperty(props, "foo", NULL);
  176. SDLTest_AssertCheck(value == NULL,
  177. "Verify property, expected NULL, got: %p", value);
  178. value_string = SDL_GetStringProperty(props, "foo", NULL);
  179. SDLTest_AssertCheck(value_string && SDL_strcmp(value_string, "true") == 0,
  180. "Verify string property, expected true, got: %s", value_string);
  181. value_number = SDL_GetNumberProperty(props, "foo", 0);
  182. SDLTest_AssertCheck(value_number == 1,
  183. "Verify number property, expected 1, got: %" SDL_PRIu64, value_number);
  184. value_float = SDL_GetFloatProperty(props, "foo", 0.0f);
  185. SDLTest_AssertCheck(value_float == 1.0f,
  186. "Verify float property, expected 1, got: %f", value_float);
  187. value_bool = SDL_GetBooleanProperty(props, "foo", SDL_FALSE);
  188. SDLTest_AssertCheck(value_bool == SDL_TRUE,
  189. "Verify boolean property, expected SDL_TRUE, got: %s", value_bool ? "SDL_TRUE" : "SDL_FALSE");
  190. /* Make sure we have exactly one property named foo */
  191. count = 0;
  192. SDL_EnumerateProperties(props, count_foo_properties, &count);
  193. SDLTest_AssertCheck(count == 1,
  194. "Verify foo property count, expected 1, got: %d", count);
  195. SDL_DestroyProperties(props);
  196. return TEST_COMPLETED;
  197. }
  198. /**
  199. * Test cleanup functionality
  200. */
  201. static void SDLCALL cleanup(void *userdata, void *value)
  202. {
  203. int *count = (int *)userdata;
  204. ++(*count);
  205. }
  206. static int properties_testCleanup(void *arg)
  207. {
  208. SDL_PropertiesID props;
  209. char key[2], expected_value[2];
  210. int i, count;
  211. props = SDL_CreateProperties();
  212. SDLTest_AssertPass("Call to SDL_SetProperty(cleanup)");
  213. count = 0;
  214. SDL_SetPropertyWithCleanup(props, "a", "0", cleanup, &count);
  215. SDL_SetPropertyWithCleanup(props, "a", NULL, cleanup, &count);
  216. SDLTest_AssertCheck(count == 1,
  217. "Verify cleanup for deleting property, got %d, expected 1", count);
  218. SDLTest_AssertPass("Call to SDL_DestroyProperties()");
  219. count = 0;
  220. for (i = 0; i < 10; ++i) {
  221. SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
  222. SDL_snprintf(expected_value, SDL_arraysize(expected_value), "%c", 'a' + i);
  223. SDL_SetPropertyWithCleanup(props, key, expected_value, cleanup, &count);
  224. }
  225. SDL_DestroyProperties(props);
  226. SDLTest_AssertCheck(count == 10,
  227. "Verify cleanup for destroying properties, got %d, expected 10", count);
  228. return TEST_COMPLETED;
  229. }
  230. /**
  231. * Test locking functionality
  232. */
  233. struct properties_thread_data
  234. {
  235. SDL_bool done;
  236. SDL_PropertiesID props;
  237. };
  238. static int properties_thread(void *arg)
  239. {
  240. struct properties_thread_data *data = (struct properties_thread_data *)arg;
  241. while (!data->done) {
  242. SDL_LockProperties(data->props);
  243. SDL_SetProperty(data->props, "a", "thread_loop");
  244. SDL_UnlockProperties(data->props);
  245. }
  246. SDL_LockProperties(data->props);
  247. SDL_SetProperty(data->props, "a", "thread_done");
  248. SDL_UnlockProperties(data->props);
  249. return 0;
  250. }
  251. static int properties_testLocking(void *arg)
  252. {
  253. struct properties_thread_data data;
  254. SDL_Thread *thread;
  255. void *value;
  256. SDLTest_AssertPass("Testing property locking");
  257. data.done = SDL_FALSE;
  258. data.props = SDL_CreateProperties();
  259. SDLTest_AssertPass("Setting property to 'init'");
  260. SDL_SetProperty(data.props, "a", "init");
  261. thread = SDL_CreateThread(properties_thread, "properties_thread", &data);
  262. if (thread) {
  263. SDLTest_AssertPass("Waiting for property to change to 'thread_loop'");
  264. for ( ; ; )
  265. {
  266. SDL_Delay(10);
  267. SDL_LockProperties(data.props);
  268. value = SDL_GetProperty(data.props, "a", NULL);
  269. SDL_UnlockProperties(data.props);
  270. if (!value || SDL_strcmp((const char *)value, "thread_loop") == 0) {
  271. break;
  272. }
  273. }
  274. SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "thread_loop") == 0,
  275. "After thread loop, property is %s, expected 'thread_loop'", value ? (const char *)value : "NULL");
  276. SDLTest_AssertPass("Setting property to 'main'");
  277. SDL_LockProperties(data.props);
  278. SDL_SetProperty(data.props, "a", "main");
  279. SDL_Delay(100);
  280. value = SDL_GetProperty(data.props, "a", NULL);
  281. SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "main") == 0,
  282. "After 100ms sleep, property is %s, expected 'main'", value ? (const char *)value : "NULL");
  283. SDL_UnlockProperties(data.props);
  284. data.done = SDL_TRUE;
  285. SDL_WaitThread(thread, NULL);
  286. value = SDL_GetProperty(data.props, "a", NULL);
  287. SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "thread_done") == 0,
  288. "After thread complete, property is %s, expected 'thread_done'", value ? (const char *)value : "NULL");
  289. }
  290. SDL_DestroyProperties(data.props);
  291. return TEST_COMPLETED;
  292. }
  293. /* ================= Test References ================== */
  294. /* Properties test cases */
  295. static const SDLTest_TestCaseReference propertiesTest1 = {
  296. (SDLTest_TestCaseFp)properties_testBasic, "properties_testBasic", "Test basic property functionality", TEST_ENABLED
  297. };
  298. static const SDLTest_TestCaseReference propertiesTest2 = {
  299. (SDLTest_TestCaseFp)properties_testCleanup, "properties_testCleanup", "Test property cleanup functionality", TEST_ENABLED
  300. };
  301. static const SDLTest_TestCaseReference propertiesTest3 = {
  302. (SDLTest_TestCaseFp)properties_testLocking, "properties_testLocking", "Test property locking functionality", TEST_ENABLED
  303. };
  304. /* Sequence of Properties test cases */
  305. static const SDLTest_TestCaseReference *propertiesTests[] = {
  306. &propertiesTest1, &propertiesTest2, &propertiesTest3, NULL
  307. };
  308. /* Properties test suite (global) */
  309. SDLTest_TestSuiteReference propertiesTestSuite = {
  310. "Properties",
  311. NULL,
  312. propertiesTests,
  313. NULL
  314. };