2015-04-18 21:41:38 +08:00
|
|
|
// Tencent is pleased to support the open source community by making RapidJSON available.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
|
2014-08-11 22:26:45 +08:00
|
|
|
//
|
2015-04-18 21:41:38 +08:00
|
|
|
// Licensed under the MIT License (the "License"); you may not use this file except
|
|
|
|
// in compliance with the License. You may obtain a copy of the License at
|
2014-08-11 22:26:45 +08:00
|
|
|
//
|
2015-04-18 21:41:38 +08:00
|
|
|
// http://opensource.org/licenses/MIT
|
2014-08-11 22:26:45 +08:00
|
|
|
//
|
2015-04-18 21:41:38 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
2014-08-11 22:26:45 +08:00
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
#ifndef UNITTEST_H_
|
|
|
|
#define UNITTEST_H_
|
|
|
|
|
2014-07-03 00:59:35 +08:00
|
|
|
|
|
|
|
// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
|
|
|
|
#ifndef __STDC_CONSTANT_MACROS
|
|
|
|
# define __STDC_CONSTANT_MACROS 1 // required by C++ standard
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define _CRTDBG_MAP_ALLOC
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#pragma warning(disable : 4996) // 'function': was declared deprecated
|
|
|
|
#endif
|
|
|
|
|
2014-07-08 14:45:19 +02:00
|
|
|
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
|
|
|
|
#if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#endif
|
2014-07-08 15:12:02 +02:00
|
|
|
#pragma GCC diagnostic ignored "-Weffc++"
|
2014-07-03 00:59:35 +08:00
|
|
|
#endif
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
#include "gtest/gtest.h"
|
2015-02-20 20:29:31 +01:00
|
|
|
#include <stdexcept>
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-07-08 14:45:19 +02:00
|
|
|
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
|
2014-07-03 00:59:35 +08:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
template <typename Ch>
|
2014-07-10 16:46:42 +02:00
|
|
|
inline unsigned StrLen(const Ch* s) {
|
2014-08-11 22:26:45 +08:00
|
|
|
const Ch* p = s;
|
|
|
|
while (*p) p++;
|
|
|
|
return unsigned(p - s);
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Ch>
|
|
|
|
inline int StrCmp(const Ch* s1, const Ch* s2) {
|
2014-08-11 22:26:45 +08:00
|
|
|
while(*s1 && (*s1 == *s2)) { s1++; s2++; }
|
|
|
|
return (unsigned)*s1 < (unsigned)*s2 ? -1 : (unsigned)*s1 > (unsigned)*s2;
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Ch>
|
|
|
|
inline Ch* StrDup(const Ch* str) {
|
2014-08-11 22:26:45 +08:00
|
|
|
size_t bufferSize = sizeof(Ch) * (StrLen(str) + 1);
|
|
|
|
Ch* buffer = (Ch*)malloc(bufferSize);
|
|
|
|
memcpy(buffer, str, bufferSize);
|
|
|
|
return buffer;
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
2014-08-28 22:19:56 +08:00
|
|
|
inline FILE* TempFile(char *filename) {
|
|
|
|
#if _MSC_VER
|
2014-08-11 22:26:45 +08:00
|
|
|
filename = tmpnam(filename);
|
2014-06-20 16:08:58 +08:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
// For Visual Studio, tmpnam() adds a backslash in front. Remove it.
|
|
|
|
if (filename[0] == '\\')
|
|
|
|
for (int i = 0; filename[i] != '\0'; i++)
|
|
|
|
filename[i] = filename[i + 1];
|
2014-08-28 22:19:56 +08:00
|
|
|
|
|
|
|
return fopen(filename, "wb");
|
|
|
|
#else
|
|
|
|
strcpy(filename, "/tmp/fileXXXXXX");
|
|
|
|
int fd = mkstemp(filename);
|
|
|
|
return fdopen(fd, "w");
|
|
|
|
#endif
|
2014-06-20 16:08:58 +08:00
|
|
|
}
|
|
|
|
|
2014-07-13 13:16:03 +08:00
|
|
|
// Use exception for catching assert
|
|
|
|
#if _MSC_VER
|
|
|
|
#pragma warning(disable : 4127)
|
|
|
|
#endif
|
|
|
|
|
2014-08-31 11:08:33 +02:00
|
|
|
class AssertException : public std::logic_error {
|
2014-07-13 13:16:03 +08:00
|
|
|
public:
|
2014-08-31 11:08:33 +02:00
|
|
|
AssertException(const char* w) : std::logic_error(w) {}
|
2014-07-13 13:16:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))
|
|
|
|
|
2014-09-03 13:27:43 +08:00
|
|
|
class Random {
|
|
|
|
public:
|
|
|
|
Random(unsigned seed = 0) : mSeed(seed) {}
|
|
|
|
|
|
|
|
unsigned operator()() {
|
|
|
|
mSeed = 214013 * mSeed + 2531011;
|
|
|
|
return mSeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned mSeed;
|
|
|
|
};
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
#endif // UNITTEST_H_
|