1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
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
8 // http://opensource.org/licenses/MIT
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.
18 // gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
19 #ifndef __STDC_CONSTANT_MACROS
21 #pragma GCC diagnostic push
22 #if __has_warning("-Wreserved-id-macro")
23 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
27 # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
30 #pragma GCC diagnostic pop
35 #define _CRTDBG_MAP_ALLOC
37 #pragma warning(disable : 4996) // 'function': was declared deprecated
40 #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
41 #if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
42 #pragma GCC diagnostic push
44 #pragma GCC diagnostic ignored "-Weffc++"
47 #include "gtest/gtest.h"
50 #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
51 #pragma GCC diagnostic pop
55 // All TEST() macro generated this warning, disable globally
56 #pragma GCC diagnostic ignored "-Wglobal-constructors"
59 template <typename Ch>
60 inline unsigned StrLen(const Ch* s) {
63 return unsigned(p - s);
67 inline int StrCmp(const Ch* s1, const Ch* s2) {
68 while(*s1 && (*s1 == *s2)) { s1++; s2++; }
69 return static_cast<unsigned>(*s1) < static_cast<unsigned>(*s2) ? -1 : static_cast<unsigned>(*s1) > static_cast<unsigned>(*s2);
72 template <typename Ch>
73 inline Ch* StrDup(const Ch* str) {
74 size_t bufferSize = sizeof(Ch) * (StrLen(str) + 1);
75 Ch* buffer = static_cast<Ch*>(malloc(bufferSize));
76 memcpy(buffer, str, bufferSize);
80 inline FILE* TempFile(char *filename) {
82 filename = tmpnam(filename);
84 // For Visual Studio, tmpnam() adds a backslash in front. Remove it.
85 if (filename[0] == '\\')
86 for (int i = 0; filename[i] != '\0'; i++)
87 filename[i] = filename[i + 1];
89 return fopen(filename, "wb");
91 strcpy(filename, "/tmp/fileXXXXXX");
92 int fd = mkstemp(filename);
93 return fdopen(fd, "w");
97 // Use exception for catching assert
99 #pragma warning(disable : 4127)
103 #pragma GCC diagnostic push
104 #if __has_warning("-Wdeprecated")
105 #pragma GCC diagnostic ignored "-Wdeprecated"
109 class AssertException : public std::logic_error {
111 AssertException(const char* w) : std::logic_error(w) {}
112 AssertException(const AssertException& rhs) : std::logic_error(rhs) {}
113 virtual ~AssertException() throw();
117 #pragma GCC diagnostic pop
120 #define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))
124 Random(unsigned seed = 0) : mSeed(seed) {}
126 unsigned operator()() {
127 mSeed = 214013 * mSeed + 2531011;
135 #endif // UNITTEST_H_