unittest.h 3.81 KB
Newer Older
1 2 3
// 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.
4
//
5 6
// 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
7
//
8
// http://opensource.org/licenses/MIT
9
//
10 11 12 13
// 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.
14

15 16 17
#ifndef UNITTEST_H_
#define UNITTEST_H_

Milo Yip's avatar
Milo Yip committed
18 19
// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
#ifndef __STDC_CONSTANT_MACROS
Milo Yip's avatar
Milo Yip committed
20 21
#ifdef __clang__
#pragma GCC diagnostic push
22
#if __has_warning("-Wreserved-id-macro")
Milo Yip's avatar
Milo Yip committed
23 24
#pragma GCC diagnostic ignored "-Wreserved-id-macro"
#endif
25
#endif
Milo Yip's avatar
Milo Yip committed
26

Milo Yip's avatar
Milo Yip committed
27
#  define __STDC_CONSTANT_MACROS 1 // required by C++ standard
Milo Yip's avatar
Milo Yip committed
28 29 30 31

#ifdef __clang__
#pragma GCC diagnostic pop
#endif
Milo Yip's avatar
Milo Yip committed
32 33
#endif

34 35 36 37 38 39
#ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#pragma warning(disable : 4996) // 'function': was declared deprecated
#endif

40 41 42 43
#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
44
#pragma GCC diagnostic ignored "-Weffc++"
Milo Yip's avatar
Milo Yip committed
45 46
#endif

47
#include "gtest/gtest.h"
48
#include <stdexcept>
49

50
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
Milo Yip's avatar
Milo Yip committed
51 52 53
#pragma GCC diagnostic pop
#endif

Milo Yip's avatar
Milo Yip committed
54 55 56 57 58
#ifdef __clang__
// All TEST() macro generated this warning, disable globally
#pragma GCC diagnostic ignored "-Wglobal-constructors"
#endif

59
template <typename Ch>
60
inline unsigned StrLen(const Ch* s) {
61 62 63
    const Ch* p = s;
    while (*p) p++;
    return unsigned(p - s);
64 65 66 67
}

template<typename Ch>
inline int StrCmp(const Ch* s1, const Ch* s2) {
68
    while(*s1 && (*s1 == *s2)) { s1++; s2++; }
Milo Yip's avatar
Milo Yip committed
69
    return static_cast<unsigned>(*s1) < static_cast<unsigned>(*s2) ? -1 : static_cast<unsigned>(*s1) > static_cast<unsigned>(*s2);
70 71 72 73
}

template <typename Ch>
inline Ch* StrDup(const Ch* str) {
74
    size_t bufferSize = sizeof(Ch) * (StrLen(str) + 1);
Milo Yip's avatar
Milo Yip committed
75
    Ch* buffer = static_cast<Ch*>(malloc(bufferSize));
76 77
    memcpy(buffer, str, bufferSize);
    return buffer;
78 79
}

80
inline FILE* TempFile(char *filename) {
Milo Yip's avatar
Milo Yip committed
81
#ifdef _MSC_VER
82
    filename = tmpnam(filename);
83

84 85 86 87
    // 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];
88 89 90 91 92 93 94
        
    return fopen(filename, "wb");
#else
    strcpy(filename, "/tmp/fileXXXXXX");
    int fd = mkstemp(filename);
    return fdopen(fd, "w");
#endif
95 96
}

97
// Use exception for catching assert
Milo Yip's avatar
Milo Yip committed
98
#ifdef _MSC_VER
99 100 101
#pragma warning(disable : 4127)
#endif

102 103 104 105 106 107 108
#ifdef __clang__
#pragma GCC diagnostic push
#if __has_warning("-Wdeprecated")
#pragma GCC diagnostic ignored "-Wdeprecated"
#endif
#endif

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) {}
Milo Yip's avatar
Milo Yip committed
113
    virtual ~AssertException() throw();
114 115
};

116 117 118 119
#ifdef __clang__
#pragma GCC diagnostic pop
#endif

120 121
#define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))

122 123 124 125 126 127 128 129 130 131 132 133 134
class Random {
public:
    Random(unsigned seed = 0) : mSeed(seed) {}

    unsigned operator()() {
        mSeed = 214013 * mSeed + 2531011;
        return mSeed;
    }

private:
    unsigned mSeed;
};

135
#endif // UNITTEST_H_