unittest.h 1.61 KB
Newer Older
1 2 3
#ifndef UNITTEST_H_
#define UNITTEST_H_

Milo Yip's avatar
Milo Yip committed
4 5 6 7 8 9

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

10 11 12 13 14 15
#ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#pragma warning(disable : 4996) // 'function': was declared deprecated
#endif

16 17 18 19
#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
20
#pragma GCC diagnostic ignored "-Weffc++"
Milo Yip's avatar
Milo Yip committed
21 22
#endif

23 24
#include "gtest/gtest.h"

25
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
Milo Yip's avatar
Milo Yip committed
26 27 28
#pragma GCC diagnostic pop
#endif

29
template <typename Ch>
30
inline unsigned StrLen(const Ch* s) {
31 32
	const Ch* p = s;
	while (*p) p++;
33
	return unsigned(p - s);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
}

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

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

50
inline void TempFilename(char *filename) {
Milo Yip's avatar
Milo Yip committed
51
	filename = tmpnam(filename);
52 53 54 55 56 57 58

	// 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];
}

59
#endif // UNITTEST_H_