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

171 lines
4.6 KiB

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