WEXAdapter.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef LLVM_CLANG_UNITTESTS_WEX_ADAPTER_H
  2. #define LLVM_CLANG_UNITTESTS_WEX_ADAPTER_H
  3. #ifndef _WIN32
  4. #include <unistd.h>
  5. #include <wchar.h>
  6. #include "dxc/Support/WinAdapter.h"
  7. #include "dxc/Support/WinFunctions.h"
  8. #include "gtest/gtest.h"
  9. #define MAX_PATH 260
  10. // Concatinate two macro fragments
  11. #define CONCAT2(a, b) a##b
  12. #define CONCAT1(a, b) CONCAT2(a, b)
  13. #define CONCAT(a, b) CONCAT1(a, b)
  14. // Determine how many arguments are passed to NARG() up to 3
  15. #define ARG_CT(_1, _2, _3, N, ...) N
  16. #define NARG(...) ARG_CT(__VA_ARGS__, 3, 2, 1, 0)
  17. // Call the appropriate arity macro based on number of arguments
  18. #define MACRO_N_(PREFIX, N, ...) CONCAT(PREFIX, N)(__VA_ARGS__)
  19. #define MACRO_N(PREFIX, ...) MACRO_N_(PREFIX, NARG(__VA_ARGS__), __VA_ARGS__)
  20. // Macros to convert TAEF macros to gtest equivalents
  21. // Single and double argument versions for optional failure messages
  22. #define VERIFY_SUCCEEDED_1(expr) EXPECT_TRUE(SUCCEEDED(expr))
  23. #define VERIFY_SUCCEEDED_2(expr, msg) EXPECT_TRUE(SUCCEEDED(expr)) << msg
  24. #define VERIFY_SUCCEEDED(...) MACRO_N(VERIFY_SUCCEEDED_, __VA_ARGS__)
  25. #define VERIFY_FAILED_1(expr) EXPECT_FALSE(SUCCEEDED(expr))
  26. #define VERIFY_FAILED_2(expr, msg) EXPECT_FALSE(SUCCEEDED(expr)) << msg
  27. #define VERIFY_FAILED(...) MACRO_N(VERIFY_FAILED_, __VA_ARGS__)
  28. #define VERIFY_ARE_EQUAL_2(A, B) EXPECT_EQ(A, B)
  29. #define VERIFY_ARE_EQUAL_3(A, B, msg) EXPECT_EQ(A, B) << msg
  30. #define VERIFY_ARE_EQUAL(...) MACRO_N(VERIFY_ARE_EQUAL_, __VA_ARGS__)
  31. #define VERIFY_ARE_NOT_EQUAL_2(A, B) EXPECT_NE(A, B)
  32. #define VERIFY_ARE_NOT_EQUAL_3(A, B, msg) EXPECT_NE(A, B) << msg
  33. #define VERIFY_ARE_NOT_EQUAL(...) MACRO_N(VERIFY_ARE_NOT_EQUAL_, __VA_ARGS__)
  34. #define VERIFY_IS_TRUE_1(expr) EXPECT_TRUE(expr)
  35. #define VERIFY_IS_TRUE_2(expr, msg) EXPECT_TRUE(expr) << msg
  36. #define VERIFY_IS_TRUE(...) MACRO_N(VERIFY_IS_TRUE_, __VA_ARGS__)
  37. #define VERIFY_IS_FALSE_1(expr) EXPECT_FALSE(expr)
  38. #define VERIFY_IS_FALSE_2(expr, msg) EXPECT_FALSE(expr) << msg
  39. #define VERIFY_IS_FALSE(...) MACRO_N(VERIFY_IS_FALSE_, __VA_ARGS__)
  40. #define VERIFY_IS_NULL_1(expr) EXPECT_EQ(nullptr, (expr))
  41. #define VERIFY_IS_NULL_2(expr, msg) EXPECT_EQ(nullptr, (expr)) << msg
  42. #define VERIFY_IS_NULL(...) MACRO_N(VERIFY_IS_NULL_, __VA_ARGS__)
  43. #define VERIFY_IS_NOT_NULL_1(expr) EXPECT_NE(nullptr, (expr))
  44. #define VERIFY_IS_NOT_NULL_2(expr, msg) EXPECT_NE(nullptr, (expr)) << msg
  45. #define VERIFY_IS_NOT_NULL(...) MACRO_N(VERIFY_IS_NOT_NULL_, __VA_ARGS__)
  46. #define VERIFY_IS_GREATER_THAN_OR_EQUAL(greater, less) EXPECT_GE(greater, less)
  47. #define VERIFY_WIN32_BOOL_SUCCEEDED_1(expr) EXPECT_TRUE(expr)
  48. #define VERIFY_WIN32_BOOL_SUCCEEDED_2(expr, msg) EXPECT_TRUE(expr) << msg
  49. #define VERIFY_WIN32_BOOL_SUCCEEDED(...) MACRO_N(VERIFY_WIN32_BOOL_SUCCEEDED_, __VA_ARGS__)
  50. #define VERIFY_FAIL ADD_FAILURE
  51. #define TEST_CLASS_SETUP(method) \
  52. bool method(); \
  53. virtual void SetUp() { EXPECT_TRUE(method()); }
  54. #define TEST_CLASS_CLEANUP(method) \
  55. bool method(); \
  56. virtual void TearDown() { EXPECT_TRUE(method()); }
  57. #define BEGIN_TEST_CLASS(test)
  58. #define TEST_CLASS_PROPERTY(str1, str2)
  59. #define TEST_METHOD_PROPERTY(str1, str2)
  60. #define END_TEST_CLASS()
  61. #define TEST_METHOD(method)
  62. #define BEGIN_TEST_METHOD(method)
  63. #define END_TEST_METHOD()
  64. // gtest lacks any module setup/cleanup. These functions are called by the
  65. // main() function before and after tests are run. This approximates the
  66. // behavior.
  67. bool moduleSetup();
  68. bool moduleTeardown();
  69. #define MODULE_SETUP(method) \
  70. bool method(); \
  71. bool moduleSetup() { return method(); }
  72. #define MODULE_CLEANUP(method) \
  73. bool method(); \
  74. bool moduleTeardown() { return method(); }
  75. // No need to expand env vars on Unix platforms, so convert the slashes instead.
  76. inline DWORD ExpandEnvironmentStringsW(_In_ LPCWSTR lpSrc,
  77. _Out_opt_ LPWSTR lpDst,
  78. _In_ DWORD nSize) {
  79. unsigned i;
  80. bool wasSlash = false;
  81. for (i = 0; i < nSize && *lpSrc; i++, lpSrc++) {
  82. if (*lpSrc == L'\\' || *lpSrc == L'/') {
  83. if (!wasSlash)
  84. *lpDst++ = L'/';
  85. wasSlash = true;
  86. } else {
  87. *lpDst++ = *lpSrc;
  88. wasSlash = false;
  89. }
  90. }
  91. *lpDst = L'\0';
  92. return i;
  93. }
  94. typedef struct _LIST_ENTRY {
  95. struct _LIST_ENTRY *Flink;
  96. struct _LIST_ENTRY *Blink;
  97. } LIST_ENTRY, *PLIST_ENTRY, PRLIST_ENTRY;
  98. // Minimal implementation of the WEX namespace functions and classes
  99. // To either stub out or approximate behavior
  100. namespace WEX {
  101. namespace Common {
  102. class String : public std::wstring {
  103. public:
  104. size_t GetLength() { return length(); }
  105. bool IsEmpty() { return empty(); }
  106. int CompareNoCase(std::wstring str) const {
  107. return -1;
  108. assert(!"unimplemented");
  109. }
  110. operator const wchar_t *() { return c_str(); }
  111. const wchar_t *GetBuffer() { return *this; }
  112. wchar_t *Format(const wchar_t *fmt, ...) {
  113. static wchar_t msg[512];
  114. va_list args;
  115. va_start(args, fmt);
  116. vswprintf(msg, 512, fmt, args);
  117. va_end(args);
  118. return msg;
  119. }
  120. };
  121. } // namespace Common
  122. namespace TestExecution {
  123. enum class VerifyOutputSettings { LogOnlyFailures };
  124. class SetVerifyOutput {
  125. public:
  126. SetVerifyOutput(VerifyOutputSettings) {}
  127. };
  128. class DisableVerifyExceptions {
  129. public:
  130. DisableVerifyExceptions() {}
  131. };
  132. namespace RuntimeParameters {
  133. HRESULT TryGetValue(const wchar_t *param, Common::String &retStr);
  134. } // namespace RuntimeParameters
  135. } // namespace TestExecution
  136. namespace Logging {
  137. namespace Log {
  138. inline void StartGroup(const wchar_t *name) { wprintf(L"BEGIN TEST(S): <%ls>\n", name); };
  139. inline void EndGroup(const wchar_t *name) { wprintf(L"END TEST(S): <%ls>\n", name); };
  140. inline void Comment(const wchar_t *msg) {
  141. fputws(msg, stdout);
  142. fputwc(L'\n', stdout);
  143. }
  144. inline void Error(const wchar_t *msg) {
  145. fputws(msg, stderr);
  146. fputwc(L'\n', stderr);
  147. ADD_FAILURE();
  148. }
  149. } // namespace Log
  150. } // namespace Logging
  151. } // namespace WEX
  152. #endif // _WIN32
  153. #endif // LLVM_CLANG_UNITTESTS_WEX_ADAPTER_H