testautomation_guid.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_StringToGUID
  63. */
  64. static int SDLCALL
  65. TestStringToGUID(void *arg)
  66. {
  67. int i;
  68. SDLTest_AssertPass("Call to SDL_StringToGUID");
  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_StringToGUID(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 SDLCALL
  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. char guid_str[33];
  91. SDL_GUID guid;
  92. upper_lower_to_bytestring(guid.data,
  93. test_guids[i].upper, test_guids[i].lower);
  94. SDL_GUIDToString(guid, guid_str, sizeof(guid_str));
  95. SDLTest_AssertCheck(SDL_strcmp(guid_str, test_guids[i].str) == 0, "Checking whether strings match, expected %s, got %s\n", test_guids[i].str, guid_str);
  96. }
  97. return TEST_COMPLETED;
  98. }
  99. /* ================= Test References ================== */
  100. /* GUID routine test cases */
  101. static const SDLTest_TestCaseReference guidTest1 = {
  102. TestStringToGUID, "TestStringToGUID", "Call to SDL_StringToGUID", TEST_ENABLED
  103. };
  104. static const SDLTest_TestCaseReference guidTest2 = {
  105. TestGUIDToString, "TestGUIDToString", "Call to SDL_GUIDToString", TEST_ENABLED
  106. };
  107. /* Sequence of GUID routine test cases */
  108. static const SDLTest_TestCaseReference *guidTests[] = {
  109. &guidTest1,
  110. &guidTest2,
  111. NULL
  112. };
  113. /* GUID routine test suite (global) */
  114. SDLTest_TestSuiteReference guidTestSuite = {
  115. "GUID",
  116. NULL,
  117. guidTests,
  118. NULL
  119. };