unittest.h 3.08 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 20 21 22 23

// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
#ifndef __STDC_CONSTANT_MACROS
#  define __STDC_CONSTANT_MACROS 1 // required by C++ standard
#endif

24 25 26 27 28 29
#ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#pragma warning(disable : 4996) // 'function': was declared deprecated
#endif

30 31 32 33
#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
34
#pragma GCC diagnostic ignored "-Weffc++"
Milo Yip's avatar
Milo Yip committed
35 36
#endif

37
#include "gtest/gtest.h"
38
#include <stdexcept>
39

40
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
Milo Yip's avatar
Milo Yip committed
41 42 43
#pragma GCC diagnostic pop
#endif

44
template <typename Ch>
45
inline unsigned StrLen(const Ch* s) {
46 47 48
    const Ch* p = s;
    while (*p) p++;
    return unsigned(p - s);
49 50 51 52
}

template<typename Ch>
inline int StrCmp(const Ch* s1, const Ch* s2) {
53 54
    while(*s1 && (*s1 == *s2)) { s1++; s2++; }
    return (unsigned)*s1 < (unsigned)*s2 ? -1 : (unsigned)*s1 > (unsigned)*s2;
55 56 57 58
}

template <typename Ch>
inline Ch* StrDup(const Ch* str) {
59 60 61 62
    size_t bufferSize = sizeof(Ch) * (StrLen(str) + 1);
    Ch* buffer = (Ch*)malloc(bufferSize);
    memcpy(buffer, str, bufferSize);
    return buffer;
63 64
}

65 66
inline FILE* TempFile(char *filename) {
#if _MSC_VER
67
    filename = tmpnam(filename);
68

69 70 71 72
    // 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];
73 74 75 76 77 78 79
        
    return fopen(filename, "wb");
#else
    strcpy(filename, "/tmp/fileXXXXXX");
    int fd = mkstemp(filename);
    return fdopen(fd, "w");
#endif
80 81
}

82 83 84 85 86
// Use exception for catching assert
#if _MSC_VER
#pragma warning(disable : 4127)
#endif

87
class AssertException : public std::logic_error {
88
public:
89
    AssertException(const char* w) : std::logic_error(w) {}
90 91 92 93
};

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

94 95 96 97 98 99 100 101 102 103 104 105 106
class Random {
public:
    Random(unsigned seed = 0) : mSeed(seed) {}

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

private:
    unsigned mSeed;
};

107
#endif // UNITTEST_H_