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

150 lines
4.3 KiB

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