| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#ifndef TESTRUNNERSWITCHER_H |
| 5 |
#define TESTRUNNERSWITCHER_H |
| 6 |
|
| 7 |
#include "azure_macro_utils/macro_utils.h" |
| 8 |
|
| 9 |
#ifdef __cplusplus |
| 10 |
#include <cstring> |
| 11 |
#include <cstdint> |
| 12 |
#else |
| 13 |
#include <stdbool.h> |
| 14 |
#include <string.h> |
| 15 |
#include <stdint.h> |
| 16 |
|
| 17 |
#endif |
| 18 |
|
| 19 |
#ifdef MBED_BUILD_TIMESTAMP |
| 20 |
#define USE_CTEST |
| 21 |
#endif |
| 22 |
|
| 23 |
typedef void* TEST_MUTEX_HANDLE; |
| 24 |
|
| 25 |
#define USE_ONLY_ENUM_NAMES(count, enum_value_name, enum_value_value) enum_value_name MU_IF_COMMA(count) |
| 26 |
#define DEFINE_ENUMERATION_CONSTANT_AS_WIDESTRING(x, y) MU_C2(L, MU_TOSTRING(x)) , |
| 27 |
|
| 28 |
#define TEST_DEFINE_ENUM_TYPE(type, ...) TEST_ENUM_TYPE_HANDLER(type, MU_FOR_EACH_1(MU_DEFINE_ENUMERATION_CONSTANT_AS_WIDESTRING, __VA_ARGS__)); |
| 29 |
#define TEST_DEFINE_ENUM_2_TYPE(type, ...) TEST_ENUM_2_TYPE_HANDLER(type, MU_FOR_EACH_2(DEFINE_ENUMERATION_CONSTANT_AS_WIDESTRING, __VA_ARGS__)); |
| 30 |
|
| 31 |
#ifdef USE_CTEST |
| 32 |
|
| 33 |
#include "ctest.h" |
| 34 |
|
| 35 |
#define BEGIN_TEST_SUITE(name) CTEST_BEGIN_TEST_SUITE(name) |
| 36 |
#define END_TEST_SUITE(name) CTEST_END_TEST_SUITE(name) |
| 37 |
|
| 38 |
#define TEST_SUITE_INITIALIZE(name) CTEST_SUITE_INITIALIZE() |
| 39 |
#define TEST_SUITE_CLEANUP(name) CTEST_SUITE_CLEANUP() |
| 40 |
#define TEST_FUNCTION_INITIALIZE(name) CTEST_FUNCTION_INITIALIZE() |
| 41 |
#define TEST_FUNCTION_CLEANUP(name) CTEST_FUNCTION_CLEANUP() |
| 42 |
|
| 43 |
#define TEST_FUNCTION(name) CTEST_FUNCTION(name) |
| 44 |
|
| 45 |
#define ASSERT_ARE_EQUAL CTEST_ASSERT_ARE_EQUAL |
| 46 |
#define ASSERT_ARE_NOT_EQUAL CTEST_ASSERT_ARE_NOT_EQUAL |
| 47 |
#define ASSERT_FAIL CTEST_ASSERT_FAIL |
| 48 |
#define ASSERT_IS_NULL CTEST_ASSERT_IS_NULL |
| 49 |
#define ASSERT_IS_NOT_NULL CTEST_ASSERT_IS_NOT_NULL |
| 50 |
#define ASSERT_IS_TRUE CTEST_ASSERT_IS_TRUE |
| 51 |
#define ASSERT_IS_FALSE CTEST_ASSERT_IS_FALSE |
| 52 |
|
| 53 |
#define RUN_TEST_SUITE(...) CTEST_RUN_TEST_SUITE(__VA_ARGS__) |
| 54 |
|
| 55 |
#define TEST_MUTEX_CREATE() (TEST_MUTEX_HANDLE)1 |
| 56 |
// the strlen check is simply to shut the compiler up and not create a hell of #pragma warning suppress |
| 57 |
#define TEST_MUTEX_ACQUIRE(mutex) (strlen("a") == 0) |
| 58 |
#define TEST_MUTEX_RELEASE(mutex) |
| 59 |
#define TEST_MUTEX_DESTROY(mutex) |
| 60 |
|
| 61 |
// This code belongs in ctest, there is an issue files to move it there: |
| 62 |
// https://github.com/Azure/azure-c-testrunnerswitcher/issues/32 |
| 63 |
#define TEST_ENUM_TYPE_HANDLER(EnumName, ...) \ |
| 64 |
static const wchar_t *EnumName##_Strings[]= \ |
| 65 |
{ \ |
| 66 |
__VA_ARGS__ \ |
| 67 |
}; \ |
| 68 |
static void EnumName##_ToString(char* dest, size_t bufferSize, EnumName enumValue) \ |
| 69 |
{ \ |
| 70 |
if((int)enumValue<(int)(sizeof(EnumName##_Strings)/sizeof(EnumName##_Strings[0]))) \ |
| 71 |
{ \ |
| 72 |
(void)snprintf(dest, bufferSize, "%ls", EnumName##_Strings[enumValue]); \ |
| 73 |
} \ |
| 74 |
else \ |
| 75 |
{ \ |
| 76 |
(void)snprintf(dest, bufferSize, "%d is out of bounds value for " MU_TOSTRING(EnumName), enumValue); \ |
| 77 |
} \ |
| 78 |
} \ |
| 79 |
static bool EnumName##_Compare(EnumName left, EnumName right) \ |
| 80 |
{ \ |
| 81 |
return left != right; \ |
| 82 |
} |
| 83 |
|
| 84 |
#define TEST_ENUM_2_TYPE_HANDLER(EnumName, ...) \ |
| 85 |
static void EnumName##_ToString(char* dest, size_t bufferSize, EnumName enumValue) \ |
| 86 |
{ \ |
| 87 |
const char* enum_string = MU_ENUM_TO_STRING_2(EnumName, enumValue); \ |
| 88 |
(void)snprintf(dest, bufferSize, "%s", enum_string); \ |
| 89 |
} \ |
| 90 |
static bool EnumName##_Compare(EnumName left, EnumName right) \ |
| 91 |
{ \ |
| 92 |
return left != right; \ |
| 93 |
} |
| 94 |
|
| 95 |
#define TEST_USE_CTEST_FUNCTIONS_FOR_TYPE(my_type) |
| 96 |
|
| 97 |
#elif defined CPP_UNITTEST |
| 98 |
|
| 99 |
#ifdef _MSC_VER |
| 100 |
#pragma warning(disable:4505) |
| 101 |
#endif |
| 102 |
|
| 103 |
#include "CppUnitTest.h" |
| 104 |
#include "testmutex.h" |
| 105 |
#include "ctrs_sprintf.h" |
| 106 |
|
| 107 |
using namespace Microsoft::VisualStudio::CppUnitTestFramework; |
| 108 |
|
| 109 |
typedef const char* char_ptr; |
| 110 |
typedef const wchar_t* wchar_ptr; |
| 111 |
typedef void* void_ptr; |
| 112 |
|
| 113 |
#ifdef CPPUNITTEST_SYMBOL |
| 114 |
extern "C" void CPPUNITTEST_SYMBOL(void) {} |
| 115 |
#endif |
| 116 |
|
| 117 |
#define BEGIN_TEST_SUITE(name) TEST_CLASS(name) { |
| 118 |
|
| 119 |
#define END_TEST_SUITE(name) }; |
| 120 |
|
| 121 |
#define TEST_SUITE_INITIALIZE(name) TEST_CLASS_INITIALIZE(name) |
| 122 |
#define TEST_SUITE_CLEANUP(name) TEST_CLASS_CLEANUP(name) |
| 123 |
#define TEST_FUNCTION_INITIALIZE(name) TEST_METHOD_INITIALIZE(name) |
| 124 |
#define TEST_FUNCTION_CLEANUP(name) TEST_METHOD_CLEANUP(name) |
| 125 |
|
| 126 |
#define TEST_FUNCTION(name) TEST_METHOD(name) |
| 127 |
|
| 128 |
// these are generic macros for formatting the optional message |
| 129 |
// they can be used in all the ASSERT macros without repeating the code over and over again |
| 130 |
#define CONSTRUCT_CTRS_MESSAGE_FORMATTED(format, ...) \ |
| 131 |
MU_IF(MU_COUNT_ARG(__VA_ARGS__), ctrs_sprintf_char(format, __VA_ARGS__), ctrs_sprintf_char(format)); |
| 132 |
|
| 133 |
#define CONSTRUCT_CTRS_MESSAGE_FORMATTED_EMPTY(...) \ |
| 134 |
NULL |
| 135 |
|
| 136 |
#define CONSTRUCT_CTRS_MESSAGE(...) \ |
| 137 |
MU_IF(MU_COUNT_ARG(__VA_ARGS__), CONSTRUCT_CTRS_MESSAGE_FORMATTED, CONSTRUCT_CTRS_MESSAGE_FORMATTED_EMPTY)(__VA_ARGS__) |
| 138 |
|
| 139 |
#define ASSERT_ARE_EQUAL(type, A, B, ...) \ |
| 140 |
do \ |
| 141 |
{ \ |
| 142 |
char* ctrs_message = CONSTRUCT_CTRS_MESSAGE(__VA_ARGS__); \ |
| 143 |
std::wstring cppUnitTestMessage = ToString(ctrs_message); \ |
| 144 |
ctrs_sprintf_free(ctrs_message); \ |
| 145 |
Assert::AreEqual((type)(A), (type)(B), cppUnitTestMessage.c_str()); \ |
| 146 |
} while ((void)0, 0) |
| 147 |
|
| 148 |
#define ASSERT_ARE_NOT_EQUAL(type, A, B, ...) \ |
| 149 |
do \ |
| 150 |
{ \ |
| 151 |
char* ctrs_message = CONSTRUCT_CTRS_MESSAGE(__VA_ARGS__); \ |
| 152 |
std::wstring cppUnitTestMessage = ToString(ctrs_message); \ |
| 153 |
ctrs_sprintf_free(ctrs_message); \ |
| 154 |
Assert::AreNotEqual((type)(A), (type)(B), cppUnitTestMessage.c_str()); \ |
| 155 |
} while ((void)0, 0) |
| 156 |
|
| 157 |
#define ASSERT_FAIL(...) \ |
| 158 |
do \ |
| 159 |
{ \ |
| 160 |
char* ctrs_message = CONSTRUCT_CTRS_MESSAGE(__VA_ARGS__); \ |
| 161 |
std::wstring cppUnitTestMessage = ToString(ctrs_message); \ |
| 162 |
ctrs_sprintf_free(ctrs_message); \ |
| 163 |
Assert::Fail(cppUnitTestMessage.c_str()); \ |
| 164 |
} while ((void)0, 0) |
| 165 |
|
| 166 |
#define ASSERT_IS_TRUE(expression, ...) \ |
| 167 |
do \ |
| 168 |
{ \ |
| 169 |
char* ctrs_message = CONSTRUCT_CTRS_MESSAGE(__VA_ARGS__); \ |
| 170 |
std::wstring cppUnitTestMessage = ToString(ctrs_message); \ |
| 171 |
ctrs_sprintf_free(ctrs_message); \ |
| 172 |
Assert::IsTrue((expression), cppUnitTestMessage.c_str()); \ |
| 173 |
} while ((void)0, 0) |
| 174 |
|
| 175 |
#define ASSERT_IS_FALSE(expression, ...) \ |
| 176 |
do \ |
| 177 |
{ \ |
| 178 |
char* ctrs_message = CONSTRUCT_CTRS_MESSAGE(__VA_ARGS__); \ |
| 179 |
std::wstring cppUnitTestMessage = ToString(ctrs_message); \ |
| 180 |
ctrs_sprintf_free(ctrs_message); \ |
| 181 |
Assert::IsFalse((expression), cppUnitTestMessage.c_str()); \ |
| 182 |
} while ((void)0, 0) |
| 183 |
|
| 184 |
#define ASSERT_IS_NOT_NULL(value, ...) \ |
| 185 |
do \ |
| 186 |
{ \ |
| 187 |
char* ctrs_message = CONSTRUCT_CTRS_MESSAGE(__VA_ARGS__); \ |
| 188 |
std::wstring cppUnitTestMessage = ToString(ctrs_message); \ |
| 189 |
ctrs_sprintf_free(ctrs_message); \ |
| 190 |
Assert::IsNotNull((value), cppUnitTestMessage.c_str()); \ |
| 191 |
} while ((void)0, 0) |
| 192 |
|
| 193 |
#define ASSERT_IS_NULL(value, ...) \ |
| 194 |
do \ |
| 195 |
{ \ |
| 196 |
char* ctrs_message = CONSTRUCT_CTRS_MESSAGE(__VA_ARGS__); \ |
| 197 |
std::wstring cppUnitTestMessage = ToString(ctrs_message); \ |
| 198 |
ctrs_sprintf_free(ctrs_message); \ |
| 199 |
Assert::IsNull((value), cppUnitTestMessage.c_str()); \ |
| 200 |
} while ((void)0, 0) |
| 201 |
|
| 202 |
#define RUN_TEST_SUITE(...) |
| 203 |
|
| 204 |
#define TEST_MUTEX_CREATE() testmutex_create() |
| 205 |
#define TEST_MUTEX_ACQUIRE(mutex) testmutex_acquire(mutex) |
| 206 |
#define TEST_MUTEX_RELEASE(mutex) testmutex_release(mutex) |
| 207 |
#define TEST_MUTEX_DESTROY(mutex) testmutex_destroy(mutex) |
| 208 |
|
| 209 |
#define TEST_ENUM_TYPE_HANDLER(EnumName, ...) \ |
| 210 |
namespace Microsoft \ |
| 211 |
{ \ |
| 212 |
namespace VisualStudio \ |
| 213 |
{ \ |
| 214 |
namespace CppUnitTestFramework \ |
| 215 |
{ \ |
| 216 |
static const wchar_t *EnumName##_Strings[]= \ |
| 217 |
{ \ |
| 218 |
__VA_ARGS__ \ |
| 219 |
}; \ |
| 220 |
template <> std::wstring ToString < EnumName > (const EnumName & q) \ |
| 221 |
{ \ |
| 222 |
if((size_t)q>=sizeof(EnumName##_Strings)/sizeof(EnumName##_Strings[0])) \ |
| 223 |
{ \ |
| 224 |
return(L"out of range value for " L#EnumName); \ |
| 225 |
} \ |
| 226 |
else \ |
| 227 |
{ \ |
| 228 |
return EnumName##_Strings[q]; \ |
| 229 |
} \ |
| 230 |
} \ |
| 231 |
} \ |
| 232 |
} \ |
| 233 |
}; |
| 234 |
|
| 235 |
#define TEST_ENUM_2_TYPE_HANDLER(EnumName, ...) \ |
| 236 |
TEST_ENUM_TYPE_HANDLER(EnumName, __VA_ARGS__) |
| 237 |
|
| 238 |
#define TEST_USE_CTEST_FUNCTIONS_FOR_TYPE(my_type) \ |
| 239 |
namespace Microsoft \ |
| 240 |
{ \ |
| 241 |
namespace VisualStudio \ |
| 242 |
{ \ |
| 243 |
namespace CppUnitTestFramework \ |
| 244 |
{ \ |
| 245 |
template<> \ |
| 246 |
inline std::wstring ToString<my_type>(const my_type& value) \ |
| 247 |
{ \ |
| 248 |
char temp_str[1024]; \ |
| 249 |
std::wstring result; \ |
| 250 |
if (MU_C2(my_type,_ToString)(temp_str, sizeof(temp_str), value) != 0) \ |
| 251 |
{ \ |
| 252 |
return L""; \ |
| 253 |
} \ |
| 254 |
else \ |
| 255 |
{ \ |
| 256 |
int size_needed_in_chars = MultiByteToWideChar(CP_UTF8, 0, &temp_str[0], -1, NULL, 0); \ |
| 257 |
if (size_needed_in_chars == 0) \ |
| 258 |
{ \ |
| 259 |
result = L""; \ |
| 260 |
} \ |
| 261 |
else \ |
| 262 |
{ \ |
| 263 |
WCHAR* widechar_string = (WCHAR*)malloc(size_needed_in_chars * sizeof(WCHAR)); \ |
| 264 |
if (widechar_string == NULL) \ |
| 265 |
{ \ |
| 266 |
result = L""; \ |
| 267 |
} \ |
| 268 |
else \ |
| 269 |
{ \ |
| 270 |
if (MultiByteToWideChar(CP_UTF8, 0, temp_str, -1, widechar_string, size_needed_in_chars) == 0) \ |
| 271 |
{ \ |
| 272 |
result = L""; \ |
| 273 |
} \ |
| 274 |
else \ |
| 275 |
{ \ |
| 276 |
result = std::wstring(widechar_string); \ |
| 277 |
} \ |
| 278 |
free(widechar_string); \ |
| 279 |
} \ |
| 280 |
} \ |
| 281 |
} \ |
| 282 |
return result; \ |
| 283 |
} \ |
| 284 |
template<> \ |
| 285 |
static void Assert::AreEqual<my_type>(const my_type& expected, const my_type& actual, const wchar_t* message, const __LineInfo* pLineInfo) \ |
| 286 |
{ \ |
| 287 |
FailOnCondition((MU_C2(my_type,_Compare)(expected, actual) == 0), EQUALS_MESSAGE(expected, actual, message), pLineInfo); \ |
| 288 |
} \ |
| 289 |
} \ |
| 290 |
} \ |
| 291 |
} \ |
| 292 |
|
| 293 |
/*because for some reason this is not defined by Visual Studio, it is defined here, so it is not multiplied in every single other unittest*/ |
| 294 |
namespace Microsoft |
| 295 |
{ |
| 296 |
namespace VisualStudio |
| 297 |
{ |
| 298 |
namespace CppUnitTestFramework |
| 299 |
{ |
| 300 |
|
| 301 |
/*Visual Studio 2019 version 16.1.0 has in C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\VS\UnitTest\include\CppUnitTestAssert.h the same template*/ |
| 302 |
#if _MSC_VER < 1921 |
| 303 |
template<> inline std::wstring ToString<int64_t>(const int64_t& t) |
| 304 |
{ |
| 305 |
RETURN_WIDE_STRING(t); |
| 306 |
} |
| 307 |
#endif |
| 308 |
template<> inline std::wstring ToString<uint16_t>(const uint16_t& t) |
| 309 |
{ |
| 310 |
RETURN_WIDE_STRING(t); |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
#else |
| 317 |
#error No test runner defined |
| 318 |
#endif |
| 319 |
|
| 320 |
#endif |