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 
15 #ifndef UNITTEST_H_
16 #define UNITTEST_H_
17 
18 
19 // gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
20 #ifndef __STDC_CONSTANT_MACROS
21 #  define __STDC_CONSTANT_MACROS 1 // required by C++ standard
22 #endif
23 
24 #ifdef _MSC_VER
25 #define _CRTDBG_MAP_ALLOC
26 #include <crtdbg.h>
27 #pragma warning(disable : 4996) // 'function': was declared deprecated
28 #endif
29 
30 #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
31 #if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
32 #pragma GCC diagnostic push
33 #endif
34 #pragma GCC diagnostic ignored "-Weffc++"
35 #endif
36 
37 #include "gtest/gtest.h"
38 #include <stdexcept>
39 
40 #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
41 #pragma GCC diagnostic pop
42 #endif
43 
44 template <typename Ch>
StrLen(const Ch * s)45 inline unsigned StrLen(const Ch* s) {
46     const Ch* p = s;
47     while (*p) p++;
48     return unsigned(p - s);
49 }
50 
51 template<typename Ch>
StrCmp(const Ch * s1,const Ch * s2)52 inline int StrCmp(const Ch* s1, const Ch* s2) {
53     while(*s1 && (*s1 == *s2)) { s1++; s2++; }
54     return (unsigned)*s1 < (unsigned)*s2 ? -1 : (unsigned)*s1 > (unsigned)*s2;
55 }
56 
57 template <typename Ch>
StrDup(const Ch * str)58 inline Ch* StrDup(const Ch* str) {
59     size_t bufferSize = sizeof(Ch) * (StrLen(str) + 1);
60     Ch* buffer = (Ch*)malloc(bufferSize);
61     memcpy(buffer, str, bufferSize);
62     return buffer;
63 }
64 
TempFile(char * filename)65 inline FILE* TempFile(char *filename) {
66 #if _MSC_VER
67     filename = tmpnam(filename);
68 
69     // For Visual Studio, tmpnam() adds a backslash in front. Remove it.
70     if (filename[0] == '\\')
71         for (int i = 0; filename[i] != '\0'; i++)
72             filename[i] = filename[i + 1];
73 
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 
82 // Use exception for catching assert
83 #if _MSC_VER
84 #pragma warning(disable : 4127)
85 #endif
86 
87 class AssertException : public std::logic_error {
88 public:
AssertException(const char * w)89     AssertException(const char* w) : std::logic_error(w) {}
90 };
91 
92 #define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))
93 
94 class Random {
95 public:
mSeed(seed)96     Random(unsigned seed = 0) : mSeed(seed) {}
97 
operator()98     unsigned operator()() {
99         mSeed = 214013 * mSeed + 2531011;
100         return mSeed;
101     }
102 
103 private:
104     unsigned mSeed;
105 };
106 
107 #endif // UNITTEST_H_
108