util.h revision beb69a876be85f28ccb203d2ec7afcb91a9d2173
1917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Copyright (c) 2011, Google Inc. 2917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// All rights reserved. 3917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// 4917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Redistribution and use in source and binary forms, with or without 5917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// modification, are permitted provided that the following conditions are 6917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// met: 7917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// 8917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// * Redistributions of source code must retain the above copyright 9917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// notice, this list of conditions and the following disclaimer. 10917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// * Redistributions in binary form must reproduce the above 11917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// copyright notice, this list of conditions and the following disclaimer 12917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// in the documentation and/or other materials provided with the 13917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// distribution. 14917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// * Neither the name of Google Inc. nor the names of its 15917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// contributors may be used to endorse or promote products derived from 16917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// this software without specific prior written permission. 17917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// 18917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// --- 30917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// 31917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Some generically useful utility routines that in google-land would 32917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// be their own projects. We make a shortened version here. 33917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 34917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifndef GFLAGS_UTIL_H_ 35917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define GFLAGS_UTIL_H_ 36917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 37917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#include <assert.h> 38917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#include <config.h> 39917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifdef HAVE_INTTYPES_H 40917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# include <inttypes.h> 41917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif 42917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#include <stdarg.h> // for va_* 43917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#include <stdlib.h> 44917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#include <stdio.h> 45917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#include <iostream> 46917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#include <string> 47917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifdef HAVE_SYS_STAT_H 48917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# include <sys/stat.h> 49917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif // for mkdir() 50917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 51917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein_START_GOOGLE_NAMESPACE_ 52917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 53917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// This is used for unittests for death-testing. It is defined in gflags.cc. 54917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteinextern GFLAGS_DLL_DECL void (*gflags_exitfunc)(int); 55917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 56917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Work properly if either strtoll or strtoq is on this system 57917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifdef HAVE_STRTOLL 58917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define strto64 strtoll 59917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define strtou64 strtoull 60917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#elif HAVE_STRTOQ 61917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define strto64 strtoq 62917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define strtou64 strtouq 63917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#else 64917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Neither strtoll nor strtoq are defined. I hope strtol works! 65917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define strto64 strtol 66917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define strtou64 strtoul 67917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif 68917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 69917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// If we have inttypes.h, it will have defined PRId32/etc for us. If 70917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// not, take our best guess. 71917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifndef PRId32 72917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define PRId32 "d" 73917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif 74917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifndef PRId64 75917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define PRId64 "lld" 76917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif 77917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifndef PRIu64 78917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein# define PRIu64 "llu" 79917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif 80917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 81917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteintypedef signed char int8; 82917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteintypedef unsigned char uint8; 83917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 84917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// -- utility macros --------------------------------------------------------- 85917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 86917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteintemplate <bool> struct CompileAssert {}; 87917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define COMPILE_ASSERT(expr, msg) \ 88917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 89917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 90917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Returns the number of elements in an array. 91917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define arraysize(arr) (sizeof(arr)/sizeof(*(arr))) 92917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 93917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 94917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// -- logging and testing --------------------------------------------------- 95917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 96917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// For now, we ignore the level for logging, and don't show *VLOG's at 97917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// all, except by hand-editing the lines below 98917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define LOG(level) std::cerr 99917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define VLOG(level) if (true) {} else std::cerr 100917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define DVLOG(level) if (true) {} else std::cerr 101917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 102917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// CHECK dies with a fatal error if condition is not true. It is *not* 103917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// controlled by NDEBUG, so the check will be executed regardless of 104917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// compilation mode. Therefore, it is safe to do things like: 105917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// CHECK(fp->Write(x) == 4) 106917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// We allow stream-like objects after this for debugging, but they're ignored. 107917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_TRUE(condition) \ 108917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (true) { \ 109917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (!(condition)) { \ 110917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "Check failed: %s\n", #condition); \ 111917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein exit(1); \ 112917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 113917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } else std::cerr << "" 114917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 115917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_OP(op, val1, val2) \ 116917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (true) { \ 117917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (!((val1) op (val2))) { \ 118917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \ 119917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein exit(1); \ 120917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 121917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } else std::cerr << "" 122917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 123917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_EQ(val1, val2) EXPECT_OP(==, val1, val2) 124917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_NE(val1, val2) EXPECT_OP(!=, val1, val2) 125917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_LE(val1, val2) EXPECT_OP(<=, val1, val2) 126917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_LT(val1, val2) EXPECT_OP(< , val1, val2) 127917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_GE(val1, val2) EXPECT_OP(>=, val1, val2) 128917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_GT(val1, val2) EXPECT_OP(> , val1, val2) 129917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_FALSE(cond) EXPECT_TRUE(!(cond)) 130917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 131917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// C99 declares isnan and isinf should be macros, so the #ifdef test 132917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// should be reliable everywhere. Of course, it's not, but these 133917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// are testing pertty marginal functionality anyway, so it's ok to 134917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// not-run them even in situations they might, with effort, be made to work. 135917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifdef isnan // Some compilers, like sun's for Solaris 10, don't define this 136917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_NAN(arg) \ 137917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein do { \ 138917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (!isnan(arg)) { \ 139917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "Check failed: isnan(%s)\n", #arg); \ 140917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein exit(1); \ 141917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 142917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } while (0) 143917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#else 144917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_NAN(arg) 145917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif 146917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 147917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#ifdef isinf // Some compilers, like sun's for Solaris 10, don't define this 148917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_INF(arg) \ 149917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein do { \ 150917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (!isinf(arg)) { \ 151917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "Check failed: isinf(%s)\n", #arg); \ 152917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein exit(1); \ 153917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 154917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } while (0) 155917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#else 156917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_INF(arg) 157917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif 158917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 159917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_DOUBLE_EQ(val1, val2) \ 160917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein do { \ 161917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (((val1) < (val2) - 0.001 || (val1) > (val2) + 0.001)) { \ 162917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "Check failed: %s == %s\n", #val1, #val2); \ 163917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein exit(1); \ 164917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 165917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } while (0) 166917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 167917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_STREQ(val1, val2) \ 168917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein do { \ 169917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (strcmp((val1), (val2)) != 0) { \ 170917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "Check failed: streq(%s, %s)\n", #val1, #val2); \ 171917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein exit(1); \ 172917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 173917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } while (0) 174917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 175917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Call this in a .cc file where you will later call RUN_ALL_TESTS in main(). 176917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define TEST_INIT \ 177917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein static std::vector<void (*)()> g_testlist; /* the tests to run */ \ 178917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein static int RUN_ALL_TESTS() { \ 179917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein std::vector<void (*)()>::const_iterator it; \ 180917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein for (it = g_testlist.begin(); it != g_testlist.end(); ++it) { \ 181917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein (*it)(); /* The test will error-exit if there's a problem. */ \ 182917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 183917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "\nPassed %d tests\n\nPASS\n", \ 184917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein static_cast<int>(g_testlist.size())); \ 185917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein return 0; \ 186917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } 187917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 188917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Note that this macro uses a FlagSaver to keep tests isolated. 189917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define TEST(a, b) \ 190917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein struct Test_##a##_##b { \ 191917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein Test_##a##_##b() { g_testlist.push_back(&Run); } \ 192917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein static void Run() { \ 193917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein FlagSaver fs; \ 194917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "Running test %s/%s\n", #a, #b); \ 195917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein RunTest(); \ 196917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 197917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein static void RunTest(); \ 198917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein }; \ 199917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein static Test_##a##_##b g_test_##a##_##b; \ 200917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein void Test_##a##_##b::RunTest() 201917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 202917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// This is a dummy class that eases the google->opensource transition. 203917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteinnamespace testing { 204917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteinclass Test {}; 205917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein} 206917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 207917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Call this in a .cc file where you will later call EXPECT_DEATH 208917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_DEATH_INIT \ 209917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein static bool g_called_exit; \ 210917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein static void CalledExit(int) { g_called_exit = true; } 211917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 212917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define EXPECT_DEATH(fn, msg) \ 213917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein do { \ 214917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein g_called_exit = false; \ 215917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein gflags_exitfunc = &CalledExit; \ 216917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fn; \ 217917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein gflags_exitfunc = &exit; /* set back to its default */ \ 218917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (!g_called_exit) { \ 219917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein fprintf(stderr, "Function didn't die (%s): %s\n", msg, #fn); \ 220917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein exit(1); \ 221917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } \ 222917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } while (0) 223917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 224917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#define GTEST_HAS_DEATH_TEST 1 225917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 226917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// -- path routines ---------------------------------------------------------- 227917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 228917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Tries to create the directory path as a temp-dir. If it fails, 229917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// changes path to some directory it *can* create. 230917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#if defined(__MINGW32__) 231357b3d9d0b6dccbfd56ae0a466494f6c2da21901Craig Silverstein#include <io.h> 232357b3d9d0b6dccbfd56ae0a466494f6c2da21901Craig Silversteininline void MakeTmpdir(std::string* path) { 233917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein // I had trouble creating a directory in /tmp from mingw 234917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein *path = "./gflags_unittest_testdir"; 235917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein mkdir(path->c_str()); // mingw has a weird one-arg mkdir 236357b3d9d0b6dccbfd56ae0a466494f6c2da21901Craig Silverstein} 237917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#elif defined(_MSC_VER) 238357b3d9d0b6dccbfd56ae0a466494f6c2da21901Craig Silverstein#include <direct.h> 239357b3d9d0b6dccbfd56ae0a466494f6c2da21901Craig Silversteininline void MakeTmpdir(std::string* path) { 240917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein char tmppath_buffer[1024]; 241917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein int tmppath_len = GetTempPathA(sizeof(tmppath_buffer), tmppath_buffer); 242917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein assert(tmppath_len > 0 && tmppath_len < sizeof(tmppath_buffer)); 243917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein assert(tmppath_buffer[tmppath_len - 1] == '\\'); // API guarantees it 244917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein *path = std::string(tmppath_buffer) + "gflags_unittest_testdir"; 245917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein _mkdir(path->c_str()); 246357b3d9d0b6dccbfd56ae0a466494f6c2da21901Craig Silverstein} 247917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#else 248357b3d9d0b6dccbfd56ae0a466494f6c2da21901Craig Silversteininline void MakeTmpdir(std::string* path) { 249917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein mkdir(path->c_str(), 0755); 250917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein} 251357b3d9d0b6dccbfd56ae0a466494f6c2da21901Craig Silverstein#endif 252917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 253917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// -- string routines -------------------------------------------------------- 254917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 255917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteininline void InternalStringPrintf(std::string* output, const char* format, 256917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_list ap) { 257917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein char space[128]; // try a small buffer and hope it fits 258917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 259917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein // It's possible for methods that use a va_list to invalidate 260917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein // the data in it upon use. The fix is to make a copy 261917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein // of the structure before using it and use that copy instead. 262917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_list backup_ap; 263917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_copy(backup_ap, ap); 264917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein int bytes_written = vsnprintf(space, sizeof(space), format, backup_ap); 265917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_end(backup_ap); 266917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 267917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if ((bytes_written >= 0) && (bytes_written < sizeof(space))) { 268917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein output->append(space, bytes_written); 269917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein return; 270917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } 271917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 272917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein // Repeatedly increase buffer size until it fits. 273917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein int length = sizeof(space); 274917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein while (true) { 275917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if (bytes_written < 0) { 276917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein // Older snprintf() behavior. :-( Just try doubling the buffer size 277917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein length *= 2; 278917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } else { 279917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein // We need exactly "bytes_written+1" characters 280917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein length = bytes_written+1; 281917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } 282917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein char* buf = new char[length]; 283917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 284917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein // Restore the va_list before we use it again 285917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_copy(backup_ap, ap); 286917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein bytes_written = vsnprintf(buf, length, format, backup_ap); 287917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_end(backup_ap); 288917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 289917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein if ((bytes_written >= 0) && (bytes_written < length)) { 290beb69a876be85f28ccb203d2ec7afcb91a9d2173Craig Silverstein output->append(buf, bytes_written); 291917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein delete[] buf; 292917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein return; 293917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } 294917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein delete[] buf; 295917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein } 296917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein} 297917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 298917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein// Clears output before writing to it. 299917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteininline void SStringPrintf(std::string* output, const char* format, ...) { 300917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_list ap; 301917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_start(ap, format); 302917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein output->clear(); 303917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein InternalStringPrintf(output, format, ap); 304917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_end(ap); 305917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein} 306917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 307917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteininline void StringAppendF(std::string* output, const char* format, ...) { 308917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_list ap; 309917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_start(ap, format); 310917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein InternalStringPrintf(output, format, ap); 311917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_end(ap); 312917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein} 313917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 314917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silversteininline std::string StringPrintf(const char* format, ...) { 315917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_list ap; 316917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_start(ap, format); 317917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein std::string output; 318917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein InternalStringPrintf(&output, format, ap); 319917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein va_end(ap); 320917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein return output; 321917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein} 322917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 323917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein_END_GOOGLE_NAMESPACE_ 324917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein 325917f4e7bf3c36b4c56a77faca639758d3aca2a92Craig Silverstein#endif // GFLAGS_UTIL_H_ 326