11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, Google Inc. 21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved. 31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without 51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are 61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met: 71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions of source code must retain the above copyright 91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer. 101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions in binary form must reproduce the above 111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer 121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the 131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution. 141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Neither the name of Google Inc. nor the names of its 151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from 161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission. 171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Utility functions and classes used by the Google C++ testing framework. 311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Author: wan@google.com (Zhanyong Wan) 331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This file contains purely Google Test's internal implementation. Please 351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DO NOT #INCLUDE IT IN A USER PROGRAM. 361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_ 381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_SRC_GTEST_INTERNAL_INL_H_ 391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// part of Google Test's implementation; otherwise it's undefined. 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if !GTEST_IMPLEMENTATION_ 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A user is trying to include this from his code - just say no. 4441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# error "gtest-internal-inl.h is part of Google Test's internal implementation." 4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# error "It must not be included except by Google Test itself." 461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_IMPLEMENTATION_ 471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifndef _WIN32_WCE 4941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <errno.h> 5041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // !_WIN32_WCE 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stddef.h> 5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <stdlib.h> // For strtoll/_strtoul64/malloc/free. 5341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <string.h> // For memmove. 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <algorithm> 561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string> 5741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <vector> 581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-port.h" 601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS 6241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <windows.h> // NOLINT 631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_OS_WINDOWS 641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest.h" // NOLINT 6641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-spi.h" 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing { 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Declares the flags. 711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We don't want the users to modify this flag in the code, but want 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test's own unit tests to be able to access it. Therefore we 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// declare it here as opposed to in gtest.h. 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_bool_(death_test_use_fork); 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The value of GetTestTypeId() as seen from within the Google Test 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// library. This is solely for testing GetTestTypeId(). 8141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ extern const TypeId kTestTypeIdInGoogleTest; 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Names of the flags (needed for parsing Google Test flags). 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests"; 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kBreakOnFailureFlag[] = "break_on_failure"; 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kCatchExceptionsFlag[] = "catch_exceptions"; 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kColorFlag[] = "color"; 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kFilterFlag[] = "filter"; 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kListTestsFlag[] = "list_tests"; 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kOutputFlag[] = "output"; 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kPrintTimeFlag[] = "print_time"; 9241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst char kRandomSeedFlag[] = "random_seed"; 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kRepeatFlag[] = "repeat"; 9441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst char kShuffleFlag[] = "shuffle"; 9541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst char kStackTraceDepthFlag[] = "stack_trace_depth"; 9641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst char kStreamResultToFlag[] = "stream_result_to"; 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kThrowOnFailureFlag[] = "throw_on_failure"; 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A valid random seed must be in [1, kMaxRandomSeed]. 10041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst int kMaxRandomSeed = 99999; 10141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 10241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// g_help_flag is true iff the --help flag or an equivalent form is 10341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// specified on the command line. 10441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ extern bool g_help_flag; 10541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 10641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the current time in milliseconds. 10741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ TimeInMillis GetTimeInMillis(); 10841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 10941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff Google Test should use colors in the output. 11041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool ShouldUseColor(bool stdout_is_tty); 11141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 11241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Formats the given time in milliseconds as seconds. 11341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms); 11441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 11541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Parses a string for an Int32 flag, in the form of "--flag=value". 11641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 11741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// On success, stores the value of the flag in *value, and returns 11841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// true. On failure, returns false without changing *value. 11941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool ParseInt32Flag( 12041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* str, const char* flag, Int32* value); 12141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 12241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns a random seed in range [1, kMaxRandomSeed] based on the 12341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// given --gtest_random_seed flag value. 12441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline int GetRandomSeedFromFlag(Int32 random_seed_flag) { 12541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const unsigned int raw_seed = (random_seed_flag == 0) ? 12641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<unsigned int>(GetTimeInMillis()) : 12741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<unsigned int>(random_seed_flag); 12841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 12941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Normalizes the actual seed to range [1, kMaxRandomSeed] such that 13041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // it's easy to type. 13141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int normalized_seed = 13241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<int>((raw_seed - 1U) % 13341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<unsigned int>(kMaxRandomSeed)) + 1; 13441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return normalized_seed; 13541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 13641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 13741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the first valid random seed after 'seed'. The behavior is 13841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// undefined if 'seed' is invalid. The seed after kMaxRandomSeed is 13941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// considered to be 1. 14041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline int GetNextRandomSeed(int seed) { 14141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed) 14241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Invalid random seed " << seed << " - must be in [1, " 14341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << kMaxRandomSeed << "]."; 14441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int next_seed = seed + 1; 14541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return (next_seed > kMaxRandomSeed) ? 1 : next_seed; 14641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 14741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class saves the values of all Google Test flags in its c'tor, and 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// restores them in its d'tor. 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass GTestFlagSaver { 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The c'tor. 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTestFlagSaver() { 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests); 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break_on_failure_ = GTEST_FLAG(break_on_failure); 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch_exceptions_ = GTEST_FLAG(catch_exceptions); 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania color_ = GTEST_FLAG(color); 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania death_test_style_ = GTEST_FLAG(death_test_style); 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania death_test_use_fork_ = GTEST_FLAG(death_test_use_fork); 1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania filter_ = GTEST_FLAG(filter); 1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal_run_death_test_ = GTEST_FLAG(internal_run_death_test); 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania list_tests_ = GTEST_FLAG(list_tests); 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania output_ = GTEST_FLAG(output); 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania print_time_ = GTEST_FLAG(print_time); 16541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot random_seed_ = GTEST_FLAG(random_seed); 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania repeat_ = GTEST_FLAG(repeat); 16741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot shuffle_ = GTEST_FLAG(shuffle); 16841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot stack_trace_depth_ = GTEST_FLAG(stack_trace_depth); 16941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot stream_result_to_ = GTEST_FLAG(stream_result_to); 1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania throw_on_failure_ = GTEST_FLAG(throw_on_failure); 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The d'tor is not virtual. DO NOT INHERIT FROM THIS CLASS. 1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~GTestFlagSaver() { 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_; 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(break_on_failure) = break_on_failure_; 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(catch_exceptions) = catch_exceptions_; 1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(color) = color_; 1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(death_test_style) = death_test_style_; 1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(death_test_use_fork) = death_test_use_fork_; 1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(filter) = filter_; 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(internal_run_death_test) = internal_run_death_test_; 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(list_tests) = list_tests_; 1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(output) = output_; 1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(print_time) = print_time_; 18641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_FLAG(random_seed) = random_seed_; 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(repeat) = repeat_; 18841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_FLAG(shuffle) = shuffle_; 18941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_FLAG(stack_trace_depth) = stack_trace_depth_; 19041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_FLAG(stream_result_to) = stream_result_to_; 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(throw_on_failure) = throw_on_failure_; 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Fields for saving the original values of flags. 1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool also_run_disabled_tests_; 1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool break_on_failure_; 1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool catch_exceptions_; 1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String color_; 1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String death_test_style_; 2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool death_test_use_fork_; 2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String filter_; 2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String internal_run_death_test_; 2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool list_tests_; 2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String output_; 2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool print_time_; 2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool pretty_; 20741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::Int32 random_seed_; 2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::Int32 repeat_; 20941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool shuffle_; 21041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::Int32 stack_trace_depth_; 21141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String stream_result_to_; 2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool throw_on_failure_; 2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} GTEST_ATTRIBUTE_UNUSED_; 2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a Unicode code point to a narrow string in UTF-8 encoding. 2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// code_point parameter is of type UInt32 because wchar_t may not be 2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// wide enough to contain a code point. 2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The output buffer str must containt at least 32 characters. 2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The function returns the address of the output buffer. 2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the code_point is not a valid Unicode code point 2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output 2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// as '(Invalid Unicode 0xXXXXXXXX)'. 22341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ char* CodePointToUtf8(UInt32 code_point, char* str); 2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a wide string to a narrow string in UTF-8 encoding. 2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The wide string is assumed to have the following encoding: 2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS) 2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UTF-32 if sizeof(wchar_t) == 4 (on Linux) 2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parameter str points to a null-terminated wide string. 2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parameter num_chars may additionally limit the number 2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of wchar_t characters processed. -1 is used when the entire string 2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// should be processed. 2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the string contains code points that are not valid Unicode code points 2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output 2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding 2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and contains invalid UTF-16 surrogate pairs, values in those pairs 2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// will be encoded as individual Unicode characters from Basic Normal Plane. 23841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ String WideStringToUtf8(const wchar_t* str, int num_chars); 2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file 2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// if the variable is present. If a file already exists at this location, this 2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// function will write over it. If the variable is present, but the file cannot 2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be created, prints an error and exits. 2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid WriteToShardStatusFileIfNeeded(); 2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Checks whether sharding is enabled by examining the relevant 2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// environment variable values. If the variables are present, 2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// but inconsistent (e.g., shard_index >= total_shards), prints 2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// an error and exits. If in_subprocess_for_death_test, sharding is 2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// disabled because it must only be applied to the original test 2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// process. Otherwise, we could filter out death tests we intended to execute. 25241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool ShouldShard(const char* total_shards_str, 25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* shard_index_str, 25441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool in_subprocess_for_death_test); 2551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses the environment variable var as an Int32. If it is unset, 2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns default_val. If it is not an Int32, prints an error and 2581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and aborts. 25941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val); 2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Given the total number of shards, the shard index, and the test id, 2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns true iff the test should be run on this shard. The test id is 2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// some arbitrary but unique non-negative integer assigned to each test 2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// method. Assumes that 0 <= shard_index < total_shards. 26541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool ShouldRunTestOnShard( 26641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int total_shards, int shard_index, int test_id); 26741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 26841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// STL container utilities. 26941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 27041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the number of elements in the given container that satisfy 27141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the given predicate. 27241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class Container, typename Predicate> 27341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline int CountIf(const Container& c, Predicate predicate) { 27441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Implemented as an explicit loop since std::count_if() in libCstd on 27541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Solaris has a non-standard signature. 27641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int count = 0; 27741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) { 27841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (predicate(*it)) 27941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ++count; 2801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return count; 28241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 2831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 28441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Applies a function/functor to each element in the container. 28541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class Container, typename Functor> 28641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid ForEach(const Container& c, Functor functor) { 28741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::for_each(c.begin(), c.end(), functor); 28841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 2891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th element of the vector, or default_value if i is not 29141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// in range [0, v.size()). 29241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename E> 29341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline E GetElementOr(const std::vector<E>& v, int i, E default_value) { 29441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i]; 29541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 2961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 29741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Performs an in-place shuffle of a range of the vector's elements. 29841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 'begin' and 'end' are element indices as an STL-style range; 29941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// i.e. [begin, end) are shuffled, where 'end' == size() means to 30041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// shuffle to the end of the vector. 30141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename E> 30241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid ShuffleRange(internal::Random* random, int begin, int end, 30341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<E>* v) { 30441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int size = static_cast<int>(v->size()); 30541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(0 <= begin && begin <= size) 30641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Invalid shuffle range start " << begin << ": must be in range [0, " 30741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << size << "]."; 30841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(begin <= end && end <= size) 30941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Invalid shuffle range finish " << end << ": must be in range [" 31041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << begin << ", " << size << "]."; 31141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 31241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fisher-Yates shuffle, from 31341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle 31441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int range_width = end - begin; range_width >= 2; range_width--) { 31541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int last_in_range = begin + range_width - 1; 31641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int selected = begin + random->Generate(range_width); 31741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::swap((*v)[selected], (*v)[last_in_range]); 3181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 31941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 32141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Performs an in-place shuffle of the vector's elements. 3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename E> 32341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline void Shuffle(internal::Random* random, std::vector<E>* v) { 32441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ShuffleRange(random, 0, static_cast<int>(v->size()), v); 3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A function for deleting an object. Handy for being used as a 3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// functor. 3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 33041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic void Delete(T* x) { 3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania delete x; 3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A predicate that checks the key of a TestProperty against a known key. 3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestPropertyKeyIs is copyable. 3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestPropertyKeyIs { 3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructor. 3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TestPropertyKeyIs has NO default constructor. 3421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit TestPropertyKeyIs(const char* key) 3431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : key_(key) {} 3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the test name of test property matches on key_. 3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator()(const TestProperty& test_property) const { 3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String(test_property.key()).Compare(key_) == 0; 3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String key_; 3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Class UnitTestOptions. 3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class contains functions for processing options the user 3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// specifies when running the tests. It has only static members. 3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In most cases, the user can specify an option using either an 3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// environment variable or a command line flag. E.g. you can set the 3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test filter using either GTEST_FILTER or --gtest_filter. If both 3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the variable and the flag are present, the latter overrides the 3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// former. 36441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ UnitTestOptions { 3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Functions for processing the gtest_output flag. 3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the output format, or "" for normal printed output. 3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String GetOutputFormat(); 3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the absolute path of the requested output file, or the 3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // default (test_detail.xml in the original working directory) if 3731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // none was explicitly specified. 3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String GetAbsolutePathToOutputFile(); 3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Functions for processing the gtest_filter flag. 3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the wildcard pattern matches the string. The 3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // first ':' or '\0' character in pattern marks the end of it. 3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This recursive algorithm isn't very efficient, but is clear and 3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // works well enough for matching test names, which are short. 3831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool PatternMatchesString(const char *pattern, const char *str); 3841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the user-specified filter matches the test case 3861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // name and the test name. 3871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool FilterMatchesTest(const String &test_case_name, 3881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String &test_name); 3891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS 3911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Function for supporting the gtest_catch_exception flag. 3921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the 3941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. 3951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This function is useful as an __except condition. 3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static int GTestShouldProcessSEH(DWORD exception_code); 3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_OS_WINDOWS 3981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true if "name" matches the ':' separated list of glob-style 4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // filters in "filter". 4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool MatchesFilter(const String& name, const char* filter); 4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current application's name, removing directory path if that 4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is present. Used by UnitTestOptions::GetOutputFile. 40641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ FilePath GetCurrentExecutableName(); 4071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The role interface for getting the OS stack trace as a string. 4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass OsStackTraceGetterInterface { 4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 4111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania OsStackTraceGetterInterface() {} 4121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~OsStackTraceGetterInterface() {} 4131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the current OS stack trace as a String. Parameters: 4151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // max_depth - the maximum number of stack frames to be included 4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // in the trace. 4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // skip_count - the number of top frames to be skipped; doesn't count 4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // against max_depth. 4201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual String CurrentStackTrace(int max_depth, int skip_count) = 0; 4211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // UponLeavingGTest() should be called immediately before Google Test calls 4231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // user code. It saves some information about the current stack that 4241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // CurrentStackTrace() will use to find and hide Google Test stack frames. 4251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void UponLeavingGTest() = 0; 4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface); 4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A working implementation of the OsStackTraceGetterInterface interface. 4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass OsStackTraceGetter : public OsStackTraceGetterInterface { 4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 43441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot OsStackTraceGetter() : caller_frame_(NULL) {} 4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual String CurrentStackTrace(int max_depth, int skip_count); 4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void UponLeavingGTest(); 4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This string is inserted in place of stack frames that are part of 4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Google Test's implementation. 4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const char* const kElidedFramesMarker; 4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Mutex mutex_; // protects all internal state 4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We save the stack frame below the frame that calls user code. 4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We do this because the address of the frame immediately below 4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the user code changes between the call to UponLeavingGTest() 4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // and any calls to CurrentStackTrace() from within the user code. 4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void* caller_frame_; 4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter); 4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Information about a Google Test trace point. 4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastruct TraceInfo { 4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* file; 4571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int line; 4581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String message; 4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is the default global test part result reporter used in UnitTestImpl. 4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class should only be used by UnitTestImpl. 4631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass DefaultGlobalTestPartResultReporter 4641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : public TestPartResultReporterInterface { 4651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 4661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test); 4671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Implements the TestPartResultReporterInterface. Reports the test part 4681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // result in the current test. 4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void ReportTestPartResult(const TestPartResult& result); 4701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 4721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTestImpl* const unit_test_; 4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter); 4751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is the default per thread test part result reporter used in 4781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UnitTestImpl. This class should only be used by UnitTestImpl. 4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass DefaultPerThreadTestPartResultReporter 4801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : public TestPartResultReporterInterface { 4811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 4821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test); 4831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Implements the TestPartResultReporterInterface. The implementation just 4841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // delegates to the current global test part result reporter of *unit_test_. 4851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void ReportTestPartResult(const TestPartResult& result); 4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 4881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTestImpl* const unit_test_; 4891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter); 4911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The private implementation of the UnitTest class. We don't protect 4941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the methods under a mutex, as this class is not accessible by a 4951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// user and the UnitTest class that delegates work to this class does 4961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// proper locking. 49741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ UnitTestImpl { 4981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 4991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit UnitTestImpl(UnitTest* parent); 5001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~UnitTestImpl(); 5011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // There are two different ways to register your own TestPartResultReporter. 5031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // You can register your own repoter to listen either only for test results 5041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // from the current thread or for results from all threads. 5051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // By default, each per-thread test result repoter just passes a new 5061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TestPartResult to the global test result reporter, which registers the 5071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test part result for the currently running test. 5081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the global test part result reporter. 5101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultReporterInterface* GetGlobalTestPartResultReporter(); 5111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets the global test part result reporter. 5131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void SetGlobalTestPartResultReporter( 5141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultReporterInterface* reporter); 5151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the test part result reporter for the current thread. 5171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread(); 5181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets the test part result reporter for the current thread. 5201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void SetTestPartResultReporterForCurrentThread( 5211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultReporterInterface* reporter); 5221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of successful test cases. 5241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int successful_test_case_count() const; 5251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of failed test cases. 5271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int failed_test_case_count() const; 5281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of all test cases. 5301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int total_test_case_count() const; 5311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of all test cases that contain at least one test 5331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // that should run. 5341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int test_case_to_run_count() const; 5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of successful tests. 5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int successful_test_count() const; 5381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of failed tests. 5401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int failed_test_count() const; 5411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of disabled tests. 5431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int disabled_test_count() const; 5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of all tests. 5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int total_test_count() const; 5471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the number of tests that should run. 5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int test_to_run_count() const; 5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the elapsed time, in milliseconds. 5521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TimeInMillis elapsed_time() const { return elapsed_time_; } 5531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the unit test passed (i.e. all test cases passed). 5551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool Passed() const { return !Failed(); } 5561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the unit test failed (i.e. some test case failed 5581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // or something outside of all tests failed). 5591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool Failed() const { 5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed(); 5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 56341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the i-th test case among all the test cases. i can range from 0 to 56441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // total_test_case_count() - 1. If i is not in that range, returns NULL. 56541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestCase* GetTestCase(int i) const { 56641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int index = GetElementOr(test_case_indices_, i, -1); 56741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return index < 0 ? NULL : test_cases_[i]; 56841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 5691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 57041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the i-th test case among all the test cases. i can range from 0 to 57141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // total_test_case_count() - 1. If i is not in that range, returns NULL. 57241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestCase* GetMutableTestCase(int i) { 57341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int index = GetElementOr(test_case_indices_, i, -1); 57441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return index < 0 ? NULL : test_cases_[index]; 5751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 5761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 57741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Provides access to the event listener list. 57841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListeners* listeners() { return &listeners_; } 57941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 58041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the TestResult for the test that's currently running, or 58141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the TestResult for the ad hoc test if no test is running. 58241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestResult* current_test_result(); 5831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 58441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the TestResult for the ad hoc test. 58541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; } 5861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets the OS stack trace getter. 5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Does nothing if the input and the current OS stack trace getter 5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // are the same; otherwise, deletes the old getter and makes the 5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // input the current getter. 5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter); 5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the current OS stack trace getter if it is not NULL; 5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // otherwise, creates an OsStackTraceGetter, makes it the current 5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // getter, and returns it. 5971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania OsStackTraceGetterInterface* os_stack_trace_getter(); 5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the current OS stack trace as a String. 6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The maximum number of stack frames to be included is specified by 6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the gtest_stack_trace_depth flag. The skip_count parameter 6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // specifies the number of top frames to be skipped, which doesn't 6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // count against the number of frames to be included. 6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // For example, if Foo() calls Bar(), which in turn calls 6071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // CurrentOsStackTraceExceptTop(1), Foo() will be included in the 6081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // trace but Bar() and CurrentOsStackTraceExceptTop() won't. 6091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String CurrentOsStackTraceExceptTop(int skip_count); 6101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Finds and returns a TestCase with the given name. If one doesn't 6121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // exist, creates one and returns it. 6131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Arguments: 6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test_case_name: name of the test case 61741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // type_param: the name of the test's type parameter, or NULL if 61841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // this is not a typed or a type-parameterized test. 6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // set_up_tc: pointer to the function that sets up the test case 6201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // tear_down_tc: pointer to the function that tears down the test case 6211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestCase* GetTestCase(const char* test_case_name, 62241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* type_param, 6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::SetUpTestCaseFunc set_up_tc, 6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::TearDownTestCaseFunc tear_down_tc); 6251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Adds a TestInfo to the unit test. 6271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Arguments: 6291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // set_up_tc: pointer to the function that sets up the test case 6311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // tear_down_tc: pointer to the function that tears down the test case 6321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test_info: the TestInfo object 6331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc, 6341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::TearDownTestCaseFunc tear_down_tc, 63541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestInfo* test_info) { 6361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // In order to support thread-safe death tests, we need to 6371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // remember the original working directory when the test program 6381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // was first invoked. We cannot do this in RUN_ALL_TESTS(), as 6391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the user may have changed the current directory before calling 6401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // RUN_ALL_TESTS(). Therefore we capture the current directory in 6411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // AddTestInfo(), which is called to register a TEST or TEST_F 6421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // before main() is reached. 6431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (original_working_dir_.IsEmpty()) { 6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania original_working_dir_.Set(FilePath::GetCurrentDir()); 64541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(!original_working_dir_.IsEmpty()) 64641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Failed to get the current working directory."; 6471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetTestCase(test_info->test_case_name(), 65041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_info->type_param(), 6511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania set_up_tc, 6521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania tear_down_tc)->AddTestInfo(test_info); 6531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST 6561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns ParameterizedTestCaseRegistry object used to keep track of 6571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value-parameterized tests and instantiate and register them. 6581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::ParameterizedTestCaseRegistry& parameterized_test_registry() { 6591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return parameterized_test_registry_; 6601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_PARAM_TEST 6621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets the TestCase object for the test that's currently running. 66441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void set_current_test_case(TestCase* a_current_test_case) { 66541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot current_test_case_ = a_current_test_case; 6661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets the TestInfo object for the test that's currently running. If 6691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // current_test_info is NULL, the assertion results will be stored in 6701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // ad_hoc_test_result_. 67141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void set_current_test_info(TestInfo* a_current_test_info) { 67241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot current_test_info_ = a_current_test_info; 6731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Registers all parameterized tests defined using TEST_P and 67641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // INSTANTIATE_TEST_CASE_P, creating regular tests for each test/parameter 67741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // combination. This method can be called more then once; it has guards 67841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // protecting from registering the tests more then once. If 67941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // value-parameterized tests are disabled, RegisterParameterizedTests is 68041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // present but does nothing. 6811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void RegisterParameterizedTests(); 6821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Runs all tests in this UnitTest object, prints the result, and 68441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // returns true if all tests are successful. If any exception is 68541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // thrown during a test, this test is considered to be failed, but 68641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the rest of the tests will still be run. 68741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool RunAllTests(); 68841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 68941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Clears the results of all tests, except the ad hoc tests. 69041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ClearNonAdHocTestResult() { 69141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ForEach(test_cases_, TestCase::ClearTestCaseResult); 69241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 69341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 69441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Clears the results of ad-hoc test assertions. 69541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ClearAdHocTestResult() { 6961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ad_hoc_test_result_.Clear(); 6971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania enum ReactionToSharding { 7001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania HONOR_SHARDING_PROTOCOL, 7011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania IGNORE_SHARDING_PROTOCOL 7021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }; 7031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Matches the full name of each test against the user-specified 7051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // filter to decide whether the test should run, then records the 7061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // result in each TestCase and TestInfo object. 7071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests 7081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // based on sharding variables in the environment. 7091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the number of tests that should run. 7101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int FilterTests(ReactionToSharding shard_tests); 7111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 71241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Prints the names of the tests matching the user-specified filter flag. 71341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ListTestsMatchingFilter(); 7141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestCase* current_test_case() const { return current_test_case_; } 7161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestInfo* current_test_info() { return current_test_info_; } 7171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestInfo* current_test_info() const { return current_test_info_; } 7181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 71941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the vector of environments that need to be set-up/torn-down 7201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // before/after the tests are run. 72141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<Environment*>& environments() { return environments_; } 7221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Getters for the per-thread Google Test trace stack. 72441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<TraceInfo>& gtest_trace_stack() { 72541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return *(gtest_trace_stack_.pointer()); 7261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 72741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const std::vector<TraceInfo>& gtest_trace_stack() const { 72841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return gtest_trace_stack_.get(); 7291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 7301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 73241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void InitDeathTestSubprocessControlInfo() { 73341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag()); 73441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 7351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns a pointer to the parsed --gtest_internal_run_death_test 7361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // flag, or NULL if that flag was not specified. 7371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This information is useful only in a death test child process. 73841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Must not be called before a call to InitGoogleTest. 7391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const InternalRunDeathTestFlag* internal_run_death_test_flag() const { 7401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return internal_run_death_test_flag_.get(); 7411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 7421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns a pointer to the current death test factory. 7441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::DeathTestFactory* death_test_factory() { 7451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return death_test_factory_.get(); 7461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 7471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 74841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void SuppressTestEventsIfInSubprocess(); 74941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 7501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania friend class ReplaceDeathTestFactory; 7511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 7521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 75341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Initializes the event listener performing XML output as specified by 75441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // UnitTestOptions. Must not be called before InitGoogleTest. 75541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ConfigureXmlOutput(); 75641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 75741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_ 75841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Initializes the event listener for streaming test results to a socket. 75941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Must not be called before InitGoogleTest. 76041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ConfigureStreamingOutput(); 76141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 76241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 76341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Performs initialization dependent upon flag values obtained in 76441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to 76541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest 76641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // this function is also called from RunAllTests. Since this function can be 76741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // called more than once, it has to be idempotent. 76841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void PostFlagParsingInit(); 76941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the random seed used at the start of the current test iteration. 77141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int random_seed() const { return random_seed_; } 77241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the random number generator. 77441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::Random* random() { return &random_; } 77541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Shuffles all test cases, and the tests within each test case, 77741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // making sure that death tests are still run first. 77841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ShuffleTests(); 77941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Restores the test cases and tests to their order before the first shuffle. 78141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void UnshuffleTests(); 78241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the value of GTEST_FLAG(catch_exceptions) at the moment 78441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // UnitTest::Run() starts. 78541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool catch_exceptions() const { return catch_exceptions_; } 78641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 7871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 7881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania friend class ::testing::UnitTest; 7891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 79041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Used by UnitTest::Run() to capture the state of 79141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // GTEST_FLAG(catch_exceptions) at the moment it starts. 79241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void set_catch_exceptions(bool value) { catch_exceptions_ = value; } 79341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 7941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The UnitTest object that owns this implementation object. 7951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest* const parent_; 7961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The working directory when the first TEST() or TEST_F() was 7981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // executed. 7991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::FilePath original_working_dir_; 8001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The default test part result reporters. 8021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_; 8031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DefaultPerThreadTestPartResultReporter 8041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default_per_thread_test_part_result_reporter_; 8051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Points to (but doesn't own) the global test part result reporter. 8071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultReporterInterface* global_test_part_result_repoter_; 8081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Protects read and write access to global_test_part_result_reporter_. 8101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::Mutex global_test_part_result_reporter_mutex_; 8111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Points to (but doesn't own) the per-thread test part result reporter. 8131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::ThreadLocal<TestPartResultReporterInterface*> 8141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania per_thread_test_part_result_reporter_; 8151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 81641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The vector of environments that need to be set-up/torn-down 81741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // before/after the tests are run. 81841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<Environment*> environments_; 81941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 82041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The vector of TestCases in their original order. It owns the 82141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // elements in the vector. 82241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<TestCase*> test_cases_; 8231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 82441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Provides a level of indirection for the test case list to allow 82541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // easy shuffling and restoring the test case order. The i-th 82641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // element of this vector is the index of the i-th test case in the 82741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // shuffled order. 82841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<int> test_case_indices_; 8291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST 8311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // ParameterizedTestRegistry object used to register value-parameterized 8321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // tests. 8331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::ParameterizedTestCaseRegistry parameterized_test_registry_; 8341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Indicates whether RegisterParameterizedTests() has been called already. 8361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool parameterized_tests_registered_; 8371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_PARAM_TEST 8381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 83941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Index of the last death test case registered. Initially -1. 84041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int last_death_test_case_; 8411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This points to the TestCase for the currently running test. It 8431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // changes as Google Test goes through one test case after another. 8441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // When no test is running, this is set to NULL and Google Test 84541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // stores assertion results in ad_hoc_test_result_. Initially NULL. 8461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestCase* current_test_case_; 8471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This points to the TestInfo for the currently running test. It 8491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // changes as Google Test goes through one test after another. When 8501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // no test is running, this is set to NULL and Google Test stores 8511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // assertion results in ad_hoc_test_result_. Initially NULL. 8521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestInfo* current_test_info_; 8531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Normally, a user only writes assertions inside a TEST or TEST_F, 8551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // or inside a function called by a TEST or TEST_F. Since Google 8561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Test keeps track of which test is current running, it can 8571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // associate such an assertion with the test it belongs to. 8581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 8591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // If an assertion is encountered when no TEST or TEST_F is running, 8601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Google Test attributes the assertion result to an imaginary "ad hoc" 8611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test, and records the result in ad_hoc_test_result_. 86241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestResult ad_hoc_test_result_; 8631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 86441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The list of event listeners that can be used to track events inside 86541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Google Test. 86641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListeners listeners_; 8671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The OS stack trace getter. Will be deleted when the UnitTest 8691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // object is destructed. By default, an OsStackTraceGetter is used, 8701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // but the user can set this field to use a custom getter if that is 8711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // desired. 8721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania OsStackTraceGetterInterface* os_stack_trace_getter_; 8731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 87441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // True iff PostFlagParsingInit() has been called. 87541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool post_flag_parse_init_performed_; 87641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 87741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The random number seed used at the beginning of the test run. 87841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int random_seed_; 87941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 88041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Our random number generator. 88141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::Random random_; 88241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 8831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // How long the test took to run, in milliseconds. 8841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TimeInMillis elapsed_time_; 8851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 8871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The decomposed components of the gtest_internal_run_death_test flag, 8881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // parsed when RUN_ALL_TESTS is called. 8891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_; 8901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_; 8911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 8921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A per-thread stack of traces created by the SCOPED_TRACE() macro. 89441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_; 89541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 89641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests() 89741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // starts. 89841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool catch_exceptions_; 8991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl); 9011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; // class UnitTestImpl 9021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Convenience function for accessing the global UnitTest 9041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementation object. 9051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline UnitTestImpl* GetUnitTestImpl() { 9061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return UnitTest::GetInstance()->impl(); 9071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 90941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_USES_SIMPLE_RE 91041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 9111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Internal helper functions for implementing the simple regular 9121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expression matcher. 91341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool IsInSet(char ch, const char* str); 91441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool IsAsciiDigit(char ch); 91541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool IsAsciiPunct(char ch); 91641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool IsRepeat(char ch); 91741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool IsAsciiWhiteSpace(char ch); 91841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool IsAsciiWordChar(char ch); 91941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool IsValidEscape(char ch); 92041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch); 92141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool ValidateRegex(const char* regex); 92241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str); 92341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool MatchRepetitionAndRegexAtHead( 9241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool escaped, char ch, char repeat, const char* regex, const char* str); 92541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str); 92641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 92741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_USES_SIMPLE_RE 9281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses the command line for Google Test flags, without initializing 9301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// other parts of Google Test. 93141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv); 93241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv); 9331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 9351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the message describing the last system error, regardless of the 9371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// platform. 93841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ String GetLastErrnoDescription(); 9391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 94041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 9411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Provides leak-safe Windows kernel handle ownership. 9421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass AutoHandle { 9431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 9441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania AutoHandle() : handle_(INVALID_HANDLE_VALUE) {} 9451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit AutoHandle(HANDLE handle) : handle_(handle) {} 9461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~AutoHandle() { Reset(); } 9481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania HANDLE Get() const { return handle_; } 9501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void Reset() { Reset(INVALID_HANDLE_VALUE); } 9511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void Reset(HANDLE handle) { 9521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (handle != handle_) { 9531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (handle_ != INVALID_HANDLE_VALUE) 9541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::CloseHandle(handle_); 9551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania handle_ = handle; 9561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 9601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania HANDLE handle_; 9611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle); 9631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 96441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 9651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Attempts to parse a string into a positive integer pointed to by the 9671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// number parameter. Returns true if that is possible. 9681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use 9691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// it here. 9701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename Integer> 9711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ParseNaturalNumber(const ::std::string& str, Integer* number) { 9721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Fail fast if the given string does not begin with a digit; 9731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // this bypasses strtoXXX's "optional leading whitespace and plus 9741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // or minus sign" semantics, which are undesirable here. 97541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (str.empty() || !IsDigit(str[0])) { 9761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return false; 9771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania errno = 0; 9791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania char* end; 9811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // BiggestConvertible is the largest integer type that system-provided 9821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // string-to-number conversion routines can return. 98341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 98441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS && !defined(__GNUC__) 98541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 98641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // MSVC and C++ Builder define __int64 instead of the standard long long. 9871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef unsigned __int64 BiggestConvertible; 9881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10); 98941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 99041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 99141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 9921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef unsigned long long BiggestConvertible; // NOLINT 9931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10); 99441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 99541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS && !defined(__GNUC__) 99641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 9971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool parse_success = *end == '\0' && errno == 0; 9981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(vladl@google.com): Convert this to compile time assertion when it is 10001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // available. 10011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed)); 10021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Integer result = static_cast<Integer>(parsed); 10041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (parse_success && static_cast<BiggestConvertible>(result) == parsed) { 10051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *number = result; 10061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 10071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 10081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return false; 10091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 10111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 101241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TestResult contains some private methods that should be hidden from 101341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Google Test user but are required for testing. This class allow our tests 101441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// to access them. 101541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 101641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This class is supplied only for the purpose of testing Google Test's own 101741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// constructs. Do not use it in user tests, either directly or indirectly. 101841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestResultAccessor { 101941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 102041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void RecordProperty(TestResult* test_result, 102141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestProperty& property) { 102241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_result->RecordProperty(property); 102341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 102441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 102541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void ClearTestPartResults(TestResult* test_result) { 102641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_result->ClearTestPartResults(); 102741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 102841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 102941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static const std::vector<testing::TestPartResult>& test_part_results( 103041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestResult& test_result) { 103141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_result.test_part_results(); 103241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 103341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 103441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 10351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 10361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 10371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_SRC_GTEST_INTERNAL_INL_H_ 1039