unittest.h
Go to the documentation of this file.
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 // gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
19 #ifndef __STDC_CONSTANT_MACROS
20 #ifdef __clang__
21 #pragma GCC diagnostic push
22 #if __has_warning("-Wreserved-id-macro")
23 #pragma GCC diagnostic ignored "-Wreserved-id-macro"
24 #endif
25 #endif
26 
27 # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
28 
29 #ifdef __clang__
30 #pragma GCC diagnostic pop
31 #endif
32 #endif
33 
34 #ifdef _MSC_VER
35 #define _CRTDBG_MAP_ALLOC
36 #include <crtdbg.h>
37 #pragma warning(disable : 4996) // 'function': was declared deprecated
38 #endif
39 
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
43 #endif
44 #pragma GCC diagnostic ignored "-Weffc++"
45 #endif
46 
47 #include "gtest/gtest.h"
48 #include <stdexcept>
49 
50 #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
51 #pragma GCC diagnostic pop
52 #endif
53 
54 #ifdef __clang__
55 // All TEST() macro generated this warning, disable globally
56 #pragma GCC diagnostic ignored "-Wglobal-constructors"
57 #endif
58 
59 template <typename Ch>
60 inline unsigned StrLen(const Ch* s) {
61  const Ch* p = s;
62  while (*p) p++;
63  return unsigned(p - s);
64 }
65 
66 template<typename Ch>
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);
70 }
71 
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);
77  return buffer;
78 }
79 
80 inline FILE* TempFile(char *filename) {
81 #if defined(__WIN32__) || defined(_MSC_VER)
82  filename = tmpnam(filename);
83 
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];
88 
89  return fopen(filename, "wb");
90 #else
91  strcpy(filename, "/tmp/fileXXXXXX");
92  int fd = mkstemp(filename);
93  return fdopen(fd, "w");
94 #endif
95 }
96 
97 // Use exception for catching assert
98 #ifdef _MSC_VER
99 #pragma warning(disable : 4127)
100 #endif
101 
102 #ifdef __clang__
103 #pragma GCC diagnostic push
104 #if __has_warning("-Wdeprecated")
105 #pragma GCC diagnostic ignored "-Wdeprecated"
106 #endif
107 #endif
108 
109 class AssertException : public std::logic_error {
110 public:
111  AssertException(const char* w) : std::logic_error(w) {}
112  AssertException(const AssertException& rhs) : std::logic_error(rhs) {}
113  virtual ~AssertException() throw();
114 };
115 
116 #ifdef __clang__
117 #pragma GCC diagnostic pop
118 #endif
119 
120 #define RAPIDJSON_ASSERT(x) (!(x) ? throw AssertException(RAPIDJSON_STRINGIFY(x)) : (void)0u)
121 
122 class Random {
123 public:
124  Random(unsigned seed = 0) : mSeed(seed) {}
125 
126  unsigned operator()() {
127  mSeed = 214013 * mSeed + 2531011;
128  return mSeed;
129  }
130 
131 private:
132  unsigned mSeed;
133 };
134 
135 #endif // UNITTEST_H_
unsigned operator()()
Definition: unittest.h:126
FILE * TempFile(char *filename)
Definition: unittest.h:80
unsigned mSeed
Definition: unittest.h:132
AssertException(const char *w)
Definition: unittest.h:111
Random(unsigned seed=0)
Definition: unittest.h:124
Ch * StrDup(const Ch *str)
Definition: unittest.h:73
int StrCmp(const Ch *s1, const Ch *s2)
Definition: unittest.h:67
unsigned StrLen(const Ch *s)
Definition: unittest.h:60
virtual ~AssertException()
Definition: unittest.cpp:25
AssertException(const AssertException &rhs)
Definition: unittest.h:112


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:09