testautomation_guid.c 4.6 KB

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