testautomation_properties.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 int properties_testBasic(void *arg)
  12. {
  13. SDL_PropertiesID props;
  14. char key[2], expected_value[2];
  15. void *value;
  16. int i, result;
  17. props = SDL_CreateProperties();
  18. SDLTest_AssertPass("Call to SDL_CreateProperties()");
  19. SDLTest_AssertCheck(props != 0,
  20. "Verify props were created, got: %" SDL_PRIu32 "", props);
  21. for (i = 0; i < 10; ++i) {
  22. SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
  23. SDL_snprintf(expected_value, SDL_arraysize(expected_value), "%c", 'a' + i);
  24. result = SDL_SetProperty(props, key, expected_value, NULL, NULL);
  25. SDLTest_AssertPass("Call to SDL_SetProperty()");
  26. SDLTest_AssertCheck(result == 0,
  27. "Verify property value was set, got: %d", result);
  28. value = SDL_GetProperty(props, key);
  29. SDLTest_AssertPass("Call to SDL_GetProperty()");
  30. SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, expected_value) == 0,
  31. "Verify property value was set, got %s, expected %s", value ? (const char *)value : "NULL", expected_value);
  32. }
  33. for (i = 0; i < 10; ++i) {
  34. SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
  35. result = SDL_SetProperty(props, key, NULL, NULL, NULL);
  36. SDLTest_AssertPass("Call to SDL_SetProperty(NULL)");
  37. SDLTest_AssertCheck(result == 0,
  38. "Verify property value was set, got: %d", result);
  39. value = SDL_GetProperty(props, key);
  40. SDLTest_AssertPass("Call to SDL_GetProperty()");
  41. SDLTest_AssertCheck(value == NULL,
  42. "Verify property value was set, got %s, expected NULL", (const char *)value);
  43. }
  44. SDL_DestroyProperties(props);
  45. return TEST_COMPLETED;
  46. }
  47. /**
  48. * Test cleanup functionality
  49. */
  50. static void SDLCALL cleanup(void *userdata, void *value)
  51. {
  52. int *count = (int *)userdata;
  53. ++(*count);
  54. }
  55. static int properties_testCleanup(void *arg)
  56. {
  57. SDL_PropertiesID props;
  58. char key[2], expected_value[2];
  59. int i, count;
  60. props = SDL_CreateProperties();
  61. SDLTest_AssertPass("Call to SDL_SetProperty(cleanup)");
  62. count = 0;
  63. SDL_SetProperty(props, "a", "0", cleanup, &count);
  64. SDL_SetProperty(props, "a", NULL, cleanup, &count);
  65. SDLTest_AssertCheck(count == 1,
  66. "Verify cleanup for deleting property, got %d, expected 1", count);
  67. SDLTest_AssertPass("Call to SDL_DestroyProperties()");
  68. count = 0;
  69. for (i = 0; i < 10; ++i) {
  70. SDL_snprintf(key, SDL_arraysize(key), "%c", 'a' + i);
  71. SDL_snprintf(expected_value, SDL_arraysize(expected_value), "%c", 'a' + i);
  72. SDL_SetProperty(props, key, expected_value, cleanup, &count);
  73. }
  74. SDL_DestroyProperties(props);
  75. SDLTest_AssertCheck(count == 10,
  76. "Verify cleanup for destroying properties, got %d, expected 10", count);
  77. return TEST_COMPLETED;
  78. }
  79. /**
  80. * Test locking functionality
  81. */
  82. struct properties_thread_data
  83. {
  84. SDL_bool done;
  85. SDL_PropertiesID props;
  86. };
  87. static int properties_thread(void *arg)
  88. {
  89. struct properties_thread_data *data = (struct properties_thread_data *)arg;
  90. while (!data->done) {
  91. SDL_LockProperties(data->props);
  92. SDL_SetProperty(data->props, "a", "thread_loop", NULL, NULL);
  93. SDL_UnlockProperties(data->props);
  94. }
  95. SDL_LockProperties(data->props);
  96. SDL_SetProperty(data->props, "a", "thread_done", NULL, NULL);
  97. SDL_UnlockProperties(data->props);
  98. return 0;
  99. }
  100. static int properties_testLocking(void *arg)
  101. {
  102. struct properties_thread_data data;
  103. SDL_Thread *thread;
  104. void *value;
  105. SDLTest_AssertPass("Testing property locking");
  106. data.done = SDL_FALSE;
  107. data.props = SDL_CreateProperties();
  108. SDLTest_AssertPass("Setting property to 'init'");
  109. SDL_SetProperty(data.props, "a", "init", NULL, NULL);
  110. thread = SDL_CreateThread(properties_thread, "properties_thread", &data);
  111. if (thread) {
  112. SDLTest_AssertPass("Waiting for property to change to 'thread_loop'");
  113. for ( ; ; )
  114. {
  115. SDL_Delay(10);
  116. SDL_LockProperties(data.props);
  117. value = SDL_GetProperty(data.props, "a");
  118. SDL_UnlockProperties(data.props);
  119. if (!value || SDL_strcmp((const char *)value, "thread_loop") == 0) {
  120. break;
  121. }
  122. }
  123. SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "thread_loop") == 0,
  124. "After thread loop, property is %s, expected 'thread_loop'", value ? (const char *)value : "NULL");
  125. SDLTest_AssertPass("Setting property to 'main'");
  126. SDL_LockProperties(data.props);
  127. SDL_SetProperty(data.props, "a", "main", NULL, NULL);
  128. SDL_Delay(100);
  129. value = SDL_GetProperty(data.props, "a");
  130. SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "main") == 0,
  131. "After 100ms sleep, property is %s, expected 'main'", value ? (const char *)value : "NULL");
  132. SDL_UnlockProperties(data.props);
  133. data.done = SDL_TRUE;
  134. SDL_WaitThread(thread, NULL);
  135. value = SDL_GetProperty(data.props, "a");
  136. SDLTest_AssertCheck(value && SDL_strcmp((const char *)value, "thread_done") == 0,
  137. "After thread complete, property is %s, expected 'thread_done'", value ? (const char *)value : "NULL");
  138. }
  139. SDL_DestroyProperties(data.props);
  140. return TEST_COMPLETED;
  141. }
  142. /* ================= Test References ================== */
  143. /* Properties test cases */
  144. static const SDLTest_TestCaseReference propertiesTest1 = {
  145. (SDLTest_TestCaseFp)properties_testBasic, "properties_testBasic", "Test basic property functionality", TEST_ENABLED
  146. };
  147. static const SDLTest_TestCaseReference propertiesTest2 = {
  148. (SDLTest_TestCaseFp)properties_testCleanup, "properties_testCleanup", "Test property cleanup functionality", TEST_ENABLED
  149. };
  150. static const SDLTest_TestCaseReference propertiesTest3 = {
  151. (SDLTest_TestCaseFp)properties_testLocking, "properties_testLocking", "Test property locking functionality", TEST_ENABLED
  152. };
  153. /* Sequence of Properties test cases */
  154. static const SDLTest_TestCaseReference *propertiesTests[] = {
  155. &propertiesTest1, &propertiesTest2, &propertiesTest3, NULL
  156. };
  157. /* Properties test suite (global) */
  158. SDLTest_TestSuiteReference propertiesTestSuite = {
  159. "Properties",
  160. NULL,
  161. propertiesTests,
  162. NULL
  163. };