testautomation_guid.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * GUID test suite
  3. */
  4. #include <SDL3/SDL.h>
  5. #include <SDL3/SDL_test.h>
  6. /* ================= Test Case Implementation ================== */
  7. /* Helper functions */
  8. #define NUM_TEST_GUIDS 5
  9. static struct
  10. {
  11. char *str;
  12. Uint64 upper, lower;
  13. } test_guids[NUM_TEST_GUIDS] = {
  14. { "0000000000000000"
  15. "ffffffffffffffff",
  16. 0x0000000000000000, 0xfffffffffffffffflu },
  17. { "0011223344556677"
  18. "8091a2b3c4d5e6f0",
  19. 0x0011223344556677lu, 0x8091a2b3c4d5e6f0lu },
  20. { "a011223344556677"
  21. "8091a2b3c4d5e6f0",
  22. 0xa011223344556677lu, 0x8091a2b3c4d5e6f0lu },
  23. { "a011223344556677"
  24. "8091a2b3c4d5e6f1",
  25. 0xa011223344556677lu, 0x8091a2b3c4d5e6f1lu },
  26. { "a011223344556677"
  27. "8191a2b3c4d5e6f0",
  28. 0xa011223344556677lu, 0x8191a2b3c4d5e6f0lu },
  29. };
  30. static void
  31. upper_lower_to_bytestring(Uint8 *out, Uint64 upper, Uint64 lower)
  32. {
  33. Uint64 values[2];
  34. int i, k;
  35. values[0] = upper;
  36. values[1] = lower;
  37. for (i = 0; i < 2; ++i) {
  38. Uint64 v = values[i];
  39. for (k = 0; k < 8; ++k) {
  40. *out++ = v >> 56;
  41. v <<= 8;
  42. }
  43. }
  44. }
  45. /* Test case functions */
  46. /**
  47. * @brief Check String-to-GUID conversion
  48. *
  49. * @sa SDL_GUIDFromString
  50. */
  51. static int
  52. TestGuidFromString(void *arg)
  53. {
  54. int i;
  55. SDLTest_AssertPass("Call to SDL_GUIDFromString");
  56. for (i = 0; i < NUM_TEST_GUIDS; ++i) {
  57. Uint8 expected[16];
  58. SDL_GUID guid;
  59. upper_lower_to_bytestring(expected,
  60. test_guids[i].upper, test_guids[i].lower);
  61. guid = SDL_GUIDFromString(test_guids[i].str);
  62. SDLTest_AssertCheck(SDL_memcmp(expected, guid.data, 16) == 0, "GUID from string, GUID was: '%s'", test_guids[i].str);
  63. }
  64. return TEST_COMPLETED;
  65. }
  66. /**
  67. * @brief Check GUID-to-String conversion
  68. *
  69. * @sa SDL_GUIDToString
  70. */
  71. static int
  72. TestGuidToString(void *arg)
  73. {
  74. int i;
  75. SDLTest_AssertPass("Call to SDL_GUIDToString");
  76. for (i = 0; i < NUM_TEST_GUIDS; ++i) {
  77. const int guid_str_offset = 4;
  78. char guid_str_buf[64];
  79. char *guid_str = guid_str_buf + guid_str_offset;
  80. SDL_GUID guid;
  81. int size;
  82. upper_lower_to_bytestring(guid.data,
  83. test_guids[i].upper, test_guids[i].lower);
  84. /* Serialise to limited-length buffers */
  85. for (size = 0; size <= 36; ++size) {
  86. const Uint8 fill_char = size + 0xa0;
  87. Uint32 expected_prefix;
  88. Uint32 actual_prefix;
  89. int written_size;
  90. SDL_memset(guid_str_buf, fill_char, sizeof(guid_str_buf));
  91. SDL_GUIDToString(guid, guid_str, size);
  92. /* Check bytes before guid_str_buf */
  93. expected_prefix = fill_char | (fill_char << 8) | (fill_char << 16) | (fill_char << 24);
  94. SDL_memcpy(&actual_prefix, guid_str_buf, 4);
  95. SDLTest_AssertCheck(expected_prefix == actual_prefix, "String buffer memory before output untouched, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32 ", at size=%d", expected_prefix, actual_prefix, size);
  96. /* Check that we did not overwrite too much */
  97. written_size = 0;
  98. while ((guid_str[written_size] & 0xff) != fill_char && written_size < 256) {
  99. ++written_size;
  100. }
  101. SDLTest_AssertCheck(written_size <= size, "Output length is within expected bounds, with length %d: wrote %d of %d permitted bytes", size, written_size, size);
  102. if (size >= 33) {
  103. SDLTest_AssertCheck(SDL_strcmp(guid_str, test_guids[i].str) == 0, "GUID string equality, from string: %s", test_guids[i].str);
  104. }
  105. }
  106. }
  107. return TEST_COMPLETED;
  108. }
  109. /* ================= Test References ================== */
  110. /* GUID routine test cases */
  111. static const SDLTest_TestCaseReference guidTest1 = {
  112. (SDLTest_TestCaseFp)TestGuidFromString, "TestGuidFromString", "Call to SDL_GUIDFromString", TEST_ENABLED
  113. };
  114. static const SDLTest_TestCaseReference guidTest2 = {
  115. (SDLTest_TestCaseFp)TestGuidToString, "TestGuidToString", "Call to SDL_GUIDToString", TEST_ENABLED
  116. };
  117. /* Sequence of GUID routine test cases */
  118. static const SDLTest_TestCaseReference *guidTests[] = {
  119. &guidTest1,
  120. &guidTest2,
  121. NULL
  122. };
  123. /* GUID routine test suite (global) */
  124. SDLTest_TestSuiteReference guidTestSuite = {
  125. "GUID",
  126. NULL,
  127. guidTests,
  128. NULL
  129. };