unittest.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef UNITTEST_H_
  15. #define UNITTEST_H_
  16. // gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
  17. #ifndef __STDC_CONSTANT_MACROS
  18. #ifdef __clang__
  19. #pragma GCC diagnostic push
  20. #if __has_warning("-Wreserved-id-macro")
  21. #pragma GCC diagnostic ignored "-Wreserved-id-macro"
  22. #endif
  23. #endif
  24. # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
  25. #ifdef __clang__
  26. #pragma GCC diagnostic pop
  27. #endif
  28. #endif
  29. #ifdef _MSC_VER
  30. #define _CRTDBG_MAP_ALLOC
  31. #include <crtdbg.h>
  32. #pragma warning(disable : 4996) // 'function': was declared deprecated
  33. #endif
  34. #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
  35. #if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  36. #pragma GCC diagnostic push
  37. #endif
  38. #pragma GCC diagnostic ignored "-Weffc++"
  39. #endif
  40. #include "gtest/gtest.h"
  41. #include <stdexcept>
  42. #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  43. #pragma GCC diagnostic pop
  44. #endif
  45. #ifdef __clang__
  46. // All TEST() macro generated this warning, disable globally
  47. #pragma GCC diagnostic ignored "-Wglobal-constructors"
  48. #endif
  49. template <typename Ch>
  50. inline unsigned StrLen(const Ch* s) {
  51. const Ch* p = s;
  52. while (*p) p++;
  53. return unsigned(p - s);
  54. }
  55. template<typename Ch>
  56. inline int StrCmp(const Ch* s1, const Ch* s2) {
  57. while(*s1 && (*s1 == *s2)) { s1++; s2++; }
  58. return static_cast<unsigned>(*s1) < static_cast<unsigned>(*s2) ? -1 : static_cast<unsigned>(*s1) > static_cast<unsigned>(*s2);
  59. }
  60. template <typename Ch>
  61. inline Ch* StrDup(const Ch* str) {
  62. size_t bufferSize = sizeof(Ch) * (StrLen(str) + 1);
  63. Ch* buffer = static_cast<Ch*>(malloc(bufferSize));
  64. memcpy(buffer, str, bufferSize);
  65. return buffer;
  66. }
  67. inline FILE* TempFile(char *filename) {
  68. #ifdef _MSC_VER
  69. filename = tmpnam(filename);
  70. // For Visual Studio, tmpnam() adds a backslash in front. Remove it.
  71. if (filename[0] == '\\')
  72. for (int i = 0; filename[i] != '\0'; i++)
  73. filename[i] = filename[i + 1];
  74. return fopen(filename, "wb");
  75. #else
  76. strcpy(filename, "/tmp/fileXXXXXX");
  77. int fd = mkstemp(filename);
  78. return fdopen(fd, "w");
  79. #endif
  80. }
  81. // Use exception for catching assert
  82. #ifdef _MSC_VER
  83. #pragma warning(disable : 4127)
  84. #endif
  85. #ifdef __clang__
  86. #pragma GCC diagnostic push
  87. #if __has_warning("-Wdeprecated")
  88. #pragma GCC diagnostic ignored "-Wdeprecated"
  89. #endif
  90. #endif
  91. class AssertException : public std::logic_error {
  92. public:
  93. AssertException(const char* w) : std::logic_error(w) {}
  94. AssertException(const AssertException& rhs) : std::logic_error(rhs) {}
  95. virtual ~AssertException() throw();
  96. };
  97. #ifdef __clang__
  98. #pragma GCC diagnostic pop
  99. #endif
  100. #define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))
  101. class Random {
  102. public:
  103. Random(unsigned seed = 0) : mSeed(seed) {}
  104. unsigned operator()() {
  105. mSeed = 214013 * mSeed + 2531011;
  106. return mSeed;
  107. }
  108. private:
  109. unsigned mSeed;
  110. };
  111. #endif // UNITTEST_H_