1233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2008, Google Inc.
2233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
3233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
4233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
5233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
6233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
7233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
8233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
9233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
10233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
11233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
12233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
13233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
14233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
15233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
16233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
17233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
18233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
30233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: mheule@google.com (Markus Heule)
31233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
32233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google C++ Testing Framework (Google Test)
33233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
34233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Sometimes it's desirable to build Google Test by compiling a single file.
35233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This file serves this purpose.
36233d2500723e5594f3e7c70896ffeeef32b9c950ywan
37233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This line ensures that gtest.h can be compiled on its own, even
38233d2500723e5594f3e7c70896ffeeef32b9c950ywan// when it's fused.
39233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include "gtest/gtest.h"
40233d2500723e5594f3e7c70896ffeeef32b9c950ywan
41233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The following lines pull in the real gtest *.cc files.
42233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2005, Google Inc.
43233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
44233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
45233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
46233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
47233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
48233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
49233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
50233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
51233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
52233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
53233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
54233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
55233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
56233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
57233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
58233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
59233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
60233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
61233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
62233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
63233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
64233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
65233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
71233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: wan@google.com (Zhanyong Wan)
72233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
73233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The Google C++ Testing Framework (Google Test)
74233d2500723e5594f3e7c70896ffeeef32b9c950ywan
75233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2007, Google Inc.
76233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
77233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
78233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
79233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
80233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
81233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
82233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
83233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
84233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
85233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
86233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
87233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
88233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
89233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
90233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
91233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
92233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
93233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
94233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
95233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
96233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
98233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
99233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
100233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
101233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
102233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
104233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: wan@google.com (Zhanyong Wan)
105233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
106233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Utilities for testing Google Test itself and code that uses Google Test
107233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (e.g. frameworks built on top of Google Test).
108233d2500723e5594f3e7c70896ffeeef32b9c950ywan
109233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_
110233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_
111233d2500723e5594f3e7c70896ffeeef32b9c950ywan
112233d2500723e5594f3e7c70896ffeeef32b9c950ywan
113233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
114233d2500723e5594f3e7c70896ffeeef32b9c950ywan
115233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This helper class can be used to mock out Google Test failure reporting
116233d2500723e5594f3e7c70896ffeeef32b9c950ywan// so that we can test Google Test or code that builds on Google Test.
117233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
118233d2500723e5594f3e7c70896ffeeef32b9c950ywan// An object of this class appends a TestPartResult object to the
119233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TestPartResultArray object given in the constructor whenever a Google Test
120233d2500723e5594f3e7c70896ffeeef32b9c950ywan// failure is reported. It can either intercept only failures that are
121233d2500723e5594f3e7c70896ffeeef32b9c950ywan// generated in the same thread that created this object or it can intercept
122233d2500723e5594f3e7c70896ffeeef32b9c950ywan// all generated failures. The scope of this mock object can be controlled with
123233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the second argument to the two arguments constructor.
124233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass GTEST_API_ ScopedFakeTestPartResultReporter
125233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : public TestPartResultReporterInterface {
126233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
127233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The two possible mocking modes of this object.
128233d2500723e5594f3e7c70896ffeeef32b9c950ywan  enum InterceptMode {
129233d2500723e5594f3e7c70896ffeeef32b9c950ywan    INTERCEPT_ONLY_CURRENT_THREAD,  // Intercepts only thread local failures.
130233d2500723e5594f3e7c70896ffeeef32b9c950ywan    INTERCEPT_ALL_THREADS           // Intercepts all failures.
131233d2500723e5594f3e7c70896ffeeef32b9c950ywan  };
132233d2500723e5594f3e7c70896ffeeef32b9c950ywan
133233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The c'tor sets this object as the test part result reporter used
134233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // by Google Test.  The 'result' parameter specifies where to report the
135233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // results. This reporter will only catch failures generated in the current
136233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // thread. DEPRECATED
137233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
138233d2500723e5594f3e7c70896ffeeef32b9c950ywan
139233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Same as above, but you can choose the interception scope of this object.
140233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
141233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   TestPartResultArray* result);
142233d2500723e5594f3e7c70896ffeeef32b9c950ywan
143233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The d'tor restores the previous test part result reporter.
144233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual ~ScopedFakeTestPartResultReporter();
145233d2500723e5594f3e7c70896ffeeef32b9c950ywan
146233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Appends the TestPartResult object to the TestPartResultArray
147233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // received in the constructor.
148233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
149233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This method is from the TestPartResultReporterInterface
150233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // interface.
151233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void ReportTestPartResult(const TestPartResult& result);
152233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
153233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void Init();
154233d2500723e5594f3e7c70896ffeeef32b9c950ywan
155233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const InterceptMode intercept_mode_;
156233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestPartResultReporterInterface* old_reporter_;
157233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestPartResultArray* const result_;
158233d2500723e5594f3e7c70896ffeeef32b9c950ywan
159233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
160233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
161233d2500723e5594f3e7c70896ffeeef32b9c950ywan
162233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
163233d2500723e5594f3e7c70896ffeeef32b9c950ywan
164233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A helper class for implementing EXPECT_FATAL_FAILURE() and
165233d2500723e5594f3e7c70896ffeeef32b9c950ywan// EXPECT_NONFATAL_FAILURE().  Its destructor verifies that the given
166233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TestPartResultArray contains exactly one failure that has the given
167233d2500723e5594f3e7c70896ffeeef32b9c950ywan// type and contains the given substring.  If that's not the case, a
168233d2500723e5594f3e7c70896ffeeef32b9c950ywan// non-fatal failure will be generated.
169233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass GTEST_API_ SingleFailureChecker {
170233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
171233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The constructor remembers the arguments.
172233d2500723e5594f3e7c70896ffeeef32b9c950ywan  SingleFailureChecker(const TestPartResultArray* results,
173233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       TestPartResult::Type type,
174233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       const string& substr);
175233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ~SingleFailureChecker();
176233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
177233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestPartResultArray* const results_;
178233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestPartResult::Type type_;
179233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const string substr_;
180233d2500723e5594f3e7c70896ffeeef32b9c950ywan
181233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
182233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
183233d2500723e5594f3e7c70896ffeeef32b9c950ywan
184233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
185233d2500723e5594f3e7c70896ffeeef32b9c950ywan
186233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
187233d2500723e5594f3e7c70896ffeeef32b9c950ywan
188233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A set of macros for testing Google Test assertions or code that's expected
189233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to generate Google Test fatal failures.  It verifies that the given
190233d2500723e5594f3e7c70896ffeeef32b9c950ywan// statement will cause exactly one fatal Google Test failure with 'substr'
191233d2500723e5594f3e7c70896ffeeef32b9c950ywan// being part of the failure message.
192233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
193233d2500723e5594f3e7c70896ffeeef32b9c950ywan// There are two different versions of this macro. EXPECT_FATAL_FAILURE only
194233d2500723e5594f3e7c70896ffeeef32b9c950ywan// affects and considers failures generated in the current thread and
195233d2500723e5594f3e7c70896ffeeef32b9c950ywan// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
196233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
197233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The verification of the assertion is done correctly even when the statement
198233d2500723e5594f3e7c70896ffeeef32b9c950ywan// throws an exception or aborts the current function.
199233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
200233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Known restrictions:
201233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   - 'statement' cannot reference local non-static variables or
202233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     non-static members of the current object.
203233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   - 'statement' cannot return a value.
204233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   - You cannot stream a failure message to this macro.
205233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
206233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Note that even though the implementations of the following two
207233d2500723e5594f3e7c70896ffeeef32b9c950ywan// macros are much alike, we cannot refactor them to use a common
208233d2500723e5594f3e7c70896ffeeef32b9c950ywan// helper macro, due to some peculiarity in how the preprocessor
209233d2500723e5594f3e7c70896ffeeef32b9c950ywan// works.  The AcceptsMacroThatExpandsToUnprotectedComma test in
210233d2500723e5594f3e7c70896ffeeef32b9c950ywan// gtest_unittest.cc will fail to compile if we do that.
211233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define EXPECT_FATAL_FAILURE(statement, substr) \
212233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do { \
213233d2500723e5594f3e7c70896ffeeef32b9c950ywan    class GTestExpectFatalFailureHelper {\
214233d2500723e5594f3e7c70896ffeeef32b9c950ywan     public:\
215233d2500723e5594f3e7c70896ffeeef32b9c950ywan      static void Execute() { statement; }\
216233d2500723e5594f3e7c70896ffeeef32b9c950ywan    };\
217233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::testing::TestPartResultArray gtest_failures;\
218233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::testing::internal::SingleFailureChecker gtest_checker(\
219233d2500723e5594f3e7c70896ffeeef32b9c950ywan        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
220233d2500723e5594f3e7c70896ffeeef32b9c950ywan    {\
221233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
222233d2500723e5594f3e7c70896ffeeef32b9c950ywan          ::testing::ScopedFakeTestPartResultReporter:: \
223233d2500723e5594f3e7c70896ffeeef32b9c950ywan          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
224233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTestExpectFatalFailureHelper::Execute();\
225233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }\
226233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (::testing::internal::AlwaysFalse())
227233d2500723e5594f3e7c70896ffeeef32b9c950ywan
228233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
229233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do { \
230233d2500723e5594f3e7c70896ffeeef32b9c950ywan    class GTestExpectFatalFailureHelper {\
231233d2500723e5594f3e7c70896ffeeef32b9c950ywan     public:\
232233d2500723e5594f3e7c70896ffeeef32b9c950ywan      static void Execute() { statement; }\
233233d2500723e5594f3e7c70896ffeeef32b9c950ywan    };\
234233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::testing::TestPartResultArray gtest_failures;\
235233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::testing::internal::SingleFailureChecker gtest_checker(\
236233d2500723e5594f3e7c70896ffeeef32b9c950ywan        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
237233d2500723e5594f3e7c70896ffeeef32b9c950ywan    {\
238233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
239233d2500723e5594f3e7c70896ffeeef32b9c950ywan          ::testing::ScopedFakeTestPartResultReporter:: \
240233d2500723e5594f3e7c70896ffeeef32b9c950ywan          INTERCEPT_ALL_THREADS, &gtest_failures);\
241233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTestExpectFatalFailureHelper::Execute();\
242233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }\
243233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (::testing::internal::AlwaysFalse())
244233d2500723e5594f3e7c70896ffeeef32b9c950ywan
245233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A macro for testing Google Test assertions or code that's expected to
246233d2500723e5594f3e7c70896ffeeef32b9c950ywan// generate Google Test non-fatal failures.  It asserts that the given
247233d2500723e5594f3e7c70896ffeeef32b9c950ywan// statement will cause exactly one non-fatal Google Test failure with 'substr'
248233d2500723e5594f3e7c70896ffeeef32b9c950ywan// being part of the failure message.
249233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
250233d2500723e5594f3e7c70896ffeeef32b9c950ywan// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
251233d2500723e5594f3e7c70896ffeeef32b9c950ywan// affects and considers failures generated in the current thread and
252233d2500723e5594f3e7c70896ffeeef32b9c950ywan// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
253233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
254233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 'statement' is allowed to reference local variables and members of
255233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the current object.
256233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
257233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The verification of the assertion is done correctly even when the statement
258233d2500723e5594f3e7c70896ffeeef32b9c950ywan// throws an exception or aborts the current function.
259233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
260233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Known restrictions:
261233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   - You cannot stream a failure message to this macro.
262233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
263233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Note that even though the implementations of the following two
264233d2500723e5594f3e7c70896ffeeef32b9c950ywan// macros are much alike, we cannot refactor them to use a common
265233d2500723e5594f3e7c70896ffeeef32b9c950ywan// helper macro, due to some peculiarity in how the preprocessor
266233d2500723e5594f3e7c70896ffeeef32b9c950ywan// works.  If we do that, the code won't compile when the user gives
267233d2500723e5594f3e7c70896ffeeef32b9c950ywan// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
268233d2500723e5594f3e7c70896ffeeef32b9c950ywan// expands to code containing an unprotected comma.  The
269233d2500723e5594f3e7c70896ffeeef32b9c950ywan// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
270233d2500723e5594f3e7c70896ffeeef32b9c950ywan// catches that.
271233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
272233d2500723e5594f3e7c70896ffeeef32b9c950ywan// For the same reason, we have to write
273233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   if (::testing::internal::AlwaysTrue()) { statement; }
274233d2500723e5594f3e7c70896ffeeef32b9c950ywan// instead of
275233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
276233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to avoid an MSVC warning on unreachable code.
277233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define EXPECT_NONFATAL_FAILURE(statement, substr) \
278233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do {\
279233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::testing::TestPartResultArray gtest_failures;\
280233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::testing::internal::SingleFailureChecker gtest_checker(\
281233d2500723e5594f3e7c70896ffeeef32b9c950ywan        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
282233d2500723e5594f3e7c70896ffeeef32b9c950ywan        (substr));\
283233d2500723e5594f3e7c70896ffeeef32b9c950ywan    {\
284233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
285233d2500723e5594f3e7c70896ffeeef32b9c950ywan          ::testing::ScopedFakeTestPartResultReporter:: \
286233d2500723e5594f3e7c70896ffeeef32b9c950ywan          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
287233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (::testing::internal::AlwaysTrue()) { statement; }\
288233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }\
289233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (::testing::internal::AlwaysFalse())
290233d2500723e5594f3e7c70896ffeeef32b9c950ywan
291233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
292233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do {\
293233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::testing::TestPartResultArray gtest_failures;\
294233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::testing::internal::SingleFailureChecker gtest_checker(\
295233d2500723e5594f3e7c70896ffeeef32b9c950ywan        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
296233d2500723e5594f3e7c70896ffeeef32b9c950ywan        (substr));\
297233d2500723e5594f3e7c70896ffeeef32b9c950ywan    {\
298233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
299233d2500723e5594f3e7c70896ffeeef32b9c950ywan          ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
300233d2500723e5594f3e7c70896ffeeef32b9c950ywan          &gtest_failures);\
301233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (::testing::internal::AlwaysTrue()) { statement; }\
302233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }\
303233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (::testing::internal::AlwaysFalse())
304233d2500723e5594f3e7c70896ffeeef32b9c950ywan
305233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_INCLUDE_GTEST_GTEST_SPI_H_
306233d2500723e5594f3e7c70896ffeeef32b9c950ywan
307233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <ctype.h>
308233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <math.h>
309233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stdarg.h>
310233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stdio.h>
311233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stdlib.h>
312233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <time.h>
313233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <wchar.h>
314233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <wctype.h>
315233d2500723e5594f3e7c70896ffeeef32b9c950ywan
316233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <algorithm>
317233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <iomanip>
318233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <limits>
319233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <ostream>  // NOLINT
320233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <sstream>
321233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <vector>
322233d2500723e5594f3e7c70896ffeeef32b9c950ywan
323233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_LINUX
324233d2500723e5594f3e7c70896ffeeef32b9c950ywan
325233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(kenton@google.com): Use autoconf to detect availability of
326233d2500723e5594f3e7c70896ffeeef32b9c950ywan// gettimeofday().
327233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_HAS_GETTIMEOFDAY_ 1
328233d2500723e5594f3e7c70896ffeeef32b9c950ywan
329233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <fcntl.h>  // NOLINT
330233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <limits.h>  // NOLINT
331233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sched.h>  // NOLINT
332233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Declares vsnprintf().  This header is not available on Windows.
333233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <strings.h>  // NOLINT
334233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/mman.h>  // NOLINT
335233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/time.h>  // NOLINT
336233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <unistd.h>  // NOLINT
337233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <string>
338233d2500723e5594f3e7c70896ffeeef32b9c950ywan
339233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_SYMBIAN
340233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_HAS_GETTIMEOFDAY_ 1
341233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/time.h>  // NOLINT
342233d2500723e5594f3e7c70896ffeeef32b9c950ywan
343233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_ZOS
344233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_HAS_GETTIMEOFDAY_ 1
345233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/time.h>  // NOLINT
346233d2500723e5594f3e7c70896ffeeef32b9c950ywan
347233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On z/OS we additionally need strings.h for strcasecmp.
348233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <strings.h>  // NOLINT
349233d2500723e5594f3e7c70896ffeeef32b9c950ywan
350233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_WINDOWS_MOBILE  // We are on Windows CE.
351233d2500723e5594f3e7c70896ffeeef32b9c950ywan
352233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <windows.h>  // NOLINT
353233d2500723e5594f3e7c70896ffeeef32b9c950ywan
354233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_WINDOWS  // We are on Windows proper.
355233d2500723e5594f3e7c70896ffeeef32b9c950ywan
356233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <io.h>  // NOLINT
357233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/timeb.h>  // NOLINT
358233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/types.h>  // NOLINT
359233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/stat.h>  // NOLINT
360233d2500723e5594f3e7c70896ffeeef32b9c950ywan
361233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS_MINGW
362233d2500723e5594f3e7c70896ffeeef32b9c950ywan// MinGW has gettimeofday() but not _ftime64().
363233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(kenton@google.com): Use autoconf to detect availability of
364233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   gettimeofday().
365233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(kenton@google.com): There are other ways to get the time on
366233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   Windows, like GetTickCount() or GetSystemTimeAsFileTime().  MinGW
367233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   supports these.  consider using them instead.
368233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  define GTEST_HAS_GETTIMEOFDAY_ 1
369233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  include <sys/time.h>  // NOLINT
370233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS_MINGW
371233d2500723e5594f3e7c70896ffeeef32b9c950ywan
372233d2500723e5594f3e7c70896ffeeef32b9c950ywan// cpplint thinks that the header is already included, so we want to
373233d2500723e5594f3e7c70896ffeeef32b9c950ywan// silence it.
374233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <windows.h>  // NOLINT
375233d2500723e5594f3e7c70896ffeeef32b9c950ywan
376233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
377233d2500723e5594f3e7c70896ffeeef32b9c950ywan
378233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Assume other platforms have gettimeofday().
379233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(kenton@google.com): Use autoconf to detect availability of
380233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   gettimeofday().
381233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_HAS_GETTIMEOFDAY_ 1
382233d2500723e5594f3e7c70896ffeeef32b9c950ywan
383233d2500723e5594f3e7c70896ffeeef32b9c950ywan// cpplint thinks that the header is already included, so we want to
384233d2500723e5594f3e7c70896ffeeef32b9c950ywan// silence it.
385233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/time.h>  // NOLINT
386233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <unistd.h>  // NOLINT
387233d2500723e5594f3e7c70896ffeeef32b9c950ywan
388233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_LINUX
389233d2500723e5594f3e7c70896ffeeef32b9c950ywan
390233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_EXCEPTIONS
391233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <stdexcept>
392233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
393233d2500723e5594f3e7c70896ffeeef32b9c950ywan
394233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_CAN_STREAM_RESULTS_
395233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <arpa/inet.h>  // NOLINT
396233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <netdb.h>  // NOLINT
397233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
398233d2500723e5594f3e7c70896ffeeef32b9c950ywan
399233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Indicates that this translation unit is part of Google Test's
400233d2500723e5594f3e7c70896ffeeef32b9c950ywan// implementation.  It must come before gtest-internal-inl.h is
401233d2500723e5594f3e7c70896ffeeef32b9c950ywan// included, or there will be a compiler error.  This trick is to
402233d2500723e5594f3e7c70896ffeeef32b9c950ywan// prevent a user from accidentally including gtest-internal-inl.h in
403233d2500723e5594f3e7c70896ffeeef32b9c950ywan// his code.
404233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_IMPLEMENTATION_ 1
405233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2005, Google Inc.
406233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
407233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
408233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
409233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
410233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
411233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
412233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
413233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
414233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
415233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
416233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
417233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
418233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
419233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
420233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
421233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
422233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
423233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
424233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
425233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
426233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
427233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
428233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
429233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
430233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
431233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
432233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
433233d2500723e5594f3e7c70896ffeeef32b9c950ywan
434233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Utility functions and classes used by the Google C++ testing framework.
435233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
436233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: wan@google.com (Zhanyong Wan)
437233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
438233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This file contains purely Google Test's internal implementation.  Please
439233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DO NOT #INCLUDE IT IN A USER PROGRAM.
440233d2500723e5594f3e7c70896ffeeef32b9c950ywan
441233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
442233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_SRC_GTEST_INTERNAL_INL_H_
443233d2500723e5594f3e7c70896ffeeef32b9c950ywan
444233d2500723e5594f3e7c70896ffeeef32b9c950ywan// GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is
445233d2500723e5594f3e7c70896ffeeef32b9c950ywan// part of Google Test's implementation; otherwise it's undefined.
446233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if !GTEST_IMPLEMENTATION_
447233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A user is trying to include this from his code - just say no.
448233d2500723e5594f3e7c70896ffeeef32b9c950ywan# error "gtest-internal-inl.h is part of Google Test's internal implementation."
449233d2500723e5594f3e7c70896ffeeef32b9c950ywan# error "It must not be included except by Google Test itself."
450233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_IMPLEMENTATION_
451233d2500723e5594f3e7c70896ffeeef32b9c950ywan
452233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifndef _WIN32_WCE
453233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <errno.h>
454233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // !_WIN32_WCE
455233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stddef.h>
456233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stdlib.h>  // For strtoll/_strtoul64/malloc/free.
457233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <string.h>  // For memmove.
458233d2500723e5594f3e7c70896ffeeef32b9c950ywan
459233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <algorithm>
460233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <string>
461233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <vector>
462233d2500723e5594f3e7c70896ffeeef32b9c950ywan
463233d2500723e5594f3e7c70896ffeeef32b9c950ywan
464233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_CAN_STREAM_RESULTS_
465233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <arpa/inet.h>  // NOLINT
466233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <netdb.h>  // NOLINT
467233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
468233d2500723e5594f3e7c70896ffeeef32b9c950ywan
469233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
470233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <windows.h>  // NOLINT
471233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
472233d2500723e5594f3e7c70896ffeeef32b9c950ywan
473233d2500723e5594f3e7c70896ffeeef32b9c950ywan
474233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
475233d2500723e5594f3e7c70896ffeeef32b9c950ywan
476233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Declares the flags.
477233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
478233d2500723e5594f3e7c70896ffeeef32b9c950ywan// We don't want the users to modify this flag in the code, but want
479233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test's own unit tests to be able to access it. Therefore we
480233d2500723e5594f3e7c70896ffeeef32b9c950ywan// declare it here as opposed to in gtest.h.
481233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DECLARE_bool_(death_test_use_fork);
482233d2500723e5594f3e7c70896ffeeef32b9c950ywan
483233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
484233d2500723e5594f3e7c70896ffeeef32b9c950ywan
485233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The value of GetTestTypeId() as seen from within the Google Test
486233d2500723e5594f3e7c70896ffeeef32b9c950ywan// library.  This is solely for testing GetTestTypeId().
487233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;
488233d2500723e5594f3e7c70896ffeeef32b9c950ywan
489233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Names of the flags (needed for parsing Google Test flags).
490233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
491233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kBreakOnFailureFlag[] = "break_on_failure";
492233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kCatchExceptionsFlag[] = "catch_exceptions";
493233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kColorFlag[] = "color";
494233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kFilterFlag[] = "filter";
495233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kListTestsFlag[] = "list_tests";
496233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kOutputFlag[] = "output";
497233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kPrintTimeFlag[] = "print_time";
498233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kRandomSeedFlag[] = "random_seed";
499233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kRepeatFlag[] = "repeat";
500233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kShuffleFlag[] = "shuffle";
501233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kStackTraceDepthFlag[] = "stack_trace_depth";
502233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kStreamResultToFlag[] = "stream_result_to";
503233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kThrowOnFailureFlag[] = "throw_on_failure";
504233d2500723e5594f3e7c70896ffeeef32b9c950ywan
505233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A valid random seed must be in [1, kMaxRandomSeed].
506233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst int kMaxRandomSeed = 99999;
507233d2500723e5594f3e7c70896ffeeef32b9c950ywan
508233d2500723e5594f3e7c70896ffeeef32b9c950ywan// g_help_flag is true iff the --help flag or an equivalent form is
509233d2500723e5594f3e7c70896ffeeef32b9c950ywan// specified on the command line.
510233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ extern bool g_help_flag;
511233d2500723e5594f3e7c70896ffeeef32b9c950ywan
512233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current time in milliseconds.
513233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ TimeInMillis GetTimeInMillis();
514233d2500723e5594f3e7c70896ffeeef32b9c950ywan
515233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff Google Test should use colors in the output.
516233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool ShouldUseColor(bool stdout_is_tty);
517233d2500723e5594f3e7c70896ffeeef32b9c950ywan
518233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats the given time in milliseconds as seconds.
519233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
520233d2500723e5594f3e7c70896ffeeef32b9c950ywan
521233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts the given time in milliseconds to a date string in the ISO 8601
522233d2500723e5594f3e7c70896ffeeef32b9c950ywan// format, without the timezone information.  N.B.: due to the use the
523233d2500723e5594f3e7c70896ffeeef32b9c950ywan// non-reentrant localtime() function, this function is not thread safe.  Do
524233d2500723e5594f3e7c70896ffeeef32b9c950ywan// not use it in any code that can be called from multiple threads.
525233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms);
526233d2500723e5594f3e7c70896ffeeef32b9c950ywan
527233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses a string for an Int32 flag, in the form of "--flag=value".
528233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
529233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On success, stores the value of the flag in *value, and returns
530233d2500723e5594f3e7c70896ffeeef32b9c950ywan// true.  On failure, returns false without changing *value.
531233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool ParseInt32Flag(
532233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* str, const char* flag, Int32* value);
533233d2500723e5594f3e7c70896ffeeef32b9c950ywan
534233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns a random seed in range [1, kMaxRandomSeed] based on the
535233d2500723e5594f3e7c70896ffeeef32b9c950ywan// given --gtest_random_seed flag value.
536233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
537233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const unsigned int raw_seed = (random_seed_flag == 0) ?
538233d2500723e5594f3e7c70896ffeeef32b9c950ywan      static_cast<unsigned int>(GetTimeInMillis()) :
539233d2500723e5594f3e7c70896ffeeef32b9c950ywan      static_cast<unsigned int>(random_seed_flag);
540233d2500723e5594f3e7c70896ffeeef32b9c950ywan
541233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Normalizes the actual seed to range [1, kMaxRandomSeed] such that
542233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it's easy to type.
543233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int normalized_seed =
544233d2500723e5594f3e7c70896ffeeef32b9c950ywan      static_cast<int>((raw_seed - 1U) %
545233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       static_cast<unsigned int>(kMaxRandomSeed)) + 1;
546233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return normalized_seed;
547233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
548233d2500723e5594f3e7c70896ffeeef32b9c950ywan
549233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the first valid random seed after 'seed'.  The behavior is
550233d2500723e5594f3e7c70896ffeeef32b9c950ywan// undefined if 'seed' is invalid.  The seed after kMaxRandomSeed is
551233d2500723e5594f3e7c70896ffeeef32b9c950ywan// considered to be 1.
552233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline int GetNextRandomSeed(int seed) {
553233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
554233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Invalid random seed " << seed << " - must be in [1, "
555233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << kMaxRandomSeed << "].";
556233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int next_seed = seed + 1;
557233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
558233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
559233d2500723e5594f3e7c70896ffeeef32b9c950ywan
560233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This class saves the values of all Google Test flags in its c'tor, and
561233d2500723e5594f3e7c70896ffeeef32b9c950ywan// restores them in its d'tor.
562233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass GTestFlagSaver {
563233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
564233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The c'tor.
565233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTestFlagSaver() {
566233d2500723e5594f3e7c70896ffeeef32b9c950ywan    also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
567233d2500723e5594f3e7c70896ffeeef32b9c950ywan    break_on_failure_ = GTEST_FLAG(break_on_failure);
568233d2500723e5594f3e7c70896ffeeef32b9c950ywan    catch_exceptions_ = GTEST_FLAG(catch_exceptions);
569233d2500723e5594f3e7c70896ffeeef32b9c950ywan    color_ = GTEST_FLAG(color);
570233d2500723e5594f3e7c70896ffeeef32b9c950ywan    death_test_style_ = GTEST_FLAG(death_test_style);
571233d2500723e5594f3e7c70896ffeeef32b9c950ywan    death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
572233d2500723e5594f3e7c70896ffeeef32b9c950ywan    filter_ = GTEST_FLAG(filter);
573233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
574233d2500723e5594f3e7c70896ffeeef32b9c950ywan    list_tests_ = GTEST_FLAG(list_tests);
575233d2500723e5594f3e7c70896ffeeef32b9c950ywan    output_ = GTEST_FLAG(output);
576233d2500723e5594f3e7c70896ffeeef32b9c950ywan    print_time_ = GTEST_FLAG(print_time);
577233d2500723e5594f3e7c70896ffeeef32b9c950ywan    random_seed_ = GTEST_FLAG(random_seed);
578233d2500723e5594f3e7c70896ffeeef32b9c950ywan    repeat_ = GTEST_FLAG(repeat);
579233d2500723e5594f3e7c70896ffeeef32b9c950ywan    shuffle_ = GTEST_FLAG(shuffle);
580233d2500723e5594f3e7c70896ffeeef32b9c950ywan    stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);
581233d2500723e5594f3e7c70896ffeeef32b9c950ywan    stream_result_to_ = GTEST_FLAG(stream_result_to);
582233d2500723e5594f3e7c70896ffeeef32b9c950ywan    throw_on_failure_ = GTEST_FLAG(throw_on_failure);
583233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
584233d2500723e5594f3e7c70896ffeeef32b9c950ywan
585233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The d'tor is not virtual.  DO NOT INHERIT FROM THIS CLASS.
586233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ~GTestFlagSaver() {
587233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
588233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(break_on_failure) = break_on_failure_;
589233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(catch_exceptions) = catch_exceptions_;
590233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(color) = color_;
591233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(death_test_style) = death_test_style_;
592233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
593233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(filter) = filter_;
594233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
595233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(list_tests) = list_tests_;
596233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(output) = output_;
597233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(print_time) = print_time_;
598233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(random_seed) = random_seed_;
599233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(repeat) = repeat_;
600233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(shuffle) = shuffle_;
601233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
602233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(stream_result_to) = stream_result_to_;
603233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_FLAG(throw_on_failure) = throw_on_failure_;
604233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
605233d2500723e5594f3e7c70896ffeeef32b9c950ywan
606233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
607233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Fields for saving the original values of flags.
608233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool also_run_disabled_tests_;
609233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool break_on_failure_;
610233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool catch_exceptions_;
611233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string color_;
612233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string death_test_style_;
613233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool death_test_use_fork_;
614233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string filter_;
615233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string internal_run_death_test_;
616233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool list_tests_;
617233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string output_;
618233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool print_time_;
619233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::Int32 random_seed_;
620233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::Int32 repeat_;
621233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool shuffle_;
622233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::Int32 stack_trace_depth_;
623233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string stream_result_to_;
624233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool throw_on_failure_;
625233d2500723e5594f3e7c70896ffeeef32b9c950ywan} GTEST_ATTRIBUTE_UNUSED_;
626233d2500723e5594f3e7c70896ffeeef32b9c950ywan
627233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts a Unicode code point to a narrow string in UTF-8 encoding.
628233d2500723e5594f3e7c70896ffeeef32b9c950ywan// code_point parameter is of type UInt32 because wchar_t may not be
629233d2500723e5594f3e7c70896ffeeef32b9c950ywan// wide enough to contain a code point.
630233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If the code_point is not a valid Unicode code point
631233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted
632233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to "(Invalid Unicode 0xXXXXXXXX)".
633233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ std::string CodePointToUtf8(UInt32 code_point);
634233d2500723e5594f3e7c70896ffeeef32b9c950ywan
635233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts a wide string to a narrow string in UTF-8 encoding.
636233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The wide string is assumed to have the following encoding:
637233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)
638233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)
639233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parameter str points to a null-terminated wide string.
640233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parameter num_chars may additionally limit the number
641233d2500723e5594f3e7c70896ffeeef32b9c950ywan// of wchar_t characters processed. -1 is used when the entire string
642233d2500723e5594f3e7c70896ffeeef32b9c950ywan// should be processed.
643233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If the string contains code points that are not valid Unicode code points
644233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
645233d2500723e5594f3e7c70896ffeeef32b9c950ywan// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
646233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and contains invalid UTF-16 surrogate pairs, values in those pairs
647233d2500723e5594f3e7c70896ffeeef32b9c950ywan// will be encoded as individual Unicode characters from Basic Normal Plane.
648233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars);
649233d2500723e5594f3e7c70896ffeeef32b9c950ywan
650233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
651233d2500723e5594f3e7c70896ffeeef32b9c950ywan// if the variable is present. If a file already exists at this location, this
652233d2500723e5594f3e7c70896ffeeef32b9c950ywan// function will write over it. If the variable is present, but the file cannot
653233d2500723e5594f3e7c70896ffeeef32b9c950ywan// be created, prints an error and exits.
654233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid WriteToShardStatusFileIfNeeded();
655233d2500723e5594f3e7c70896ffeeef32b9c950ywan
656233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Checks whether sharding is enabled by examining the relevant
657233d2500723e5594f3e7c70896ffeeef32b9c950ywan// environment variable values. If the variables are present,
658233d2500723e5594f3e7c70896ffeeef32b9c950ywan// but inconsistent (e.g., shard_index >= total_shards), prints
659233d2500723e5594f3e7c70896ffeeef32b9c950ywan// an error and exits. If in_subprocess_for_death_test, sharding is
660233d2500723e5594f3e7c70896ffeeef32b9c950ywan// disabled because it must only be applied to the original test
661233d2500723e5594f3e7c70896ffeeef32b9c950ywan// process. Otherwise, we could filter out death tests we intended to execute.
662233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool ShouldShard(const char* total_shards_str,
663233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            const char* shard_index_str,
664233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            bool in_subprocess_for_death_test);
665233d2500723e5594f3e7c70896ffeeef32b9c950ywan
666233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses the environment variable var as an Int32. If it is unset,
667233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returns default_val. If it is not an Int32, prints an error and
668233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and aborts.
669233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
670233d2500723e5594f3e7c70896ffeeef32b9c950ywan
671233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Given the total number of shards, the shard index, and the test id,
672233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returns true iff the test should be run on this shard. The test id is
673233d2500723e5594f3e7c70896ffeeef32b9c950ywan// some arbitrary but unique non-negative integer assigned to each test
674233d2500723e5594f3e7c70896ffeeef32b9c950ywan// method. Assumes that 0 <= shard_index < total_shards.
675233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool ShouldRunTestOnShard(
676233d2500723e5594f3e7c70896ffeeef32b9c950ywan    int total_shards, int shard_index, int test_id);
677233d2500723e5594f3e7c70896ffeeef32b9c950ywan
678233d2500723e5594f3e7c70896ffeeef32b9c950ywan// STL container utilities.
679233d2500723e5594f3e7c70896ffeeef32b9c950ywan
680233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the number of elements in the given container that satisfy
681233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the given predicate.
682233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <class Container, typename Predicate>
683233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline int CountIf(const Container& c, Predicate predicate) {
684233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Implemented as an explicit loop since std::count_if() in libCstd on
685233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Solaris has a non-standard signature.
686233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int count = 0;
687233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
688233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (predicate(*it))
689233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ++count;
690233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
691233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return count;
692233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
693233d2500723e5594f3e7c70896ffeeef32b9c950ywan
694233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Applies a function/functor to each element in the container.
695233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <class Container, typename Functor>
696233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ForEach(const Container& c, Functor functor) {
697233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::for_each(c.begin(), c.end(), functor);
698233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
699233d2500723e5594f3e7c70896ffeeef32b9c950ywan
700233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the i-th element of the vector, or default_value if i is not
701233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in range [0, v.size()).
702233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename E>
703233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline E GetElementOr(const std::vector<E>& v, int i, E default_value) {
704233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i];
705233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
706233d2500723e5594f3e7c70896ffeeef32b9c950ywan
707233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Performs an in-place shuffle of a range of the vector's elements.
708233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 'begin' and 'end' are element indices as an STL-style range;
709233d2500723e5594f3e7c70896ffeeef32b9c950ywan// i.e. [begin, end) are shuffled, where 'end' == size() means to
710233d2500723e5594f3e7c70896ffeeef32b9c950ywan// shuffle to the end of the vector.
711233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename E>
712233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ShuffleRange(internal::Random* random, int begin, int end,
713233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  std::vector<E>* v) {
714233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int size = static_cast<int>(v->size());
715233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(0 <= begin && begin <= size)
716233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Invalid shuffle range start " << begin << ": must be in range [0, "
717233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << size << "].";
718233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(begin <= end && end <= size)
719233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Invalid shuffle range finish " << end << ": must be in range ["
720233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << begin << ", " << size << "].";
721233d2500723e5594f3e7c70896ffeeef32b9c950ywan
722233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Fisher-Yates shuffle, from
723233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
724233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int range_width = end - begin; range_width >= 2; range_width--) {
725233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const int last_in_range = begin + range_width - 1;
726233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const int selected = begin + random->Generate(range_width);
727233d2500723e5594f3e7c70896ffeeef32b9c950ywan    std::swap((*v)[selected], (*v)[last_in_range]);
728233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
729233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
730233d2500723e5594f3e7c70896ffeeef32b9c950ywan
731233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Performs an in-place shuffle of the vector's elements.
732233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename E>
733233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline void Shuffle(internal::Random* random, std::vector<E>* v) {
734233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ShuffleRange(random, 0, static_cast<int>(v->size()), v);
735233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
736233d2500723e5594f3e7c70896ffeeef32b9c950ywan
737233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A function for deleting an object.  Handy for being used as a
738233d2500723e5594f3e7c70896ffeeef32b9c950ywan// functor.
739233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename T>
740233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void Delete(T* x) {
741233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete x;
742233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
743233d2500723e5594f3e7c70896ffeeef32b9c950ywan
744233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A predicate that checks the key of a TestProperty against a known key.
745233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
746233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TestPropertyKeyIs is copyable.
747233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass TestPropertyKeyIs {
748233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
749233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Constructor.
750233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
751233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TestPropertyKeyIs has NO default constructor.
752233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit TestPropertyKeyIs(const std::string& key) : key_(key) {}
753233d2500723e5594f3e7c70896ffeeef32b9c950ywan
754233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns true iff the test name of test property matches on key_.
755233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool operator()(const TestProperty& test_property) const {
756233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return test_property.key() == key_;
757233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
758233d2500723e5594f3e7c70896ffeeef32b9c950ywan
759233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
760233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string key_;
761233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
762233d2500723e5594f3e7c70896ffeeef32b9c950ywan
763233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Class UnitTestOptions.
764233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
765233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This class contains functions for processing options the user
766233d2500723e5594f3e7c70896ffeeef32b9c950ywan// specifies when running the tests.  It has only static members.
767233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
768233d2500723e5594f3e7c70896ffeeef32b9c950ywan// In most cases, the user can specify an option using either an
769233d2500723e5594f3e7c70896ffeeef32b9c950ywan// environment variable or a command line flag.  E.g. you can set the
770233d2500723e5594f3e7c70896ffeeef32b9c950ywan// test filter using either GTEST_FILTER or --gtest_filter.  If both
771233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the variable and the flag are present, the latter overrides the
772233d2500723e5594f3e7c70896ffeeef32b9c950ywan// former.
773233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass GTEST_API_ UnitTestOptions {
774233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
775233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Functions for processing the gtest_output flag.
776233d2500723e5594f3e7c70896ffeeef32b9c950ywan
777233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the output format, or "" for normal printed output.
778233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static std::string GetOutputFormat();
779233d2500723e5594f3e7c70896ffeeef32b9c950ywan
780233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the absolute path of the requested output file, or the
781233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // default (test_detail.xml in the original working directory) if
782233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // none was explicitly specified.
783233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static std::string GetAbsolutePathToOutputFile();
784233d2500723e5594f3e7c70896ffeeef32b9c950ywan
785233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Functions for processing the gtest_filter flag.
786233d2500723e5594f3e7c70896ffeeef32b9c950ywan
787233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns true iff the wildcard pattern matches the string.  The
788233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // first ':' or '\0' character in pattern marks the end of it.
789233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
790233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This recursive algorithm isn't very efficient, but is clear and
791233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // works well enough for matching test names, which are short.
792233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static bool PatternMatchesString(const char *pattern, const char *str);
793233d2500723e5594f3e7c70896ffeeef32b9c950ywan
794233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns true iff the user-specified filter matches the test case
795233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // name and the test name.
796233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static bool FilterMatchesTest(const std::string &test_case_name,
797233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                const std::string &test_name);
798233d2500723e5594f3e7c70896ffeeef32b9c950ywan
799233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
800233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Function for supporting the gtest_catch_exception flag.
801233d2500723e5594f3e7c70896ffeeef32b9c950ywan
802233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
803233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
804233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This function is useful as an __except condition.
805233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static int GTestShouldProcessSEH(DWORD exception_code);
806233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
807233d2500723e5594f3e7c70896ffeeef32b9c950ywan
808233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns true if "name" matches the ':' separated list of glob-style
809233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // filters in "filter".
810233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static bool MatchesFilter(const std::string& name, const char* filter);
811233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
812233d2500723e5594f3e7c70896ffeeef32b9c950ywan
813233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current application's name, removing directory path if that
814233d2500723e5594f3e7c70896ffeeef32b9c950ywan// is present.  Used by UnitTestOptions::GetOutputFile.
815233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ FilePath GetCurrentExecutableName();
816233d2500723e5594f3e7c70896ffeeef32b9c950ywan
817233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The role interface for getting the OS stack trace as a string.
818233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass OsStackTraceGetterInterface {
819233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
820233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OsStackTraceGetterInterface() {}
821233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual ~OsStackTraceGetterInterface() {}
822233d2500723e5594f3e7c70896ffeeef32b9c950ywan
823233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the current OS stack trace as an std::string.  Parameters:
824233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
825233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   max_depth  - the maximum number of stack frames to be included
826233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //                in the trace.
827233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   skip_count - the number of top frames to be skipped; doesn't count
828233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //                against max_depth.
829233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual string CurrentStackTrace(int max_depth, int skip_count) = 0;
830233d2500723e5594f3e7c70896ffeeef32b9c950ywan
831233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // UponLeavingGTest() should be called immediately before Google Test calls
832233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // user code. It saves some information about the current stack that
833233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // CurrentStackTrace() will use to find and hide Google Test stack frames.
834233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void UponLeavingGTest() = 0;
835233d2500723e5594f3e7c70896ffeeef32b9c950ywan
836233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
837233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
838233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
839233d2500723e5594f3e7c70896ffeeef32b9c950ywan
840233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A working implementation of the OsStackTraceGetterInterface interface.
841233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass OsStackTraceGetter : public OsStackTraceGetterInterface {
842233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
843233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OsStackTraceGetter() : caller_frame_(NULL) {}
844233d2500723e5594f3e7c70896ffeeef32b9c950ywan
845233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual string CurrentStackTrace(int max_depth, int skip_count)
846233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTEST_LOCK_EXCLUDED_(mutex_);
847233d2500723e5594f3e7c70896ffeeef32b9c950ywan
848233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_);
849233d2500723e5594f3e7c70896ffeeef32b9c950ywan
850233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This string is inserted in place of stack frames that are part of
851233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Google Test's implementation.
852233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static const char* const kElidedFramesMarker;
853233d2500723e5594f3e7c70896ffeeef32b9c950ywan
854233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
855233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Mutex mutex_;  // protects all internal state
856233d2500723e5594f3e7c70896ffeeef32b9c950ywan
857233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We save the stack frame below the frame that calls user code.
858233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We do this because the address of the frame immediately below
859233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the user code changes between the call to UponLeavingGTest()
860233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // and any calls to CurrentStackTrace() from within the user code.
861233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void* caller_frame_;
862233d2500723e5594f3e7c70896ffeeef32b9c950ywan
863233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
864233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
865233d2500723e5594f3e7c70896ffeeef32b9c950ywan
866233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Information about a Google Test trace point.
867233d2500723e5594f3e7c70896ffeeef32b9c950ywanstruct TraceInfo {
868233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* file;
869233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int line;
870233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string message;
871233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
872233d2500723e5594f3e7c70896ffeeef32b9c950ywan
873233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This is the default global test part result reporter used in UnitTestImpl.
874233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This class should only be used by UnitTestImpl.
875233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass DefaultGlobalTestPartResultReporter
876233d2500723e5594f3e7c70896ffeeef32b9c950ywan  : public TestPartResultReporterInterface {
877233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
878233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
879233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Implements the TestPartResultReporterInterface. Reports the test part
880233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // result in the current test.
881233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void ReportTestPartResult(const TestPartResult& result);
882233d2500723e5594f3e7c70896ffeeef32b9c950ywan
883233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
884233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTestImpl* const unit_test_;
885233d2500723e5594f3e7c70896ffeeef32b9c950ywan
886233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
887233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
888233d2500723e5594f3e7c70896ffeeef32b9c950ywan
889233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This is the default per thread test part result reporter used in
890233d2500723e5594f3e7c70896ffeeef32b9c950ywan// UnitTestImpl. This class should only be used by UnitTestImpl.
891233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass DefaultPerThreadTestPartResultReporter
892233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : public TestPartResultReporterInterface {
893233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
894233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
895233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Implements the TestPartResultReporterInterface. The implementation just
896233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // delegates to the current global test part result reporter of *unit_test_.
897233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void ReportTestPartResult(const TestPartResult& result);
898233d2500723e5594f3e7c70896ffeeef32b9c950ywan
899233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
900233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTestImpl* const unit_test_;
901233d2500723e5594f3e7c70896ffeeef32b9c950ywan
902233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
903233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
904233d2500723e5594f3e7c70896ffeeef32b9c950ywan
905233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The private implementation of the UnitTest class.  We don't protect
906233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the methods under a mutex, as this class is not accessible by a
907233d2500723e5594f3e7c70896ffeeef32b9c950ywan// user and the UnitTest class that delegates work to this class does
908233d2500723e5594f3e7c70896ffeeef32b9c950ywan// proper locking.
909233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass GTEST_API_ UnitTestImpl {
910233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
911233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit UnitTestImpl(UnitTest* parent);
912233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual ~UnitTestImpl();
913233d2500723e5594f3e7c70896ffeeef32b9c950ywan
914233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // There are two different ways to register your own TestPartResultReporter.
915233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // You can register your own repoter to listen either only for test results
916233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // from the current thread or for results from all threads.
917233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // By default, each per-thread test result repoter just passes a new
918233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TestPartResult to the global test result reporter, which registers the
919233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // test part result for the currently running test.
920233d2500723e5594f3e7c70896ffeeef32b9c950ywan
921233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the global test part result reporter.
922233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
923233d2500723e5594f3e7c70896ffeeef32b9c950ywan
924233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Sets the global test part result reporter.
925233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void SetGlobalTestPartResultReporter(
926233d2500723e5594f3e7c70896ffeeef32b9c950ywan      TestPartResultReporterInterface* reporter);
927233d2500723e5594f3e7c70896ffeeef32b9c950ywan
928233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the test part result reporter for the current thread.
929233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
930233d2500723e5594f3e7c70896ffeeef32b9c950ywan
931233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Sets the test part result reporter for the current thread.
932233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void SetTestPartResultReporterForCurrentThread(
933233d2500723e5594f3e7c70896ffeeef32b9c950ywan      TestPartResultReporterInterface* reporter);
934233d2500723e5594f3e7c70896ffeeef32b9c950ywan
935233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of successful test cases.
936233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int successful_test_case_count() const;
937233d2500723e5594f3e7c70896ffeeef32b9c950ywan
938233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of failed test cases.
939233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int failed_test_case_count() const;
940233d2500723e5594f3e7c70896ffeeef32b9c950ywan
941233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of all test cases.
942233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int total_test_case_count() const;
943233d2500723e5594f3e7c70896ffeeef32b9c950ywan
944233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of all test cases that contain at least one test
945233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // that should run.
946233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int test_case_to_run_count() const;
947233d2500723e5594f3e7c70896ffeeef32b9c950ywan
948233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of successful tests.
949233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int successful_test_count() const;
950233d2500723e5594f3e7c70896ffeeef32b9c950ywan
951233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of failed tests.
952233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int failed_test_count() const;
953233d2500723e5594f3e7c70896ffeeef32b9c950ywan
954233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of disabled tests that will be reported in the XML report.
955233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int reportable_disabled_test_count() const;
956233d2500723e5594f3e7c70896ffeeef32b9c950ywan
957233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of disabled tests.
958233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int disabled_test_count() const;
959233d2500723e5594f3e7c70896ffeeef32b9c950ywan
960233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of tests to be printed in the XML report.
961233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int reportable_test_count() const;
962233d2500723e5594f3e7c70896ffeeef32b9c950ywan
963233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of all tests.
964233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int total_test_count() const;
965233d2500723e5594f3e7c70896ffeeef32b9c950ywan
966233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the number of tests that should run.
967233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int test_to_run_count() const;
968233d2500723e5594f3e7c70896ffeeef32b9c950ywan
969233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the time of the test program start, in ms from the start of the
970233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // UNIX epoch.
971233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TimeInMillis start_timestamp() const { return start_timestamp_; }
972233d2500723e5594f3e7c70896ffeeef32b9c950ywan
973233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the elapsed time, in milliseconds.
974233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TimeInMillis elapsed_time() const { return elapsed_time_; }
975233d2500723e5594f3e7c70896ffeeef32b9c950ywan
976233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns true iff the unit test passed (i.e. all test cases passed).
977233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool Passed() const { return !Failed(); }
978233d2500723e5594f3e7c70896ffeeef32b9c950ywan
979233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns true iff the unit test failed (i.e. some test case failed
980233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // or something outside of all tests failed).
981233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool Failed() const {
982233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
983233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
984233d2500723e5594f3e7c70896ffeeef32b9c950ywan
985233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the i-th test case among all the test cases. i can range from 0 to
986233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // total_test_case_count() - 1. If i is not in that range, returns NULL.
987233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestCase* GetTestCase(int i) const {
988233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const int index = GetElementOr(test_case_indices_, i, -1);
989233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return index < 0 ? NULL : test_cases_[i];
990233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
991233d2500723e5594f3e7c70896ffeeef32b9c950ywan
992233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the i-th test case among all the test cases. i can range from 0 to
993233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // total_test_case_count() - 1. If i is not in that range, returns NULL.
994233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestCase* GetMutableTestCase(int i) {
995233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const int index = GetElementOr(test_case_indices_, i, -1);
996233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return index < 0 ? NULL : test_cases_[index];
997233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
998233d2500723e5594f3e7c70896ffeeef32b9c950ywan
999233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Provides access to the event listener list.
1000233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestEventListeners* listeners() { return &listeners_; }
1001233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1002233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the TestResult for the test that's currently running, or
1003233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the TestResult for the ad hoc test if no test is running.
1004233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestResult* current_test_result();
1005233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1006233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the TestResult for the ad hoc test.
1007233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
1008233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1009233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Sets the OS stack trace getter.
1010233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
1011233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Does nothing if the input and the current OS stack trace getter
1012233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // are the same; otherwise, deletes the old getter and makes the
1013233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // input the current getter.
1014233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
1015233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1016233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the current OS stack trace getter if it is not NULL;
1017233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // otherwise, creates an OsStackTraceGetter, makes it the current
1018233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // getter, and returns it.
1019233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OsStackTraceGetterInterface* os_stack_trace_getter();
1020233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1021233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the current OS stack trace as an std::string.
1022233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
1023233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The maximum number of stack frames to be included is specified by
1024233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the gtest_stack_trace_depth flag.  The skip_count parameter
1025233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // specifies the number of top frames to be skipped, which doesn't
1026233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // count against the number of frames to be included.
1027233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
1028233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // For example, if Foo() calls Bar(), which in turn calls
1029233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // CurrentOsStackTraceExceptTop(1), Foo() will be included in the
1030233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // trace but Bar() and CurrentOsStackTraceExceptTop() won't.
1031233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string CurrentOsStackTraceExceptTop(int skip_count) GTEST_NO_INLINE_;
1032233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1033233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Finds and returns a TestCase with the given name.  If one doesn't
1034233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // exist, creates one and returns it.
1035233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
1036233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Arguments:
1037233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
1038233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   test_case_name: name of the test case
1039233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   type_param:     the name of the test's type parameter, or NULL if
1040233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //                   this is not a typed or a type-parameterized test.
1041233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   set_up_tc:      pointer to the function that sets up the test case
1042233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   tear_down_tc:   pointer to the function that tears down the test case
1043233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestCase* GetTestCase(const char* test_case_name,
1044233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        const char* type_param,
1045233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        Test::SetUpTestCaseFunc set_up_tc,
1046233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        Test::TearDownTestCaseFunc tear_down_tc);
1047233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1048233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Adds a TestInfo to the unit test.
1049233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
1050233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Arguments:
1051233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
1052233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   set_up_tc:    pointer to the function that sets up the test case
1053233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   tear_down_tc: pointer to the function that tears down the test case
1054233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   test_info:    the TestInfo object
1055233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,
1056233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   Test::TearDownTestCaseFunc tear_down_tc,
1057233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   TestInfo* test_info) {
1058233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // In order to support thread-safe death tests, we need to
1059233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // remember the original working directory when the test program
1060233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // was first invoked.  We cannot do this in RUN_ALL_TESTS(), as
1061233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // the user may have changed the current directory before calling
1062233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // RUN_ALL_TESTS().  Therefore we capture the current directory in
1063233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // AddTestInfo(), which is called to register a TEST or TEST_F
1064233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // before main() is reached.
1065233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (original_working_dir_.IsEmpty()) {
1066233d2500723e5594f3e7c70896ffeeef32b9c950ywan      original_working_dir_.Set(FilePath::GetCurrentDir());
1067233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTEST_CHECK_(!original_working_dir_.IsEmpty())
1068233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "Failed to get the current working directory.";
1069233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1070233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1071233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GetTestCase(test_info->test_case_name(),
1072233d2500723e5594f3e7c70896ffeeef32b9c950ywan                test_info->type_param(),
1073233d2500723e5594f3e7c70896ffeeef32b9c950ywan                set_up_tc,
1074233d2500723e5594f3e7c70896ffeeef32b9c950ywan                tear_down_tc)->AddTestInfo(test_info);
1075233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1076233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1077233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_PARAM_TEST
1078233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns ParameterizedTestCaseRegistry object used to keep track of
1079233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // value-parameterized tests and instantiate and register them.
1080233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {
1081233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return parameterized_test_registry_;
1082233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1083233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_PARAM_TEST
1084233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1085233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Sets the TestCase object for the test that's currently running.
1086233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_current_test_case(TestCase* a_current_test_case) {
1087233d2500723e5594f3e7c70896ffeeef32b9c950ywan    current_test_case_ = a_current_test_case;
1088233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1089233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1090233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Sets the TestInfo object for the test that's currently running.  If
1091233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // current_test_info is NULL, the assertion results will be stored in
1092233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // ad_hoc_test_result_.
1093233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_current_test_info(TestInfo* a_current_test_info) {
1094233d2500723e5594f3e7c70896ffeeef32b9c950ywan    current_test_info_ = a_current_test_info;
1095233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1096233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1097233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Registers all parameterized tests defined using TEST_P and
1098233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // INSTANTIATE_TEST_CASE_P, creating regular tests for each test/parameter
1099233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // combination. This method can be called more then once; it has guards
1100233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // protecting from registering the tests more then once.  If
1101233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // value-parameterized tests are disabled, RegisterParameterizedTests is
1102233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // present but does nothing.
1103233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void RegisterParameterizedTests();
1104233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1105233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Runs all tests in this UnitTest object, prints the result, and
1106233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // returns true if all tests are successful.  If any exception is
1107233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // thrown during a test, this test is considered to be failed, but
1108233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the rest of the tests will still be run.
1109233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool RunAllTests();
1110233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1111233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Clears the results of all tests, except the ad hoc tests.
1112233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void ClearNonAdHocTestResult() {
1113233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ForEach(test_cases_, TestCase::ClearTestCaseResult);
1114233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1115233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1116233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Clears the results of ad-hoc test assertions.
1117233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void ClearAdHocTestResult() {
1118233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ad_hoc_test_result_.Clear();
1119233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1120233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1121233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Adds a TestProperty to the current TestResult object when invoked in a
1122233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // context of a test or a test case, or to the global property set. If the
1123233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // result already contains a property with the same key, the value will be
1124233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // updated.
1125233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void RecordProperty(const TestProperty& test_property);
1126233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1127233d2500723e5594f3e7c70896ffeeef32b9c950ywan  enum ReactionToSharding {
1128233d2500723e5594f3e7c70896ffeeef32b9c950ywan    HONOR_SHARDING_PROTOCOL,
1129233d2500723e5594f3e7c70896ffeeef32b9c950ywan    IGNORE_SHARDING_PROTOCOL
1130233d2500723e5594f3e7c70896ffeeef32b9c950ywan  };
1131233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1132233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Matches the full name of each test against the user-specified
1133233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // filter to decide whether the test should run, then records the
1134233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // result in each TestCase and TestInfo object.
1135233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests
1136233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // based on sharding variables in the environment.
1137233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the number of tests that should run.
1138233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int FilterTests(ReactionToSharding shard_tests);
1139233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1140233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Prints the names of the tests matching the user-specified filter flag.
1141233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void ListTestsMatchingFilter();
1142233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1143233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestCase* current_test_case() const { return current_test_case_; }
1144233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestInfo* current_test_info() { return current_test_info_; }
1145233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestInfo* current_test_info() const { return current_test_info_; }
1146233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1147233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the vector of environments that need to be set-up/torn-down
1148233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // before/after the tests are run.
1149233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::vector<Environment*>& environments() { return environments_; }
1150233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1151233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Getters for the per-thread Google Test trace stack.
1152233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::vector<TraceInfo>& gtest_trace_stack() {
1153233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return *(gtest_trace_stack_.pointer());
1154233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1155233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::vector<TraceInfo>& gtest_trace_stack() const {
1156233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return gtest_trace_stack_.get();
1157233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1158233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1159233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
1160233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void InitDeathTestSubprocessControlInfo() {
1161233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
1162233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1163233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns a pointer to the parsed --gtest_internal_run_death_test
1164233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // flag, or NULL if that flag was not specified.
1165233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This information is useful only in a death test child process.
1166233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Must not be called before a call to InitGoogleTest.
1167233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const InternalRunDeathTestFlag* internal_run_death_test_flag() const {
1168233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return internal_run_death_test_flag_.get();
1169233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1170233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1171233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns a pointer to the current death test factory.
1172233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::DeathTestFactory* death_test_factory() {
1173233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return death_test_factory_.get();
1174233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1175233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1176233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void SuppressTestEventsIfInSubprocess();
1177233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1178233d2500723e5594f3e7c70896ffeeef32b9c950ywan  friend class ReplaceDeathTestFactory;
1179233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
1180233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1181233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Initializes the event listener performing XML output as specified by
1182233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // UnitTestOptions. Must not be called before InitGoogleTest.
1183233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void ConfigureXmlOutput();
1184233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1185233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_CAN_STREAM_RESULTS_
1186233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Initializes the event listener for streaming test results to a socket.
1187233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Must not be called before InitGoogleTest.
1188233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void ConfigureStreamingOutput();
1189233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
1190233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1191233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Performs initialization dependent upon flag values obtained in
1192233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to
1193233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest
1194233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // this function is also called from RunAllTests.  Since this function can be
1195233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // called more than once, it has to be idempotent.
1196233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void PostFlagParsingInit();
1197233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1198233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the random seed used at the start of the current test iteration.
1199233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int random_seed() const { return random_seed_; }
1200233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1201233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the random number generator.
1202233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::Random* random() { return &random_; }
1203233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1204233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Shuffles all test cases, and the tests within each test case,
1205233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // making sure that death tests are still run first.
1206233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void ShuffleTests();
1207233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1208233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Restores the test cases and tests to their order before the first shuffle.
1209233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void UnshuffleTests();
1210233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1211233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the value of GTEST_FLAG(catch_exceptions) at the moment
1212233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // UnitTest::Run() starts.
1213233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool catch_exceptions() const { return catch_exceptions_; }
1214233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1215233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
1216233d2500723e5594f3e7c70896ffeeef32b9c950ywan  friend class ::testing::UnitTest;
1217233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1218233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Used by UnitTest::Run() to capture the state of
1219233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // GTEST_FLAG(catch_exceptions) at the moment it starts.
1220233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
1221233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1222233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The UnitTest object that owns this implementation object.
1223233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTest* const parent_;
1224233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1225233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The working directory when the first TEST() or TEST_F() was
1226233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // executed.
1227233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::FilePath original_working_dir_;
1228233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1229233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The default test part result reporters.
1230233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
1231233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DefaultPerThreadTestPartResultReporter
1232233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default_per_thread_test_part_result_reporter_;
1233233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1234233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Points to (but doesn't own) the global test part result reporter.
1235233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestPartResultReporterInterface* global_test_part_result_repoter_;
1236233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1237233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Protects read and write access to global_test_part_result_reporter_.
1238233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::Mutex global_test_part_result_reporter_mutex_;
1239233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1240233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Points to (but doesn't own) the per-thread test part result reporter.
1241233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::ThreadLocal<TestPartResultReporterInterface*>
1242233d2500723e5594f3e7c70896ffeeef32b9c950ywan      per_thread_test_part_result_reporter_;
1243233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1244233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The vector of environments that need to be set-up/torn-down
1245233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // before/after the tests are run.
1246233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::vector<Environment*> environments_;
1247233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1248233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The vector of TestCases in their original order.  It owns the
1249233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // elements in the vector.
1250233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::vector<TestCase*> test_cases_;
1251233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1252233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Provides a level of indirection for the test case list to allow
1253233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // easy shuffling and restoring the test case order.  The i-th
1254233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // element of this vector is the index of the i-th test case in the
1255233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // shuffled order.
1256233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::vector<int> test_case_indices_;
1257233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1258233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_PARAM_TEST
1259233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // ParameterizedTestRegistry object used to register value-parameterized
1260233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // tests.
1261233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::ParameterizedTestCaseRegistry parameterized_test_registry_;
1262233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1263233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Indicates whether RegisterParameterizedTests() has been called already.
1264233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool parameterized_tests_registered_;
1265233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_PARAM_TEST
1266233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1267233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Index of the last death test case registered.  Initially -1.
1268233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int last_death_test_case_;
1269233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1270233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This points to the TestCase for the currently running test.  It
1271233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // changes as Google Test goes through one test case after another.
1272233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // When no test is running, this is set to NULL and Google Test
1273233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // stores assertion results in ad_hoc_test_result_.  Initially NULL.
1274233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestCase* current_test_case_;
1275233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1276233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This points to the TestInfo for the currently running test.  It
1277233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // changes as Google Test goes through one test after another.  When
1278233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // no test is running, this is set to NULL and Google Test stores
1279233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // assertion results in ad_hoc_test_result_.  Initially NULL.
1280233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestInfo* current_test_info_;
1281233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1282233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Normally, a user only writes assertions inside a TEST or TEST_F,
1283233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // or inside a function called by a TEST or TEST_F.  Since Google
1284233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Test keeps track of which test is current running, it can
1285233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // associate such an assertion with the test it belongs to.
1286233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
1287233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // If an assertion is encountered when no TEST or TEST_F is running,
1288233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Google Test attributes the assertion result to an imaginary "ad hoc"
1289233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // test, and records the result in ad_hoc_test_result_.
1290233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestResult ad_hoc_test_result_;
1291233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1292233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The list of event listeners that can be used to track events inside
1293233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Google Test.
1294233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestEventListeners listeners_;
1295233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1296233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The OS stack trace getter.  Will be deleted when the UnitTest
1297233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // object is destructed.  By default, an OsStackTraceGetter is used,
1298233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // but the user can set this field to use a custom getter if that is
1299233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // desired.
1300233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OsStackTraceGetterInterface* os_stack_trace_getter_;
1301233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1302233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // True iff PostFlagParsingInit() has been called.
1303233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool post_flag_parse_init_performed_;
1304233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1305233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The random number seed used at the beginning of the test run.
1306233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int random_seed_;
1307233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1308233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Our random number generator.
1309233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::Random random_;
1310233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1311233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The time of the test program start, in ms from the start of the
1312233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // UNIX epoch.
1313233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TimeInMillis start_timestamp_;
1314233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1315233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // How long the test took to run, in milliseconds.
1316233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TimeInMillis elapsed_time_;
1317233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1318233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
1319233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The decomposed components of the gtest_internal_run_death_test flag,
1320233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // parsed when RUN_ALL_TESTS is called.
1321233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
1322233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_;
1323233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
1324233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1325233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // A per-thread stack of traces created by the SCOPED_TRACE() macro.
1326233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
1327233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1328233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests()
1329233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // starts.
1330233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool catch_exceptions_;
1331233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1332233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
1333233d2500723e5594f3e7c70896ffeeef32b9c950ywan};  // class UnitTestImpl
1334233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1335233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Convenience function for accessing the global UnitTest
1336233d2500723e5594f3e7c70896ffeeef32b9c950ywan// implementation object.
1337233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline UnitTestImpl* GetUnitTestImpl() {
1338233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return UnitTest::GetInstance()->impl();
1339233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1340233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1341233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_USES_SIMPLE_RE
1342233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1343233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Internal helper functions for implementing the simple regular
1344233d2500723e5594f3e7c70896ffeeef32b9c950ywan// expression matcher.
1345233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool IsInSet(char ch, const char* str);
1346233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool IsAsciiDigit(char ch);
1347233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool IsAsciiPunct(char ch);
1348233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool IsRepeat(char ch);
1349233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool IsAsciiWhiteSpace(char ch);
1350233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool IsAsciiWordChar(char ch);
1351233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool IsValidEscape(char ch);
1352233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch);
1353233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool ValidateRegex(const char* regex);
1354233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str);
1355233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool MatchRepetitionAndRegexAtHead(
1356233d2500723e5594f3e7c70896ffeeef32b9c950ywan    bool escaped, char ch, char repeat, const char* regex, const char* str);
1357233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);
1358233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1359233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_USES_SIMPLE_RE
1360233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1361233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses the command line for Google Test flags, without initializing
1362233d2500723e5594f3e7c70896ffeeef32b9c950ywan// other parts of Google Test.
1363233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);
1364233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
1365233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1366233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
1367233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1368233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the message describing the last system error, regardless of the
1369233d2500723e5594f3e7c70896ffeeef32b9c950ywan// platform.
1370233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ std::string GetLastErrnoDescription();
1371233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1372233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
1373233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Provides leak-safe Windows kernel handle ownership.
1374233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass AutoHandle {
1375233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
1376233d2500723e5594f3e7c70896ffeeef32b9c950ywan  AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
1377233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit AutoHandle(HANDLE handle) : handle_(handle) {}
1378233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1379233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ~AutoHandle() { Reset(); }
1380233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1381233d2500723e5594f3e7c70896ffeeef32b9c950ywan  HANDLE Get() const { return handle_; }
1382233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void Reset() { Reset(INVALID_HANDLE_VALUE); }
1383233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void Reset(HANDLE handle) {
1384233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (handle != handle_) {
1385233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (handle_ != INVALID_HANDLE_VALUE)
1386233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ::CloseHandle(handle_);
1387233d2500723e5594f3e7c70896ffeeef32b9c950ywan      handle_ = handle;
1388233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1389233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1390233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1391233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
1392233d2500723e5594f3e7c70896ffeeef32b9c950ywan  HANDLE handle_;
1393233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1394233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
1395233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
1396233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS
1397233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1398233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Attempts to parse a string into a positive integer pointed to by the
1399233d2500723e5594f3e7c70896ffeeef32b9c950ywan// number parameter.  Returns true if that is possible.
1400233d2500723e5594f3e7c70896ffeeef32b9c950ywan// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use
1401233d2500723e5594f3e7c70896ffeeef32b9c950ywan// it here.
1402233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename Integer>
1403233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ParseNaturalNumber(const ::std::string& str, Integer* number) {
1404233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Fail fast if the given string does not begin with a digit;
1405233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // this bypasses strtoXXX's "optional leading whitespace and plus
1406233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // or minus sign" semantics, which are undesirable here.
1407233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (str.empty() || !IsDigit(str[0])) {
1408233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
1409233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1410233d2500723e5594f3e7c70896ffeeef32b9c950ywan  errno = 0;
1411233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1412233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* end;
1413233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // BiggestConvertible is the largest integer type that system-provided
1414233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // string-to-number conversion routines can return.
1415233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1416233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS && !defined(__GNUC__)
1417233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1418233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // MSVC and C++ Builder define __int64 instead of the standard long long.
1419233d2500723e5594f3e7c70896ffeeef32b9c950ywan  typedef unsigned __int64 BiggestConvertible;
1420233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
1421233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1422233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
1423233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1424233d2500723e5594f3e7c70896ffeeef32b9c950ywan  typedef unsigned long long BiggestConvertible;  // NOLINT
1425233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
1426233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1427233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS && !defined(__GNUC__)
1428233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1429233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool parse_success = *end == '\0' && errno == 0;
1430233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1431233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TODO(vladl@google.com): Convert this to compile time assertion when it is
1432233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // available.
1433233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
1434233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1435233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const Integer result = static_cast<Integer>(parsed);
1436233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
1437233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *number = result;
1438233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return true;
1439233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1440233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return false;
1441233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1442233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
1443233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1444233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TestResult contains some private methods that should be hidden from
1445233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test user but are required for testing. This class allow our tests
1446233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to access them.
1447233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
1448233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This class is supplied only for the purpose of testing Google Test's own
1449233d2500723e5594f3e7c70896ffeeef32b9c950ywan// constructs. Do not use it in user tests, either directly or indirectly.
1450233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass TestResultAccessor {
1451233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
1452233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void RecordProperty(TestResult* test_result,
1453233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             const std::string& xml_element,
1454233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             const TestProperty& property) {
1455233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_result->RecordProperty(xml_element, property);
1456233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1457233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1458233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void ClearTestPartResults(TestResult* test_result) {
1459233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_result->ClearTestPartResults();
1460233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1461233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1462233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static const std::vector<testing::TestPartResult>& test_part_results(
1463233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const TestResult& test_result) {
1464233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return test_result.test_part_results();
1465233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1466233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
1467233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1468233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_CAN_STREAM_RESULTS_
1469233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1470233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Streams test results to the given port on the given host machine.
1471233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass StreamingListener : public EmptyTestEventListener {
1472233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
1473233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Abstract base class for writing strings to a socket.
1474233d2500723e5594f3e7c70896ffeeef32b9c950ywan  class AbstractSocketWriter {
1475233d2500723e5594f3e7c70896ffeeef32b9c950ywan   public:
1476233d2500723e5594f3e7c70896ffeeef32b9c950ywan    virtual ~AbstractSocketWriter() {}
1477233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1478233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Sends a string to the socket.
1479233d2500723e5594f3e7c70896ffeeef32b9c950ywan    virtual void Send(const string& message) = 0;
1480233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1481233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Closes the socket.
1482233d2500723e5594f3e7c70896ffeeef32b9c950ywan    virtual void CloseConnection() {}
1483233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1484233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Sends a string and a newline to the socket.
1485233d2500723e5594f3e7c70896ffeeef32b9c950ywan    void SendLn(const string& message) {
1486233d2500723e5594f3e7c70896ffeeef32b9c950ywan      Send(message + "\n");
1487233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1488233d2500723e5594f3e7c70896ffeeef32b9c950ywan  };
1489233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1490233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Concrete class for actually writing strings to a socket.
1491233d2500723e5594f3e7c70896ffeeef32b9c950ywan  class SocketWriter : public AbstractSocketWriter {
1492233d2500723e5594f3e7c70896ffeeef32b9c950ywan   public:
1493233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SocketWriter(const string& host, const string& port)
1494233d2500723e5594f3e7c70896ffeeef32b9c950ywan        : sockfd_(-1), host_name_(host), port_num_(port) {
1495233d2500723e5594f3e7c70896ffeeef32b9c950ywan      MakeConnection();
1496233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1497233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1498233d2500723e5594f3e7c70896ffeeef32b9c950ywan    virtual ~SocketWriter() {
1499233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (sockfd_ != -1)
1500233d2500723e5594f3e7c70896ffeeef32b9c950ywan        CloseConnection();
1501233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1502233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1503233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Sends a string to the socket.
1504233d2500723e5594f3e7c70896ffeeef32b9c950ywan    virtual void Send(const string& message) {
1505233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTEST_CHECK_(sockfd_ != -1)
1506233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "Send() can be called only when there is a connection.";
1507233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1508233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const int len = static_cast<int>(message.length());
1509233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (write(sockfd_, message.c_str(), len) != len) {
1510233d2500723e5594f3e7c70896ffeeef32b9c950ywan        GTEST_LOG_(WARNING)
1511233d2500723e5594f3e7c70896ffeeef32b9c950ywan            << "stream_result_to: failed to stream to "
1512233d2500723e5594f3e7c70896ffeeef32b9c950ywan            << host_name_ << ":" << port_num_;
1513233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
1514233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1515233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1516233d2500723e5594f3e7c70896ffeeef32b9c950ywan   private:
1517233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Creates a client socket and connects to the server.
1518233d2500723e5594f3e7c70896ffeeef32b9c950ywan    void MakeConnection();
1519233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1520233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Closes the socket.
1521233d2500723e5594f3e7c70896ffeeef32b9c950ywan    void CloseConnection() {
1522233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTEST_CHECK_(sockfd_ != -1)
1523233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "CloseConnection() can be called only when there is a connection.";
1524233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1525233d2500723e5594f3e7c70896ffeeef32b9c950ywan      close(sockfd_);
1526233d2500723e5594f3e7c70896ffeeef32b9c950ywan      sockfd_ = -1;
1527233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1528233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1529233d2500723e5594f3e7c70896ffeeef32b9c950ywan    int sockfd_;  // socket file descriptor
1530233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const string host_name_;
1531233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const string port_num_;
1532233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1533233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_DISALLOW_COPY_AND_ASSIGN_(SocketWriter);
1534233d2500723e5594f3e7c70896ffeeef32b9c950ywan  };  // class SocketWriter
1535233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1536233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Escapes '=', '&', '%', and '\n' characters in str as "%xx".
1537233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static string UrlEncode(const char* str);
1538233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1539233d2500723e5594f3e7c70896ffeeef32b9c950ywan  StreamingListener(const string& host, const string& port)
1540233d2500723e5594f3e7c70896ffeeef32b9c950ywan      : socket_writer_(new SocketWriter(host, port)) { Start(); }
1541233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1542233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit StreamingListener(AbstractSocketWriter* socket_writer)
1543233d2500723e5594f3e7c70896ffeeef32b9c950ywan      : socket_writer_(socket_writer) { Start(); }
1544233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1545233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestProgramStart(const UnitTest& /* unit_test */) {
1546233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn("event=TestProgramStart");
1547233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1548233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1549233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestProgramEnd(const UnitTest& unit_test) {
1550233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Note that Google Test current only report elapsed time for each
1551233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // test iteration, not for the entire test program.
1552233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn("event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1553233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1554233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Notify the streaming server to stop.
1555233d2500723e5594f3e7c70896ffeeef32b9c950ywan    socket_writer_->CloseConnection();
1556233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1557233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1558233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestIterationStart(const UnitTest& /* unit_test */, int iteration) {
1559233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn("event=TestIterationStart&iteration=" +
1560233d2500723e5594f3e7c70896ffeeef32b9c950ywan           StreamableToString(iteration));
1561233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1562233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1563233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestIterationEnd(const UnitTest& unit_test, int /* iteration */) {
1564233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn("event=TestIterationEnd&passed=" +
1565233d2500723e5594f3e7c70896ffeeef32b9c950ywan           FormatBool(unit_test.Passed()) + "&elapsed_time=" +
1566233d2500723e5594f3e7c70896ffeeef32b9c950ywan           StreamableToString(unit_test.elapsed_time()) + "ms");
1567233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1568233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1569233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestCaseStart(const TestCase& test_case) {
1570233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn(std::string("event=TestCaseStart&name=") + test_case.name());
1571233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1572233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1573233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestCaseEnd(const TestCase& test_case) {
1574233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn("event=TestCaseEnd&passed=" + FormatBool(test_case.Passed())
1575233d2500723e5594f3e7c70896ffeeef32b9c950ywan           + "&elapsed_time=" + StreamableToString(test_case.elapsed_time())
1576233d2500723e5594f3e7c70896ffeeef32b9c950ywan           + "ms");
1577233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1578233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1579233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestStart(const TestInfo& test_info) {
1580233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn(std::string("event=TestStart&name=") + test_info.name());
1581233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1582233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1583233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestEnd(const TestInfo& test_info) {
1584233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn("event=TestEnd&passed=" +
1585233d2500723e5594f3e7c70896ffeeef32b9c950ywan           FormatBool((test_info.result())->Passed()) +
1586233d2500723e5594f3e7c70896ffeeef32b9c950ywan           "&elapsed_time=" +
1587233d2500723e5594f3e7c70896ffeeef32b9c950ywan           StreamableToString((test_info.result())->elapsed_time()) + "ms");
1588233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1589233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1590233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void OnTestPartResult(const TestPartResult& test_part_result) {
1591233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* file_name = test_part_result.file_name();
1592233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (file_name == NULL)
1593233d2500723e5594f3e7c70896ffeeef32b9c950ywan      file_name = "";
1594233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SendLn("event=TestPartResult&file=" + UrlEncode(file_name) +
1595233d2500723e5594f3e7c70896ffeeef32b9c950ywan           "&line=" + StreamableToString(test_part_result.line_number()) +
1596233d2500723e5594f3e7c70896ffeeef32b9c950ywan           "&message=" + UrlEncode(test_part_result.message()));
1597233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1598233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1599233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
1600233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Sends the given message and a newline to the socket.
1601233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void SendLn(const string& message) { socket_writer_->SendLn(message); }
1602233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1603233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Called at the start of streaming to notify the receiver what
1604233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // protocol we are using.
1605233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void Start() { SendLn("gtest_streaming_protocol_version=1.0"); }
1606233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1607233d2500723e5594f3e7c70896ffeeef32b9c950ywan  string FormatBool(bool value) { return value ? "1" : "0"; }
1608233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1609233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const scoped_ptr<AbstractSocketWriter> socket_writer_;
1610233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1611233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener);
1612233d2500723e5594f3e7c70896ffeeef32b9c950ywan};  // class StreamingListener
1613233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1614233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_CAN_STREAM_RESULTS_
1615233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1616233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
1617233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
1618233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1619233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_SRC_GTEST_INTERNAL_INL_H_
1620233d2500723e5594f3e7c70896ffeeef32b9c950ywan#undef GTEST_IMPLEMENTATION_
1621233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1622233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
1623233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define vsnprintf _vsnprintf
1624233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
1625233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1626233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
1627233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1628233d2500723e5594f3e7c70896ffeeef32b9c950ywanusing internal::CountIf;
1629233d2500723e5594f3e7c70896ffeeef32b9c950ywanusing internal::ForEach;
1630233d2500723e5594f3e7c70896ffeeef32b9c950ywanusing internal::GetElementOr;
1631233d2500723e5594f3e7c70896ffeeef32b9c950ywanusing internal::Shuffle;
1632233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1633233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Constants.
1634233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1635233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A test whose test case name or test name matches this filter is
1636233d2500723e5594f3e7c70896ffeeef32b9c950ywan// disabled and not run.
1637233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kDisableTestFilter[] = "DISABLED_*:*/DISABLED_*";
1638233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1639233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A test case whose name matches this filter is considered a death
1640233d2500723e5594f3e7c70896ffeeef32b9c950ywan// test case and will be run before test cases whose name doesn't
1641233d2500723e5594f3e7c70896ffeeef32b9c950ywan// match this filter.
1642233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*";
1643233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1644233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A test filter that matches everything.
1645233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kUniversalFilter[] = "*";
1646233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1647233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The default output file for XML output.
1648233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kDefaultOutputFile[] = "test_detail.xml";
1649233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1650233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The environment variable name for the test shard index.
1651233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kTestShardIndex[] = "GTEST_SHARD_INDEX";
1652233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The environment variable name for the total number of test shards.
1653233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS";
1654233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The environment variable name for the test shard status file.
1655233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kTestShardStatusFile[] = "GTEST_SHARD_STATUS_FILE";
1656233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1657233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
1658233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1659233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The text used in failure messages to indicate the start of the
1660233d2500723e5594f3e7c70896ffeeef32b9c950ywan// stack trace.
1661233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kStackTraceMarker[] = "\nStack trace:\n";
1662233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1663233d2500723e5594f3e7c70896ffeeef32b9c950ywan// g_help_flag is true iff the --help flag or an equivalent form is
1664233d2500723e5594f3e7c70896ffeeef32b9c950ywan// specified on the command line.
1665233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool g_help_flag = false;
1666233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1667233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
1668233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1669233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char* GetDefaultFilter() {
1670233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return kUniversalFilter;
1671233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1672233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1673233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(
1674233d2500723e5594f3e7c70896ffeeef32b9c950ywan    also_run_disabled_tests,
1675233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::BoolFromGTestEnv("also_run_disabled_tests", false),
1676233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Run disabled tests too, in addition to the tests normally being run.");
1677233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1678233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(
1679233d2500723e5594f3e7c70896ffeeef32b9c950ywan    break_on_failure,
1680233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::BoolFromGTestEnv("break_on_failure", false),
1681233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "True iff a failed assertion should be a debugger break-point.");
1682233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1683233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(
1684233d2500723e5594f3e7c70896ffeeef32b9c950ywan    catch_exceptions,
1685233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::BoolFromGTestEnv("catch_exceptions", true),
1686233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "True iff " GTEST_NAME_
1687233d2500723e5594f3e7c70896ffeeef32b9c950ywan    " should catch exceptions and treat them as test failures.");
1688233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1689233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_string_(
1690233d2500723e5594f3e7c70896ffeeef32b9c950ywan    color,
1691233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::StringFromGTestEnv("color", "auto"),
1692233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Whether to use colors in the output.  Valid values: yes, no, "
1693233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "and auto.  'auto' means to use colors if the output is "
1694233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "being sent to a terminal and the TERM environment variable "
1695233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "is set to a terminal type that supports colors.");
1696233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1697233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_string_(
1698233d2500723e5594f3e7c70896ffeeef32b9c950ywan    filter,
1699233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::StringFromGTestEnv("filter", GetDefaultFilter()),
1700233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "A colon-separated list of glob (not regex) patterns "
1701233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "for filtering the tests to run, optionally followed by a "
1702233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "'-' and a : separated list of negative patterns (tests to "
1703233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "exclude).  A test is run if it matches one of the positive "
1704233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "patterns and does not match any of the negative patterns.");
1705233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1706233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(list_tests, false,
1707233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   "List all tests without running them.");
1708233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1709233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_string_(
1710233d2500723e5594f3e7c70896ffeeef32b9c950ywan    output,
1711233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::StringFromGTestEnv("output", ""),
1712233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "A format (currently must be \"xml\"), optionally followed "
1713233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "by a colon and an output file name or directory. A directory "
1714233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "is indicated by a trailing pathname separator. "
1715233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Examples: \"xml:filename.xml\", \"xml::directoryname/\". "
1716233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "If a directory is specified, output files will be created "
1717233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "within that directory, with file-names based on the test "
1718233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "executable's name and, if necessary, made unique by adding "
1719233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "digits.");
1720233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1721233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(
1722233d2500723e5594f3e7c70896ffeeef32b9c950ywan    print_time,
1723233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::BoolFromGTestEnv("print_time", true),
1724233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "True iff " GTEST_NAME_
1725233d2500723e5594f3e7c70896ffeeef32b9c950ywan    " should display elapsed time in text output.");
1726233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1727233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_int32_(
1728233d2500723e5594f3e7c70896ffeeef32b9c950ywan    random_seed,
1729233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::Int32FromGTestEnv("random_seed", 0),
1730233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Random number seed to use when shuffling test orders.  Must be in range "
1731233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "[1, 99999], or 0 to use a seed based on the current time.");
1732233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1733233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_int32_(
1734233d2500723e5594f3e7c70896ffeeef32b9c950ywan    repeat,
1735233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::Int32FromGTestEnv("repeat", 1),
1736233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "How many times to repeat each test.  Specify a negative number "
1737233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "for repeating forever.  Useful for shaking out flaky tests.");
1738233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1739233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(
1740233d2500723e5594f3e7c70896ffeeef32b9c950ywan    show_internal_stack_frames, false,
1741233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "True iff " GTEST_NAME_ " should include internal stack frames when "
1742233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "printing test failure stack traces.");
1743233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1744233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(
1745233d2500723e5594f3e7c70896ffeeef32b9c950ywan    shuffle,
1746233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::BoolFromGTestEnv("shuffle", false),
1747233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "True iff " GTEST_NAME_
1748233d2500723e5594f3e7c70896ffeeef32b9c950ywan    " should randomize tests' order on every run.");
1749233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1750233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_int32_(
1751233d2500723e5594f3e7c70896ffeeef32b9c950ywan    stack_trace_depth,
1752233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::Int32FromGTestEnv("stack_trace_depth", kMaxStackTraceDepth),
1753233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "The maximum number of stack frames to print when an "
1754233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "assertion fails.  The valid range is 0 through 100, inclusive.");
1755233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1756233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_string_(
1757233d2500723e5594f3e7c70896ffeeef32b9c950ywan    stream_result_to,
1758233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::StringFromGTestEnv("stream_result_to", ""),
1759233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "This flag specifies the host name and the port number on which to stream "
1760233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "test results. Example: \"localhost:555\". The flag is effective only on "
1761233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Linux.");
1762233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1763233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(
1764233d2500723e5594f3e7c70896ffeeef32b9c950ywan    throw_on_failure,
1765233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::BoolFromGTestEnv("throw_on_failure", false),
1766233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "When this flag is specified, a failed assertion will throw an exception "
1767233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "if exceptions are enabled or exit the program with a non-zero code "
1768233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "otherwise.");
1769233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1770233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
1771233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1772233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Generates a random number from [0, range), using a Linear
1773233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Congruential Generator (LCG).  Crashes if 'range' is 0 or greater
1774233d2500723e5594f3e7c70896ffeeef32b9c950ywan// than kMaxRange.
1775233d2500723e5594f3e7c70896ffeeef32b9c950ywanUInt32 Random::Generate(UInt32 range) {
1776233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // These constants are the same as are used in glibc's rand(3).
1777233d2500723e5594f3e7c70896ffeeef32b9c950ywan  state_ = (1103515245U*state_ + 12345U) % kMaxRange;
1778233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1779233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(range > 0)
1780233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Cannot generate a number in the range [0, 0).";
1781233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(range <= kMaxRange)
1782233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Generation of a number in [0, " << range << ") was requested, "
1783233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "but this can only generate numbers in [0, " << kMaxRange << ").";
1784233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1785233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Converting via modulus introduces a bit of downward bias, but
1786233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it's simple, and a linear congruential generator isn't too good
1787233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // to begin with.
1788233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return state_ % range;
1789233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1790233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1791233d2500723e5594f3e7c70896ffeeef32b9c950ywan// GTestIsInitialized() returns true iff the user has initialized
1792233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test.  Useful for catching the user mistake of not initializing
1793233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test before calling RUN_ALL_TESTS().
1794233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
1795233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A user must call testing::InitGoogleTest() to initialize Google
1796233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Test.  g_init_gtest_count is set to the number of times
1797233d2500723e5594f3e7c70896ffeeef32b9c950ywan// InitGoogleTest() has been called.  We don't protect this variable
1798233d2500723e5594f3e7c70896ffeeef32b9c950ywan// under a mutex as it is only accessed in the main thread.
1799233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ int g_init_gtest_count = 0;
1800233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool GTestIsInitialized() { return g_init_gtest_count != 0; }
1801233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1802233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Iterates over a vector of TestCases, keeping a running sum of the
1803233d2500723e5594f3e7c70896ffeeef32b9c950ywan// results of calling a given int-returning method on each.
1804233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the sum.
1805233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic int SumOverTestCaseList(const std::vector<TestCase*>& case_list,
1806233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               int (TestCase::*method)() const) {
1807233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int sum = 0;
1808233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < case_list.size(); i++) {
1809233d2500723e5594f3e7c70896ffeeef32b9c950ywan    sum += (case_list[i]->*method)();
1810233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1811233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return sum;
1812233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1813233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1814233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the test case passed.
1815233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool TestCasePassed(const TestCase* test_case) {
1816233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return test_case->should_run() && test_case->Passed();
1817233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1818233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1819233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the test case failed.
1820233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool TestCaseFailed(const TestCase* test_case) {
1821233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return test_case->should_run() && test_case->Failed();
1822233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1823233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1824233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff test_case contains at least one test that should
1825233d2500723e5594f3e7c70896ffeeef32b9c950ywan// run.
1826233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool ShouldRunTestCase(const TestCase* test_case) {
1827233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return test_case->should_run();
1828233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1829233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1830233d2500723e5594f3e7c70896ffeeef32b9c950ywan// AssertHelper constructor.
1831233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertHelper::AssertHelper(TestPartResult::Type type,
1832233d2500723e5594f3e7c70896ffeeef32b9c950ywan                           const char* file,
1833233d2500723e5594f3e7c70896ffeeef32b9c950ywan                           int line,
1834233d2500723e5594f3e7c70896ffeeef32b9c950ywan                           const char* message)
1835233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : data_(new AssertHelperData(type, file, line, message)) {
1836233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1837233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1838233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertHelper::~AssertHelper() {
1839233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete data_;
1840233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1841233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1842233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Message assignment, for assertion streaming support.
1843233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid AssertHelper::operator=(const Message& message) const {
1844233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTest::GetInstance()->
1845233d2500723e5594f3e7c70896ffeeef32b9c950ywan    AddTestPartResult(data_->type, data_->file, data_->line,
1846233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      AppendUserMessage(data_->message, message),
1847233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      UnitTest::GetInstance()->impl()
1848233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      ->CurrentOsStackTraceExceptTop(1)
1849233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      // Skips the stack frame for this function itself.
1850233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      );  // NOLINT
1851233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1852233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1853233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Mutex for linked pointers.
1854233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_linked_ptr_mutex);
1855233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1856233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Application pathname gotten in InitGoogleTest.
1857233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string g_executable_path;
1858233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1859233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current application's name, removing directory path if that
1860233d2500723e5594f3e7c70896ffeeef32b9c950ywan// is present.
1861233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath GetCurrentExecutableName() {
1862233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FilePath result;
1863233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1864233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
1865233d2500723e5594f3e7c70896ffeeef32b9c950ywan  result.Set(FilePath(g_executable_path).RemoveExtension("exe"));
1866233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
1867233d2500723e5594f3e7c70896ffeeef32b9c950ywan  result.Set(FilePath(g_executable_path));
1868233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
1869233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1870233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result.RemoveDirectoryName();
1871233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1872233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1873233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Functions for processing the gtest_output flag.
1874233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1875233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the output format, or "" for normal printed output.
1876233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string UnitTestOptions::GetOutputFormat() {
1877233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const gtest_output_flag = GTEST_FLAG(output).c_str();
1878233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (gtest_output_flag == NULL) return std::string("");
1879233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1880233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const colon = strchr(gtest_output_flag, ':');
1881233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (colon == NULL) ?
1882233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::string(gtest_output_flag) :
1883233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::string(gtest_output_flag, colon - gtest_output_flag);
1884233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1885233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1886233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the name of the requested output file, or the default if none
1887233d2500723e5594f3e7c70896ffeeef32b9c950ywan// was explicitly specified.
1888233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string UnitTestOptions::GetAbsolutePathToOutputFile() {
1889233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const gtest_output_flag = GTEST_FLAG(output).c_str();
1890233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (gtest_output_flag == NULL)
1891233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return "";
1892233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1893233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const colon = strchr(gtest_output_flag, ':');
1894233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (colon == NULL)
1895233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return internal::FilePath::ConcatPaths(
1896233d2500723e5594f3e7c70896ffeeef32b9c950ywan        internal::FilePath(
1897233d2500723e5594f3e7c70896ffeeef32b9c950ywan            UnitTest::GetInstance()->original_working_dir()),
1898233d2500723e5594f3e7c70896ffeeef32b9c950ywan        internal::FilePath(kDefaultOutputFile)).string();
1899233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1900233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::FilePath output_name(colon + 1);
1901233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!output_name.IsAbsolutePath())
1902233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // TODO(wan@google.com): on Windows \some\path is not an absolute
1903233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // path (as its meaning depends on the current drive), yet the
1904233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // following logic for turning it into an absolute path is wrong.
1905233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Fix it.
1906233d2500723e5594f3e7c70896ffeeef32b9c950ywan    output_name = internal::FilePath::ConcatPaths(
1907233d2500723e5594f3e7c70896ffeeef32b9c950ywan        internal::FilePath(UnitTest::GetInstance()->original_working_dir()),
1908233d2500723e5594f3e7c70896ffeeef32b9c950ywan        internal::FilePath(colon + 1));
1909233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1910233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!output_name.IsDirectory())
1911233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return output_name.string();
1912233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1913233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::FilePath result(internal::FilePath::GenerateUniqueFileName(
1914233d2500723e5594f3e7c70896ffeeef32b9c950ywan      output_name, internal::GetCurrentExecutableName(),
1915233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GetOutputFormat().c_str()));
1916233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result.string();
1917233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1918233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1919233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the wildcard pattern matches the string.  The
1920233d2500723e5594f3e7c70896ffeeef32b9c950ywan// first ':' or '\0' character in pattern marks the end of it.
1921233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
1922233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This recursive algorithm isn't very efficient, but is clear and
1923233d2500723e5594f3e7c70896ffeeef32b9c950ywan// works well enough for matching test names, which are short.
1924233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool UnitTestOptions::PatternMatchesString(const char *pattern,
1925233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                           const char *str) {
1926233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (*pattern) {
1927233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case '\0':
1928233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case ':':  // Either ':' or '\0' marks the end of the pattern.
1929233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return *str == '\0';
1930233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case '?':  // Matches any single character.
1931233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return *str != '\0' && PatternMatchesString(pattern + 1, str + 1);
1932233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case '*':  // Matches any string (possibly empty) of characters.
1933233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return (*str != '\0' && PatternMatchesString(pattern, str + 1)) ||
1934233d2500723e5594f3e7c70896ffeeef32b9c950ywan          PatternMatchesString(pattern + 1, str);
1935233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:  // Non-special character.  Matches itself.
1936233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return *pattern == *str &&
1937233d2500723e5594f3e7c70896ffeeef32b9c950ywan          PatternMatchesString(pattern + 1, str + 1);
1938233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1939233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1940233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1941233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool UnitTestOptions::MatchesFilter(
1942233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& name, const char* filter) {
1943233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char *cur_pattern = filter;
1944233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (;;) {
1945233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (PatternMatchesString(cur_pattern, name.c_str())) {
1946233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return true;
1947233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1948233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1949233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Finds the next pattern in the filter.
1950233d2500723e5594f3e7c70896ffeeef32b9c950ywan    cur_pattern = strchr(cur_pattern, ':');
1951233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1952233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Returns if no more pattern can be found.
1953233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (cur_pattern == NULL) {
1954233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return false;
1955233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1956233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1957233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Skips the pattern separater (the ':' character).
1958233d2500723e5594f3e7c70896ffeeef32b9c950ywan    cur_pattern++;
1959233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1960233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1961233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1962233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the user-specified filter matches the test case
1963233d2500723e5594f3e7c70896ffeeef32b9c950ywan// name and the test name.
1964233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool UnitTestOptions::FilterMatchesTest(const std::string &test_case_name,
1965233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                        const std::string &test_name) {
1966233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string& full_name = test_case_name + "." + test_name.c_str();
1967233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1968233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Split --gtest_filter at '-', if there is one, to separate into
1969233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // positive filter and negative filter portions
1970233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const p = GTEST_FLAG(filter).c_str();
1971233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const dash = strchr(p, '-');
1972233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string positive;
1973233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string negative;
1974233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (dash == NULL) {
1975233d2500723e5594f3e7c70896ffeeef32b9c950ywan    positive = GTEST_FLAG(filter).c_str();  // Whole string is a positive filter
1976233d2500723e5594f3e7c70896ffeeef32b9c950ywan    negative = "";
1977233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
1978233d2500723e5594f3e7c70896ffeeef32b9c950ywan    positive = std::string(p, dash);   // Everything up to the dash
1979233d2500723e5594f3e7c70896ffeeef32b9c950ywan    negative = std::string(dash + 1);  // Everything after the dash
1980233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (positive.empty()) {
1981233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Treat '-test1' as the same as '*-test1'
1982233d2500723e5594f3e7c70896ffeeef32b9c950ywan      positive = kUniversalFilter;
1983233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
1984233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
1985233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1986233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // A filter is a colon-separated list of patterns.  It matches a
1987233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // test if any pattern in it matches the test.
1988233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (MatchesFilter(full_name, positive.c_str()) &&
1989233d2500723e5594f3e7c70896ffeeef32b9c950ywan          !MatchesFilter(full_name, negative.c_str()));
1990233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
1991233d2500723e5594f3e7c70896ffeeef32b9c950ywan
1992233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_SEH
1993233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
1994233d2500723e5594f3e7c70896ffeeef32b9c950ywan// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
1995233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This function is useful as an __except condition.
1996233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) {
1997233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Google Test should handle a SEH exception if:
1998233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   1. the user wants it to, AND
1999233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   2. this is not a breakpoint exception, AND
2000233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   3. this is not a C++ exception (VC++ implements them via SEH,
2001233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //      apparently).
2002233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
2003233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // SEH exception code for C++ exceptions.
2004233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // (see http://support.microsoft.com/kb/185294 for more information).
2005233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const DWORD kCxxExceptionCode = 0xe06d7363;
2006233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2007233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool should_handle = true;
2008233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2009233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!GTEST_FLAG(catch_exceptions))
2010233d2500723e5594f3e7c70896ffeeef32b9c950ywan    should_handle = false;
2011233d2500723e5594f3e7c70896ffeeef32b9c950ywan  else if (exception_code == EXCEPTION_BREAKPOINT)
2012233d2500723e5594f3e7c70896ffeeef32b9c950ywan    should_handle = false;
2013233d2500723e5594f3e7c70896ffeeef32b9c950ywan  else if (exception_code == kCxxExceptionCode)
2014233d2500723e5594f3e7c70896ffeeef32b9c950ywan    should_handle = false;
2015233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2016233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
2017233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2018233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_SEH
2019233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2020233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
2021233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2022233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The c'tor sets this object as the test part result reporter used by
2023233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test.  The 'result' parameter specifies where to report the
2024233d2500723e5594f3e7c70896ffeeef32b9c950ywan// results. Intercepts only failures from the current thread.
2025233d2500723e5594f3e7c70896ffeeef32b9c950ywanScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter(
2026233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TestPartResultArray* result)
2027233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD),
2028233d2500723e5594f3e7c70896ffeeef32b9c950ywan      result_(result) {
2029233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Init();
2030233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2031233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2032233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The c'tor sets this object as the test part result reporter used by
2033233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test.  The 'result' parameter specifies where to report the
2034233d2500723e5594f3e7c70896ffeeef32b9c950ywan// results.
2035233d2500723e5594f3e7c70896ffeeef32b9c950ywanScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter(
2036233d2500723e5594f3e7c70896ffeeef32b9c950ywan    InterceptMode intercept_mode, TestPartResultArray* result)
2037233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : intercept_mode_(intercept_mode),
2038233d2500723e5594f3e7c70896ffeeef32b9c950ywan      result_(result) {
2039233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Init();
2040233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2041233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2042233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ScopedFakeTestPartResultReporter::Init() {
2043233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
2044233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (intercept_mode_ == INTERCEPT_ALL_THREADS) {
2045233d2500723e5594f3e7c70896ffeeef32b9c950ywan    old_reporter_ = impl->GetGlobalTestPartResultReporter();
2046233d2500723e5594f3e7c70896ffeeef32b9c950ywan    impl->SetGlobalTestPartResultReporter(this);
2047233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
2048233d2500723e5594f3e7c70896ffeeef32b9c950ywan    old_reporter_ = impl->GetTestPartResultReporterForCurrentThread();
2049233d2500723e5594f3e7c70896ffeeef32b9c950ywan    impl->SetTestPartResultReporterForCurrentThread(this);
2050233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2051233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2052233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2053233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The d'tor restores the test part result reporter used by Google Test
2054233d2500723e5594f3e7c70896ffeeef32b9c950ywan// before.
2055233d2500723e5594f3e7c70896ffeeef32b9c950ywanScopedFakeTestPartResultReporter::~ScopedFakeTestPartResultReporter() {
2056233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
2057233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (intercept_mode_ == INTERCEPT_ALL_THREADS) {
2058233d2500723e5594f3e7c70896ffeeef32b9c950ywan    impl->SetGlobalTestPartResultReporter(old_reporter_);
2059233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
2060233d2500723e5594f3e7c70896ffeeef32b9c950ywan    impl->SetTestPartResultReporterForCurrentThread(old_reporter_);
2061233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2062233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2063233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2064233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Increments the test part result count and remembers the result.
2065233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This method is from the TestPartResultReporterInterface interface.
2066233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ScopedFakeTestPartResultReporter::ReportTestPartResult(
2067233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& result) {
2068233d2500723e5594f3e7c70896ffeeef32b9c950ywan  result_->Append(result);
2069233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2070233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2071233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
2072233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2073233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the type ID of ::testing::Test.  We should always call this
2074233d2500723e5594f3e7c70896ffeeef32b9c950ywan// instead of GetTypeId< ::testing::Test>() to get the type ID of
2075233d2500723e5594f3e7c70896ffeeef32b9c950ywan// testing::Test.  This is to work around a suspected linker bug when
2076233d2500723e5594f3e7c70896ffeeef32b9c950ywan// using Google Test as a framework on Mac OS X.  The bug causes
2077233d2500723e5594f3e7c70896ffeeef32b9c950ywan// GetTypeId< ::testing::Test>() to return different values depending
2078233d2500723e5594f3e7c70896ffeeef32b9c950ywan// on whether the call is from the Google Test framework itself or
2079233d2500723e5594f3e7c70896ffeeef32b9c950ywan// from user test code.  GetTestTypeId() is guaranteed to always
2080233d2500723e5594f3e7c70896ffeeef32b9c950ywan// return the same value, as it always calls GetTypeId<>() from the
2081233d2500723e5594f3e7c70896ffeeef32b9c950ywan// gtest.cc, which is within the Google Test framework.
2082233d2500723e5594f3e7c70896ffeeef32b9c950ywanTypeId GetTestTypeId() {
2083233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return GetTypeId<Test>();
2084233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2085233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2086233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The value of GetTestTypeId() as seen from within the Google Test
2087233d2500723e5594f3e7c70896ffeeef32b9c950ywan// library.  This is solely for testing GetTestTypeId().
2088233d2500723e5594f3e7c70896ffeeef32b9c950ywanextern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId();
2089233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2090233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This predicate-formatter checks that 'results' contains a test part
2091233d2500723e5594f3e7c70896ffeeef32b9c950ywan// failure of the given type and that the failure message contains the
2092233d2500723e5594f3e7c70896ffeeef32b9c950ywan// given substring.
2093233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult HasOneFailure(const char* /* results_expr */,
2094233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const char* /* type_expr */,
2095233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const char* /* substr_expr */,
2096233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const TestPartResultArray& results,
2097233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              TestPartResult::Type type,
2098233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const string& substr) {
2099233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string expected(type == TestPartResult::kFatalFailure ?
2100233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        "1 fatal failure" :
2101233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        "1 non-fatal failure");
2102233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message msg;
2103233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (results.size() != 1) {
2104233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << "Expected: " << expected << "\n"
2105233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << "  Actual: " << results.size() << " failures";
2106233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (int i = 0; i < results.size(); i++) {
2107233d2500723e5594f3e7c70896ffeeef32b9c950ywan      msg << "\n" << results.GetTestPartResult(i);
2108233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
2109233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionFailure() << msg;
2110233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2111233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2112233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestPartResult& r = results.GetTestPartResult(0);
2113233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (r.type() != type) {
2114233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionFailure() << "Expected: " << expected << "\n"
2115233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              << "  Actual:\n"
2116233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              << r;
2117233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2118233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2119233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (strstr(r.message(), substr.c_str()) == NULL) {
2120233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionFailure() << "Expected: " << expected << " containing \""
2121233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              << substr << "\"\n"
2122233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              << "  Actual:\n"
2123233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              << r;
2124233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2125233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2126233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionSuccess();
2127233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2128233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2129233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The constructor of SingleFailureChecker remembers where to look up
2130233d2500723e5594f3e7c70896ffeeef32b9c950ywan// test part results, what type of failure we expect, and what
2131233d2500723e5594f3e7c70896ffeeef32b9c950ywan// substring the failure message should contain.
2132233d2500723e5594f3e7c70896ffeeef32b9c950ywanSingleFailureChecker:: SingleFailureChecker(
2133233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResultArray* results,
2134233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TestPartResult::Type type,
2135233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const string& substr)
2136233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : results_(results),
2137233d2500723e5594f3e7c70896ffeeef32b9c950ywan      type_(type),
2138233d2500723e5594f3e7c70896ffeeef32b9c950ywan      substr_(substr) {}
2139233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2140233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The destructor of SingleFailureChecker verifies that the given
2141233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TestPartResultArray contains exactly one failure that has the given
2142233d2500723e5594f3e7c70896ffeeef32b9c950ywan// type and contains the given substring.  If that's not the case, a
2143233d2500723e5594f3e7c70896ffeeef32b9c950ywan// non-fatal failure will be generated.
2144233d2500723e5594f3e7c70896ffeeef32b9c950ywanSingleFailureChecker::~SingleFailureChecker() {
2145233d2500723e5594f3e7c70896ffeeef32b9c950ywan  EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_);
2146233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2147233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2148233d2500723e5594f3e7c70896ffeeef32b9c950ywanDefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter(
2149233d2500723e5594f3e7c70896ffeeef32b9c950ywan    UnitTestImpl* unit_test) : unit_test_(unit_test) {}
2150233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2151233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid DefaultGlobalTestPartResultReporter::ReportTestPartResult(
2152233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& result) {
2153233d2500723e5594f3e7c70896ffeeef32b9c950ywan  unit_test_->current_test_result()->AddTestPartResult(result);
2154233d2500723e5594f3e7c70896ffeeef32b9c950ywan  unit_test_->listeners()->repeater()->OnTestPartResult(result);
2155233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2156233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2157233d2500723e5594f3e7c70896ffeeef32b9c950ywanDefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter(
2158233d2500723e5594f3e7c70896ffeeef32b9c950ywan    UnitTestImpl* unit_test) : unit_test_(unit_test) {}
2159233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2160233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid DefaultPerThreadTestPartResultReporter::ReportTestPartResult(
2161233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& result) {
2162233d2500723e5594f3e7c70896ffeeef32b9c950ywan  unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result);
2163233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2164233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2165233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the global test part result reporter.
2166233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestPartResultReporterInterface*
2167233d2500723e5594f3e7c70896ffeeef32b9c950ywanUnitTestImpl::GetGlobalTestPartResultReporter() {
2168233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::MutexLock lock(&global_test_part_result_reporter_mutex_);
2169233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return global_test_part_result_repoter_;
2170233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2171233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2172233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Sets the global test part result reporter.
2173233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::SetGlobalTestPartResultReporter(
2174233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TestPartResultReporterInterface* reporter) {
2175233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::MutexLock lock(&global_test_part_result_reporter_mutex_);
2176233d2500723e5594f3e7c70896ffeeef32b9c950ywan  global_test_part_result_repoter_ = reporter;
2177233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2178233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2179233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the test part result reporter for the current thread.
2180233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestPartResultReporterInterface*
2181233d2500723e5594f3e7c70896ffeeef32b9c950ywanUnitTestImpl::GetTestPartResultReporterForCurrentThread() {
2182233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return per_thread_test_part_result_reporter_.get();
2183233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2184233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2185233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Sets the test part result reporter for the current thread.
2186233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::SetTestPartResultReporterForCurrentThread(
2187233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TestPartResultReporterInterface* reporter) {
2188233d2500723e5594f3e7c70896ffeeef32b9c950ywan  per_thread_test_part_result_reporter_.set(reporter);
2189233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2190233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2191233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of successful test cases.
2192233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::successful_test_case_count() const {
2193233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_cases_, TestCasePassed);
2194233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2195233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2196233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of failed test cases.
2197233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::failed_test_case_count() const {
2198233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_cases_, TestCaseFailed);
2199233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2200233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2201233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of all test cases.
2202233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::total_test_case_count() const {
2203233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return static_cast<int>(test_cases_.size());
2204233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2205233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2206233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of all test cases that contain at least one test
2207233d2500723e5594f3e7c70896ffeeef32b9c950ywan// that should run.
2208233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::test_case_to_run_count() const {
2209233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_cases_, ShouldRunTestCase);
2210233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2211233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2212233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of successful tests.
2213233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::successful_test_count() const {
2214233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return SumOverTestCaseList(test_cases_, &TestCase::successful_test_count);
2215233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2216233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2217233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of failed tests.
2218233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::failed_test_count() const {
2219233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return SumOverTestCaseList(test_cases_, &TestCase::failed_test_count);
2220233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2221233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2222233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of disabled tests that will be reported in the XML report.
2223233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::reportable_disabled_test_count() const {
2224233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return SumOverTestCaseList(test_cases_,
2225233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             &TestCase::reportable_disabled_test_count);
2226233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2227233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2228233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of disabled tests.
2229233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::disabled_test_count() const {
2230233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return SumOverTestCaseList(test_cases_, &TestCase::disabled_test_count);
2231233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2232233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2233233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of tests to be printed in the XML report.
2234233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::reportable_test_count() const {
2235233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return SumOverTestCaseList(test_cases_, &TestCase::reportable_test_count);
2236233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2237233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2238233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of all tests.
2239233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::total_test_count() const {
2240233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return SumOverTestCaseList(test_cases_, &TestCase::total_test_count);
2241233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2242233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2243233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of tests that should run.
2244233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::test_to_run_count() const {
2245233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return SumOverTestCaseList(test_cases_, &TestCase::test_to_run_count);
2246233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2247233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2248233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current OS stack trace as an std::string.
2249233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
2250233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The maximum number of stack frames to be included is specified by
2251233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the gtest_stack_trace_depth flag.  The skip_count parameter
2252233d2500723e5594f3e7c70896ffeeef32b9c950ywan// specifies the number of top frames to be skipped, which doesn't
2253233d2500723e5594f3e7c70896ffeeef32b9c950ywan// count against the number of frames to be included.
2254233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
2255233d2500723e5594f3e7c70896ffeeef32b9c950ywan// For example, if Foo() calls Bar(), which in turn calls
2256233d2500723e5594f3e7c70896ffeeef32b9c950ywan// CurrentOsStackTraceExceptTop(1), Foo() will be included in the
2257233d2500723e5594f3e7c70896ffeeef32b9c950ywan// trace but Bar() and CurrentOsStackTraceExceptTop() won't.
2258233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) {
2259233d2500723e5594f3e7c70896ffeeef32b9c950ywan  (void)skip_count;
2260233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return "";
2261233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2262233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2263233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current time in milliseconds.
2264233d2500723e5594f3e7c70896ffeeef32b9c950ywanTimeInMillis GetTimeInMillis() {
2265233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE || defined(__BORLANDC__)
2266233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Difference between 1970-01-01 and 1601-01-01 in milliseconds.
2267233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // http://analogous.blogspot.com/2005/04/epoch.html
2268233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TimeInMillis kJavaEpochToWinFileTimeDelta =
2269233d2500723e5594f3e7c70896ffeeef32b9c950ywan    static_cast<TimeInMillis>(116444736UL) * 100000UL;
2270233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const DWORD kTenthMicrosInMilliSecond = 10000;
2271233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2272233d2500723e5594f3e7c70896ffeeef32b9c950ywan  SYSTEMTIME now_systime;
2273233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FILETIME now_filetime;
2274233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ULARGE_INTEGER now_int64;
2275233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TODO(kenton@google.com): Shouldn't this just use
2276233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   GetSystemTimeAsFileTime()?
2277233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetSystemTime(&now_systime);
2278233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (SystemTimeToFileTime(&now_systime, &now_filetime)) {
2279233d2500723e5594f3e7c70896ffeeef32b9c950ywan    now_int64.LowPart = now_filetime.dwLowDateTime;
2280233d2500723e5594f3e7c70896ffeeef32b9c950ywan    now_int64.HighPart = now_filetime.dwHighDateTime;
2281233d2500723e5594f3e7c70896ffeeef32b9c950ywan    now_int64.QuadPart = (now_int64.QuadPart / kTenthMicrosInMilliSecond) -
2282233d2500723e5594f3e7c70896ffeeef32b9c950ywan      kJavaEpochToWinFileTimeDelta;
2283233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return now_int64.QuadPart;
2284233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2285233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return 0;
2286233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_WINDOWS && !GTEST_HAS_GETTIMEOFDAY_
2287233d2500723e5594f3e7c70896ffeeef32b9c950ywan  __timeb64 now;
2288233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2289233d2500723e5594f3e7c70896ffeeef32b9c950ywan# ifdef _MSC_VER
2290233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2291233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // MSVC 8 deprecates _ftime64(), so we want to suppress warning 4996
2292233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // (deprecated function) there.
2293233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TODO(kenton@google.com): Use GetTickCount()?  Or use
2294233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   SystemTimeToFileTime()
2295233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  pragma warning(push)          // Saves the current warning state.
2296233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  pragma warning(disable:4996)  // Temporarily disables warning 4996.
2297233d2500723e5594f3e7c70896ffeeef32b9c950ywan  _ftime64(&now);
2298233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  pragma warning(pop)           // Restores the warning state.
2299233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
2300233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2301233d2500723e5594f3e7c70896ffeeef32b9c950ywan  _ftime64(&now);
2302233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2303233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // _MSC_VER
2304233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2305233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return static_cast<TimeInMillis>(now.time) * 1000 + now.millitm;
2306233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_HAS_GETTIMEOFDAY_
2307233d2500723e5594f3e7c70896ffeeef32b9c950ywan  struct timeval now;
2308233d2500723e5594f3e7c70896ffeeef32b9c950ywan  gettimeofday(&now, NULL);
2309233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return static_cast<TimeInMillis>(now.tv_sec) * 1000 + now.tv_usec / 1000;
2310233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
2311233d2500723e5594f3e7c70896ffeeef32b9c950ywan# error "Don't know how to get the current time on your system."
2312233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
2313233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2314233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2315233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Utilities
2316233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2317233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class String.
2318233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2319233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE
2320233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates a UTF-16 wide string from the given ANSI string, allocating
2321233d2500723e5594f3e7c70896ffeeef32b9c950ywan// memory using new. The caller is responsible for deleting the return
2322233d2500723e5594f3e7c70896ffeeef32b9c950ywan// value using delete[]. Returns the wide string, or NULL if the
2323233d2500723e5594f3e7c70896ffeeef32b9c950ywan// input is NULL.
2324233d2500723e5594f3e7c70896ffeeef32b9c950ywanLPCWSTR String::AnsiToUtf16(const char* ansi) {
2325233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!ansi) return NULL;
2326233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int length = strlen(ansi);
2327233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int unicode_length =
2328233d2500723e5594f3e7c70896ffeeef32b9c950ywan      MultiByteToWideChar(CP_ACP, 0, ansi, length,
2329233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          NULL, 0);
2330233d2500723e5594f3e7c70896ffeeef32b9c950ywan  WCHAR* unicode = new WCHAR[unicode_length + 1];
2331233d2500723e5594f3e7c70896ffeeef32b9c950ywan  MultiByteToWideChar(CP_ACP, 0, ansi, length,
2332233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      unicode, unicode_length);
2333233d2500723e5594f3e7c70896ffeeef32b9c950ywan  unicode[unicode_length] = 0;
2334233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return unicode;
2335233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2336233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2337233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates an ANSI string from the given wide string, allocating
2338233d2500723e5594f3e7c70896ffeeef32b9c950ywan// memory using new. The caller is responsible for deleting the return
2339233d2500723e5594f3e7c70896ffeeef32b9c950ywan// value using delete[]. Returns the ANSI string, or NULL if the
2340233d2500723e5594f3e7c70896ffeeef32b9c950ywan// input is NULL.
2341233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* String::Utf16ToAnsi(LPCWSTR utf16_str)  {
2342233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!utf16_str) return NULL;
2343233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int ansi_length =
2344233d2500723e5594f3e7c70896ffeeef32b9c950ywan      WideCharToMultiByte(CP_ACP, 0, utf16_str, -1,
2345233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          NULL, 0, NULL, NULL);
2346233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* ansi = new char[ansi_length + 1];
2347233d2500723e5594f3e7c70896ffeeef32b9c950ywan  WideCharToMultiByte(CP_ACP, 0, utf16_str, -1,
2348233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      ansi, ansi_length, NULL, NULL);
2349233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ansi[ansi_length] = 0;
2350233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ansi;
2351233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2352233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2353233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE
2354233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2355233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Compares two C strings.  Returns true iff they have the same content.
2356233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
2357233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Unlike strcmp(), this function can handle NULL argument(s).  A NULL
2358233d2500723e5594f3e7c70896ffeeef32b9c950ywan// C string is considered different to any non-NULL C string,
2359233d2500723e5594f3e7c70896ffeeef32b9c950ywan// including the empty string.
2360233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool String::CStringEquals(const char * lhs, const char * rhs) {
2361233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if ( lhs == NULL ) return rhs == NULL;
2362233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2363233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if ( rhs == NULL ) return false;
2364233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2365233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return strcmp(lhs, rhs) == 0;
2366233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2367233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2368233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
2369233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2370233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts an array of wide chars to a narrow string using the UTF-8
2371233d2500723e5594f3e7c70896ffeeef32b9c950ywan// encoding, and streams the result to the given Message object.
2372233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void StreamWideCharsToMessage(const wchar_t* wstr, size_t length,
2373233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     Message* msg) {
2374233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i != length; ) {  // NOLINT
2375233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (wstr[i] != L'\0') {
2376233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *msg << WideStringToUtf8(wstr + i, static_cast<int>(length - i));
2377233d2500723e5594f3e7c70896ffeeef32b9c950ywan      while (i != length && wstr[i] != L'\0')
2378233d2500723e5594f3e7c70896ffeeef32b9c950ywan        i++;
2379233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
2380233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *msg << '\0';
2381233d2500723e5594f3e7c70896ffeeef32b9c950ywan      i++;
2382233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
2383233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2384233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2385233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2386233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
2387233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2388233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
2389233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2390233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Constructs an empty Message.
2391233d2500723e5594f3e7c70896ffeeef32b9c950ywan// We allocate the stringstream separately because otherwise each use of
2392233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's
2393233d2500723e5594f3e7c70896ffeeef32b9c950ywan// stack frame leading to huge stack frames in some cases; gcc does not reuse
2394233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the stack space.
2395233d2500723e5594f3e7c70896ffeeef32b9c950ywanMessage::Message() : ss_(new ::std::stringstream) {
2396233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // By default, we want there to be enough precision when printing
2397233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // a double to a Message.
2398233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *ss_ << std::setprecision(std::numeric_limits<double>::digits10 + 2);
2399233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2400233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2401233d2500723e5594f3e7c70896ffeeef32b9c950ywan// These two overloads allow streaming a wide C string to a Message
2402233d2500723e5594f3e7c70896ffeeef32b9c950ywan// using the UTF-8 encoding.
2403233d2500723e5594f3e7c70896ffeeef32b9c950ywanMessage& Message::operator <<(const wchar_t* wide_c_str) {
2404233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return *this << internal::String::ShowWideCString(wide_c_str);
2405233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2406233d2500723e5594f3e7c70896ffeeef32b9c950ywanMessage& Message::operator <<(wchar_t* wide_c_str) {
2407233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return *this << internal::String::ShowWideCString(wide_c_str);
2408233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2409233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2410233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_STD_WSTRING
2411233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts the given wide string to a narrow string using the UTF-8
2412233d2500723e5594f3e7c70896ffeeef32b9c950ywan// encoding, and streams the result to this Message object.
2413233d2500723e5594f3e7c70896ffeeef32b9c950ywanMessage& Message::operator <<(const ::std::wstring& wstr) {
2414233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this);
2415233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return *this;
2416233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2417233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_STD_WSTRING
2418233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2419233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_GLOBAL_WSTRING
2420233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts the given wide string to a narrow string using the UTF-8
2421233d2500723e5594f3e7c70896ffeeef32b9c950ywan// encoding, and streams the result to this Message object.
2422233d2500723e5594f3e7c70896ffeeef32b9c950ywanMessage& Message::operator <<(const ::wstring& wstr) {
2423233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this);
2424233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return *this;
2425233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2426233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_GLOBAL_WSTRING
2427233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2428233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the text streamed to this object so far as an std::string.
2429233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Each '\0' character in the buffer is replaced with "\\0".
2430233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string Message::GetString() const {
2431233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return internal::StringStreamToString(ss_.get());
2432233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2433233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2434233d2500723e5594f3e7c70896ffeeef32b9c950ywan// AssertionResult constructors.
2435233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Used in EXPECT_TRUE/FALSE(assertion_result).
2436233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult::AssertionResult(const AssertionResult& other)
2437233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : success_(other.success_),
2438233d2500723e5594f3e7c70896ffeeef32b9c950ywan      message_(other.message_.get() != NULL ?
2439233d2500723e5594f3e7c70896ffeeef32b9c950ywan               new ::std::string(*other.message_) :
2440233d2500723e5594f3e7c70896ffeeef32b9c950ywan               static_cast< ::std::string*>(NULL)) {
2441233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2442233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2443233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
2444233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult AssertionResult::operator!() const {
2445233d2500723e5594f3e7c70896ffeeef32b9c950ywan  AssertionResult negation(!success_);
2446233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (message_.get() != NULL)
2447233d2500723e5594f3e7c70896ffeeef32b9c950ywan    negation << *message_;
2448233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return negation;
2449233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2450233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2451233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Makes a successful assertion result.
2452233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult AssertionSuccess() {
2453233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionResult(true);
2454233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2455233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2456233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Makes a failed assertion result.
2457233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult AssertionFailure() {
2458233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionResult(false);
2459233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2460233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2461233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Makes a failed assertion result with the given failure message.
2462233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Deprecated; use AssertionFailure() << message.
2463233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult AssertionFailure(const Message& message) {
2464233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionFailure() << message;
2465233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2466233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2467233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
2468233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2469233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Constructs and returns the message for an equality assertion
2470233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.
2471233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
2472233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The first four parameters are the expressions used in the assertion
2473233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and their values, as strings.  For example, for ASSERT_EQ(foo, bar)
2474233d2500723e5594f3e7c70896ffeeef32b9c950ywan// where foo is 5 and bar is 6, we have:
2475233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
2476233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   expected_expression: "foo"
2477233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   actual_expression:   "bar"
2478233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   expected_value:      "5"
2479233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   actual_value:        "6"
2480233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
2481233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The ignoring_case parameter is true iff the assertion is a
2482233d2500723e5594f3e7c70896ffeeef32b9c950ywan// *_STRCASEEQ*.  When it's true, the string " (ignoring case)" will
2483233d2500723e5594f3e7c70896ffeeef32b9c950ywan// be inserted into the message.
2484233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult EqFailure(const char* expected_expression,
2485233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          const char* actual_expression,
2486233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          const std::string& expected_value,
2487233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          const std::string& actual_value,
2488233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          bool ignoring_case) {
2489233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message msg;
2490233d2500723e5594f3e7c70896ffeeef32b9c950ywan  msg << "Value of: " << actual_expression;
2491233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (actual_value != actual_expression) {
2492233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << "\n  Actual: " << actual_value;
2493233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2494233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2495233d2500723e5594f3e7c70896ffeeef32b9c950ywan  msg << "\nExpected: " << expected_expression;
2496233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (ignoring_case) {
2497233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << " (ignoring case)";
2498233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2499233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (expected_value != expected_expression) {
2500233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << "\nWhich is: " << expected_value;
2501233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2502233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2503233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionFailure() << msg;
2504233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2505233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2506233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Constructs a failure message for Boolean assertions such as EXPECT_TRUE.
2507233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string GetBoolAssertionFailureMessage(
2508233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const AssertionResult& assertion_result,
2509233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* expression_text,
2510233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* actual_predicate_value,
2511233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* expected_predicate_value) {
2512233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* actual_message = assertion_result.message();
2513233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message msg;
2514233d2500723e5594f3e7c70896ffeeef32b9c950ywan  msg << "Value of: " << expression_text
2515233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "\n  Actual: " << actual_predicate_value;
2516233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (actual_message[0] != '\0')
2517233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << " (" << actual_message << ")";
2518233d2500723e5594f3e7c70896ffeeef32b9c950ywan  msg << "\nExpected: " << expected_predicate_value;
2519233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return msg.GetString();
2520233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2521233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2522233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helper function for implementing ASSERT_NEAR.
2523233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult DoubleNearPredFormat(const char* expr1,
2524233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     const char* expr2,
2525233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     const char* abs_error_expr,
2526233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     double val1,
2527233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     double val2,
2528233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     double abs_error) {
2529233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const double diff = fabs(val1 - val2);
2530233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (diff <= abs_error) return AssertionSuccess();
2531233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2532233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TODO(wan): do not print the value of an expression if it's
2533233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // already a literal.
2534233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionFailure()
2535233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "The difference between " << expr1 << " and " << expr2
2536233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << " is " << diff << ", which exceeds " << abs_error_expr << ", where\n"
2537233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << expr1 << " evaluates to " << val1 << ",\n"
2538233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << expr2 << " evaluates to " << val2 << ", and\n"
2539233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << abs_error_expr << " evaluates to " << abs_error << ".";
2540233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2541233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2542233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2543233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helper template for implementing FloatLE() and DoubleLE().
2544233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename RawType>
2545233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult FloatingPointLE(const char* expr1,
2546233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                const char* expr2,
2547233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                RawType val1,
2548233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                RawType val2) {
2549233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns success if val1 is less than val2,
2550233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (val1 < val2) {
2551233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2552233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2553233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2554233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // or if val1 is almost equal to val2.
2555233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const FloatingPoint<RawType> lhs(val1), rhs(val2);
2556233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (lhs.AlmostEquals(rhs)) {
2557233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2558233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2559233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2560233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Note that the above two checks will both fail if either val1 or
2561233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // val2 is NaN, as the IEEE floating-point standard requires that
2562233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // any predicate involving a NaN must return false.
2563233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2564233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::stringstream val1_ss;
2565233d2500723e5594f3e7c70896ffeeef32b9c950ywan  val1_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
2566233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << val1;
2567233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2568233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::stringstream val2_ss;
2569233d2500723e5594f3e7c70896ffeeef32b9c950ywan  val2_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
2570233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << val2;
2571233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2572233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionFailure()
2573233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Expected: (" << expr1 << ") <= (" << expr2 << ")\n"
2574233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "  Actual: " << StringStreamToString(&val1_ss) << " vs "
2575233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << StringStreamToString(&val2_ss);
2576233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2577233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2578233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
2579233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2580233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Asserts that val1 is less than, or almost equal to, val2.  Fails
2581233d2500723e5594f3e7c70896ffeeef32b9c950ywan// otherwise.  In particular, it fails if either val1 or val2 is NaN.
2582233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult FloatLE(const char* expr1, const char* expr2,
2583233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        float val1, float val2) {
2584233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return internal::FloatingPointLE<float>(expr1, expr2, val1, val2);
2585233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2586233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2587233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Asserts that val1 is less than, or almost equal to, val2.  Fails
2588233d2500723e5594f3e7c70896ffeeef32b9c950ywan// otherwise.  In particular, it fails if either val1 or val2 is NaN.
2589233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult DoubleLE(const char* expr1, const char* expr2,
2590233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         double val1, double val2) {
2591233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return internal::FloatingPointLE<double>(expr1, expr2, val1, val2);
2592233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2593233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2594233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
2595233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2596233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The helper function for {ASSERT|EXPECT}_EQ with int or enum
2597233d2500723e5594f3e7c70896ffeeef32b9c950ywan// arguments.
2598233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult CmpHelperEQ(const char* expected_expression,
2599233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            const char* actual_expression,
2600233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            BiggestInt expected,
2601233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            BiggestInt actual) {
2602233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (expected == actual) {
2603233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2604233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2605233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2606233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return EqFailure(expected_expression,
2607233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   actual_expression,
2608233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   FormatForComparisonFailureMessage(expected, actual),
2609233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   FormatForComparisonFailureMessage(actual, expected),
2610233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   false);
2611233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2612233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2613233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A macro for implementing the helper functions needed to implement
2614233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ASSERT_?? and EXPECT_?? with integer or enum arguments.  It is here
2615233d2500723e5594f3e7c70896ffeeef32b9c950ywan// just to avoid copy-and-paste of similar code.
2616233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
2617233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
2618233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   BiggestInt val1, BiggestInt val2) {\
2619233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (val1 op val2) {\
2620233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();\
2621233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {\
2622233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionFailure() \
2623233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << "Expected: (" << expr1 << ") " #op " (" << expr2\
2624233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
2625233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << " vs " << FormatForComparisonFailureMessage(val2, val1);\
2626233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }\
2627233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2628233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2629233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Implements the helper function for {ASSERT|EXPECT}_NE with int or
2630233d2500723e5594f3e7c70896ffeeef32b9c950ywan// enum arguments.
2631233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_IMPL_CMP_HELPER_(NE, !=)
2632233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Implements the helper function for {ASSERT|EXPECT}_LE with int or
2633233d2500723e5594f3e7c70896ffeeef32b9c950ywan// enum arguments.
2634233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_IMPL_CMP_HELPER_(LE, <=)
2635233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Implements the helper function for {ASSERT|EXPECT}_LT with int or
2636233d2500723e5594f3e7c70896ffeeef32b9c950ywan// enum arguments.
2637233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_IMPL_CMP_HELPER_(LT, < )
2638233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Implements the helper function for {ASSERT|EXPECT}_GE with int or
2639233d2500723e5594f3e7c70896ffeeef32b9c950ywan// enum arguments.
2640233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_IMPL_CMP_HELPER_(GE, >=)
2641233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Implements the helper function for {ASSERT|EXPECT}_GT with int or
2642233d2500723e5594f3e7c70896ffeeef32b9c950ywan// enum arguments.
2643233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_IMPL_CMP_HELPER_(GT, > )
2644233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2645233d2500723e5594f3e7c70896ffeeef32b9c950ywan#undef GTEST_IMPL_CMP_HELPER_
2646233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2647233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The helper function for {ASSERT|EXPECT}_STREQ.
2648233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult CmpHelperSTREQ(const char* expected_expression,
2649233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* actual_expression,
2650233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* expected,
2651233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* actual) {
2652233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (String::CStringEquals(expected, actual)) {
2653233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2654233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2655233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2656233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return EqFailure(expected_expression,
2657233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   actual_expression,
2658233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   PrintToString(expected),
2659233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   PrintToString(actual),
2660233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   false);
2661233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2662233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2663233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The helper function for {ASSERT|EXPECT}_STRCASEEQ.
2664233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult CmpHelperSTRCASEEQ(const char* expected_expression,
2665233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   const char* actual_expression,
2666233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   const char* expected,
2667233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   const char* actual) {
2668233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (String::CaseInsensitiveCStringEquals(expected, actual)) {
2669233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2670233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2671233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2672233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return EqFailure(expected_expression,
2673233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   actual_expression,
2674233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   PrintToString(expected),
2675233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   PrintToString(actual),
2676233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   true);
2677233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2678233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2679233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The helper function for {ASSERT|EXPECT}_STRNE.
2680233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult CmpHelperSTRNE(const char* s1_expression,
2681233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* s2_expression,
2682233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* s1,
2683233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* s2) {
2684233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!String::CStringEquals(s1, s2)) {
2685233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2686233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
2687233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionFailure() << "Expected: (" << s1_expression << ") != ("
2688233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              << s2_expression << "), actual: \""
2689233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              << s1 << "\" vs \"" << s2 << "\"";
2690233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2691233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2692233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2693233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The helper function for {ASSERT|EXPECT}_STRCASENE.
2694233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult CmpHelperSTRCASENE(const char* s1_expression,
2695233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   const char* s2_expression,
2696233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   const char* s1,
2697233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   const char* s2) {
2698233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!String::CaseInsensitiveCStringEquals(s1, s2)) {
2699233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2700233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
2701233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionFailure()
2702233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << "Expected: (" << s1_expression << ") != ("
2703233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << s2_expression << ") (ignoring case), actual: \""
2704233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << s1 << "\" vs \"" << s2 << "\"";
2705233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2706233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2707233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2708233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
2709233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2710233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace {
2711233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2712233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helper functions for implementing IsSubString() and IsNotSubstring().
2713233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2714233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This group of overloaded functions return true iff needle is a
2715233d2500723e5594f3e7c70896ffeeef32b9c950ywan// substring of haystack.  NULL is considered a substring of itself
2716233d2500723e5594f3e7c70896ffeeef32b9c950ywan// only.
2717233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2718233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsSubstringPred(const char* needle, const char* haystack) {
2719233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (needle == NULL || haystack == NULL)
2720233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return needle == haystack;
2721233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2722233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return strstr(haystack, needle) != NULL;
2723233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2724233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2725233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) {
2726233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (needle == NULL || haystack == NULL)
2727233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return needle == haystack;
2728233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2729233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return wcsstr(haystack, needle) != NULL;
2730233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2731233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2732233d2500723e5594f3e7c70896ffeeef32b9c950ywan// StringType here can be either ::std::string or ::std::wstring.
2733233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename StringType>
2734233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsSubstringPred(const StringType& needle,
2735233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     const StringType& haystack) {
2736233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return haystack.find(needle) != StringType::npos;
2737233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2738233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2739233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This function implements either IsSubstring() or IsNotSubstring(),
2740233d2500723e5594f3e7c70896ffeeef32b9c950ywan// depending on the value of the expected_to_be_substring parameter.
2741233d2500723e5594f3e7c70896ffeeef32b9c950ywan// StringType here can be const char*, const wchar_t*, ::std::string,
2742233d2500723e5594f3e7c70896ffeeef32b9c950ywan// or ::std::wstring.
2743233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename StringType>
2744233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsSubstringImpl(
2745233d2500723e5594f3e7c70896ffeeef32b9c950ywan    bool expected_to_be_substring,
2746233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2747233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const StringType& needle, const StringType& haystack) {
2748233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (IsSubstringPred(needle, haystack) == expected_to_be_substring)
2749233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2750233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2751233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool is_wide_string = sizeof(needle[0]) > 1;
2752233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
2753233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionFailure()
2754233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Value of: " << needle_expr << "\n"
2755233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "  Actual: " << begin_string_quote << needle << "\"\n"
2756233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Expected: " << (expected_to_be_substring ? "" : "not ")
2757233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "a substring of " << haystack_expr << "\n"
2758233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Which is: " << begin_string_quote << haystack << "\"";
2759233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2760233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2761233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace
2762233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2763233d2500723e5594f3e7c70896ffeeef32b9c950ywan// IsSubstring() and IsNotSubstring() check whether needle is a
2764233d2500723e5594f3e7c70896ffeeef32b9c950ywan// substring of haystack (NULL is considered a substring of itself
2765233d2500723e5594f3e7c70896ffeeef32b9c950ywan// only), and return an appropriate error message when they fail.
2766233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2767233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsSubstring(
2768233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2769233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle, const char* haystack) {
2770233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
2771233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2772233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2773233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsSubstring(
2774233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2775233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const wchar_t* needle, const wchar_t* haystack) {
2776233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
2777233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2778233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2779233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsNotSubstring(
2780233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2781233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle, const char* haystack) {
2782233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
2783233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2784233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2785233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsNotSubstring(
2786233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2787233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const wchar_t* needle, const wchar_t* haystack) {
2788233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
2789233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2790233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2791233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsSubstring(
2792233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2793233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const ::std::string& needle, const ::std::string& haystack) {
2794233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
2795233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2796233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2797233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsNotSubstring(
2798233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2799233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const ::std::string& needle, const ::std::string& haystack) {
2800233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
2801233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2802233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2803233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_STD_WSTRING
2804233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsSubstring(
2805233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2806233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const ::std::wstring& needle, const ::std::wstring& haystack) {
2807233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
2808233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2809233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2810233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsNotSubstring(
2811233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* needle_expr, const char* haystack_expr,
2812233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const ::std::wstring& needle, const ::std::wstring& haystack) {
2813233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
2814233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2815233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_STD_WSTRING
2816233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2817233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
2818233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2819233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
2820233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2821233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace {
2822233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2823233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helper function for IsHRESULT{SuccessFailure} predicates
2824233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult HRESULTFailureHelper(const char* expr,
2825233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     const char* expected,
2826233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     long hr) {  // NOLINT
2827233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS_MOBILE
2828233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2829233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Windows CE doesn't support FormatMessage.
2830233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char error_text[] = "";
2831233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2832233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
2833233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2834233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Looks up the human-readable system message for the HRESULT code
2835233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // and since we're not passing any params to FormatMessage, we don't
2836233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // want inserts expanded.
2837233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM |
2838233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       FORMAT_MESSAGE_IGNORE_INSERTS;
2839233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const DWORD kBufSize = 4096;
2840233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the system's human readable message string for this HRESULT.
2841233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char error_text[kBufSize] = { '\0' };
2842233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DWORD message_length = ::FormatMessageA(kFlags,
2843233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          0,  // no source, we're asking system
2844233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          hr,  // the error
2845233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          0,  // no line width restrictions
2846233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          error_text,  // output buffer
2847233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          kBufSize,  // buf size
2848233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          NULL);  // no arguments for inserts
2849233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Trims tailing white space (FormatMessage leaves a trailing CR-LF)
2850233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (; message_length && IsSpace(error_text[message_length - 1]);
2851233d2500723e5594f3e7c70896ffeeef32b9c950ywan          --message_length) {
2852233d2500723e5594f3e7c70896ffeeef32b9c950ywan    error_text[message_length - 1] = '\0';
2853233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2854233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2855233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS_MOBILE
2856233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2857233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string error_hex("0x" + String::FormatHexInt(hr));
2858233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ::testing::AssertionFailure()
2859233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Expected: " << expr << " " << expected << ".\n"
2860233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "  Actual: " << error_hex << " " << error_text << "\n";
2861233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2862233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2863233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace
2864233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2865233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsHRESULTSuccess(const char* expr, long hr) {  // NOLINT
2866233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (SUCCEEDED(hr)) {
2867233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2868233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2869233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return HRESULTFailureHelper(expr, "succeeds", hr);
2870233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2871233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2872233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult IsHRESULTFailure(const char* expr, long hr) {  // NOLINT
2873233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (FAILED(hr)) {
2874233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
2875233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2876233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return HRESULTFailureHelper(expr, "fails", hr);
2877233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2878233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2879233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
2880233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2881233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Utility functions for encoding Unicode text (wide strings) in
2882233d2500723e5594f3e7c70896ffeeef32b9c950ywan// UTF-8.
2883233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2884233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A Unicode code-point can have upto 21 bits, and is encoded in UTF-8
2885233d2500723e5594f3e7c70896ffeeef32b9c950ywan// like this:
2886233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
2887233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Code-point length   Encoding
2888233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   0 -  7 bits       0xxxxxxx
2889233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   8 - 11 bits       110xxxxx 10xxxxxx
2890233d2500723e5594f3e7c70896ffeeef32b9c950ywan//  12 - 16 bits       1110xxxx 10xxxxxx 10xxxxxx
2891233d2500723e5594f3e7c70896ffeeef32b9c950ywan//  17 - 21 bits       11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
2892233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2893233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The maximum code-point a one-byte UTF-8 sequence can represent.
2894233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst UInt32 kMaxCodePoint1 = (static_cast<UInt32>(1) <<  7) - 1;
2895233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2896233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The maximum code-point a two-byte UTF-8 sequence can represent.
2897233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst UInt32 kMaxCodePoint2 = (static_cast<UInt32>(1) << (5 + 6)) - 1;
2898233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2899233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The maximum code-point a three-byte UTF-8 sequence can represent.
2900233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst UInt32 kMaxCodePoint3 = (static_cast<UInt32>(1) << (4 + 2*6)) - 1;
2901233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2902233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The maximum code-point a four-byte UTF-8 sequence can represent.
2903233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst UInt32 kMaxCodePoint4 = (static_cast<UInt32>(1) << (3 + 3*6)) - 1;
2904233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2905233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Chops off the n lowest bits from a bit pattern.  Returns the n
2906233d2500723e5594f3e7c70896ffeeef32b9c950ywan// lowest bits.  As a side effect, the original bit pattern will be
2907233d2500723e5594f3e7c70896ffeeef32b9c950ywan// shifted to the right by n bits.
2908233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline UInt32 ChopLowBits(UInt32* bits, int n) {
2909233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const UInt32 low_bits = *bits & ((static_cast<UInt32>(1) << n) - 1);
2910233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *bits >>= n;
2911233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return low_bits;
2912233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2913233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2914233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts a Unicode code point to a narrow string in UTF-8 encoding.
2915233d2500723e5594f3e7c70896ffeeef32b9c950ywan// code_point parameter is of type UInt32 because wchar_t may not be
2916233d2500723e5594f3e7c70896ffeeef32b9c950ywan// wide enough to contain a code point.
2917233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If the code_point is not a valid Unicode code point
2918233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted
2919233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to "(Invalid Unicode 0xXXXXXXXX)".
2920233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string CodePointToUtf8(UInt32 code_point) {
2921233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (code_point > kMaxCodePoint4) {
2922233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return "(Invalid Unicode 0x" + String::FormatHexInt(code_point) + ")";
2923233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2924233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2925233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char str[5];  // Big enough for the largest valid code point.
2926233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (code_point <= kMaxCodePoint1) {
2927233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[1] = '\0';
2928233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[0] = static_cast<char>(code_point);                          // 0xxxxxxx
2929233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (code_point <= kMaxCodePoint2) {
2930233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[2] = '\0';
2931233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
2932233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[0] = static_cast<char>(0xC0 | code_point);                   // 110xxxxx
2933233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (code_point <= kMaxCodePoint3) {
2934233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[3] = '\0';
2935233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
2936233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
2937233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[0] = static_cast<char>(0xE0 | code_point);                   // 1110xxxx
2938233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {  // code_point <= kMaxCodePoint4
2939233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[4] = '\0';
2940233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[3] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
2941233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
2942233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
2943233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str[0] = static_cast<char>(0xF0 | code_point);                   // 11110xxx
2944233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
2945233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return str;
2946233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2947233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2948233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The following two functions only make sense if the the system
2949233d2500723e5594f3e7c70896ffeeef32b9c950ywan// uses UTF-16 for wide string encoding. All supported systems
2950233d2500723e5594f3e7c70896ffeeef32b9c950ywan// with 16 bit wchar_t (Windows, Cygwin, Symbian OS) do use UTF-16.
2951233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2952233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Determines if the arguments constitute UTF-16 surrogate pair
2953233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and thus should be combined into a single Unicode code point
2954233d2500723e5594f3e7c70896ffeeef32b9c950ywan// using CreateCodePointFromUtf16SurrogatePair.
2955233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) {
2956233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return sizeof(wchar_t) == 2 &&
2957233d2500723e5594f3e7c70896ffeeef32b9c950ywan      (first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00;
2958233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2959233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2960233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates a Unicode code point from UTF16 surrogate pair.
2961233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline UInt32 CreateCodePointFromUtf16SurrogatePair(wchar_t first,
2962233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                    wchar_t second) {
2963233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const UInt32 mask = (1 << 10) - 1;
2964233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (sizeof(wchar_t) == 2) ?
2965233d2500723e5594f3e7c70896ffeeef32b9c950ywan      (((first & mask) << 10) | (second & mask)) + 0x10000 :
2966233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // This function should not be called when the condition is
2967233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // false, but we provide a sensible default in case it is.
2968233d2500723e5594f3e7c70896ffeeef32b9c950ywan      static_cast<UInt32>(first);
2969233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
2970233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2971233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts a wide string to a narrow string in UTF-8 encoding.
2972233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The wide string is assumed to have the following encoding:
2973233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)
2974233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)
2975233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parameter str points to a null-terminated wide string.
2976233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parameter num_chars may additionally limit the number
2977233d2500723e5594f3e7c70896ffeeef32b9c950ywan// of wchar_t characters processed. -1 is used when the entire string
2978233d2500723e5594f3e7c70896ffeeef32b9c950ywan// should be processed.
2979233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If the string contains code points that are not valid Unicode code points
2980233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
2981233d2500723e5594f3e7c70896ffeeef32b9c950ywan// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
2982233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and contains invalid UTF-16 surrogate pairs, values in those pairs
2983233d2500723e5594f3e7c70896ffeeef32b9c950ywan// will be encoded as individual Unicode characters from Basic Normal Plane.
2984233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string WideStringToUtf8(const wchar_t* str, int num_chars) {
2985233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (num_chars == -1)
2986233d2500723e5594f3e7c70896ffeeef32b9c950ywan    num_chars = static_cast<int>(wcslen(str));
2987233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2988233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::stringstream stream;
2989233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i < num_chars; ++i) {
2990233d2500723e5594f3e7c70896ffeeef32b9c950ywan    UInt32 unicode_code_point;
2991233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2992233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (str[i] == L'\0') {
2993233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
2994233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) {
2995233d2500723e5594f3e7c70896ffeeef32b9c950ywan      unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i],
2996233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                                 str[i + 1]);
2997233d2500723e5594f3e7c70896ffeeef32b9c950ywan      i++;
2998233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
2999233d2500723e5594f3e7c70896ffeeef32b9c950ywan      unicode_code_point = static_cast<UInt32>(str[i]);
3000233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
3001233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3002233d2500723e5594f3e7c70896ffeeef32b9c950ywan    stream << CodePointToUtf8(unicode_code_point);
3003233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3004233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return StringStreamToString(&stream);
3005233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3006233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3007233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts a wide C string to an std::string using the UTF-8 encoding.
3008233d2500723e5594f3e7c70896ffeeef32b9c950ywan// NULL will be converted to "(null)".
3009233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string String::ShowWideCString(const wchar_t * wide_c_str) {
3010233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (wide_c_str == NULL)  return "(null)";
3011233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3012233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return internal::WideStringToUtf8(wide_c_str, -1);
3013233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3014233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3015233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Compares two wide C strings.  Returns true iff they have the same
3016233d2500723e5594f3e7c70896ffeeef32b9c950ywan// content.
3017233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3018233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Unlike wcscmp(), this function can handle NULL argument(s).  A NULL
3019233d2500723e5594f3e7c70896ffeeef32b9c950ywan// C string is considered different to any non-NULL C string,
3020233d2500723e5594f3e7c70896ffeeef32b9c950ywan// including the empty string.
3021233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool String::WideCStringEquals(const wchar_t * lhs, const wchar_t * rhs) {
3022233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (lhs == NULL) return rhs == NULL;
3023233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3024233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (rhs == NULL) return false;
3025233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3026233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return wcscmp(lhs, rhs) == 0;
3027233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3028233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3029233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helper function for *_STREQ on wide strings.
3030233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult CmpHelperSTREQ(const char* expected_expression,
3031233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* actual_expression,
3032233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const wchar_t* expected,
3033233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const wchar_t* actual) {
3034233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (String::WideCStringEquals(expected, actual)) {
3035233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
3036233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3037233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3038233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return EqFailure(expected_expression,
3039233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   actual_expression,
3040233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   PrintToString(expected),
3041233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   PrintToString(actual),
3042233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   false);
3043233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3044233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3045233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helper function for *_STRNE on wide strings.
3046233d2500723e5594f3e7c70896ffeeef32b9c950ywanAssertionResult CmpHelperSTRNE(const char* s1_expression,
3047233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* s2_expression,
3048233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const wchar_t* s1,
3049233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const wchar_t* s2) {
3050233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!String::WideCStringEquals(s1, s2)) {
3051233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return AssertionSuccess();
3052233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3053233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3054233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return AssertionFailure() << "Expected: (" << s1_expression << ") != ("
3055233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            << s2_expression << "), actual: "
3056233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            << PrintToString(s1)
3057233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            << " vs " << PrintToString(s2);
3058233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3059233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3060233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Compares two C strings, ignoring case.  Returns true iff they have
3061233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the same content.
3062233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3063233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Unlike strcasecmp(), this function can handle NULL argument(s).  A
3064233d2500723e5594f3e7c70896ffeeef32b9c950ywan// NULL C string is considered different to any non-NULL C string,
3065233d2500723e5594f3e7c70896ffeeef32b9c950ywan// including the empty string.
3066233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool String::CaseInsensitiveCStringEquals(const char * lhs, const char * rhs) {
3067233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (lhs == NULL)
3068233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return rhs == NULL;
3069233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (rhs == NULL)
3070233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
3071233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return posix::StrCaseCmp(lhs, rhs) == 0;
3072233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3073233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3074233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Compares two wide C strings, ignoring case.  Returns true iff they
3075233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // have the same content.
3076233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
3077233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Unlike wcscasecmp(), this function can handle NULL argument(s).
3078233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // A NULL C string is considered different to any non-NULL wide C string,
3079233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // including the empty string.
3080233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // NB: The implementations on different platforms slightly differ.
3081233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // On windows, this method uses _wcsicmp which compares according to LC_CTYPE
3082233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // environment variable. On GNU platform this method uses wcscasecmp
3083233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // which compares according to LC_CTYPE category of the current locale.
3084233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the
3085233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // current locale.
3086233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool String::CaseInsensitiveWideCStringEquals(const wchar_t* lhs,
3087233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                              const wchar_t* rhs) {
3088233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (lhs == NULL) return rhs == NULL;
3089233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3090233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (rhs == NULL) return false;
3091233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3092233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
3093233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return _wcsicmp(lhs, rhs) == 0;
3094233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID
3095233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return wcscasecmp(lhs, rhs) == 0;
3096233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
3097233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Android, Mac OS X and Cygwin don't define wcscasecmp.
3098233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Other unknown OSes may not define it either.
3099233d2500723e5594f3e7c70896ffeeef32b9c950ywan  wint_t left, right;
3100233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do {
3101233d2500723e5594f3e7c70896ffeeef32b9c950ywan    left = towlower(*lhs++);
3102233d2500723e5594f3e7c70896ffeeef32b9c950ywan    right = towlower(*rhs++);
3103233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (left && left == right);
3104233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return left == right;
3105233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // OS selector
3106233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3107233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3108233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff str ends with the given suffix, ignoring case.
3109233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Any string is considered to end with an empty suffix.
3110233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool String::EndsWithCaseInsensitive(
3111233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& str, const std::string& suffix) {
3112233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t str_len = str.length();
3113233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t suffix_len = suffix.length();
3114233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (str_len >= suffix_len) &&
3115233d2500723e5594f3e7c70896ffeeef32b9c950ywan         CaseInsensitiveCStringEquals(str.c_str() + str_len - suffix_len,
3116233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                      suffix.c_str());
3117233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3118233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3119233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats an int value as "%02d".
3120233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string String::FormatIntWidth2(int value) {
3121233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::stringstream ss;
3122233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ss << std::setfill('0') << std::setw(2) << value;
3123233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ss.str();
3124233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3125233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3126233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats an int value as "%X".
3127233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string String::FormatHexInt(int value) {
3128233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::stringstream ss;
3129233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ss << std::hex << std::uppercase << value;
3130233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ss.str();
3131233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3132233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3133233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats a byte as "%02X".
3134233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string String::FormatByte(unsigned char value) {
3135233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::stringstream ss;
3136233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ss << std::setfill('0') << std::setw(2) << std::hex << std::uppercase
3137233d2500723e5594f3e7c70896ffeeef32b9c950ywan     << static_cast<unsigned int>(value);
3138233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ss.str();
3139233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3140233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3141233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts the buffer in a stringstream to an std::string, converting NUL
3142233d2500723e5594f3e7c70896ffeeef32b9c950ywan// bytes to "\\0" along the way.
3143233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string StringStreamToString(::std::stringstream* ss) {
3144233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const ::std::string& str = ss->str();
3145233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const start = str.c_str();
3146233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const end = start + str.length();
3147233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3148233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string result;
3149233d2500723e5594f3e7c70896ffeeef32b9c950ywan  result.reserve(2 * (end - start));
3150233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (const char* ch = start; ch != end; ++ch) {
3151233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (*ch == '\0') {
3152233d2500723e5594f3e7c70896ffeeef32b9c950ywan      result += "\\0";  // Replaces NUL with "\\0";
3153233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
3154233d2500723e5594f3e7c70896ffeeef32b9c950ywan      result += *ch;
3155233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
3156233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3157233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3158233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result;
3159233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3160233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3161233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Appends the user-supplied message to the Google-Test-generated message.
3162233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string AppendUserMessage(const std::string& gtest_msg,
3163233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const Message& user_msg) {
3164233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Appends the user message if it's non-empty.
3165233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string user_msg_string = user_msg.GetString();
3166233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (user_msg_string.empty()) {
3167233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return gtest_msg;
3168233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3169233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3170233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return gtest_msg + "\n" + user_msg_string;
3171233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3172233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3173233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
3174233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3175233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class TestResult
3176233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3177233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates an empty TestResult.
3178233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestResult::TestResult()
3179233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : death_test_count_(0),
3180233d2500723e5594f3e7c70896ffeeef32b9c950ywan      elapsed_time_(0) {
3181233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3182233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3183233d2500723e5594f3e7c70896ffeeef32b9c950ywan// D'tor.
3184233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestResult::~TestResult() {
3185233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3186233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3187233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the i-th test part result among all the results. i can
3188233d2500723e5594f3e7c70896ffeeef32b9c950ywan// range from 0 to total_part_count() - 1. If i is not in that range,
3189233d2500723e5594f3e7c70896ffeeef32b9c950ywan// aborts the program.
3190233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst TestPartResult& TestResult::GetTestPartResult(int i) const {
3191233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (i < 0 || i >= total_part_count())
3192233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::posix::Abort();
3193233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return test_part_results_.at(i);
3194233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3195233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3196233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the i-th test property. i can range from 0 to
3197233d2500723e5594f3e7c70896ffeeef32b9c950ywan// test_property_count() - 1. If i is not in that range, aborts the
3198233d2500723e5594f3e7c70896ffeeef32b9c950ywan// program.
3199233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst TestProperty& TestResult::GetTestProperty(int i) const {
3200233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (i < 0 || i >= test_property_count())
3201233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::posix::Abort();
3202233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return test_properties_.at(i);
3203233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3204233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3205233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Clears the test part results.
3206233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestResult::ClearTestPartResults() {
3207233d2500723e5594f3e7c70896ffeeef32b9c950ywan  test_part_results_.clear();
3208233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3209233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3210233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds a test part result to the list.
3211233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestResult::AddTestPartResult(const TestPartResult& test_part_result) {
3212233d2500723e5594f3e7c70896ffeeef32b9c950ywan  test_part_results_.push_back(test_part_result);
3213233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3214233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3215233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds a test property to the list. If a property with the same key as the
3216233d2500723e5594f3e7c70896ffeeef32b9c950ywan// supplied property is already represented, the value of this test_property
3217233d2500723e5594f3e7c70896ffeeef32b9c950ywan// replaces the old value for that key.
3218233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestResult::RecordProperty(const std::string& xml_element,
3219233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                const TestProperty& test_property) {
3220233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!ValidateTestProperty(xml_element, test_property)) {
3221233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
3222233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3223233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::MutexLock lock(&test_properites_mutex_);
3224233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::vector<TestProperty>::iterator property_with_matching_key =
3225233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::find_if(test_properties_.begin(), test_properties_.end(),
3226233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   internal::TestPropertyKeyIs(test_property.key()));
3227233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (property_with_matching_key == test_properties_.end()) {
3228233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_properties_.push_back(test_property);
3229233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
3230233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3231233d2500723e5594f3e7c70896ffeeef32b9c950ywan  property_with_matching_key->SetValue(test_property.value());
3232233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3233233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3234233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The list of reserved attributes used in the <testsuites> element of XML
3235233d2500723e5594f3e7c70896ffeeef32b9c950ywan// output.
3236233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char* const kReservedTestSuitesAttributes[] = {
3237233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "disabled",
3238233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "errors",
3239233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "failures",
3240233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "name",
3241233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "random_seed",
3242233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "tests",
3243233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "time",
3244233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "timestamp"
3245233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
3246233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3247233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The list of reserved attributes used in the <testsuite> element of XML
3248233d2500723e5594f3e7c70896ffeeef32b9c950ywan// output.
3249233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char* const kReservedTestSuiteAttributes[] = {
3250233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "disabled",
3251233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "errors",
3252233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "failures",
3253233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "name",
3254233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "tests",
3255233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "time"
3256233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
3257233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3258233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The list of reserved attributes used in the <testcase> element of XML output.
3259233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char* const kReservedTestCaseAttributes[] = {
3260233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "classname",
3261233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "name",
3262233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "status",
3263233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "time",
3264233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "type_param",
3265233d2500723e5594f3e7c70896ffeeef32b9c950ywan  "value_param"
3266233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
3267233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3268233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <int kSize>
3269233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::vector<std::string> ArrayAsVector(const char* const (&array)[kSize]) {
3270233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return std::vector<std::string>(array, array + kSize);
3271233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3272233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3273233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::vector<std::string> GetReservedAttributesForElement(
3274233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& xml_element) {
3275233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (xml_element == "testsuites") {
3276233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return ArrayAsVector(kReservedTestSuitesAttributes);
3277233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (xml_element == "testsuite") {
3278233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return ArrayAsVector(kReservedTestSuiteAttributes);
3279233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (xml_element == "testcase") {
3280233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return ArrayAsVector(kReservedTestCaseAttributes);
3281233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
3282233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_CHECK_(false) << "Unrecognized xml_element provided: " << xml_element;
3283233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3284233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This code is unreachable but some compilers may not realizes that.
3285233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return std::vector<std::string>();
3286233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3287233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3288233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string FormatWordList(const std::vector<std::string>& words) {
3289233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message word_list;
3290233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < words.size(); ++i) {
3291233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (i > 0 && words.size() > 2) {
3292233d2500723e5594f3e7c70896ffeeef32b9c950ywan      word_list << ", ";
3293233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
3294233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (i == words.size() - 1) {
3295233d2500723e5594f3e7c70896ffeeef32b9c950ywan      word_list << "and ";
3296233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
3297233d2500723e5594f3e7c70896ffeeef32b9c950ywan    word_list << "'" << words[i] << "'";
3298233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3299233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return word_list.GetString();
3300233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3301233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3302233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ValidateTestPropertyName(const std::string& property_name,
3303233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const std::vector<std::string>& reserved_names) {
3304233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (std::find(reserved_names.begin(), reserved_names.end(), property_name) !=
3305233d2500723e5594f3e7c70896ffeeef32b9c950ywan          reserved_names.end()) {
3306233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ADD_FAILURE() << "Reserved key used in RecordProperty(): " << property_name
3307233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  << " (" << FormatWordList(reserved_names)
3308233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  << " are reserved by " << GTEST_NAME_ << ")";
3309233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
3310233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3311233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return true;
3312233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3313233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3314233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds a failure if the key is a reserved attribute of the element named
3315233d2500723e5594f3e7c70896ffeeef32b9c950ywan// xml_element.  Returns true if the property is valid.
3316233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool TestResult::ValidateTestProperty(const std::string& xml_element,
3317233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                      const TestProperty& test_property) {
3318233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ValidateTestPropertyName(test_property.key(),
3319233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                  GetReservedAttributesForElement(xml_element));
3320233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3321233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3322233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Clears the object.
3323233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestResult::Clear() {
3324233d2500723e5594f3e7c70896ffeeef32b9c950ywan  test_part_results_.clear();
3325233d2500723e5594f3e7c70896ffeeef32b9c950ywan  test_properties_.clear();
3326233d2500723e5594f3e7c70896ffeeef32b9c950ywan  death_test_count_ = 0;
3327233d2500723e5594f3e7c70896ffeeef32b9c950ywan  elapsed_time_ = 0;
3328233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3329233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3330233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the test failed.
3331233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool TestResult::Failed() const {
3332233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i < total_part_count(); ++i) {
3333233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (GetTestPartResult(i).failed())
3334233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return true;
3335233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3336233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return false;
3337233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3338233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3339233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the test part fatally failed.
3340233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool TestPartFatallyFailed(const TestPartResult& result) {
3341233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result.fatally_failed();
3342233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3343233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3344233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the test fatally failed.
3345233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool TestResult::HasFatalFailure() const {
3346233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_part_results_, TestPartFatallyFailed) > 0;
3347233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3348233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3349233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the test part non-fatally failed.
3350233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool TestPartNonfatallyFailed(const TestPartResult& result) {
3351233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result.nonfatally_failed();
3352233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3353233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3354233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the test has a non-fatal failure.
3355233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool TestResult::HasNonfatalFailure() const {
3356233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_part_results_, TestPartNonfatallyFailed) > 0;
3357233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3358233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3359233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of all test parts.  This is the sum of the number
3360233d2500723e5594f3e7c70896ffeeef32b9c950ywan// of successful test parts and the number of failed test parts.
3361233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestResult::total_part_count() const {
3362233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return static_cast<int>(test_part_results_.size());
3363233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3364233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3365233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the number of the test properties.
3366233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestResult::test_property_count() const {
3367233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return static_cast<int>(test_properties_.size());
3368233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3369233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3370233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class Test
3371233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3372233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates a Test object.
3373233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3374233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The c'tor saves the values of all Google Test flags.
3375233d2500723e5594f3e7c70896ffeeef32b9c950ywanTest::Test()
3376233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : gtest_flag_saver_(new internal::GTestFlagSaver) {
3377233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3378233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3379233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The d'tor restores the values of all Google Test flags.
3380233d2500723e5594f3e7c70896ffeeef32b9c950ywanTest::~Test() {
3381233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete gtest_flag_saver_;
3382233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3383233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3384233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Sets up the test fixture.
3385233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3386233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A sub-class may override this.
3387233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid Test::SetUp() {
3388233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3389233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3390233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Tears down the test fixture.
3391233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3392233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A sub-class may override this.
3393233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid Test::TearDown() {
3394233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3395233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3396233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Allows user supplied key value pairs to be recorded for later output.
3397233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid Test::RecordProperty(const std::string& key, const std::string& value) {
3398233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTest::GetInstance()->RecordProperty(key, value);
3399233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3400233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3401233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Allows user supplied key value pairs to be recorded for later output.
3402233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid Test::RecordProperty(const std::string& key, int value) {
3403233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message value_message;
3404233d2500723e5594f3e7c70896ffeeef32b9c950ywan  value_message << value;
3405233d2500723e5594f3e7c70896ffeeef32b9c950ywan  RecordProperty(key, value_message.GetString().c_str());
3406233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3407233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3408233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
3409233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3410233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type,
3411233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    const std::string& message) {
3412233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This function is a friend of UnitTest and as such has access to
3413233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // AddTestPartResult.
3414233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTest::GetInstance()->AddTestPartResult(
3415233d2500723e5594f3e7c70896ffeeef32b9c950ywan      result_type,
3416233d2500723e5594f3e7c70896ffeeef32b9c950ywan      NULL,  // No info about the source file where the exception occurred.
3417233d2500723e5594f3e7c70896ffeeef32b9c950ywan      -1,    // We have no info on which line caused the exception.
3418233d2500723e5594f3e7c70896ffeeef32b9c950ywan      message,
3419233d2500723e5594f3e7c70896ffeeef32b9c950ywan      "");   // No stack trace, either.
3420233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3421233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3422233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
3423233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3424233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test requires all tests in the same test case to use the same test
3425233d2500723e5594f3e7c70896ffeeef32b9c950ywan// fixture class.  This function checks if the current test has the
3426233d2500723e5594f3e7c70896ffeeef32b9c950ywan// same fixture class as the first test in the current test case.  If
3427233d2500723e5594f3e7c70896ffeeef32b9c950ywan// yes, it returns true; otherwise it generates a Google Test failure and
3428233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returns false.
3429233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool Test::HasSameFixtureClass() {
3430233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
3431233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestCase* const test_case = impl->current_test_case();
3432233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3433233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Info about the first test in the current test case.
3434233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestInfo* const first_test_info = test_case->test_info_list()[0];
3435233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const internal::TypeId first_fixture_id = first_test_info->fixture_class_id_;
3436233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const first_test_name = first_test_info->name();
3437233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3438233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Info about the current test.
3439233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestInfo* const this_test_info = impl->current_test_info();
3440233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const internal::TypeId this_fixture_id = this_test_info->fixture_class_id_;
3441233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const this_test_name = this_test_info->name();
3442233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3443233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (this_fixture_id != first_fixture_id) {
3444233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Is the first test defined using TEST?
3445233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const bool first_is_TEST = first_fixture_id == internal::GetTestTypeId();
3446233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Is this test defined using TEST?
3447233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const bool this_is_TEST = this_fixture_id == internal::GetTestTypeId();
3448233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3449233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (first_is_TEST || this_is_TEST) {
3450233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // The user mixed TEST and TEST_F in this test case - we'll tell
3451233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // him/her how to fix it.
3452233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3453233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Gets the name of the TEST and the name of the TEST_F.  Note
3454233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // that first_is_TEST and this_is_TEST cannot both be true, as
3455233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // the fixture IDs are different for the two tests.
3456233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const char* const TEST_name =
3457233d2500723e5594f3e7c70896ffeeef32b9c950ywan          first_is_TEST ? first_test_name : this_test_name;
3458233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const char* const TEST_F_name =
3459233d2500723e5594f3e7c70896ffeeef32b9c950ywan          first_is_TEST ? this_test_name : first_test_name;
3460233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3461233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ADD_FAILURE()
3462233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "All tests in the same test case must use the same test fixture\n"
3463233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "class, so mixing TEST_F and TEST in the same test case is\n"
3464233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "illegal.  In test case " << this_test_info->test_case_name()
3465233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << ",\n"
3466233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "test " << TEST_F_name << " is defined using TEST_F but\n"
3467233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "test " << TEST_name << " is defined using TEST.  You probably\n"
3468233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "want to change the TEST to TEST_F or move it to another test\n"
3469233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "case.";
3470233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
3471233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // The user defined two fixture classes with the same name in
3472233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // two namespaces - we'll tell him/her how to fix it.
3473233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ADD_FAILURE()
3474233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "All tests in the same test case must use the same test fixture\n"
3475233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "class.  However, in test case "
3476233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << this_test_info->test_case_name() << ",\n"
3477233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "you defined test " << first_test_name
3478233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << " and test " << this_test_name << "\n"
3479233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "using two different test fixture classes.  This can happen if\n"
3480233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "the two classes are from different namespaces or translation\n"
3481233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "units and have the same name.  You should probably rename one\n"
3482233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "of the classes to put the tests into different test cases.";
3483233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
3484233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
3485233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3486233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3487233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return true;
3488233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3489233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3490233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_SEH
3491233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3492233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds an "exception thrown" fatal failure to the current test.  This
3493233d2500723e5594f3e7c70896ffeeef32b9c950ywan// function returns its result via an output parameter pointer because VC++
3494233d2500723e5594f3e7c70896ffeeef32b9c950ywan// prohibits creation of objects with destructors on stack in functions
3495233d2500723e5594f3e7c70896ffeeef32b9c950ywan// using __try (see error C2712).
3496233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string* FormatSehExceptionMessage(DWORD exception_code,
3497233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                              const char* location) {
3498233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message message;
3499233d2500723e5594f3e7c70896ffeeef32b9c950ywan  message << "SEH exception with code 0x" << std::setbase(16) <<
3500233d2500723e5594f3e7c70896ffeeef32b9c950ywan    exception_code << std::setbase(10) << " thrown in " << location << ".";
3501233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3502233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return new std::string(message.GetString());
3503233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3504233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3505233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_SEH
3506233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3507233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
3508233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3509233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_EXCEPTIONS
3510233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3511233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds an "exception thrown" fatal failure to the current test.
3512233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string FormatCxxExceptionMessage(const char* description,
3513233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                             const char* location) {
3514233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message message;
3515233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (description != NULL) {
3516233d2500723e5594f3e7c70896ffeeef32b9c950ywan    message << "C++ exception with description \"" << description << "\"";
3517233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
3518233d2500723e5594f3e7c70896ffeeef32b9c950ywan    message << "Unknown C++ exception";
3519233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3520233d2500723e5594f3e7c70896ffeeef32b9c950ywan  message << " thrown in " << location << ".";
3521233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3522233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return message.GetString();
3523233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3524233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3525233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string PrintTestPartResultToString(
3526233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& test_part_result);
3527233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3528233d2500723e5594f3e7c70896ffeeef32b9c950ywanGoogleTestFailureException::GoogleTestFailureException(
3529233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& failure)
3530233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : ::std::runtime_error(PrintTestPartResultToString(failure).c_str()) {}
3531233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3532233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_EXCEPTIONS
3533233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3534233d2500723e5594f3e7c70896ffeeef32b9c950ywan// We put these helper functions in the internal namespace as IBM's xlC
3535233d2500723e5594f3e7c70896ffeeef32b9c950ywan// compiler rejects the code if they were declared static.
3536233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3537233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Runs the given method and handles SEH exceptions it throws, when
3538233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SEH is supported; returns the 0-value for type Result in case of an
3539233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SEH exception.  (Microsoft compilers cannot handle SEH and C++
3540233d2500723e5594f3e7c70896ffeeef32b9c950ywan// exceptions in the same function.  Therefore, we provide a separate
3541233d2500723e5594f3e7c70896ffeeef32b9c950ywan// wrapper function for handling SEH exceptions.)
3542233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <class T, typename Result>
3543233d2500723e5594f3e7c70896ffeeef32b9c950ywanResult HandleSehExceptionsInMethodIfSupported(
3544233d2500723e5594f3e7c70896ffeeef32b9c950ywan    T* object, Result (T::*method)(), const char* location) {
3545233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_SEH
3546233d2500723e5594f3e7c70896ffeeef32b9c950ywan  __try {
3547233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return (object->*method)();
3548233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } __except (internal::UnitTestOptions::GTestShouldProcessSEH(  // NOLINT
3549233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GetExceptionCode())) {
3550233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // We create the exception message on the heap because VC++ prohibits
3551233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // creation of objects with destructors on stack in functions using __try
3552233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // (see error C2712).
3553233d2500723e5594f3e7c70896ffeeef32b9c950ywan    std::string* exception_message = FormatSehExceptionMessage(
3554233d2500723e5594f3e7c70896ffeeef32b9c950ywan        GetExceptionCode(), location);
3555233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure,
3556233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                             *exception_message);
3557233d2500723e5594f3e7c70896ffeeef32b9c950ywan    delete exception_message;
3558233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return static_cast<Result>(0);
3559233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3560233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
3561233d2500723e5594f3e7c70896ffeeef32b9c950ywan  (void)location;
3562233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (object->*method)();
3563233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_SEH
3564233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3565233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3566233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Runs the given method and catches and reports C++ and/or SEH-style
3567233d2500723e5594f3e7c70896ffeeef32b9c950ywan// exceptions, if they are supported; returns the 0-value for type
3568233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Result in case of an SEH exception.
3569233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <class T, typename Result>
3570233d2500723e5594f3e7c70896ffeeef32b9c950ywanResult HandleExceptionsInMethodIfSupported(
3571233d2500723e5594f3e7c70896ffeeef32b9c950ywan    T* object, Result (T::*method)(), const char* location) {
3572233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // NOTE: The user code can affect the way in which Google Test handles
3573233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // exceptions by setting GTEST_FLAG(catch_exceptions), but only before
3574233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // RUN_ALL_TESTS() starts. It is technically possible to check the flag
3575233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // after the exception is caught and either report or re-throw the
3576233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // exception based on the flag's value:
3577233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
3578233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // try {
3579233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   // Perform the test method.
3580233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // } catch (...) {
3581233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   if (GTEST_FLAG(catch_exceptions))
3582233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //     // Report the exception as failure.
3583233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   else
3584233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //     throw;  // Re-throws the original exception.
3585233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // }
3586233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
3587233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // However, the purpose of this flag is to allow the program to drop into
3588233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the debugger when the exception is thrown. On most platforms, once the
3589233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // control enters the catch block, the exception origin information is
3590233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // lost and the debugger will stop the program at the point of the
3591233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // re-throw in this function -- instead of at the point of the original
3592233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // throw statement in the code under test.  For this reason, we perform
3593233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the check early, sacrificing the ability to affect Google Test's
3594233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // exception handling in the method where the exception is thrown.
3595233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (internal::GetUnitTestImpl()->catch_exceptions()) {
3596233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_EXCEPTIONS
3597233d2500723e5594f3e7c70896ffeeef32b9c950ywan    try {
3598233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return HandleSehExceptionsInMethodIfSupported(object, method, location);
3599233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } catch (const internal::GoogleTestFailureException&) {  // NOLINT
3600233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // This exception type can only be thrown by a failed Google
3601233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Test assertion with the intention of letting another testing
3602233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // framework catch it.  Therefore we just re-throw it.
3603233d2500723e5594f3e7c70896ffeeef32b9c950ywan      throw;
3604233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } catch (const std::exception& e) {  // NOLINT
3605233d2500723e5594f3e7c70896ffeeef32b9c950ywan      internal::ReportFailureInUnknownLocation(
3606233d2500723e5594f3e7c70896ffeeef32b9c950ywan          TestPartResult::kFatalFailure,
3607233d2500723e5594f3e7c70896ffeeef32b9c950ywan          FormatCxxExceptionMessage(e.what(), location));
3608233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } catch (...) {  // NOLINT
3609233d2500723e5594f3e7c70896ffeeef32b9c950ywan      internal::ReportFailureInUnknownLocation(
3610233d2500723e5594f3e7c70896ffeeef32b9c950ywan          TestPartResult::kFatalFailure,
3611233d2500723e5594f3e7c70896ffeeef32b9c950ywan          FormatCxxExceptionMessage(NULL, location));
3612233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
3613233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return static_cast<Result>(0);
3614233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
3615233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return HandleSehExceptionsInMethodIfSupported(object, method, location);
3616233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_EXCEPTIONS
3617233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
3618233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return (object->*method)();
3619233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3620233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3621233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3622233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
3623233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3624233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Runs the test and updates the test result.
3625233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid Test::Run() {
3626233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!HasSameFixtureClass()) return;
3627233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3628233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
3629233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->os_stack_trace_getter()->UponLeavingGTest();
3630233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, "SetUp()");
3631233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We will run the test only if SetUp() was successful.
3632233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!HasFatalFailure()) {
3633233d2500723e5594f3e7c70896ffeeef32b9c950ywan    impl->os_stack_trace_getter()->UponLeavingGTest();
3634233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::HandleExceptionsInMethodIfSupported(
3635233d2500723e5594f3e7c70896ffeeef32b9c950ywan        this, &Test::TestBody, "the test body");
3636233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3637233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3638233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // However, we want to clean up as much as possible.  Hence we will
3639233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // always call TearDown(), even if SetUp() or the test body has
3640233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // failed.
3641233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->os_stack_trace_getter()->UponLeavingGTest();
3642233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::HandleExceptionsInMethodIfSupported(
3643233d2500723e5594f3e7c70896ffeeef32b9c950ywan      this, &Test::TearDown, "TearDown()");
3644233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3645233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3646233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the current test has a fatal failure.
3647233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool Test::HasFatalFailure() {
3648233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure();
3649233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3650233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3651233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the current test has a non-fatal failure.
3652233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool Test::HasNonfatalFailure() {
3653233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return internal::GetUnitTestImpl()->current_test_result()->
3654233d2500723e5594f3e7c70896ffeeef32b9c950ywan      HasNonfatalFailure();
3655233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3656233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3657233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class TestInfo
3658233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3659233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Constructs a TestInfo object. It assumes ownership of the test factory
3660233d2500723e5594f3e7c70896ffeeef32b9c950ywan// object.
3661233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestInfo::TestInfo(const std::string& a_test_case_name,
3662233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   const std::string& a_name,
3663233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   const char* a_type_param,
3664233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   const char* a_value_param,
3665233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   internal::TypeId fixture_class_id,
3666233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   internal::TestFactoryBase* factory)
3667233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : test_case_name_(a_test_case_name),
3668233d2500723e5594f3e7c70896ffeeef32b9c950ywan      name_(a_name),
3669233d2500723e5594f3e7c70896ffeeef32b9c950ywan      type_param_(a_type_param ? new std::string(a_type_param) : NULL),
3670233d2500723e5594f3e7c70896ffeeef32b9c950ywan      value_param_(a_value_param ? new std::string(a_value_param) : NULL),
3671233d2500723e5594f3e7c70896ffeeef32b9c950ywan      fixture_class_id_(fixture_class_id),
3672233d2500723e5594f3e7c70896ffeeef32b9c950ywan      should_run_(false),
3673233d2500723e5594f3e7c70896ffeeef32b9c950ywan      is_disabled_(false),
3674233d2500723e5594f3e7c70896ffeeef32b9c950ywan      matches_filter_(false),
3675233d2500723e5594f3e7c70896ffeeef32b9c950ywan      factory_(factory),
3676233d2500723e5594f3e7c70896ffeeef32b9c950ywan      result_() {}
3677233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3678233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Destructs a TestInfo object.
3679233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestInfo::~TestInfo() { delete factory_; }
3680233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3681233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
3682233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3683233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates a new TestInfo object and registers it with Google Test;
3684233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returns the created object.
3685233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3686233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Arguments:
3687233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3688233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   test_case_name:   name of the test case
3689233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   name:             name of the test
3690233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   type_param:       the name of the test's type parameter, or NULL if
3691233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                     this is not a typed or a type-parameterized test.
3692233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   value_param:      text representation of the test's value parameter,
3693233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                     or NULL if this is not a value-parameterized test.
3694233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   fixture_class_id: ID of the test fixture class
3695233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   set_up_tc:        pointer to the function that sets up the test case
3696233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   tear_down_tc:     pointer to the function that tears down the test case
3697233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   factory:          pointer to the factory that creates a test object.
3698233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                     The newly created TestInfo instance will assume
3699233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                     ownership of the factory object.
3700233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestInfo* MakeAndRegisterTestInfo(
3701233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* test_case_name,
3702233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* name,
3703233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* type_param,
3704233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* value_param,
3705233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TypeId fixture_class_id,
3706233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SetUpTestCaseFunc set_up_tc,
3707233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TearDownTestCaseFunc tear_down_tc,
3708233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TestFactoryBase* factory) {
3709233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestInfo* const test_info =
3710233d2500723e5594f3e7c70896ffeeef32b9c950ywan      new TestInfo(test_case_name, name, type_param, value_param,
3711233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   fixture_class_id, factory);
3712233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetUnitTestImpl()->AddTestInfo(set_up_tc, tear_down_tc, test_info);
3713233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return test_info;
3714233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3715233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3716233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_PARAM_TEST
3717233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ReportInvalidTestCaseType(const char* test_case_name,
3718233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const char* file, int line) {
3719233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message errors;
3720233d2500723e5594f3e7c70896ffeeef32b9c950ywan  errors
3721233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Attempted redefinition of test case " << test_case_name << ".\n"
3722233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "All tests in the same test case must use the same test fixture\n"
3723233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "class.  However, in test case " << test_case_name << ", you tried\n"
3724233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "to define a test using a fixture class different from the one\n"
3725233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "used earlier. This can happen if the two fixture classes are\n"
3726233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "from different namespaces and have the same name. You should\n"
3727233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "probably rename one of the classes to put the tests into different\n"
3728233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "test cases.";
3729233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3730233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(),
3731233d2500723e5594f3e7c70896ffeeef32b9c950ywan          errors.GetString().c_str());
3732233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3733233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_PARAM_TEST
3734233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3735233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
3736233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3737233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace {
3738233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3739233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A predicate that checks the test name of a TestInfo against a known
3740233d2500723e5594f3e7c70896ffeeef32b9c950ywan// value.
3741233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3742233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This is used for implementation of the TestCase class only.  We put
3743233d2500723e5594f3e7c70896ffeeef32b9c950ywan// it in the anonymous namespace to prevent polluting the outer
3744233d2500723e5594f3e7c70896ffeeef32b9c950ywan// namespace.
3745233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3746233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TestNameIs is copyable.
3747233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass TestNameIs {
3748233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
3749233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Constructor.
3750233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
3751233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TestNameIs has NO default constructor.
3752233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit TestNameIs(const char* name)
3753233d2500723e5594f3e7c70896ffeeef32b9c950ywan      : name_(name) {}
3754233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3755233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns true iff the test name of test_info matches name_.
3756233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool operator()(const TestInfo * test_info) const {
3757233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return test_info && test_info->name() == name_;
3758233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3759233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3760233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
3761233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string name_;
3762233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
3763233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3764233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace
3765233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3766233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
3767233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3768233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This method expands all parameterized tests registered with macros TEST_P
3769233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and INSTANTIATE_TEST_CASE_P into regular tests and registers those.
3770233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This will be done just once during the program runtime.
3771233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::RegisterParameterizedTests() {
3772233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_PARAM_TEST
3773233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!parameterized_tests_registered_) {
3774233d2500723e5594f3e7c70896ffeeef32b9c950ywan    parameterized_test_registry_.RegisterTests();
3775233d2500723e5594f3e7c70896ffeeef32b9c950ywan    parameterized_tests_registered_ = true;
3776233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3777233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
3778233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3779233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3780233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
3781233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3782233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates the test object, runs it, records its result, and then
3783233d2500723e5594f3e7c70896ffeeef32b9c950ywan// deletes it.
3784233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestInfo::Run() {
3785233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!should_run_) return;
3786233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3787233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Tells UnitTest where to store test result.
3788233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
3789233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->set_current_test_info(this);
3790233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3791233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
3792233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3793233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Notifies the unit test event listeners that a test is about to start.
3794233d2500723e5594f3e7c70896ffeeef32b9c950ywan  repeater->OnTestStart(*this);
3795233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3796233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TimeInMillis start = internal::GetTimeInMillis();
3797233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3798233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->os_stack_trace_getter()->UponLeavingGTest();
3799233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3800233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Creates the test object.
3801233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Test* const test = internal::HandleExceptionsInMethodIfSupported(
3802233d2500723e5594f3e7c70896ffeeef32b9c950ywan      factory_, &internal::TestFactoryBase::CreateTest,
3803233d2500723e5594f3e7c70896ffeeef32b9c950ywan      "the test fixture's constructor");
3804233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3805233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Runs the test only if the test object was created and its
3806233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // constructor didn't generate a fatal failure.
3807233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if ((test != NULL) && !Test::HasFatalFailure()) {
3808233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // This doesn't throw as all user code that can throw are wrapped into
3809233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // exception handling code.
3810233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test->Run();
3811233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3812233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3813233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Deletes the test object.
3814233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->os_stack_trace_getter()->UponLeavingGTest();
3815233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::HandleExceptionsInMethodIfSupported(
3816233d2500723e5594f3e7c70896ffeeef32b9c950ywan      test, &Test::DeleteSelf_, "the test fixture's destructor");
3817233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3818233d2500723e5594f3e7c70896ffeeef32b9c950ywan  result_.set_elapsed_time(internal::GetTimeInMillis() - start);
3819233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3820233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Notifies the unit test event listener that a test has just finished.
3821233d2500723e5594f3e7c70896ffeeef32b9c950ywan  repeater->OnTestEnd(*this);
3822233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3823233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Tells UnitTest to stop associating assertion results to this
3824233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // test.
3825233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->set_current_test_info(NULL);
3826233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3827233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3828233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class TestCase
3829233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3830233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of successful tests in this test case.
3831233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestCase::successful_test_count() const {
3832233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_info_list_, TestPassed);
3833233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3834233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3835233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of failed tests in this test case.
3836233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestCase::failed_test_count() const {
3837233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_info_list_, TestFailed);
3838233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3839233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3840233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of disabled tests that will be reported in the XML report.
3841233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestCase::reportable_disabled_test_count() const {
3842233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_info_list_, TestReportableDisabled);
3843233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3844233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3845233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of disabled tests in this test case.
3846233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestCase::disabled_test_count() const {
3847233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_info_list_, TestDisabled);
3848233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3849233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3850233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of tests to be printed in the XML report.
3851233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestCase::reportable_test_count() const {
3852233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_info_list_, TestReportable);
3853233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3854233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3855233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Get the number of tests in this test case that should run.
3856233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestCase::test_to_run_count() const {
3857233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return CountIf(test_info_list_, ShouldRunTest);
3858233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3859233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3860233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of all tests.
3861233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestCase::total_test_count() const {
3862233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return static_cast<int>(test_info_list_.size());
3863233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3864233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3865233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates a TestCase with the given name.
3866233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3867233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Arguments:
3868233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3869233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   name:         name of the test case
3870233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   a_type_param: the name of the test case's type parameter, or NULL if
3871233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                 this is not a typed or a type-parameterized test case.
3872233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   set_up_tc:    pointer to the function that sets up the test case
3873233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   tear_down_tc: pointer to the function that tears down the test case
3874233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestCase::TestCase(const char* a_name, const char* a_type_param,
3875233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   Test::SetUpTestCaseFunc set_up_tc,
3876233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   Test::TearDownTestCaseFunc tear_down_tc)
3877233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : name_(a_name),
3878233d2500723e5594f3e7c70896ffeeef32b9c950ywan      type_param_(a_type_param ? new std::string(a_type_param) : NULL),
3879233d2500723e5594f3e7c70896ffeeef32b9c950ywan      set_up_tc_(set_up_tc),
3880233d2500723e5594f3e7c70896ffeeef32b9c950ywan      tear_down_tc_(tear_down_tc),
3881233d2500723e5594f3e7c70896ffeeef32b9c950ywan      should_run_(false),
3882233d2500723e5594f3e7c70896ffeeef32b9c950ywan      elapsed_time_(0) {
3883233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3884233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3885233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Destructor of TestCase.
3886233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestCase::~TestCase() {
3887233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Deletes every Test in the collection.
3888233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ForEach(test_info_list_, internal::Delete<TestInfo>);
3889233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3890233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3891233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the i-th test among all the tests. i can range from 0 to
3892233d2500723e5594f3e7c70896ffeeef32b9c950ywan// total_test_count() - 1. If i is not in that range, returns NULL.
3893233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst TestInfo* TestCase::GetTestInfo(int i) const {
3894233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int index = GetElementOr(test_indices_, i, -1);
3895233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return index < 0 ? NULL : test_info_list_[index];
3896233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3897233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3898233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the i-th test among all the tests. i can range from 0 to
3899233d2500723e5594f3e7c70896ffeeef32b9c950ywan// total_test_count() - 1. If i is not in that range, returns NULL.
3900233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestInfo* TestCase::GetMutableTestInfo(int i) {
3901233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int index = GetElementOr(test_indices_, i, -1);
3902233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return index < 0 ? NULL : test_info_list_[index];
3903233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3904233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3905233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds a test to this test case.  Will delete the test upon
3906233d2500723e5594f3e7c70896ffeeef32b9c950ywan// destruction of the TestCase object.
3907233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestCase::AddTestInfo(TestInfo * test_info) {
3908233d2500723e5594f3e7c70896ffeeef32b9c950ywan  test_info_list_.push_back(test_info);
3909233d2500723e5594f3e7c70896ffeeef32b9c950ywan  test_indices_.push_back(static_cast<int>(test_indices_.size()));
3910233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3911233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3912233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Runs every test in this TestCase.
3913233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestCase::Run() {
3914233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!should_run_) return;
3915233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3916233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
3917233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->set_current_test_case(this);
3918233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3919233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
3920233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3921233d2500723e5594f3e7c70896ffeeef32b9c950ywan  repeater->OnTestCaseStart(*this);
3922233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->os_stack_trace_getter()->UponLeavingGTest();
3923233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::HandleExceptionsInMethodIfSupported(
3924233d2500723e5594f3e7c70896ffeeef32b9c950ywan      this, &TestCase::RunSetUpTestCase, "SetUpTestCase()");
3925233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3926233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const internal::TimeInMillis start = internal::GetTimeInMillis();
3927233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i < total_test_count(); i++) {
3928233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GetMutableTestInfo(i)->Run();
3929233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3930233d2500723e5594f3e7c70896ffeeef32b9c950ywan  elapsed_time_ = internal::GetTimeInMillis() - start;
3931233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3932233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->os_stack_trace_getter()->UponLeavingGTest();
3933233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::HandleExceptionsInMethodIfSupported(
3934233d2500723e5594f3e7c70896ffeeef32b9c950ywan      this, &TestCase::RunTearDownTestCase, "TearDownTestCase()");
3935233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3936233d2500723e5594f3e7c70896ffeeef32b9c950ywan  repeater->OnTestCaseEnd(*this);
3937233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl->set_current_test_case(NULL);
3938233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3939233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3940233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Clears the results of all tests in this test case.
3941233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestCase::ClearResult() {
3942233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ad_hoc_test_result_.Clear();
3943233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ForEach(test_info_list_, TestInfo::ClearTestResult);
3944233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3945233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3946233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Shuffles the tests in this test case.
3947233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestCase::ShuffleTests(internal::Random* random) {
3948233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Shuffle(random, &test_indices_);
3949233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3950233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3951233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Restores the test order to before the first shuffle.
3952233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestCase::UnshuffleTests() {
3953233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < test_indices_.size(); i++) {
3954233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_indices_[i] = static_cast<int>(i);
3955233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3956233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3957233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3958233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats a countable noun.  Depending on its quantity, either the
3959233d2500723e5594f3e7c70896ffeeef32b9c950ywan// singular form or the plural form is used. e.g.
3960233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
3961233d2500723e5594f3e7c70896ffeeef32b9c950ywan// FormatCountableNoun(1, "formula", "formuli") returns "1 formula".
3962233d2500723e5594f3e7c70896ffeeef32b9c950ywan// FormatCountableNoun(5, "book", "books") returns "5 books".
3963233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string FormatCountableNoun(int count,
3964233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                       const char * singular_form,
3965233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                       const char * plural_form) {
3966233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return internal::StreamableToString(count) + " " +
3967233d2500723e5594f3e7c70896ffeeef32b9c950ywan      (count == 1 ? singular_form : plural_form);
3968233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3969233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3970233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats the count of tests.
3971233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string FormatTestCount(int test_count) {
3972233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return FormatCountableNoun(test_count, "test", "tests");
3973233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3974233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3975233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats the count of test cases.
3976233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string FormatTestCaseCount(int test_case_count) {
3977233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return FormatCountableNoun(test_case_count, "test case", "test cases");
3978233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
3979233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3980233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts a TestPartResult::Type enum to human-friendly string
3981233d2500723e5594f3e7c70896ffeeef32b9c950ywan// representation.  Both kNonFatalFailure and kFatalFailure are translated
3982233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to "Failure", as the user usually doesn't care about the difference
3983233d2500723e5594f3e7c70896ffeeef32b9c950ywan// between the two when viewing the test result.
3984233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char * TestPartResultTypeToString(TestPartResult::Type type) {
3985233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (type) {
3986233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case TestPartResult::kSuccess:
3987233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return "Success";
3988233d2500723e5594f3e7c70896ffeeef32b9c950ywan
3989233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case TestPartResult::kNonFatalFailure:
3990233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case TestPartResult::kFatalFailure:
3991233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifdef _MSC_VER
3992233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return "error: ";
3993233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
3994233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return "Failure\n";
3995233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
3996233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:
3997233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return "Unknown result type";
3998233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
3999233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4000233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4001233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
4002233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4003233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a TestPartResult to an std::string.
4004233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string PrintTestPartResultToString(
4005233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& test_part_result) {
4006233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (Message()
4007233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << internal::FormatFileLocation(test_part_result.file_name(),
4008233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          test_part_result.line_number())
4009233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << " " << TestPartResultTypeToString(test_part_result.type())
4010233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << test_part_result.message()).GetString();
4011233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4012233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4013233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a TestPartResult.
4014233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void PrintTestPartResult(const TestPartResult& test_part_result) {
4015233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string& result =
4016233d2500723e5594f3e7c70896ffeeef32b9c950ywan      PrintTestPartResultToString(test_part_result);
4017233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("%s\n", result.c_str());
4018233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4019233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // If the test program runs in Visual Studio or a debugger, the
4020233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // following statements add the test part result message to the Output
4021233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // window such that the user can double-click on it to jump to the
4022233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // corresponding source code location; otherwise they do nothing.
4023233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
4024233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We don't call OutputDebugString*() on Windows Mobile, as printing
4025233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // to stdout is done by OutputDebugString() there already - we don't
4026233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // want the same message printed twice.
4027233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::OutputDebugStringA(result.c_str());
4028233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::OutputDebugStringA("\n");
4029233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
4030233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4031233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4032233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class PrettyUnitTestResultPrinter
4033233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4034233d2500723e5594f3e7c70896ffeeef32b9c950ywanenum GTestColor {
4035233d2500723e5594f3e7c70896ffeeef32b9c950ywan  COLOR_DEFAULT,
4036233d2500723e5594f3e7c70896ffeeef32b9c950ywan  COLOR_RED,
4037233d2500723e5594f3e7c70896ffeeef32b9c950ywan  COLOR_GREEN,
4038233d2500723e5594f3e7c70896ffeeef32b9c950ywan  COLOR_YELLOW
4039233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
4040233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4041233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
4042233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4043233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the character attribute for the given color.
4044233d2500723e5594f3e7c70896ffeeef32b9c950ywanWORD GetColorAttribute(GTestColor color) {
4045233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (color) {
4046233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case COLOR_RED:    return FOREGROUND_RED;
4047233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case COLOR_GREEN:  return FOREGROUND_GREEN;
4048233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case COLOR_YELLOW: return FOREGROUND_RED | FOREGROUND_GREEN;
4049233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:           return 0;
4050233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4051233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4052233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4053233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
4054233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4055233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the ANSI color code for the given color.  COLOR_DEFAULT is
4056233d2500723e5594f3e7c70896ffeeef32b9c950ywan// an invalid input.
4057233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* GetAnsiColorCode(GTestColor color) {
4058233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (color) {
4059233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case COLOR_RED:     return "1";
4060233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case COLOR_GREEN:   return "2";
4061233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case COLOR_YELLOW:  return "3";
4062233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:            return NULL;
4063233d2500723e5594f3e7c70896ffeeef32b9c950ywan  };
4064233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4065233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4066233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
4067233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4068233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff Google Test should use colors in the output.
4069233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ShouldUseColor(bool stdout_is_tty) {
4070233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const gtest_color = GTEST_FLAG(color).c_str();
4071233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4072233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) {
4073233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
4074233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // On Windows the TERM variable is usually not set, but the
4075233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // console there does support colors.
4076233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return stdout_is_tty;
4077233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
4078233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // On non-Windows platforms, we rely on the TERM variable.
4079233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* const term = posix::GetEnv("TERM");
4080233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const bool term_supports_color =
4081233d2500723e5594f3e7c70896ffeeef32b9c950ywan        String::CStringEquals(term, "xterm") ||
4082233d2500723e5594f3e7c70896ffeeef32b9c950ywan        String::CStringEquals(term, "xterm-color") ||
4083233d2500723e5594f3e7c70896ffeeef32b9c950ywan        String::CStringEquals(term, "xterm-256color") ||
4084233d2500723e5594f3e7c70896ffeeef32b9c950ywan        String::CStringEquals(term, "screen") ||
4085233d2500723e5594f3e7c70896ffeeef32b9c950ywan        String::CStringEquals(term, "screen-256color") ||
4086233d2500723e5594f3e7c70896ffeeef32b9c950ywan        String::CStringEquals(term, "linux") ||
4087233d2500723e5594f3e7c70896ffeeef32b9c950ywan        String::CStringEquals(term, "cygwin");
4088233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return stdout_is_tty && term_supports_color;
4089233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
4090233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4091233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4092233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return String::CaseInsensitiveCStringEquals(gtest_color, "yes") ||
4093233d2500723e5594f3e7c70896ffeeef32b9c950ywan      String::CaseInsensitiveCStringEquals(gtest_color, "true") ||
4094233d2500723e5594f3e7c70896ffeeef32b9c950ywan      String::CaseInsensitiveCStringEquals(gtest_color, "t") ||
4095233d2500723e5594f3e7c70896ffeeef32b9c950ywan      String::CStringEquals(gtest_color, "1");
4096233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We take "yes", "true", "t", and "1" as meaning "yes".  If the
4097233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // value is neither one of these nor "auto", we treat it as "no" to
4098233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // be conservative.
4099233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4100233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4101233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helpers for printing colored strings to stdout. Note that on Windows, we
4102233d2500723e5594f3e7c70896ffeeef32b9c950ywan// cannot simply emit special characters and have the terminal change colors.
4103233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This routine must actually emit the characters rather than return a string
4104233d2500723e5594f3e7c70896ffeeef32b9c950ywan// that would be colored when printed, as can be done on Linux.
4105233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ColoredPrintf(GTestColor color, const char* fmt, ...) {
4106233d2500723e5594f3e7c70896ffeeef32b9c950ywan  va_list args;
4107233d2500723e5594f3e7c70896ffeeef32b9c950ywan  va_start(args, fmt);
4108233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4109233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS || GTEST_OS_IOS
4110233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool use_color = false;
4111233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
4112233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static const bool in_color_mode =
4113233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0);
4114233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool use_color = in_color_mode && (color != COLOR_DEFAULT);
4115233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS
4116233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The '!= 0' comparison is necessary to satisfy MSVC 7.1.
4117233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4118233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!use_color) {
4119233d2500723e5594f3e7c70896ffeeef32b9c950ywan    vprintf(fmt, args);
4120233d2500723e5594f3e7c70896ffeeef32b9c950ywan    va_end(args);
4121233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
4122233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4123233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4124233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
4125233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
4126233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4127233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the current text color.
4128233d2500723e5594f3e7c70896ffeeef32b9c950ywan  CONSOLE_SCREEN_BUFFER_INFO buffer_info;
4129233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
4130233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const WORD old_color_attrs = buffer_info.wAttributes;
4131233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4132233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We need to flush the stream buffers into the console before each
4133233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // SetConsoleTextAttribute call lest it affect the text that is already
4134233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // printed but has not yet reached the console.
4135233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4136233d2500723e5594f3e7c70896ffeeef32b9c950ywan  SetConsoleTextAttribute(stdout_handle,
4137233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          GetColorAttribute(color) | FOREGROUND_INTENSITY);
4138233d2500723e5594f3e7c70896ffeeef32b9c950ywan  vprintf(fmt, args);
4139233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4140233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4141233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Restores the text color.
4142233d2500723e5594f3e7c70896ffeeef32b9c950ywan  SetConsoleTextAttribute(stdout_handle, old_color_attrs);
4143233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
4144233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("\033[0;3%sm", GetAnsiColorCode(color));
4145233d2500723e5594f3e7c70896ffeeef32b9c950ywan  vprintf(fmt, args);
4146233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("\033[m");  // Resets the terminal to default.
4147233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
4148233d2500723e5594f3e7c70896ffeeef32b9c950ywan  va_end(args);
4149233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4150233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4151233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Text printed in Google Test's text output and --gunit_list_tests
4152233d2500723e5594f3e7c70896ffeeef32b9c950ywan// output to label the type parameter and value parameter for a test.
4153233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kTypeParamLabel[] = "TypeParam";
4154233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kValueParamLabel[] = "GetParam()";
4155233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4156233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintFullTestCommentIfPresent(const TestInfo& test_info) {
4157233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const type_param = test_info.type_param();
4158233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const value_param = test_info.value_param();
4159233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4160233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (type_param != NULL || value_param != NULL) {
4161233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf(", where ");
4162233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (type_param != NULL) {
4163233d2500723e5594f3e7c70896ffeeef32b9c950ywan      printf("%s = %s", kTypeParamLabel, type_param);
4164233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (value_param != NULL)
4165233d2500723e5594f3e7c70896ffeeef32b9c950ywan        printf(" and ");
4166233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4167233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (value_param != NULL) {
4168233d2500723e5594f3e7c70896ffeeef32b9c950ywan      printf("%s = %s", kValueParamLabel, value_param);
4169233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4170233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4171233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4172233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4173233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This class implements the TestEventListener interface.
4174233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
4175233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Class PrettyUnitTestResultPrinter is copyable.
4176233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass PrettyUnitTestResultPrinter : public TestEventListener {
4177233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
4178233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrettyUnitTestResultPrinter() {}
4179233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void PrintTestName(const char * test_case, const char * test) {
4180233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("%s.%s", test_case, test);
4181233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4182233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4183233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The following methods override what's in the TestEventListener class.
4184233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {}
4185233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);
4186233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);
4187233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {}
4188233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestCaseStart(const TestCase& test_case);
4189233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestStart(const TestInfo& test_info);
4190233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestPartResult(const TestPartResult& result);
4191233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestEnd(const TestInfo& test_info);
4192233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestCaseEnd(const TestCase& test_case);
4193233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);
4194233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {}
4195233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
4196233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {}
4197233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4198233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
4199233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void PrintFailedTests(const UnitTest& unit_test);
4200233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
4201233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4202233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Fired before each iteration of tests starts.
4203233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnTestIterationStart(
4204233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const UnitTest& unit_test, int iteration) {
4205233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(repeat) != 1)
4206233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1);
4207233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4208233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const filter = GTEST_FLAG(filter).c_str();
4209233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4210233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Prints the filter if it's not *.  This reminds the user that some
4211233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // tests may be skipped.
4212233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!String::CStringEquals(filter, kUniversalFilter)) {
4213233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_YELLOW,
4214233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  "Note: %s filter = %s\n", GTEST_NAME_, filter);
4215233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4216233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4217233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) {
4218233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const Int32 shard_index = Int32FromEnvOrDie(kTestShardIndex, -1);
4219233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_YELLOW,
4220233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  "Note: This is test shard %d of %s.\n",
4221233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  static_cast<int>(shard_index) + 1,
4222233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  internal::posix::GetEnv(kTestTotalShards));
4223233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4224233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4225233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(shuffle)) {
4226233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_YELLOW,
4227233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  "Note: Randomizing tests' orders with a seed of %d .\n",
4228233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  unit_test.random_seed());
4229233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4230233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4231233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ColoredPrintf(COLOR_GREEN,  "[==========] ");
4232233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("Running %s from %s.\n",
4233233d2500723e5594f3e7c70896ffeeef32b9c950ywan         FormatTestCount(unit_test.test_to_run_count()).c_str(),
4234233d2500723e5594f3e7c70896ffeeef32b9c950ywan         FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str());
4235233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4236233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4237233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4238233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnEnvironmentsSetUpStart(
4239233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const UnitTest& /*unit_test*/) {
4240233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ColoredPrintf(COLOR_GREEN,  "[----------] ");
4241233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("Global test environment set-up.\n");
4242233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4243233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4244233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4245233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) {
4246233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string counts =
4247233d2500723e5594f3e7c70896ffeeef32b9c950ywan      FormatCountableNoun(test_case.test_to_run_count(), "test", "tests");
4248233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ColoredPrintf(COLOR_GREEN, "[----------] ");
4249233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("%s from %s", counts.c_str(), test_case.name());
4250233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (test_case.type_param() == NULL) {
4251233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("\n");
4252233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
4253233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf(", where %s = %s\n", kTypeParamLabel, test_case.type_param());
4254233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4255233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4256233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4257233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4258233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) {
4259233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ColoredPrintf(COLOR_GREEN,  "[ RUN      ] ");
4260233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintTestName(test_info.test_case_name(), test_info.name());
4261233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("\n");
4262233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4263233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4264233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4265233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Called after an assertion failure.
4266233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnTestPartResult(
4267233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& result) {
4268233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // If the test part succeeded, we don't need to do anything.
4269233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (result.type() == TestPartResult::kSuccess)
4270233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
4271233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4272233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Print failure message from the assertion (e.g. expected this and got that).
4273233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintTestPartResult(result);
4274233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4275233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4276233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4277233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) {
4278233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (test_info.result()->Passed()) {
4279233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_GREEN, "[       OK ] ");
4280233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
4281233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_RED, "[  FAILED  ] ");
4282233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4283233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintTestName(test_info.test_case_name(), test_info.name());
4284233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (test_info.result()->Failed())
4285233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintFullTestCommentIfPresent(test_info);
4286233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4287233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(print_time)) {
4288233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf(" (%s ms)\n", internal::StreamableToString(
4289233d2500723e5594f3e7c70896ffeeef32b9c950ywan           test_info.result()->elapsed_time()).c_str());
4290233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
4291233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("\n");
4292233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4293233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4294233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4295233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4296233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) {
4297233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!GTEST_FLAG(print_time)) return;
4298233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4299233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string counts =
4300233d2500723e5594f3e7c70896ffeeef32b9c950ywan      FormatCountableNoun(test_case.test_to_run_count(), "test", "tests");
4301233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ColoredPrintf(COLOR_GREEN, "[----------] ");
4302233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("%s from %s (%s ms total)\n\n",
4303233d2500723e5594f3e7c70896ffeeef32b9c950ywan         counts.c_str(), test_case.name(),
4304233d2500723e5594f3e7c70896ffeeef32b9c950ywan         internal::StreamableToString(test_case.elapsed_time()).c_str());
4305233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4306233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4307233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4308233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnEnvironmentsTearDownStart(
4309233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const UnitTest& /*unit_test*/) {
4310233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ColoredPrintf(COLOR_GREEN,  "[----------] ");
4311233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("Global test environment tear-down\n");
4312233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4313233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4314233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4315233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Internal helper for printing the list of failed tests.
4316233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) {
4317233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int failed_test_count = unit_test.failed_test_count();
4318233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (failed_test_count == 0) {
4319233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
4320233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4321233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4322233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
4323233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestCase& test_case = *unit_test.GetTestCase(i);
4324233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (!test_case.should_run() || (test_case.failed_test_count() == 0)) {
4325233d2500723e5594f3e7c70896ffeeef32b9c950ywan      continue;
4326233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4327233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (int j = 0; j < test_case.total_test_count(); ++j) {
4328233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const TestInfo& test_info = *test_case.GetTestInfo(j);
4329233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (!test_info.should_run() || test_info.result()->Passed()) {
4330233d2500723e5594f3e7c70896ffeeef32b9c950ywan        continue;
4331233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
4332233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ColoredPrintf(COLOR_RED, "[  FAILED  ] ");
4333233d2500723e5594f3e7c70896ffeeef32b9c950ywan      printf("%s.%s", test_case.name(), test_info.name());
4334233d2500723e5594f3e7c70896ffeeef32b9c950ywan      PrintFullTestCommentIfPresent(test_info);
4335233d2500723e5594f3e7c70896ffeeef32b9c950ywan      printf("\n");
4336233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4337233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4338233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4339233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4340233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
4341233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                     int /*iteration*/) {
4342233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ColoredPrintf(COLOR_GREEN,  "[==========] ");
4343233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("%s from %s ran.",
4344233d2500723e5594f3e7c70896ffeeef32b9c950ywan         FormatTestCount(unit_test.test_to_run_count()).c_str(),
4345233d2500723e5594f3e7c70896ffeeef32b9c950ywan         FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str());
4346233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(print_time)) {
4347233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf(" (%s ms total)",
4348233d2500723e5594f3e7c70896ffeeef32b9c950ywan           internal::StreamableToString(unit_test.elapsed_time()).c_str());
4349233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4350233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("\n");
4351233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ColoredPrintf(COLOR_GREEN,  "[  PASSED  ] ");
4352233d2500723e5594f3e7c70896ffeeef32b9c950ywan  printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str());
4353233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4354233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int num_failures = unit_test.failed_test_count();
4355233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!unit_test.Passed()) {
4356233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const int failed_test_count = unit_test.failed_test_count();
4357233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_RED,  "[  FAILED  ] ");
4358233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("%s, listed below:\n", FormatTestCount(failed_test_count).c_str());
4359233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintFailedTests(unit_test);
4360233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("\n%2d FAILED %s\n", num_failures,
4361233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        num_failures == 1 ? "TEST" : "TESTS");
4362233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4363233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4364233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int num_disabled = unit_test.reportable_disabled_test_count();
4365233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) {
4366233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (!num_failures) {
4367233d2500723e5594f3e7c70896ffeeef32b9c950ywan      printf("\n");  // Add a spacer if no FAILURE banner is displayed.
4368233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4369233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_YELLOW,
4370233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  "  YOU HAVE %d DISABLED %s\n\n",
4371233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  num_disabled,
4372233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  num_disabled == 1 ? "TEST" : "TESTS");
4373233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4374233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Ensure that Google Test output is printed before, e.g., heapchecker output.
4375233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
4376233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4377233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4378233d2500723e5594f3e7c70896ffeeef32b9c950ywan// End PrettyUnitTestResultPrinter
4379233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4380233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class TestEventRepeater
4381233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
4382233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This class forwards events to other event listeners.
4383233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass TestEventRepeater : public TestEventListener {
4384233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
4385233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestEventRepeater() : forwarding_enabled_(true) {}
4386233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual ~TestEventRepeater();
4387233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void Append(TestEventListener *listener);
4388233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestEventListener* Release(TestEventListener* listener);
4389233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4390233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Controls whether events will be forwarded to listeners_. Set to false
4391233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // in death test child processes.
4392233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool forwarding_enabled() const { return forwarding_enabled_; }
4393233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; }
4394233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4395233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestProgramStart(const UnitTest& unit_test);
4396233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);
4397233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);
4398233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test);
4399233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestCaseStart(const TestCase& test_case);
4400233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestStart(const TestInfo& test_info);
4401233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestPartResult(const TestPartResult& result);
4402233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestEnd(const TestInfo& test_info);
4403233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestCaseEnd(const TestCase& test_case);
4404233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);
4405233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test);
4406233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
4407233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestProgramEnd(const UnitTest& unit_test);
4408233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4409233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
4410233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Controls whether events will be forwarded to listeners_. Set to false
4411233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // in death test child processes.
4412233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool forwarding_enabled_;
4413233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The list of listeners that receive events.
4414233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::vector<TestEventListener*> listeners_;
4415233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4416233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventRepeater);
4417233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
4418233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4419233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestEventRepeater::~TestEventRepeater() {
4420233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ForEach(listeners_, Delete<TestEventListener>);
4421233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4422233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4423233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventRepeater::Append(TestEventListener *listener) {
4424233d2500723e5594f3e7c70896ffeeef32b9c950ywan  listeners_.push_back(listener);
4425233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4426233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4427233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(vladl@google.com): Factor the search functionality into Vector::Find.
4428233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestEventListener* TestEventRepeater::Release(TestEventListener *listener) {
4429233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < listeners_.size(); ++i) {
4430233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (listeners_[i] == listener) {
4431233d2500723e5594f3e7c70896ffeeef32b9c950ywan      listeners_.erase(listeners_.begin() + i);
4432233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return listener;
4433233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4434233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4435233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4436233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return NULL;
4437233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4438233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4439233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Since most methods are very similar, use macros to reduce boilerplate.
4440233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This defines a member that forwards the call to all listeners.
4441233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_REPEATER_METHOD_(Name, Type) \
4442233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventRepeater::Name(const Type& parameter) { \
4443233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (forwarding_enabled_) { \
4444233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (size_t i = 0; i < listeners_.size(); i++) { \
4445233d2500723e5594f3e7c70896ffeeef32b9c950ywan      listeners_[i]->Name(parameter); \
4446233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } \
4447233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } \
4448233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4449233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This defines a member that forwards the call to all listeners in reverse
4450233d2500723e5594f3e7c70896ffeeef32b9c950ywan// order.
4451233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \
4452233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventRepeater::Name(const Type& parameter) { \
4453233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (forwarding_enabled_) { \
4454233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) { \
4455233d2500723e5594f3e7c70896ffeeef32b9c950ywan      listeners_[i]->Name(parameter); \
4456233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } \
4457233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } \
4458233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4459233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4460233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest)
4461233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest)
4462233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REPEATER_METHOD_(OnTestCaseStart, TestCase)
4463233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REPEATER_METHOD_(OnTestStart, TestInfo)
4464233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult)
4465233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest)
4466233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest)
4467233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest)
4468233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo)
4469233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestCase)
4470233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_REVERSE_REPEATER_METHOD_(OnTestProgramEnd, UnitTest)
4471233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4472233d2500723e5594f3e7c70896ffeeef32b9c950ywan#undef GTEST_REPEATER_METHOD_
4473233d2500723e5594f3e7c70896ffeeef32b9c950ywan#undef GTEST_REVERSE_REPEATER_METHOD_
4474233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4475233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventRepeater::OnTestIterationStart(const UnitTest& unit_test,
4476233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                             int iteration) {
4477233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (forwarding_enabled_) {
4478233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (size_t i = 0; i < listeners_.size(); i++) {
4479233d2500723e5594f3e7c70896ffeeef32b9c950ywan      listeners_[i]->OnTestIterationStart(unit_test, iteration);
4480233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4481233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4482233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4483233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4484233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventRepeater::OnTestIterationEnd(const UnitTest& unit_test,
4485233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                           int iteration) {
4486233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (forwarding_enabled_) {
4487233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) {
4488233d2500723e5594f3e7c70896ffeeef32b9c950ywan      listeners_[i]->OnTestIterationEnd(unit_test, iteration);
4489233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4490233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4491233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4492233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4493233d2500723e5594f3e7c70896ffeeef32b9c950ywan// End TestEventRepeater
4494233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4495233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This class generates an XML output file.
4496233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass XmlUnitTestResultPrinter : public EmptyTestEventListener {
4497233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
4498233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit XmlUnitTestResultPrinter(const char* output_file);
4499233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4500233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
4501233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4502233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
4503233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Is c a whitespace character that is normalized to a space character
4504233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // when it appears in an XML attribute value?
4505233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static bool IsNormalizableWhitespace(char c) {
4506233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return c == 0x9 || c == 0xA || c == 0xD;
4507233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4508233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4509233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // May c appear in a well-formed XML document?
4510233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static bool IsValidXmlCharacter(char c) {
4511233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return IsNormalizableWhitespace(c) || c >= 0x20;
4512233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4513233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4514233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns an XML-escaped copy of the input string str.  If
4515233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // is_attribute is true, the text is meant to appear as an attribute
4516233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // value, and normalizable whitespace is preserved by replacing it
4517233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // with character references.
4518233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static std::string EscapeXml(const std::string& str, bool is_attribute);
4519233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4520233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the given string with all characters invalid in XML removed.
4521233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static std::string RemoveInvalidXmlCharacters(const std::string& str);
4522233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4523233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Convenience wrapper around EscapeXml when str is an attribute value.
4524233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static std::string EscapeXmlAttribute(const std::string& str) {
4525233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return EscapeXml(str, true);
4526233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4527233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4528233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Convenience wrapper around EscapeXml when str is not an attribute value.
4529233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static std::string EscapeXmlText(const char* str) {
4530233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return EscapeXml(str, false);
4531233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4532233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4533233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Verifies that the given attribute belongs to the given element and
4534233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // streams the attribute as XML.
4535233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void OutputXmlAttribute(std::ostream* stream,
4536233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                 const std::string& element_name,
4537233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                 const std::string& name,
4538233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                 const std::string& value);
4539233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4540233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Streams an XML CDATA section, escaping invalid CDATA sequences as needed.
4541233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void OutputXmlCDataSection(::std::ostream* stream, const char* data);
4542233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4543233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Streams an XML representation of a TestInfo object.
4544233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void OutputXmlTestInfo(::std::ostream* stream,
4545233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                const char* test_case_name,
4546233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                const TestInfo& test_info);
4547233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4548233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Prints an XML representation of a TestCase object
4549233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void PrintXmlTestCase(::std::ostream* stream,
4550233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const TestCase& test_case);
4551233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4552233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Prints an XML summary of unit_test to output stream out.
4553233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static void PrintXmlUnitTest(::std::ostream* stream,
4554233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const UnitTest& unit_test);
4555233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4556233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Produces a string representing the test properties in a result as space
4557233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // delimited XML attributes based on the property key="value" pairs.
4558233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // When the std::string is not empty, it includes a space at the beginning,
4559233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // to delimit this attribute from prior attributes.
4560233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static std::string TestPropertiesAsXmlAttributes(const TestResult& result);
4561233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4562233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The output file.
4563233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string output_file_;
4564233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4565233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(XmlUnitTestResultPrinter);
4566233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
4567233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4568233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates a new XmlUnitTestResultPrinter.
4569233d2500723e5594f3e7c70896ffeeef32b9c950ywanXmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file)
4570233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : output_file_(output_file) {
4571233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (output_file_.c_str() == NULL || output_file_.empty()) {
4572233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fprintf(stderr, "XML output file may not be null\n");
4573233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stderr);
4574233d2500723e5594f3e7c70896ffeeef32b9c950ywan    exit(EXIT_FAILURE);
4575233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4576233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4577233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4578233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Called after the unit test ends.
4579233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid XmlUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
4580233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                  int /*iteration*/) {
4581233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FILE* xmlout = NULL;
4582233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FilePath output_file(output_file_);
4583233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FilePath output_dir(output_file.RemoveFileName());
4584233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4585233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (output_dir.CreateDirectoriesRecursively()) {
4586233d2500723e5594f3e7c70896ffeeef32b9c950ywan    xmlout = posix::FOpen(output_file_.c_str(), "w");
4587233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4588233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (xmlout == NULL) {
4589233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // TODO(wan): report the reason of the failure.
4590233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //
4591233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // We don't do it for now as:
4592233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //
4593233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //   1. There is no urgent need for it.
4594233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //   2. It's a bit involved to make the errno variable thread-safe on
4595233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //      all three operating systems (Linux, Windows, and Mac OS).
4596233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //   3. To interpret the meaning of errno in a thread-safe way,
4597233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //      we need the strerror_r() function, which is not available on
4598233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //      Windows.
4599233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fprintf(stderr,
4600233d2500723e5594f3e7c70896ffeeef32b9c950ywan            "Unable to open file \"%s\"\n",
4601233d2500723e5594f3e7c70896ffeeef32b9c950ywan            output_file_.c_str());
4602233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stderr);
4603233d2500723e5594f3e7c70896ffeeef32b9c950ywan    exit(EXIT_FAILURE);
4604233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4605233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::stringstream stream;
4606233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintXmlUnitTest(&stream, unit_test);
4607233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fprintf(xmlout, "%s", StringStreamToString(&stream).c_str());
4608233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fclose(xmlout);
4609233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4610233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4611233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns an XML-escaped copy of the input string str.  If is_attribute
4612233d2500723e5594f3e7c70896ffeeef32b9c950ywan// is true, the text is meant to appear as an attribute value, and
4613233d2500723e5594f3e7c70896ffeeef32b9c950ywan// normalizable whitespace is preserved by replacing it with character
4614233d2500723e5594f3e7c70896ffeeef32b9c950ywan// references.
4615233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
4616233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Invalid XML characters in str, if any, are stripped from the output.
4617233d2500723e5594f3e7c70896ffeeef32b9c950ywan// It is expected that most, if not all, of the text processed by this
4618233d2500723e5594f3e7c70896ffeeef32b9c950ywan// module will consist of ordinary English text.
4619233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If this module is ever modified to produce version 1.1 XML output,
4620233d2500723e5594f3e7c70896ffeeef32b9c950ywan// most invalid characters can be retained using character references.
4621233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(wan): It might be nice to have a minimally invasive, human-readable
4622233d2500723e5594f3e7c70896ffeeef32b9c950ywan// escaping scheme for invalid characters, rather than dropping them.
4623233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string XmlUnitTestResultPrinter::EscapeXml(
4624233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& str, bool is_attribute) {
4625233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message m;
4626233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4627233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < str.size(); ++i) {
4628233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char ch = str[i];
4629233d2500723e5594f3e7c70896ffeeef32b9c950ywan    switch (ch) {
4630233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '<':
4631233d2500723e5594f3e7c70896ffeeef32b9c950ywan        m << "&lt;";
4632233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
4633233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '>':
4634233d2500723e5594f3e7c70896ffeeef32b9c950ywan        m << "&gt;";
4635233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
4636233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '&':
4637233d2500723e5594f3e7c70896ffeeef32b9c950ywan        m << "&amp;";
4638233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
4639233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '\'':
4640233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if (is_attribute)
4641233d2500723e5594f3e7c70896ffeeef32b9c950ywan          m << "&apos;";
4642233d2500723e5594f3e7c70896ffeeef32b9c950ywan        else
4643233d2500723e5594f3e7c70896ffeeef32b9c950ywan          m << '\'';
4644233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
4645233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '"':
4646233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if (is_attribute)
4647233d2500723e5594f3e7c70896ffeeef32b9c950ywan          m << "&quot;";
4648233d2500723e5594f3e7c70896ffeeef32b9c950ywan        else
4649233d2500723e5594f3e7c70896ffeeef32b9c950ywan          m << '"';
4650233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
4651233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default:
4652233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if (IsValidXmlCharacter(ch)) {
4653233d2500723e5594f3e7c70896ffeeef32b9c950ywan          if (is_attribute && IsNormalizableWhitespace(ch))
4654233d2500723e5594f3e7c70896ffeeef32b9c950ywan            m << "&#x" << String::FormatByte(static_cast<unsigned char>(ch))
4655233d2500723e5594f3e7c70896ffeeef32b9c950ywan              << ";";
4656233d2500723e5594f3e7c70896ffeeef32b9c950ywan          else
4657233d2500723e5594f3e7c70896ffeeef32b9c950ywan            m << ch;
4658233d2500723e5594f3e7c70896ffeeef32b9c950ywan        }
4659233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
4660233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4661233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4662233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4663233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return m.GetString();
4664233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4665233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4666233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the given string with all characters invalid in XML removed.
4667233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Currently invalid characters are dropped from the string. An
4668233d2500723e5594f3e7c70896ffeeef32b9c950ywan// alternative is to replace them with certain characters such as . or ?.
4669233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(
4670233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& str) {
4671233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string output;
4672233d2500723e5594f3e7c70896ffeeef32b9c950ywan  output.reserve(str.size());
4673233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
4674233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (IsValidXmlCharacter(*it))
4675233d2500723e5594f3e7c70896ffeeef32b9c950ywan      output.push_back(*it);
4676233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4677233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return output;
4678233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4679233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4680233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The following routines generate an XML representation of a UnitTest
4681233d2500723e5594f3e7c70896ffeeef32b9c950ywan// object.
4682233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
4683233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This is how Google Test concepts map to the DTD:
4684233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
4685233d2500723e5594f3e7c70896ffeeef32b9c950ywan// <testsuites name="AllTests">        <-- corresponds to a UnitTest object
4686233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   <testsuite name="testcase-name">  <-- corresponds to a TestCase object
4687233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     <testcase name="test-name">     <-- corresponds to a TestInfo object
4688233d2500723e5594f3e7c70896ffeeef32b9c950ywan//       <failure message="...">...</failure>
4689233d2500723e5594f3e7c70896ffeeef32b9c950ywan//       <failure message="...">...</failure>
4690233d2500723e5594f3e7c70896ffeeef32b9c950ywan//       <failure message="...">...</failure>
4691233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                                     <-- individual assertion failures
4692233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     </testcase>
4693233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   </testsuite>
4694233d2500723e5594f3e7c70896ffeeef32b9c950ywan// </testsuites>
4695233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4696233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats the given time in milliseconds as seconds.
4697233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string FormatTimeInMillisAsSeconds(TimeInMillis ms) {
4698233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::stringstream ss;
4699233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ss << ms/1000.0;
4700233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ss.str();
4701233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4702233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4703233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Converts the given epoch time in milliseconds to a date string in the ISO
4704233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 8601 format, without the timezone information.
4705233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) {
4706233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Using non-reentrant version as localtime_r is not portable.
4707233d2500723e5594f3e7c70896ffeeef32b9c950ywan  time_t seconds = static_cast<time_t>(ms / 1000);
4708233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifdef _MSC_VER
4709233d2500723e5594f3e7c70896ffeeef32b9c950ywan# pragma warning(push)          // Saves the current warning state.
4710233d2500723e5594f3e7c70896ffeeef32b9c950ywan# pragma warning(disable:4996)  // Temporarily disables warning 4996
4711233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                // (function or variable may be unsafe).
4712233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const struct tm* const time_struct = localtime(&seconds);  // NOLINT
4713233d2500723e5594f3e7c70896ffeeef32b9c950ywan# pragma warning(pop)           // Restores the warning state again.
4714233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
4715233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const struct tm* const time_struct = localtime(&seconds);  // NOLINT
4716233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
4717233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (time_struct == NULL)
4718233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return "";  // Invalid ms value
4719233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4720233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // YYYY-MM-DDThh:mm:ss
4721233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return StreamableToString(time_struct->tm_year + 1900) + "-" +
4722233d2500723e5594f3e7c70896ffeeef32b9c950ywan      String::FormatIntWidth2(time_struct->tm_mon + 1) + "-" +
4723233d2500723e5594f3e7c70896ffeeef32b9c950ywan      String::FormatIntWidth2(time_struct->tm_mday) + "T" +
4724233d2500723e5594f3e7c70896ffeeef32b9c950ywan      String::FormatIntWidth2(time_struct->tm_hour) + ":" +
4725233d2500723e5594f3e7c70896ffeeef32b9c950ywan      String::FormatIntWidth2(time_struct->tm_min) + ":" +
4726233d2500723e5594f3e7c70896ffeeef32b9c950ywan      String::FormatIntWidth2(time_struct->tm_sec);
4727233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4728233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4729233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Streams an XML CDATA section, escaping invalid CDATA sequences as needed.
4730233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream,
4731233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                     const char* data) {
4732233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* segment = data;
4733233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << "<![CDATA[";
4734233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (;;) {
4735233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* const next_segment = strstr(segment, "]]>");
4736233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (next_segment != NULL) {
4737233d2500723e5594f3e7c70896ffeeef32b9c950ywan      stream->write(
4738233d2500723e5594f3e7c70896ffeeef32b9c950ywan          segment, static_cast<std::streamsize>(next_segment - segment));
4739233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *stream << "]]>]]&gt;<![CDATA[";
4740233d2500723e5594f3e7c70896ffeeef32b9c950ywan      segment = next_segment + strlen("]]>");
4741233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
4742233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *stream << segment;
4743233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
4744233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4745233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4746233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << "]]>";
4747233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4748233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4749233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid XmlUnitTestResultPrinter::OutputXmlAttribute(
4750233d2500723e5594f3e7c70896ffeeef32b9c950ywan    std::ostream* stream,
4751233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& element_name,
4752233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& name,
4753233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& value) {
4754233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::vector<std::string>& allowed_names =
4755233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GetReservedAttributesForElement(element_name);
4756233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4757233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) !=
4758233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   allowed_names.end())
4759233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Attribute " << name << " is not allowed for element <" << element_name
4760233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << ">.";
4761233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4762233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << " " << name << "=\"" << EscapeXmlAttribute(value) << "\"";
4763233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4764233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4765233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints an XML representation of a TestInfo object.
4766233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(wan): There is also value in printing properties with the plain printer.
4767233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream,
4768233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                 const char* test_case_name,
4769233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                 const TestInfo& test_info) {
4770233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestResult& result = *test_info.result();
4771233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string kTestcase = "testcase";
4772233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4773233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << "    <testcase";
4774233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestcase, "name", test_info.name());
4775233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4776233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (test_info.value_param() != NULL) {
4777233d2500723e5594f3e7c70896ffeeef32b9c950ywan    OutputXmlAttribute(stream, kTestcase, "value_param",
4778233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       test_info.value_param());
4779233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4780233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (test_info.type_param() != NULL) {
4781233d2500723e5594f3e7c70896ffeeef32b9c950ywan    OutputXmlAttribute(stream, kTestcase, "type_param", test_info.type_param());
4782233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4783233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4784233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestcase, "status",
4785233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     test_info.should_run() ? "run" : "notrun");
4786233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestcase, "time",
4787233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     FormatTimeInMillisAsSeconds(result.elapsed_time()));
4788233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestcase, "classname", test_case_name);
4789233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << TestPropertiesAsXmlAttributes(result);
4790233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4791233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int failures = 0;
4792233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i < result.total_part_count(); ++i) {
4793233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& part = result.GetTestPartResult(i);
4794233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (part.failed()) {
4795233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (++failures == 1) {
4796233d2500723e5594f3e7c70896ffeeef32b9c950ywan        *stream << ">\n";
4797233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
4798233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const string location = internal::FormatCompilerIndependentFileLocation(
4799233d2500723e5594f3e7c70896ffeeef32b9c950ywan          part.file_name(), part.line_number());
4800233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const string summary = location + "\n" + part.summary();
4801233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *stream << "      <failure message=\""
4802233d2500723e5594f3e7c70896ffeeef32b9c950ywan              << EscapeXmlAttribute(summary.c_str())
4803233d2500723e5594f3e7c70896ffeeef32b9c950ywan              << "\" type=\"\">";
4804233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const string detail = location + "\n" + part.message();
4805233d2500723e5594f3e7c70896ffeeef32b9c950ywan      OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str());
4806233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *stream << "</failure>\n";
4807233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4808233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4809233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4810233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (failures == 0)
4811233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *stream << " />\n";
4812233d2500723e5594f3e7c70896ffeeef32b9c950ywan  else
4813233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *stream << "    </testcase>\n";
4814233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4815233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4816233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints an XML representation of a TestCase object
4817233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid XmlUnitTestResultPrinter::PrintXmlTestCase(std::ostream* stream,
4818233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                const TestCase& test_case) {
4819233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string kTestsuite = "testsuite";
4820233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << "  <" << kTestsuite;
4821233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuite, "name", test_case.name());
4822233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuite, "tests",
4823233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     StreamableToString(test_case.reportable_test_count()));
4824233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuite, "failures",
4825233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     StreamableToString(test_case.failed_test_count()));
4826233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(
4827233d2500723e5594f3e7c70896ffeeef32b9c950ywan      stream, kTestsuite, "disabled",
4828233d2500723e5594f3e7c70896ffeeef32b9c950ywan      StreamableToString(test_case.reportable_disabled_test_count()));
4829233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuite, "errors", "0");
4830233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuite, "time",
4831233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     FormatTimeInMillisAsSeconds(test_case.elapsed_time()));
4832233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << TestPropertiesAsXmlAttributes(test_case.ad_hoc_test_result())
4833233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << ">\n";
4834233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4835233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i < test_case.total_test_count(); ++i) {
4836233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (test_case.GetTestInfo(i)->is_reportable())
4837233d2500723e5594f3e7c70896ffeeef32b9c950ywan      OutputXmlTestInfo(stream, test_case.name(), *test_case.GetTestInfo(i));
4838233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4839233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << "  </" << kTestsuite << ">\n";
4840233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4841233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4842233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints an XML summary of unit_test to output stream out.
4843233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid XmlUnitTestResultPrinter::PrintXmlUnitTest(std::ostream* stream,
4844233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                const UnitTest& unit_test) {
4845233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string kTestsuites = "testsuites";
4846233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4847233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
4848233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << "<" << kTestsuites;
4849233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4850233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuites, "tests",
4851233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     StreamableToString(unit_test.reportable_test_count()));
4852233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuites, "failures",
4853233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     StreamableToString(unit_test.failed_test_count()));
4854233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(
4855233d2500723e5594f3e7c70896ffeeef32b9c950ywan      stream, kTestsuites, "disabled",
4856233d2500723e5594f3e7c70896ffeeef32b9c950ywan      StreamableToString(unit_test.reportable_disabled_test_count()));
4857233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuites, "errors", "0");
4858233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(
4859233d2500723e5594f3e7c70896ffeeef32b9c950ywan      stream, kTestsuites, "timestamp",
4860233d2500723e5594f3e7c70896ffeeef32b9c950ywan      FormatEpochTimeInMillisAsIso8601(unit_test.start_timestamp()));
4861233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuites, "time",
4862233d2500723e5594f3e7c70896ffeeef32b9c950ywan                     FormatTimeInMillisAsSeconds(unit_test.elapsed_time()));
4863233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4864233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(shuffle)) {
4865233d2500723e5594f3e7c70896ffeeef32b9c950ywan    OutputXmlAttribute(stream, kTestsuites, "random_seed",
4866233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       StreamableToString(unit_test.random_seed()));
4867233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4868233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4869233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << TestPropertiesAsXmlAttributes(unit_test.ad_hoc_test_result());
4870233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4871233d2500723e5594f3e7c70896ffeeef32b9c950ywan  OutputXmlAttribute(stream, kTestsuites, "name", "AllTests");
4872233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << ">\n";
4873233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4874233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
4875233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (unit_test.GetTestCase(i)->reportable_test_count() > 0)
4876233d2500723e5594f3e7c70896ffeeef32b9c950ywan      PrintXmlTestCase(stream, *unit_test.GetTestCase(i));
4877233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4878233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream << "</" << kTestsuites << ">\n";
4879233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4880233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4881233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Produces a string representing the test properties in a result as space
4882233d2500723e5594f3e7c70896ffeeef32b9c950ywan// delimited XML attributes based on the property key="value" pairs.
4883233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(
4884233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestResult& result) {
4885233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message attributes;
4886233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i < result.test_property_count(); ++i) {
4887233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestProperty& property = result.GetTestProperty(i);
4888233d2500723e5594f3e7c70896ffeeef32b9c950ywan    attributes << " " << property.key() << "="
4889233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << "\"" << EscapeXmlAttribute(property.value()) << "\"";
4890233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4891233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return attributes.GetString();
4892233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4893233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4894233d2500723e5594f3e7c70896ffeeef32b9c950ywan// End XmlUnitTestResultPrinter
4895233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4896233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_CAN_STREAM_RESULTS_
4897233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4898233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Checks if str contains '=', '&', '%' or '\n' characters. If yes,
4899233d2500723e5594f3e7c70896ffeeef32b9c950ywan// replaces them by "%xx" where xx is their hexadecimal value. For
4900233d2500723e5594f3e7c70896ffeeef32b9c950ywan// example, replaces "=" with "%3D".  This algorithm is O(strlen(str))
4901233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in both time and space -- important as the input str may contain an
4902233d2500723e5594f3e7c70896ffeeef32b9c950ywan// arbitrarily long test failure message and stack trace.
4903233d2500723e5594f3e7c70896ffeeef32b9c950ywanstring StreamingListener::UrlEncode(const char* str) {
4904233d2500723e5594f3e7c70896ffeeef32b9c950ywan  string result;
4905233d2500723e5594f3e7c70896ffeeef32b9c950ywan  result.reserve(strlen(str) + 1);
4906233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (char ch = *str; ch != '\0'; ch = *++str) {
4907233d2500723e5594f3e7c70896ffeeef32b9c950ywan    switch (ch) {
4908233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '%':
4909233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '=':
4910233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '&':
4911233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case '\n':
4912233d2500723e5594f3e7c70896ffeeef32b9c950ywan        result.append("%" + String::FormatByte(static_cast<unsigned char>(ch)));
4913233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
4914233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default:
4915233d2500723e5594f3e7c70896ffeeef32b9c950ywan        result.push_back(ch);
4916233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
4917233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4918233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4919233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result;
4920233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4921233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4922233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid StreamingListener::SocketWriter::MakeConnection() {
4923233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(sockfd_ == -1)
4924233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "MakeConnection() can't be called when there is already a connection.";
4925233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4926233d2500723e5594f3e7c70896ffeeef32b9c950ywan  addrinfo hints;
4927233d2500723e5594f3e7c70896ffeeef32b9c950ywan  memset(&hints, 0, sizeof(hints));
4928233d2500723e5594f3e7c70896ffeeef32b9c950ywan  hints.ai_family = AF_UNSPEC;    // To allow both IPv4 and IPv6 addresses.
4929233d2500723e5594f3e7c70896ffeeef32b9c950ywan  hints.ai_socktype = SOCK_STREAM;
4930233d2500723e5594f3e7c70896ffeeef32b9c950ywan  addrinfo* servinfo = NULL;
4931233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4932233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Use the getaddrinfo() to get a linked list of IP addresses for
4933233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the given host name.
4934233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int error_num = getaddrinfo(
4935233d2500723e5594f3e7c70896ffeeef32b9c950ywan      host_name_.c_str(), port_num_.c_str(), &hints, &servinfo);
4936233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (error_num != 0) {
4937233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: "
4938233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        << gai_strerror(error_num);
4939233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4940233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4941233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Loop through all the results and connect to the first we can.
4942233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != NULL;
4943233d2500723e5594f3e7c70896ffeeef32b9c950ywan       cur_addr = cur_addr->ai_next) {
4944233d2500723e5594f3e7c70896ffeeef32b9c950ywan    sockfd_ = socket(
4945233d2500723e5594f3e7c70896ffeeef32b9c950ywan        cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol);
4946233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (sockfd_ != -1) {
4947233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Connect the client socket to the server socket.
4948233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) {
4949233d2500723e5594f3e7c70896ffeeef32b9c950ywan        close(sockfd_);
4950233d2500723e5594f3e7c70896ffeeef32b9c950ywan        sockfd_ = -1;
4951233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
4952233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
4953233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4954233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4955233d2500723e5594f3e7c70896ffeeef32b9c950ywan  freeaddrinfo(servinfo);  // all done with this structure
4956233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4957233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (sockfd_ == -1) {
4958233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOG_(WARNING) << "stream_result_to: failed to connect to "
4959233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        << host_name_ << ":" << port_num_;
4960233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
4961233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4962233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4963233d2500723e5594f3e7c70896ffeeef32b9c950ywan// End of class Streaming Listener
4964233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_CAN_STREAM_RESULTS__
4965233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4966233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Class ScopedTrace
4967233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4968233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Pushes the given source file location and message onto a per-thread
4969233d2500723e5594f3e7c70896ffeeef32b9c950ywan// trace stack maintained by Google Test.
4970233d2500723e5594f3e7c70896ffeeef32b9c950ywanScopedTrace::ScopedTrace(const char* file, int line, const Message& message)
4971233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) {
4972233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TraceInfo trace;
4973233d2500723e5594f3e7c70896ffeeef32b9c950ywan  trace.file = file;
4974233d2500723e5594f3e7c70896ffeeef32b9c950ywan  trace.line = line;
4975233d2500723e5594f3e7c70896ffeeef32b9c950ywan  trace.message = message.GetString();
4976233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4977233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTest::GetInstance()->PushGTestTrace(trace);
4978233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4979233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4980233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Pops the info pushed by the c'tor.
4981233d2500723e5594f3e7c70896ffeeef32b9c950ywanScopedTrace::~ScopedTrace()
4982233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) {
4983233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTest::GetInstance()->PopGTestTrace();
4984233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
4985233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4986233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4987233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class OsStackTraceGetter
4988233d2500723e5594f3e7c70896ffeeef32b9c950ywan
4989233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current OS stack trace as an std::string.  Parameters:
4990233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
4991233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   max_depth  - the maximum number of stack frames to be included
4992233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                in the trace.
4993233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   skip_count - the number of top frames to be skipped; doesn't count
4994233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                against max_depth.
4995233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
4996233d2500723e5594f3e7c70896ffeeef32b9c950ywanstring OsStackTraceGetter::CurrentStackTrace(int /* max_depth */,
4997233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                             int /* skip_count */)
4998233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOCK_EXCLUDED_(mutex_) {
4999233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return "";
5000233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5001233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5002233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid OsStackTraceGetter::UponLeavingGTest()
5003233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOCK_EXCLUDED_(mutex_) {
5004233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5005233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5006233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* const
5007233d2500723e5594f3e7c70896ffeeef32b9c950ywanOsStackTraceGetter::kElidedFramesMarker =
5008233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "... " GTEST_NAME_ " internal frames ...";
5009233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5010233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A helper class that creates the premature-exit file in its
5011233d2500723e5594f3e7c70896ffeeef32b9c950ywan// constructor and deletes the file in its destructor.
5012233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass ScopedPrematureExitFile {
5013233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
5014233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit ScopedPrematureExitFile(const char* premature_exit_filepath)
5015233d2500723e5594f3e7c70896ffeeef32b9c950ywan      : premature_exit_filepath_(premature_exit_filepath) {
5016233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // If a path to the premature-exit file is specified...
5017233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (premature_exit_filepath != NULL && *premature_exit_filepath != '\0') {
5018233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // create the file with a single "0" character in it.  I/O
5019233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // errors are ignored as there's nothing better we can do and we
5020233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // don't want to fail the test because of this.
5021233d2500723e5594f3e7c70896ffeeef32b9c950ywan      FILE* pfile = posix::FOpen(premature_exit_filepath, "w");
5022233d2500723e5594f3e7c70896ffeeef32b9c950ywan      fwrite("0", 1, 1, pfile);
5023233d2500723e5594f3e7c70896ffeeef32b9c950ywan      fclose(pfile);
5024233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5025233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5026233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5027233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ~ScopedPrematureExitFile() {
5028233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (premature_exit_filepath_ != NULL && *premature_exit_filepath_ != '\0') {
5029233d2500723e5594f3e7c70896ffeeef32b9c950ywan      remove(premature_exit_filepath_);
5030233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5031233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5032233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5033233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
5034233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const premature_exit_filepath_;
5035233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5036233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedPrematureExitFile);
5037233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
5038233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5039233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
5040233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5041233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class TestEventListeners
5042233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5043233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestEventListeners::TestEventListeners()
5044233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : repeater_(new internal::TestEventRepeater()),
5045233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default_result_printer_(NULL),
5046233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default_xml_generator_(NULL) {
5047233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5048233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5049233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestEventListeners::~TestEventListeners() { delete repeater_; }
5050233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5051233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the standard listener responsible for the default console
5052233d2500723e5594f3e7c70896ffeeef32b9c950ywan// output.  Can be removed from the listeners list to shut down default
5053233d2500723e5594f3e7c70896ffeeef32b9c950ywan// console output.  Note that removing this object from the listener list
5054233d2500723e5594f3e7c70896ffeeef32b9c950ywan// with Release transfers its ownership to the user.
5055233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventListeners::Append(TestEventListener* listener) {
5056233d2500723e5594f3e7c70896ffeeef32b9c950ywan  repeater_->Append(listener);
5057233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5058233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5059233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Removes the given event listener from the list and returns it.  It then
5060233d2500723e5594f3e7c70896ffeeef32b9c950ywan// becomes the caller's responsibility to delete the listener. Returns
5061233d2500723e5594f3e7c70896ffeeef32b9c950ywan// NULL if the listener is not found in the list.
5062233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestEventListener* TestEventListeners::Release(TestEventListener* listener) {
5063233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (listener == default_result_printer_)
5064233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default_result_printer_ = NULL;
5065233d2500723e5594f3e7c70896ffeeef32b9c950ywan  else if (listener == default_xml_generator_)
5066233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default_xml_generator_ = NULL;
5067233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return repeater_->Release(listener);
5068233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5069233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5070233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns repeater that broadcasts the TestEventListener events to all
5071233d2500723e5594f3e7c70896ffeeef32b9c950ywan// subscribers.
5072233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestEventListener* TestEventListeners::repeater() { return repeater_; }
5073233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5074233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Sets the default_result_printer attribute to the provided listener.
5075233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The listener is also added to the listener list and previous
5076233d2500723e5594f3e7c70896ffeeef32b9c950ywan// default_result_printer is removed from it and deleted. The listener can
5077233d2500723e5594f3e7c70896ffeeef32b9c950ywan// also be NULL in which case it will not be added to the list. Does
5078233d2500723e5594f3e7c70896ffeeef32b9c950ywan// nothing if the previous and the current listener objects are the same.
5079233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventListeners::SetDefaultResultPrinter(TestEventListener* listener) {
5080233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (default_result_printer_ != listener) {
5081233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // It is an error to pass this method a listener that is already in the
5082233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // list.
5083233d2500723e5594f3e7c70896ffeeef32b9c950ywan    delete Release(default_result_printer_);
5084233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default_result_printer_ = listener;
5085233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (listener != NULL)
5086233d2500723e5594f3e7c70896ffeeef32b9c950ywan      Append(listener);
5087233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5088233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5089233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5090233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Sets the default_xml_generator attribute to the provided listener.  The
5091233d2500723e5594f3e7c70896ffeeef32b9c950ywan// listener is also added to the listener list and previous
5092233d2500723e5594f3e7c70896ffeeef32b9c950ywan// default_xml_generator is removed from it and deleted. The listener can
5093233d2500723e5594f3e7c70896ffeeef32b9c950ywan// also be NULL in which case it will not be added to the list. Does
5094233d2500723e5594f3e7c70896ffeeef32b9c950ywan// nothing if the previous and the current listener objects are the same.
5095233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventListeners::SetDefaultXmlGenerator(TestEventListener* listener) {
5096233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (default_xml_generator_ != listener) {
5097233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // It is an error to pass this method a listener that is already in the
5098233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // list.
5099233d2500723e5594f3e7c70896ffeeef32b9c950ywan    delete Release(default_xml_generator_);
5100233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default_xml_generator_ = listener;
5101233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (listener != NULL)
5102233d2500723e5594f3e7c70896ffeeef32b9c950ywan      Append(listener);
5103233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5104233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5105233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5106233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Controls whether events will be forwarded by the repeater to the
5107233d2500723e5594f3e7c70896ffeeef32b9c950ywan// listeners in the list.
5108233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool TestEventListeners::EventForwardingEnabled() const {
5109233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return repeater_->forwarding_enabled();
5110233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5111233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5112233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestEventListeners::SuppressEventForwarding() {
5113233d2500723e5594f3e7c70896ffeeef32b9c950ywan  repeater_->set_forwarding_enabled(false);
5114233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5115233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5116233d2500723e5594f3e7c70896ffeeef32b9c950ywan// class UnitTest
5117233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5118233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the singleton UnitTest object.  The first time this method is
5119233d2500723e5594f3e7c70896ffeeef32b9c950ywan// called, a UnitTest object is constructed and returned.  Consecutive
5120233d2500723e5594f3e7c70896ffeeef32b9c950ywan// calls will return the same object.
5121233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5122233d2500723e5594f3e7c70896ffeeef32b9c950ywan// We don't protect this under mutex_ as a user is not supposed to
5123233d2500723e5594f3e7c70896ffeeef32b9c950ywan// call this before main() starts, from which point on the return
5124233d2500723e5594f3e7c70896ffeeef32b9c950ywan// value will never change.
5125233d2500723e5594f3e7c70896ffeeef32b9c950ywanUnitTest* UnitTest::GetInstance() {
5126233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // When compiled with MSVC 7.1 in optimized mode, destroying the
5127233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // UnitTest object upon exiting the program messes up the exit code,
5128233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // causing successful tests to appear failed.  We have to use a
5129233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // different implementation in this case to bypass the compiler bug.
5130233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This implementation makes the compiler happy, at the cost of
5131233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // leaking the UnitTest object.
5132233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5133233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // CodeGear C++Builder insists on a public destructor for the
5134233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // default implementation.  Use this implementation to keep good OO
5135233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // design with private destructor.
5136233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5137233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__)
5138233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static UnitTest* const instance = new UnitTest;
5139233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return instance;
5140233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
5141233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static UnitTest instance;
5142233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return &instance;
5143233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__)
5144233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5145233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5146233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of successful test cases.
5147233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::successful_test_case_count() const {
5148233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->successful_test_case_count();
5149233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5150233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5151233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of failed test cases.
5152233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::failed_test_case_count() const {
5153233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->failed_test_case_count();
5154233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5155233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5156233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of all test cases.
5157233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::total_test_case_count() const {
5158233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->total_test_case_count();
5159233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5160233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5161233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of all test cases that contain at least one test
5162233d2500723e5594f3e7c70896ffeeef32b9c950ywan// that should run.
5163233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::test_case_to_run_count() const {
5164233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->test_case_to_run_count();
5165233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5166233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5167233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of successful tests.
5168233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::successful_test_count() const {
5169233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->successful_test_count();
5170233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5171233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5172233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of failed tests.
5173233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::failed_test_count() const { return impl()->failed_test_count(); }
5174233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5175233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of disabled tests that will be reported in the XML report.
5176233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::reportable_disabled_test_count() const {
5177233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->reportable_disabled_test_count();
5178233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5179233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5180233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of disabled tests.
5181233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::disabled_test_count() const {
5182233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->disabled_test_count();
5183233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5184233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5185233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of tests to be printed in the XML report.
5186233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::reportable_test_count() const {
5187233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->reportable_test_count();
5188233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5189233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5190233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of all tests.
5191233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::total_test_count() const { return impl()->total_test_count(); }
5192233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5193233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the number of tests that should run.
5194233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); }
5195233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5196233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the time of the test program start, in ms from the start of the
5197233d2500723e5594f3e7c70896ffeeef32b9c950ywan// UNIX epoch.
5198233d2500723e5594f3e7c70896ffeeef32b9c950ywaninternal::TimeInMillis UnitTest::start_timestamp() const {
5199233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return impl()->start_timestamp();
5200233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5201233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5202233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the elapsed time, in milliseconds.
5203233d2500723e5594f3e7c70896ffeeef32b9c950ywaninternal::TimeInMillis UnitTest::elapsed_time() const {
5204233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->elapsed_time();
5205233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5206233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5207233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the unit test passed (i.e. all test cases passed).
5208233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool UnitTest::Passed() const { return impl()->Passed(); }
5209233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5210233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the unit test failed (i.e. some test case failed
5211233d2500723e5594f3e7c70896ffeeef32b9c950ywan// or something outside of all tests failed).
5212233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool UnitTest::Failed() const { return impl()->Failed(); }
5213233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5214233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the i-th test case among all the test cases. i can range from 0 to
5215233d2500723e5594f3e7c70896ffeeef32b9c950ywan// total_test_case_count() - 1. If i is not in that range, returns NULL.
5216233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst TestCase* UnitTest::GetTestCase(int i) const {
5217233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->GetTestCase(i);
5218233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5219233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5220233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the TestResult containing information on test failures and
5221233d2500723e5594f3e7c70896ffeeef32b9c950ywan// properties logged outside of individual test cases.
5222233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst TestResult& UnitTest::ad_hoc_test_result() const {
5223233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return *impl()->ad_hoc_test_result();
5224233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5225233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5226233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the i-th test case among all the test cases. i can range from 0 to
5227233d2500723e5594f3e7c70896ffeeef32b9c950ywan// total_test_case_count() - 1. If i is not in that range, returns NULL.
5228233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestCase* UnitTest::GetMutableTestCase(int i) {
5229233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl()->GetMutableTestCase(i);
5230233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5231233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5232233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the list of event listeners that can be used to track events
5233233d2500723e5594f3e7c70896ffeeef32b9c950ywan// inside Google Test.
5234233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestEventListeners& UnitTest::listeners() {
5235233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return *impl()->listeners();
5236233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5237233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5238233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Registers and returns a global test environment.  When a test
5239233d2500723e5594f3e7c70896ffeeef32b9c950ywan// program is run, all global test environments will be set-up in the
5240233d2500723e5594f3e7c70896ffeeef32b9c950ywan// order they were registered.  After all tests in the program have
5241233d2500723e5594f3e7c70896ffeeef32b9c950ywan// finished, all global test environments will be torn-down in the
5242233d2500723e5594f3e7c70896ffeeef32b9c950ywan// *reverse* order they were registered.
5243233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5244233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The UnitTest object takes ownership of the given environment.
5245233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5246233d2500723e5594f3e7c70896ffeeef32b9c950ywan// We don't protect this under mutex_, as we only support calling it
5247233d2500723e5594f3e7c70896ffeeef32b9c950ywan// from the main thread.
5248233d2500723e5594f3e7c70896ffeeef32b9c950ywanEnvironment* UnitTest::AddEnvironment(Environment* env) {
5249233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (env == NULL) {
5250233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return NULL;
5251233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5252233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5253233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl_->environments().push_back(env);
5254233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return env;
5255233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5256233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5257233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds a TestPartResult to the current TestResult object.  All Google Test
5258233d2500723e5594f3e7c70896ffeeef32b9c950ywan// assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call
5259233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this to report their results.  The user code should use the
5260233d2500723e5594f3e7c70896ffeeef32b9c950ywan// assertion macros instead of calling this directly.
5261233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTest::AddTestPartResult(
5262233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TestPartResult::Type result_type,
5263233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* file_name,
5264233d2500723e5594f3e7c70896ffeeef32b9c950ywan    int line_number,
5265233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& message,
5266233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string& os_stack_trace) GTEST_LOCK_EXCLUDED_(mutex_) {
5267233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message msg;
5268233d2500723e5594f3e7c70896ffeeef32b9c950ywan  msg << message;
5269233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5270233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::MutexLock lock(&mutex_);
5271233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (impl_->gtest_trace_stack().size() > 0) {
5272233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << "\n" << GTEST_NAME_ << " trace:";
5273233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5274233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (int i = static_cast<int>(impl_->gtest_trace_stack().size());
5275233d2500723e5594f3e7c70896ffeeef32b9c950ywan         i > 0; --i) {
5276233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1];
5277233d2500723e5594f3e7c70896ffeeef32b9c950ywan      msg << "\n" << internal::FormatFileLocation(trace.file, trace.line)
5278233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << " " << trace.message;
5279233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5280233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5281233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5282233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (os_stack_trace.c_str() != NULL && !os_stack_trace.empty()) {
5283233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << internal::kStackTraceMarker << os_stack_trace;
5284233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5285233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5286233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestPartResult result =
5287233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TestPartResult(result_type, file_name, line_number,
5288233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   msg.GetString().c_str());
5289233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl_->GetTestPartResultReporterForCurrentThread()->
5290233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ReportTestPartResult(result);
5291233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5292233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (result_type != TestPartResult::kSuccess) {
5293233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // gtest_break_on_failure takes precedence over
5294233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // gtest_throw_on_failure.  This allows a user to set the latter
5295233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // in the code (perhaps in order to use Google Test assertions
5296233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // with another testing framework) and specify the former on the
5297233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // command line for debugging.
5298233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (GTEST_FLAG(break_on_failure)) {
5299233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
5300233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Using DebugBreak on Windows allows gtest to still break into a debugger
5301233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // when a failure happens and both the --gtest_break_on_failure and
5302233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // the --gtest_catch_exceptions flags are specified.
5303233d2500723e5594f3e7c70896ffeeef32b9c950ywan      DebugBreak();
5304233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
5305233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Dereference NULL through a volatile pointer to prevent the compiler
5306233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // from removing. We use this rather than abort() or __builtin_trap() for
5307233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // portability: Symbian doesn't implement abort() well, and some debuggers
5308233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // don't correctly trap abort().
5309233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *static_cast<volatile int*>(NULL) = 1;
5310233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
5311233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else if (GTEST_FLAG(throw_on_failure)) {
5312233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_EXCEPTIONS
5313233d2500723e5594f3e7c70896ffeeef32b9c950ywan      throw internal::GoogleTestFailureException(result);
5314233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
5315233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // We cannot call abort() as it generates a pop-up in debug mode
5316233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // that cannot be suppressed in VC 7.1 or below.
5317233d2500723e5594f3e7c70896ffeeef32b9c950ywan      exit(1);
5318233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
5319233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5320233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5321233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5322233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5323233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds a TestProperty to the current TestResult object when invoked from
5324233d2500723e5594f3e7c70896ffeeef32b9c950ywan// inside a test, to current TestCase's ad_hoc_test_result_ when invoked
5325233d2500723e5594f3e7c70896ffeeef32b9c950ywan// from SetUpTestCase or TearDownTestCase, or to the global property set
5326233d2500723e5594f3e7c70896ffeeef32b9c950ywan// when invoked elsewhere.  If the result already contains a property with
5327233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the same key, the value will be updated.
5328233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTest::RecordProperty(const std::string& key,
5329233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const std::string& value) {
5330233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl_->RecordProperty(TestProperty(key, value));
5331233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5332233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5333233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Runs all tests in this UnitTest object and prints the result.
5334233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns 0 if successful, or 1 otherwise.
5335233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5336233d2500723e5594f3e7c70896ffeeef32b9c950ywan// We don't protect this under mutex_, as we only support calling it
5337233d2500723e5594f3e7c70896ffeeef32b9c950ywan// from the main thread.
5338233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::Run() {
5339233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool in_death_test_child_process =
5340233d2500723e5594f3e7c70896ffeeef32b9c950ywan      internal::GTEST_FLAG(internal_run_death_test).length() > 0;
5341233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5342233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Google Test implements this protocol for catching that a test
5343233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // program exits before returning control to Google Test:
5344233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
5345233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   1. Upon start, Google Test creates a file whose absolute path
5346233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //      is specified by the environment variable
5347233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //      TEST_PREMATURE_EXIT_FILE.
5348233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   2. When Google Test has finished its work, it deletes the file.
5349233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
5350233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This allows a test runner to set TEST_PREMATURE_EXIT_FILE before
5351233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // running a Google-Test-based test program and check the existence
5352233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // of the file at the end of the test execution to see if it has
5353233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // exited prematurely.
5354233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5355233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // If we are in the child process of a death test, don't
5356233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // create/delete the premature exit file, as doing so is unnecessary
5357233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // and will confuse the parent process.  Otherwise, create/delete
5358233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the file upon entering/leaving this function.  If the program
5359233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // somehow exits before this function has a chance to return, the
5360233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // premature-exit file will be left undeleted, causing a test runner
5361233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // that understands the premature-exit-file protocol to report the
5362233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // test as having failed.
5363233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const internal::ScopedPrematureExitFile premature_exit_file(
5364233d2500723e5594f3e7c70896ffeeef32b9c950ywan      in_death_test_child_process ?
5365233d2500723e5594f3e7c70896ffeeef32b9c950ywan      NULL : internal::posix::GetEnv("TEST_PREMATURE_EXIT_FILE"));
5366233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5367233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Captures the value of GTEST_FLAG(catch_exceptions).  This value will be
5368233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // used for the duration of the program.
5369233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl()->set_catch_exceptions(GTEST_FLAG(catch_exceptions));
5370233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5371233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_SEH
5372233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Either the user wants Google Test to catch exceptions thrown by the
5373233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // tests or this is executing in the context of death test child
5374233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // process. In either case the user does not want to see pop-up dialogs
5375233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // about crashes - they are expected.
5376233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (impl()->catch_exceptions() || in_death_test_child_process) {
5377233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if !GTEST_OS_WINDOWS_MOBILE
5378233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // SetErrorMode doesn't exist on CE.
5379233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |
5380233d2500723e5594f3e7c70896ffeeef32b9c950ywan                 SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
5381233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // !GTEST_OS_WINDOWS_MOBILE
5382233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5383233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE
5384233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Death test children can be terminated with _abort().  On Windows,
5385233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // _abort() can show a dialog with a warning message.  This forces the
5386233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // abort message to go to stderr instead.
5387233d2500723e5594f3e7c70896ffeeef32b9c950ywan    _set_error_mode(_OUT_TO_STDERR);
5388233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif
5389233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5390233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
5391233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // In the debug version, Visual Studio pops up a separate dialog
5392233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // offering a choice to debug the aborted program. We need to suppress
5393233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement
5394233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // executed. Google Test will notify the user of any unexpected
5395233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // failure via stderr.
5396233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //
5397233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // VC++ doesn't define _set_abort_behavior() prior to the version 8.0.
5398233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Users of prior VC versions shall suffer the agony and pain of
5399233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // clicking through the countless debug dialogs.
5400233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // TODO(vladl@google.com): find a way to suppress the abort dialog() in the
5401233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // debug mode when compiled with VC 7.1 or lower.
5402233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (!GTEST_FLAG(break_on_failure))
5403233d2500723e5594f3e7c70896ffeeef32b9c950ywan      _set_abort_behavior(
5404233d2500723e5594f3e7c70896ffeeef32b9c950ywan          0x0,                                    // Clear the following flags:
5405233d2500723e5594f3e7c70896ffeeef32b9c950ywan          _WRITE_ABORT_MSG | _CALL_REPORTFAULT);  // pop-up window, core dump.
5406233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif
5407233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5408233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_SEH
5409233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5410233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return internal::HandleExceptionsInMethodIfSupported(
5411233d2500723e5594f3e7c70896ffeeef32b9c950ywan      impl(),
5412233d2500723e5594f3e7c70896ffeeef32b9c950ywan      &internal::UnitTestImpl::RunAllTests,
5413233d2500723e5594f3e7c70896ffeeef32b9c950ywan      "auxiliary test code (environments or event listeners)") ? 0 : 1;
5414233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5415233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5416233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the working directory when the first TEST() or TEST_F() was
5417233d2500723e5594f3e7c70896ffeeef32b9c950ywan// executed.
5418233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* UnitTest::original_working_dir() const {
5419233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl_->original_working_dir_.c_str();
5420233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5421233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5422233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the TestCase object for the test that's currently running,
5423233d2500723e5594f3e7c70896ffeeef32b9c950ywan// or NULL if no test is running.
5424233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst TestCase* UnitTest::current_test_case() const
5425233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOCK_EXCLUDED_(mutex_) {
5426233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::MutexLock lock(&mutex_);
5427233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl_->current_test_case();
5428233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5429233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5430233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the TestInfo object for the test that's currently running,
5431233d2500723e5594f3e7c70896ffeeef32b9c950ywan// or NULL if no test is running.
5432233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst TestInfo* UnitTest::current_test_info() const
5433233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOCK_EXCLUDED_(mutex_) {
5434233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::MutexLock lock(&mutex_);
5435233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl_->current_test_info();
5436233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5437233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5438233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the random seed used at the start of the current test run.
5439233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTest::random_seed() const { return impl_->random_seed(); }
5440233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5441233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_PARAM_TEST
5442233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns ParameterizedTestCaseRegistry object used to keep track of
5443233d2500723e5594f3e7c70896ffeeef32b9c950ywan// value-parameterized tests and instantiate and register them.
5444233d2500723e5594f3e7c70896ffeeef32b9c950ywaninternal::ParameterizedTestCaseRegistry&
5445233d2500723e5594f3e7c70896ffeeef32b9c950ywan    UnitTest::parameterized_test_registry()
5446233d2500723e5594f3e7c70896ffeeef32b9c950ywan        GTEST_LOCK_EXCLUDED_(mutex_) {
5447233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return impl_->parameterized_test_registry();
5448233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5449233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_PARAM_TEST
5450233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5451233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates an empty UnitTest.
5452233d2500723e5594f3e7c70896ffeeef32b9c950ywanUnitTest::UnitTest() {
5453233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl_ = new internal::UnitTestImpl(this);
5454233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5455233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5456233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Destructor of UnitTest.
5457233d2500723e5594f3e7c70896ffeeef32b9c950ywanUnitTest::~UnitTest() {
5458233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete impl_;
5459233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5460233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5461233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Pushes a trace defined by SCOPED_TRACE() on to the per-thread
5462233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test trace stack.
5463233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTest::PushGTestTrace(const internal::TraceInfo& trace)
5464233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOCK_EXCLUDED_(mutex_) {
5465233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::MutexLock lock(&mutex_);
5466233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl_->gtest_trace_stack().push_back(trace);
5467233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5468233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5469233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Pops a trace from the per-thread Google Test trace stack.
5470233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTest::PopGTestTrace()
5471233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOCK_EXCLUDED_(mutex_) {
5472233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::MutexLock lock(&mutex_);
5473233d2500723e5594f3e7c70896ffeeef32b9c950ywan  impl_->gtest_trace_stack().pop_back();
5474233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5475233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5476233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
5477233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5478233d2500723e5594f3e7c70896ffeeef32b9c950ywanUnitTestImpl::UnitTestImpl(UnitTest* parent)
5479233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : parent_(parent),
5480233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifdef _MSC_VER
5481233d2500723e5594f3e7c70896ffeeef32b9c950ywan# pragma warning(push)                    // Saves the current warning state.
5482233d2500723e5594f3e7c70896ffeeef32b9c950ywan# pragma warning(disable:4355)            // Temporarily disables warning 4355
5483233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                         // (using this in initializer).
5484233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default_global_test_part_result_reporter_(this),
5485233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default_per_thread_test_part_result_reporter_(this),
5486233d2500723e5594f3e7c70896ffeeef32b9c950ywan# pragma warning(pop)                     // Restores the warning state again.
5487233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
5488233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default_global_test_part_result_reporter_(this),
5489233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default_per_thread_test_part_result_reporter_(this),
5490233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // _MSC_VER
5491233d2500723e5594f3e7c70896ffeeef32b9c950ywan      global_test_part_result_repoter_(
5492233d2500723e5594f3e7c70896ffeeef32b9c950ywan          &default_global_test_part_result_reporter_),
5493233d2500723e5594f3e7c70896ffeeef32b9c950ywan      per_thread_test_part_result_reporter_(
5494233d2500723e5594f3e7c70896ffeeef32b9c950ywan          &default_per_thread_test_part_result_reporter_),
5495233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_PARAM_TEST
5496233d2500723e5594f3e7c70896ffeeef32b9c950ywan      parameterized_test_registry_(),
5497233d2500723e5594f3e7c70896ffeeef32b9c950ywan      parameterized_tests_registered_(false),
5498233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_PARAM_TEST
5499233d2500723e5594f3e7c70896ffeeef32b9c950ywan      last_death_test_case_(-1),
5500233d2500723e5594f3e7c70896ffeeef32b9c950ywan      current_test_case_(NULL),
5501233d2500723e5594f3e7c70896ffeeef32b9c950ywan      current_test_info_(NULL),
5502233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ad_hoc_test_result_(),
5503233d2500723e5594f3e7c70896ffeeef32b9c950ywan      os_stack_trace_getter_(NULL),
5504233d2500723e5594f3e7c70896ffeeef32b9c950ywan      post_flag_parse_init_performed_(false),
5505233d2500723e5594f3e7c70896ffeeef32b9c950ywan      random_seed_(0),  // Will be overridden by the flag before first use.
5506233d2500723e5594f3e7c70896ffeeef32b9c950ywan      random_(0),  // Will be reseeded before first use.
5507233d2500723e5594f3e7c70896ffeeef32b9c950ywan      start_timestamp_(0),
5508233d2500723e5594f3e7c70896ffeeef32b9c950ywan      elapsed_time_(0),
5509233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
5510233d2500723e5594f3e7c70896ffeeef32b9c950ywan      death_test_factory_(new DefaultDeathTestFactory),
5511233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
5512233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Will be overridden by the flag before first use.
5513233d2500723e5594f3e7c70896ffeeef32b9c950ywan      catch_exceptions_(false) {
5514233d2500723e5594f3e7c70896ffeeef32b9c950ywan  listeners()->SetDefaultResultPrinter(new PrettyUnitTestResultPrinter);
5515233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5516233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5517233d2500723e5594f3e7c70896ffeeef32b9c950ywanUnitTestImpl::~UnitTestImpl() {
5518233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Deletes every TestCase.
5519233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ForEach(test_cases_, internal::Delete<TestCase>);
5520233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5521233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Deletes every Environment.
5522233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ForEach(environments_, internal::Delete<Environment>);
5523233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5524233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete os_stack_trace_getter_;
5525233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5526233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5527233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Adds a TestProperty to the current TestResult object when invoked in a
5528233d2500723e5594f3e7c70896ffeeef32b9c950ywan// context of a test, to current test case's ad_hoc_test_result when invoke
5529233d2500723e5594f3e7c70896ffeeef32b9c950ywan// from SetUpTestCase/TearDownTestCase, or to the global property set
5530233d2500723e5594f3e7c70896ffeeef32b9c950ywan// otherwise.  If the result already contains a property with the same key,
5531233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the value will be updated.
5532233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::RecordProperty(const TestProperty& test_property) {
5533233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string xml_element;
5534233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestResult* test_result;  // TestResult appropriate for property recording.
5535233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5536233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (current_test_info_ != NULL) {
5537233d2500723e5594f3e7c70896ffeeef32b9c950ywan    xml_element = "testcase";
5538233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_result = &(current_test_info_->result_);
5539233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (current_test_case_ != NULL) {
5540233d2500723e5594f3e7c70896ffeeef32b9c950ywan    xml_element = "testsuite";
5541233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_result = &(current_test_case_->ad_hoc_test_result_);
5542233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
5543233d2500723e5594f3e7c70896ffeeef32b9c950ywan    xml_element = "testsuites";
5544233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_result = &ad_hoc_test_result_;
5545233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5546233d2500723e5594f3e7c70896ffeeef32b9c950ywan  test_result->RecordProperty(xml_element, test_property);
5547233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5548233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5549233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
5550233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Disables event forwarding if the control is currently in a death test
5551233d2500723e5594f3e7c70896ffeeef32b9c950ywan// subprocess. Must not be called before InitGoogleTest.
5552233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::SuppressTestEventsIfInSubprocess() {
5553233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (internal_run_death_test_flag_.get() != NULL)
5554233d2500723e5594f3e7c70896ffeeef32b9c950ywan    listeners()->SuppressEventForwarding();
5555233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5556233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
5557233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5558233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Initializes event listeners performing XML output as specified by
5559233d2500723e5594f3e7c70896ffeeef32b9c950ywan// UnitTestOptions. Must not be called before InitGoogleTest.
5560233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::ConfigureXmlOutput() {
5561233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string& output_format = UnitTestOptions::GetOutputFormat();
5562233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (output_format == "xml") {
5563233d2500723e5594f3e7c70896ffeeef32b9c950ywan    listeners()->SetDefaultXmlGenerator(new XmlUnitTestResultPrinter(
5564233d2500723e5594f3e7c70896ffeeef32b9c950ywan        UnitTestOptions::GetAbsolutePathToOutputFile().c_str()));
5565233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (output_format != "") {
5566233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("WARNING: unrecognized output format \"%s\" ignored.\n",
5567233d2500723e5594f3e7c70896ffeeef32b9c950ywan           output_format.c_str());
5568233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stdout);
5569233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5570233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5571233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5572233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_CAN_STREAM_RESULTS_
5573233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Initializes event listeners for streaming test results in string form.
5574233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Must not be called before InitGoogleTest.
5575233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::ConfigureStreamingOutput() {
5576233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string& target = GTEST_FLAG(stream_result_to);
5577233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!target.empty()) {
5578233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const size_t pos = target.find(':');
5579233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (pos != std::string::npos) {
5580233d2500723e5594f3e7c70896ffeeef32b9c950ywan      listeners()->Append(new StreamingListener(target.substr(0, pos),
5581233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                target.substr(pos+1)));
5582233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
5583233d2500723e5594f3e7c70896ffeeef32b9c950ywan      printf("WARNING: unrecognized streaming target \"%s\" ignored.\n",
5584233d2500723e5594f3e7c70896ffeeef32b9c950ywan             target.c_str());
5585233d2500723e5594f3e7c70896ffeeef32b9c950ywan      fflush(stdout);
5586233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5587233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5588233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5589233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_CAN_STREAM_RESULTS_
5590233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5591233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Performs initialization dependent upon flag values obtained in
5592233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to
5593233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest
5594233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this function is also called from RunAllTests.  Since this function can be
5595233d2500723e5594f3e7c70896ffeeef32b9c950ywan// called more than once, it has to be idempotent.
5596233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::PostFlagParsingInit() {
5597233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Ensures that this function does not execute more than once.
5598233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!post_flag_parse_init_performed_) {
5599233d2500723e5594f3e7c70896ffeeef32b9c950ywan    post_flag_parse_init_performed_ = true;
5600233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5601233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
5602233d2500723e5594f3e7c70896ffeeef32b9c950ywan    InitDeathTestSubprocessControlInfo();
5603233d2500723e5594f3e7c70896ffeeef32b9c950ywan    SuppressTestEventsIfInSubprocess();
5604233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
5605233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5606233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Registers parameterized tests. This makes parameterized tests
5607233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // available to the UnitTest reflection API without running
5608233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // RUN_ALL_TESTS.
5609233d2500723e5594f3e7c70896ffeeef32b9c950ywan    RegisterParameterizedTests();
5610233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5611233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Configures listeners for XML output. This makes it possible for users
5612233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // to shut down the default XML output before invoking RUN_ALL_TESTS.
5613233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ConfigureXmlOutput();
5614233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5615233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_CAN_STREAM_RESULTS_
5616233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Configures listeners for streaming test results to the specified server.
5617233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ConfigureStreamingOutput();
5618233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_CAN_STREAM_RESULTS_
5619233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5620233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5621233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5622233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A predicate that checks the name of a TestCase against a known
5623233d2500723e5594f3e7c70896ffeeef32b9c950ywan// value.
5624233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5625233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This is used for implementation of the UnitTest class only.  We put
5626233d2500723e5594f3e7c70896ffeeef32b9c950ywan// it in the anonymous namespace to prevent polluting the outer
5627233d2500723e5594f3e7c70896ffeeef32b9c950ywan// namespace.
5628233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5629233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TestCaseNameIs is copyable.
5630233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass TestCaseNameIs {
5631233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
5632233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Constructor.
5633233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit TestCaseNameIs(const std::string& name)
5634233d2500723e5594f3e7c70896ffeeef32b9c950ywan      : name_(name) {}
5635233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5636233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns true iff the name of test_case matches name_.
5637233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool operator()(const TestCase* test_case) const {
5638233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return test_case != NULL && strcmp(test_case->name(), name_.c_str()) == 0;
5639233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5640233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5641233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
5642233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string name_;
5643233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
5644233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5645233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Finds and returns a TestCase with the given name.  If one doesn't
5646233d2500723e5594f3e7c70896ffeeef32b9c950ywan// exist, creates one and returns it.  It's the CALLER'S
5647233d2500723e5594f3e7c70896ffeeef32b9c950ywan// RESPONSIBILITY to ensure that this function is only called WHEN THE
5648233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TESTS ARE NOT SHUFFLED.
5649233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5650233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Arguments:
5651233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5652233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   test_case_name: name of the test case
5653233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   type_param:     the name of the test case's type parameter, or NULL if
5654233d2500723e5594f3e7c70896ffeeef32b9c950ywan//                   this is not a typed or a type-parameterized test case.
5655233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   set_up_tc:      pointer to the function that sets up the test case
5656233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   tear_down_tc:   pointer to the function that tears down the test case
5657233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestCase* UnitTestImpl::GetTestCase(const char* test_case_name,
5658233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    const char* type_param,
5659233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    Test::SetUpTestCaseFunc set_up_tc,
5660233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    Test::TearDownTestCaseFunc tear_down_tc) {
5661233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Can we find a TestCase with the given name?
5662233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::vector<TestCase*>::const_iterator test_case =
5663233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::find_if(test_cases_.begin(), test_cases_.end(),
5664233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   TestCaseNameIs(test_case_name));
5665233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5666233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (test_case != test_cases_.end())
5667233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return *test_case;
5668233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5669233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // No.  Let's create one.
5670233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestCase* const new_test_case =
5671233d2500723e5594f3e7c70896ffeeef32b9c950ywan      new TestCase(test_case_name, type_param, set_up_tc, tear_down_tc);
5672233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5673233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Is this a death test case?
5674233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (internal::UnitTestOptions::MatchesFilter(test_case_name,
5675233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                               kDeathTestCaseFilter)) {
5676233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Yes.  Inserts the test case after the last death test case
5677233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // defined so far.  This only works when the test cases haven't
5678233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // been shuffled.  Otherwise we may end up running a death test
5679233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // after a non-death test.
5680233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ++last_death_test_case_;
5681233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_cases_.insert(test_cases_.begin() + last_death_test_case_,
5682233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       new_test_case);
5683233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
5684233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // No.  Appends to the end of the list.
5685233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_cases_.push_back(new_test_case);
5686233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5687233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5688233d2500723e5594f3e7c70896ffeeef32b9c950ywan  test_case_indices_.push_back(static_cast<int>(test_case_indices_.size()));
5689233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return new_test_case;
5690233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5691233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5692233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helpers for setting up / tearing down the given environment.  They
5693233d2500723e5594f3e7c70896ffeeef32b9c950ywan// are for use in the ForEach() function.
5694233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void SetUpEnvironment(Environment* env) { env->SetUp(); }
5695233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void TearDownEnvironment(Environment* env) { env->TearDown(); }
5696233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5697233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Runs all tests in this UnitTest object, prints the result, and
5698233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returns true if all tests are successful.  If any exception is
5699233d2500723e5594f3e7c70896ffeeef32b9c950ywan// thrown during a test, the test is considered to be failed, but the
5700233d2500723e5594f3e7c70896ffeeef32b9c950ywan// rest of the tests will still be run.
5701233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
5702233d2500723e5594f3e7c70896ffeeef32b9c950ywan// When parameterized tests are enabled, it expands and registers
5703233d2500723e5594f3e7c70896ffeeef32b9c950ywan// parameterized tests first in RegisterParameterizedTests().
5704233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All other functions called from RunAllTests() may safely assume that
5705233d2500723e5594f3e7c70896ffeeef32b9c950ywan// parameterized tests are ready to be counted and run.
5706233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool UnitTestImpl::RunAllTests() {
5707233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Makes sure InitGoogleTest() was called.
5708233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!GTestIsInitialized()) {
5709233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("%s",
5710233d2500723e5594f3e7c70896ffeeef32b9c950ywan           "\nThis test program did NOT call ::testing::InitGoogleTest "
5711233d2500723e5594f3e7c70896ffeeef32b9c950ywan           "before calling RUN_ALL_TESTS().  Please fix it.\n");
5712233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
5713233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5714233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5715233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Do not run any test if the --help flag was specified.
5716233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (g_help_flag)
5717233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return true;
5718233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5719233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Repeats the call to the post-flag parsing initialization in case the
5720233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // user didn't call InitGoogleTest.
5721233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PostFlagParsingInit();
5722233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5723233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Even if sharding is not on, test runners may want to use the
5724233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding
5725233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // protocol.
5726233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::WriteToShardStatusFileIfNeeded();
5727233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5728233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // True iff we are in a subprocess for running a thread-safe-style
5729233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // death test.
5730233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool in_subprocess_for_death_test = false;
5731233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5732233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
5733233d2500723e5594f3e7c70896ffeeef32b9c950ywan  in_subprocess_for_death_test = (internal_run_death_test_flag_.get() != NULL);
5734233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
5735233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5736233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex,
5737233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                        in_subprocess_for_death_test);
5738233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5739233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Compares the full test names with the filter to decide which
5740233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // tests to run.
5741233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool has_tests_to_run = FilterTests(should_shard
5742233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                              ? HONOR_SHARDING_PROTOCOL
5743233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                              : IGNORE_SHARDING_PROTOCOL) > 0;
5744233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5745233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Lists the tests and exits if the --gtest_list_tests flag was specified.
5746233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(list_tests)) {
5747233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // This must be called *after* FilterTests() has been called.
5748233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ListTestsMatchingFilter();
5749233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return true;
5750233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5751233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5752233d2500723e5594f3e7c70896ffeeef32b9c950ywan  random_seed_ = GTEST_FLAG(shuffle) ?
5753233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0;
5754233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5755233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // True iff at least one test has failed.
5756233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool failed = false;
5757233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5758233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestEventListener* repeater = listeners()->repeater();
5759233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5760233d2500723e5594f3e7c70896ffeeef32b9c950ywan  start_timestamp_ = GetTimeInMillis();
5761233d2500723e5594f3e7c70896ffeeef32b9c950ywan  repeater->OnTestProgramStart(*parent_);
5762233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5763233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // How many times to repeat the tests?  We don't want to repeat them
5764233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // when we are inside the subprocess of a death test.
5765233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat);
5766233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Repeats forever if the repeat count is negative.
5767233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool forever = repeat < 0;
5768233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; forever || i != repeat; i++) {
5769233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // We want to preserve failures generated by ad-hoc test
5770233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // assertions executed before RUN_ALL_TESTS().
5771233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ClearNonAdHocTestResult();
5772233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5773233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TimeInMillis start = GetTimeInMillis();
5774233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5775233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Shuffles test cases and tests if requested.
5776233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (has_tests_to_run && GTEST_FLAG(shuffle)) {
5777233d2500723e5594f3e7c70896ffeeef32b9c950ywan      random()->Reseed(random_seed_);
5778233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // This should be done before calling OnTestIterationStart(),
5779233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // such that a test event listener can see the actual test order
5780233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // in the event.
5781233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ShuffleTests();
5782233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5783233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5784233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Tells the unit test event listeners that the tests are about to start.
5785233d2500723e5594f3e7c70896ffeeef32b9c950ywan    repeater->OnTestIterationStart(*parent_, i);
5786233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5787233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Runs each test case if there is at least one test to run.
5788233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (has_tests_to_run) {
5789233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Sets up all environments beforehand.
5790233d2500723e5594f3e7c70896ffeeef32b9c950ywan      repeater->OnEnvironmentsSetUpStart(*parent_);
5791233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ForEach(environments_, SetUpEnvironment);
5792233d2500723e5594f3e7c70896ffeeef32b9c950ywan      repeater->OnEnvironmentsSetUpEnd(*parent_);
5793233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5794233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Runs the tests only if there was no fatal failure during global
5795233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // set-up.
5796233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (!Test::HasFatalFailure()) {
5797233d2500723e5594f3e7c70896ffeeef32b9c950ywan        for (int test_index = 0; test_index < total_test_case_count();
5798233d2500723e5594f3e7c70896ffeeef32b9c950ywan             test_index++) {
5799233d2500723e5594f3e7c70896ffeeef32b9c950ywan          GetMutableTestCase(test_index)->Run();
5800233d2500723e5594f3e7c70896ffeeef32b9c950ywan        }
5801233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
5802233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5803233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Tears down all environments in reverse order afterwards.
5804233d2500723e5594f3e7c70896ffeeef32b9c950ywan      repeater->OnEnvironmentsTearDownStart(*parent_);
5805233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::for_each(environments_.rbegin(), environments_.rend(),
5806233d2500723e5594f3e7c70896ffeeef32b9c950ywan                    TearDownEnvironment);
5807233d2500723e5594f3e7c70896ffeeef32b9c950ywan      repeater->OnEnvironmentsTearDownEnd(*parent_);
5808233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5809233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5810233d2500723e5594f3e7c70896ffeeef32b9c950ywan    elapsed_time_ = GetTimeInMillis() - start;
5811233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5812233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Tells the unit test event listener that the tests have just finished.
5813233d2500723e5594f3e7c70896ffeeef32b9c950ywan    repeater->OnTestIterationEnd(*parent_, i);
5814233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5815233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Gets the result and clears it.
5816233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (!Passed()) {
5817233d2500723e5594f3e7c70896ffeeef32b9c950ywan      failed = true;
5818233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5819233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5820233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Restores the original test order after the iteration.  This
5821233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // allows the user to quickly repro a failure that happens in the
5822233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // N-th iteration without repeating the first (N - 1) iterations.
5823233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // This is not enclosed in "if (GTEST_FLAG(shuffle)) { ... }", in
5824233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // case the user somehow changes the value of the flag somewhere
5825233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // (it's always safe to unshuffle the tests).
5826233d2500723e5594f3e7c70896ffeeef32b9c950ywan    UnshuffleTests();
5827233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5828233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (GTEST_FLAG(shuffle)) {
5829233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Picks a new random seed for each iteration.
5830233d2500723e5594f3e7c70896ffeeef32b9c950ywan      random_seed_ = GetNextRandomSeed(random_seed_);
5831233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5832233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5833233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5834233d2500723e5594f3e7c70896ffeeef32b9c950ywan  repeater->OnTestProgramEnd(*parent_);
5835233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5836233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return !failed;
5837233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5838233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5839233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
5840233d2500723e5594f3e7c70896ffeeef32b9c950ywan// if the variable is present. If a file already exists at this location, this
5841233d2500723e5594f3e7c70896ffeeef32b9c950ywan// function will write over it. If the variable is present, but the file cannot
5842233d2500723e5594f3e7c70896ffeeef32b9c950ywan// be created, prints an error and exits.
5843233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid WriteToShardStatusFileIfNeeded() {
5844233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const test_shard_file = posix::GetEnv(kTestShardStatusFile);
5845233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (test_shard_file != NULL) {
5846233d2500723e5594f3e7c70896ffeeef32b9c950ywan    FILE* const file = posix::FOpen(test_shard_file, "w");
5847233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (file == NULL) {
5848233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ColoredPrintf(COLOR_RED,
5849233d2500723e5594f3e7c70896ffeeef32b9c950ywan                    "Could not write to the test shard status file \"%s\" "
5850233d2500723e5594f3e7c70896ffeeef32b9c950ywan                    "specified by the %s environment variable.\n",
5851233d2500723e5594f3e7c70896ffeeef32b9c950ywan                    test_shard_file, kTestShardStatusFile);
5852233d2500723e5594f3e7c70896ffeeef32b9c950ywan      fflush(stdout);
5853233d2500723e5594f3e7c70896ffeeef32b9c950ywan      exit(EXIT_FAILURE);
5854233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5855233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fclose(file);
5856233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5857233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5858233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5859233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Checks whether sharding is enabled by examining the relevant
5860233d2500723e5594f3e7c70896ffeeef32b9c950ywan// environment variable values. If the variables are present,
5861233d2500723e5594f3e7c70896ffeeef32b9c950ywan// but inconsistent (i.e., shard_index >= total_shards), prints
5862233d2500723e5594f3e7c70896ffeeef32b9c950ywan// an error and exits. If in_subprocess_for_death_test, sharding is
5863233d2500723e5594f3e7c70896ffeeef32b9c950ywan// disabled because it must only be applied to the original test
5864233d2500723e5594f3e7c70896ffeeef32b9c950ywan// process. Otherwise, we could filter out death tests we intended to execute.
5865233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ShouldShard(const char* total_shards_env,
5866233d2500723e5594f3e7c70896ffeeef32b9c950ywan                 const char* shard_index_env,
5867233d2500723e5594f3e7c70896ffeeef32b9c950ywan                 bool in_subprocess_for_death_test) {
5868233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (in_subprocess_for_death_test) {
5869233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
5870233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5871233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5872233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const Int32 total_shards = Int32FromEnvOrDie(total_shards_env, -1);
5873233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const Int32 shard_index = Int32FromEnvOrDie(shard_index_env, -1);
5874233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5875233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (total_shards == -1 && shard_index == -1) {
5876233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
5877233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (total_shards == -1 && shard_index != -1) {
5878233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const Message msg = Message()
5879233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Invalid environment variables: you have "
5880233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << kTestShardIndex << " = " << shard_index
5881233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << ", but have left " << kTestTotalShards << " unset.\n";
5882233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_RED, msg.GetString().c_str());
5883233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stdout);
5884233d2500723e5594f3e7c70896ffeeef32b9c950ywan    exit(EXIT_FAILURE);
5885233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (total_shards != -1 && shard_index == -1) {
5886233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const Message msg = Message()
5887233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Invalid environment variables: you have "
5888233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << kTestTotalShards << " = " << total_shards
5889233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << ", but have left " << kTestShardIndex << " unset.\n";
5890233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_RED, msg.GetString().c_str());
5891233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stdout);
5892233d2500723e5594f3e7c70896ffeeef32b9c950ywan    exit(EXIT_FAILURE);
5893233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (shard_index < 0 || shard_index >= total_shards) {
5894233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const Message msg = Message()
5895233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Invalid environment variables: we require 0 <= "
5896233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << kTestShardIndex << " < " << kTestTotalShards
5897233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << ", but you have " << kTestShardIndex << "=" << shard_index
5898233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << ", " << kTestTotalShards << "=" << total_shards << ".\n";
5899233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(COLOR_RED, msg.GetString().c_str());
5900233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stdout);
5901233d2500723e5594f3e7c70896ffeeef32b9c950ywan    exit(EXIT_FAILURE);
5902233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5903233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5904233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return total_shards > 1;
5905233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5906233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5907233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses the environment variable var as an Int32. If it is unset,
5908233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returns default_val. If it is not an Int32, prints an error
5909233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and aborts.
5910233d2500723e5594f3e7c70896ffeeef32b9c950ywanInt32 Int32FromEnvOrDie(const char* var, Int32 default_val) {
5911233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* str_val = posix::GetEnv(var);
5912233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (str_val == NULL) {
5913233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return default_val;
5914233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5915233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5916233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Int32 result;
5917233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!ParseInt32(Message() << "The value of environment variable " << var,
5918233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  str_val, &result)) {
5919233d2500723e5594f3e7c70896ffeeef32b9c950ywan    exit(EXIT_FAILURE);
5920233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5921233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result;
5922233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5923233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5924233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Given the total number of shards, the shard index, and the test id,
5925233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returns true iff the test should be run on this shard. The test id is
5926233d2500723e5594f3e7c70896ffeeef32b9c950ywan// some arbitrary but unique non-negative integer assigned to each test
5927233d2500723e5594f3e7c70896ffeeef32b9c950ywan// method. Assumes that 0 <= shard_index < total_shards.
5928233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) {
5929233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (test_id % total_shards) == shard_index;
5930233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5931233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5932233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Compares the name of each test with the user-specified filter to
5933233d2500723e5594f3e7c70896ffeeef32b9c950ywan// decide whether the test should be run, then records the result in
5934233d2500723e5594f3e7c70896ffeeef32b9c950ywan// each TestCase and TestInfo object.
5935233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If shard_tests == true, further filters tests based on sharding
5936233d2500723e5594f3e7c70896ffeeef32b9c950ywan// variables in the environment - see
5937233d2500723e5594f3e7c70896ffeeef32b9c950ywan// http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide.
5938233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the number of tests that should run.
5939233d2500723e5594f3e7c70896ffeeef32b9c950ywanint UnitTestImpl::FilterTests(ReactionToSharding shard_tests) {
5940233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const Int32 total_shards = shard_tests == HONOR_SHARDING_PROTOCOL ?
5941233d2500723e5594f3e7c70896ffeeef32b9c950ywan      Int32FromEnvOrDie(kTestTotalShards, -1) : -1;
5942233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const Int32 shard_index = shard_tests == HONOR_SHARDING_PROTOCOL ?
5943233d2500723e5594f3e7c70896ffeeef32b9c950ywan      Int32FromEnvOrDie(kTestShardIndex, -1) : -1;
5944233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5945233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // num_runnable_tests are the number of tests that will
5946233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // run across all shards (i.e., match filter and are not disabled).
5947233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // num_selected_tests are the number of tests to be run on
5948233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // this shard.
5949233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int num_runnable_tests = 0;
5950233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int num_selected_tests = 0;
5951233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < test_cases_.size(); i++) {
5952233d2500723e5594f3e7c70896ffeeef32b9c950ywan    TestCase* const test_case = test_cases_[i];
5953233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string &test_case_name = test_case->name();
5954233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_case->set_should_run(false);
5955233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5956233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (size_t j = 0; j < test_case->test_info_list().size(); j++) {
5957233d2500723e5594f3e7c70896ffeeef32b9c950ywan      TestInfo* const test_info = test_case->test_info_list()[j];
5958233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const std::string test_name(test_info->name());
5959233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // A test is disabled if test case name or test name matches
5960233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // kDisableTestFilter.
5961233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const bool is_disabled =
5962233d2500723e5594f3e7c70896ffeeef32b9c950ywan          internal::UnitTestOptions::MatchesFilter(test_case_name,
5963233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                   kDisableTestFilter) ||
5964233d2500723e5594f3e7c70896ffeeef32b9c950ywan          internal::UnitTestOptions::MatchesFilter(test_name,
5965233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                   kDisableTestFilter);
5966233d2500723e5594f3e7c70896ffeeef32b9c950ywan      test_info->is_disabled_ = is_disabled;
5967233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5968233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const bool matches_filter =
5969233d2500723e5594f3e7c70896ffeeef32b9c950ywan          internal::UnitTestOptions::FilterMatchesTest(test_case_name,
5970233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                       test_name);
5971233d2500723e5594f3e7c70896ffeeef32b9c950ywan      test_info->matches_filter_ = matches_filter;
5972233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5973233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const bool is_runnable =
5974233d2500723e5594f3e7c70896ffeeef32b9c950ywan          (GTEST_FLAG(also_run_disabled_tests) || !is_disabled) &&
5975233d2500723e5594f3e7c70896ffeeef32b9c950ywan          matches_filter;
5976233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5977233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const bool is_selected = is_runnable &&
5978233d2500723e5594f3e7c70896ffeeef32b9c950ywan          (shard_tests == IGNORE_SHARDING_PROTOCOL ||
5979233d2500723e5594f3e7c70896ffeeef32b9c950ywan           ShouldRunTestOnShard(total_shards, shard_index,
5980233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                num_runnable_tests));
5981233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5982233d2500723e5594f3e7c70896ffeeef32b9c950ywan      num_runnable_tests += is_runnable;
5983233d2500723e5594f3e7c70896ffeeef32b9c950ywan      num_selected_tests += is_selected;
5984233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5985233d2500723e5594f3e7c70896ffeeef32b9c950ywan      test_info->should_run_ = is_selected;
5986233d2500723e5594f3e7c70896ffeeef32b9c950ywan      test_case->set_should_run(test_case->should_run() || is_selected);
5987233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
5988233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
5989233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return num_selected_tests;
5990233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
5991233d2500723e5594f3e7c70896ffeeef32b9c950ywan
5992233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints the given C-string on a single line by replacing all '\n'
5993233d2500723e5594f3e7c70896ffeeef32b9c950ywan// characters with string "\\n".  If the output takes more than
5994233d2500723e5594f3e7c70896ffeeef32b9c950ywan// max_length characters, only prints the first max_length characters
5995233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and "...".
5996233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void PrintOnOneLine(const char* str, int max_length) {
5997233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (str != NULL) {
5998233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (int i = 0; *str != '\0'; ++str) {
5999233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (i >= max_length) {
6000233d2500723e5594f3e7c70896ffeeef32b9c950ywan        printf("...");
6001233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
6002233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
6003233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (*str == '\n') {
6004233d2500723e5594f3e7c70896ffeeef32b9c950ywan        printf("\\n");
6005233d2500723e5594f3e7c70896ffeeef32b9c950ywan        i += 2;
6006233d2500723e5594f3e7c70896ffeeef32b9c950ywan      } else {
6007233d2500723e5594f3e7c70896ffeeef32b9c950ywan        printf("%c", *str);
6008233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ++i;
6009233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
6010233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
6011233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6012233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6013233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6014233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints the names of the tests matching the user-specified filter flag.
6015233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::ListTestsMatchingFilter() {
6016233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Print at most this many characters for each type/value parameter.
6017233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int kMaxParamLength = 250;
6018233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6019233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < test_cases_.size(); i++) {
6020233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestCase* const test_case = test_cases_[i];
6021233d2500723e5594f3e7c70896ffeeef32b9c950ywan    bool printed_test_case_name = false;
6022233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6023233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (size_t j = 0; j < test_case->test_info_list().size(); j++) {
6024233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const TestInfo* const test_info =
6025233d2500723e5594f3e7c70896ffeeef32b9c950ywan          test_case->test_info_list()[j];
6026233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (test_info->matches_filter_) {
6027233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if (!printed_test_case_name) {
6028233d2500723e5594f3e7c70896ffeeef32b9c950ywan          printed_test_case_name = true;
6029233d2500723e5594f3e7c70896ffeeef32b9c950ywan          printf("%s.", test_case->name());
6030233d2500723e5594f3e7c70896ffeeef32b9c950ywan          if (test_case->type_param() != NULL) {
6031233d2500723e5594f3e7c70896ffeeef32b9c950ywan            printf("  # %s = ", kTypeParamLabel);
6032233d2500723e5594f3e7c70896ffeeef32b9c950ywan            // We print the type parameter on a single line to make
6033233d2500723e5594f3e7c70896ffeeef32b9c950ywan            // the output easy to parse by a program.
6034233d2500723e5594f3e7c70896ffeeef32b9c950ywan            PrintOnOneLine(test_case->type_param(), kMaxParamLength);
6035233d2500723e5594f3e7c70896ffeeef32b9c950ywan          }
6036233d2500723e5594f3e7c70896ffeeef32b9c950ywan          printf("\n");
6037233d2500723e5594f3e7c70896ffeeef32b9c950ywan        }
6038233d2500723e5594f3e7c70896ffeeef32b9c950ywan        printf("  %s", test_info->name());
6039233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if (test_info->value_param() != NULL) {
6040233d2500723e5594f3e7c70896ffeeef32b9c950ywan          printf("  # %s = ", kValueParamLabel);
6041233d2500723e5594f3e7c70896ffeeef32b9c950ywan          // We print the value parameter on a single line to make the
6042233d2500723e5594f3e7c70896ffeeef32b9c950ywan          // output easy to parse by a program.
6043233d2500723e5594f3e7c70896ffeeef32b9c950ywan          PrintOnOneLine(test_info->value_param(), kMaxParamLength);
6044233d2500723e5594f3e7c70896ffeeef32b9c950ywan        }
6045233d2500723e5594f3e7c70896ffeeef32b9c950ywan        printf("\n");
6046233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
6047233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
6048233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6049233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fflush(stdout);
6050233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6051233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6052233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Sets the OS stack trace getter.
6053233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6054233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Does nothing if the input and the current OS stack trace getter are
6055233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the same; otherwise, deletes the old getter and makes the input the
6056233d2500723e5594f3e7c70896ffeeef32b9c950ywan// current getter.
6057233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::set_os_stack_trace_getter(
6058233d2500723e5594f3e7c70896ffeeef32b9c950ywan    OsStackTraceGetterInterface* getter) {
6059233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (os_stack_trace_getter_ != getter) {
6060233d2500723e5594f3e7c70896ffeeef32b9c950ywan    delete os_stack_trace_getter_;
6061233d2500723e5594f3e7c70896ffeeef32b9c950ywan    os_stack_trace_getter_ = getter;
6062233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6063233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6064233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6065233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current OS stack trace getter if it is not NULL;
6066233d2500723e5594f3e7c70896ffeeef32b9c950ywan// otherwise, creates an OsStackTraceGetter, makes it the current
6067233d2500723e5594f3e7c70896ffeeef32b9c950ywan// getter, and returns it.
6068233d2500723e5594f3e7c70896ffeeef32b9c950ywanOsStackTraceGetterInterface* UnitTestImpl::os_stack_trace_getter() {
6069233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (os_stack_trace_getter_ == NULL) {
6070233d2500723e5594f3e7c70896ffeeef32b9c950ywan    os_stack_trace_getter_ = new OsStackTraceGetter;
6071233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6072233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6073233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return os_stack_trace_getter_;
6074233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6075233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6076233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the TestResult for the test that's currently running, or
6077233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the TestResult for the ad hoc test if no test is running.
6078233d2500723e5594f3e7c70896ffeeef32b9c950ywanTestResult* UnitTestImpl::current_test_result() {
6079233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return current_test_info_ ?
6080233d2500723e5594f3e7c70896ffeeef32b9c950ywan      &(current_test_info_->result_) : &ad_hoc_test_result_;
6081233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6082233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6083233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Shuffles all test cases, and the tests within each test case,
6084233d2500723e5594f3e7c70896ffeeef32b9c950ywan// making sure that death tests are still run first.
6085233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::ShuffleTests() {
6086233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Shuffles the death test cases.
6087233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ShuffleRange(random(), 0, last_death_test_case_ + 1, &test_case_indices_);
6088233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6089233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Shuffles the non-death test cases.
6090233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ShuffleRange(random(), last_death_test_case_ + 1,
6091233d2500723e5594f3e7c70896ffeeef32b9c950ywan               static_cast<int>(test_cases_.size()), &test_case_indices_);
6092233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6093233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Shuffles the tests inside each test case.
6094233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < test_cases_.size(); i++) {
6095233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_cases_[i]->ShuffleTests(random());
6096233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6097233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6098233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6099233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Restores the test cases and tests to their order before the first shuffle.
6100233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UnitTestImpl::UnshuffleTests() {
6101233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i < test_cases_.size(); i++) {
6102233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Unshuffles the tests in each test case.
6103233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_cases_[i]->UnshuffleTests();
6104233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Resets the index of each test case.
6105233d2500723e5594f3e7c70896ffeeef32b9c950ywan    test_case_indices_[i] = static_cast<int>(i);
6106233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6107233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6108233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6109233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current OS stack trace as an std::string.
6110233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6111233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The maximum number of stack frames to be included is specified by
6112233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the gtest_stack_trace_depth flag.  The skip_count parameter
6113233d2500723e5594f3e7c70896ffeeef32b9c950ywan// specifies the number of top frames to be skipped, which doesn't
6114233d2500723e5594f3e7c70896ffeeef32b9c950ywan// count against the number of frames to be included.
6115233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6116233d2500723e5594f3e7c70896ffeeef32b9c950ywan// For example, if Foo() calls Bar(), which in turn calls
6117233d2500723e5594f3e7c70896ffeeef32b9c950ywan// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
6118233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
6119233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/,
6120233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                            int skip_count) {
6121233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We pass skip_count + 1 to skip this wrapper function in addition
6122233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // to what the user really wants to skip.
6123233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1);
6124233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6125233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6126233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to
6127233d2500723e5594f3e7c70896ffeeef32b9c950ywan// suppress unreachable code warnings.
6128233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace {
6129233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass ClassUniqueToAlwaysTrue {};
6130233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6131233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6132233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsTrue(bool condition) { return condition; }
6133233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6134233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool AlwaysTrue() {
6135233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_EXCEPTIONS
6136233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This condition is always false so AlwaysTrue() never actually throws,
6137233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // but it makes the compiler think that it may throw.
6138233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (IsTrue(false))
6139233d2500723e5594f3e7c70896ffeeef32b9c950ywan    throw ClassUniqueToAlwaysTrue();
6140233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_EXCEPTIONS
6141233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return true;
6142233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6143233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6144233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If *pstr starts with the given prefix, modifies *pstr to be right
6145233d2500723e5594f3e7c70896ffeeef32b9c950ywan// past the prefix and returns true; otherwise leaves *pstr unchanged
6146233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and returns false.  None of pstr, *pstr, and prefix can be NULL.
6147233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool SkipPrefix(const char* prefix, const char** pstr) {
6148233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t prefix_len = strlen(prefix);
6149233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (strncmp(*pstr, prefix, prefix_len) == 0) {
6150233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *pstr += prefix_len;
6151233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return true;
6152233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6153233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return false;
6154233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6155233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6156233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses a string as a command line flag.  The string should have
6157233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the format "--flag=value".  When def_optional is true, the "=value"
6158233d2500723e5594f3e7c70896ffeeef32b9c950ywan// part can be omitted.
6159233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6160233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the value of the flag, or NULL if the parsing failed.
6161233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* ParseFlagValue(const char* str,
6162233d2500723e5594f3e7c70896ffeeef32b9c950ywan                           const char* flag,
6163233d2500723e5594f3e7c70896ffeeef32b9c950ywan                           bool def_optional) {
6164233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // str and flag must not be NULL.
6165233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (str == NULL || flag == NULL) return NULL;
6166233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6167233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The flag must start with "--" followed by GTEST_FLAG_PREFIX_.
6168233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string flag_str = std::string("--") + GTEST_FLAG_PREFIX_ + flag;
6169233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t flag_len = flag_str.length();
6170233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
6171233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6172233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Skips the flag name.
6173233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* flag_end = str + flag_len;
6174233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6175233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // When def_optional is true, it's OK to not have a "=value" part.
6176233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (def_optional && (flag_end[0] == '\0')) {
6177233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return flag_end;
6178233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6179233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6180233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // If def_optional is true and there are more characters after the
6181233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // flag name, or if def_optional is false, there must be a '=' after
6182233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the flag name.
6183233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (flag_end[0] != '=') return NULL;
6184233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6185233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the string after "=".
6186233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return flag_end + 1;
6187233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6188233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6189233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses a string for a bool flag, in the form of either
6190233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "--flag=value" or "--flag".
6191233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6192233d2500723e5594f3e7c70896ffeeef32b9c950ywan// In the former case, the value is taken as true as long as it does
6193233d2500723e5594f3e7c70896ffeeef32b9c950ywan// not start with '0', 'f', or 'F'.
6194233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6195233d2500723e5594f3e7c70896ffeeef32b9c950ywan// In the latter case, the value is taken as true.
6196233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6197233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On success, stores the value of the flag in *value, and returns
6198233d2500723e5594f3e7c70896ffeeef32b9c950ywan// true.  On failure, returns false without changing *value.
6199233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ParseBoolFlag(const char* str, const char* flag, bool* value) {
6200233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the value of the flag as a string.
6201233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const value_str = ParseFlagValue(str, flag, true);
6202233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6203233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Aborts if the parsing failed.
6204233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (value_str == NULL) return false;
6205233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6206233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Converts the string value to a bool.
6207233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
6208233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return true;
6209233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6210233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6211233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses a string for an Int32 flag, in the form of
6212233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "--flag=value".
6213233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6214233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On success, stores the value of the flag in *value, and returns
6215233d2500723e5594f3e7c70896ffeeef32b9c950ywan// true.  On failure, returns false without changing *value.
6216233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ParseInt32Flag(const char* str, const char* flag, Int32* value) {
6217233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the value of the flag as a string.
6218233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const value_str = ParseFlagValue(str, flag, false);
6219233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6220233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Aborts if the parsing failed.
6221233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (value_str == NULL) return false;
6222233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6223233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Sets *value to the value of the flag.
6224233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ParseInt32(Message() << "The value of flag --" << flag,
6225233d2500723e5594f3e7c70896ffeeef32b9c950ywan                    value_str, value);
6226233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6227233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6228233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses a string for a string flag, in the form of
6229233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "--flag=value".
6230233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6231233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On success, stores the value of the flag in *value, and returns
6232233d2500723e5594f3e7c70896ffeeef32b9c950ywan// true.  On failure, returns false without changing *value.
6233233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ParseStringFlag(const char* str, const char* flag, std::string* value) {
6234233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Gets the value of the flag as a string.
6235233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const value_str = ParseFlagValue(str, flag, false);
6236233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6237233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Aborts if the parsing failed.
6238233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (value_str == NULL) return false;
6239233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6240233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Sets *value to the value of the flag.
6241233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *value = value_str;
6242233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return true;
6243233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6244233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6245233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Determines whether a string has a prefix that Google Test uses for its
6246233d2500723e5594f3e7c70896ffeeef32b9c950ywan// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_.
6247233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If Google Test detects that a command line flag has its prefix but is not
6248233d2500723e5594f3e7c70896ffeeef32b9c950ywan// recognized, it will print its help message. Flags starting with
6249233d2500723e5594f3e7c70896ffeeef32b9c950ywan// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test
6250233d2500723e5594f3e7c70896ffeeef32b9c950ywan// internal flags and do not trigger the help message.
6251233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool HasGoogleTestFlagPrefix(const char* str) {
6252233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (SkipPrefix("--", &str) ||
6253233d2500723e5594f3e7c70896ffeeef32b9c950ywan          SkipPrefix("-", &str) ||
6254233d2500723e5594f3e7c70896ffeeef32b9c950ywan          SkipPrefix("/", &str)) &&
6255233d2500723e5594f3e7c70896ffeeef32b9c950ywan         !SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) &&
6256233d2500723e5594f3e7c70896ffeeef32b9c950ywan         (SkipPrefix(GTEST_FLAG_PREFIX_, &str) ||
6257233d2500723e5594f3e7c70896ffeeef32b9c950ywan          SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str));
6258233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6259233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6260233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a string containing code-encoded text.  The following escape
6261233d2500723e5594f3e7c70896ffeeef32b9c950ywan// sequences can be used in the string to control the text color:
6262233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6263233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   @@    prints a single '@' character.
6264233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   @R    changes the color to red.
6265233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   @G    changes the color to green.
6266233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   @Y    changes the color to yellow.
6267233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   @D    changes to the default terminal text color.
6268233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6269233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(wan@google.com): Write tests for this once we add stdout
6270233d2500723e5594f3e7c70896ffeeef32b9c950ywan// capturing to Google Test.
6271233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void PrintColorEncoded(const char* str) {
6272233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTestColor color = COLOR_DEFAULT;  // The current color.
6273233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6274233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Conceptually, we split the string into segments divided by escape
6275233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // sequences.  Then we print one segment at a time.  At the end of
6276233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // each iteration, the str pointer advances to the beginning of the
6277233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // next segment.
6278233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (;;) {
6279233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* p = strchr(str, '@');
6280233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (p == NULL) {
6281233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ColoredPrintf(color, "%s", str);
6282233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return;
6283233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
6284233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6285233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ColoredPrintf(color, "%s", std::string(str, p).c_str());
6286233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6287233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char ch = p[1];
6288233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str = p + 2;
6289233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (ch == '@') {
6290233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ColoredPrintf(color, "@");
6291233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else if (ch == 'D') {
6292233d2500723e5594f3e7c70896ffeeef32b9c950ywan      color = COLOR_DEFAULT;
6293233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else if (ch == 'R') {
6294233d2500723e5594f3e7c70896ffeeef32b9c950ywan      color = COLOR_RED;
6295233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else if (ch == 'G') {
6296233d2500723e5594f3e7c70896ffeeef32b9c950ywan      color = COLOR_GREEN;
6297233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else if (ch == 'Y') {
6298233d2500723e5594f3e7c70896ffeeef32b9c950ywan      color = COLOR_YELLOW;
6299233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
6300233d2500723e5594f3e7c70896ffeeef32b9c950ywan      --str;
6301233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
6302233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6303233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6304233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6305233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kColorEncodedHelpMessage[] =
6306233d2500723e5594f3e7c70896ffeeef32b9c950ywan"This program contains tests written using " GTEST_NAME_ ". You can use the\n"
6307233d2500723e5594f3e7c70896ffeeef32b9c950ywan"following command line flags to control its behavior:\n"
6308233d2500723e5594f3e7c70896ffeeef32b9c950ywan"\n"
6309233d2500723e5594f3e7c70896ffeeef32b9c950ywan"Test Selection:\n"
6310233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "list_tests@D\n"
6311233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      List the names of all tests instead of running them. The name of\n"
6312233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      TEST(Foo, Bar) is \"Foo.Bar\".\n"
6313233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "filter=@YPOSTIVE_PATTERNS"
6314233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "[@G-@YNEGATIVE_PATTERNS]@D\n"
6315233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Run only the tests whose name matches one of the positive patterns but\n"
6316233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      none of the negative patterns. '?' matches any single character; '*'\n"
6317233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      matches any substring; ':' separates two patterns.\n"
6318233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "also_run_disabled_tests@D\n"
6319233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Run all disabled tests too.\n"
6320233d2500723e5594f3e7c70896ffeeef32b9c950ywan"\n"
6321233d2500723e5594f3e7c70896ffeeef32b9c950ywan"Test Execution:\n"
6322233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "repeat=@Y[COUNT]@D\n"
6323233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Run the tests repeatedly; use a negative count to repeat forever.\n"
6324233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "shuffle@D\n"
6325233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Randomize tests' orders on every iteration.\n"
6326233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "random_seed=@Y[NUMBER]@D\n"
6327233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Random number seed to use for shuffling test orders (between 1 and\n"
6328233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      99999, or 0 to use a seed based on the current time).\n"
6329233d2500723e5594f3e7c70896ffeeef32b9c950ywan"\n"
6330233d2500723e5594f3e7c70896ffeeef32b9c950ywan"Test Output:\n"
6331233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n"
6332233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Enable/disable colored output. The default is @Gauto@D.\n"
6333233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  -@G-" GTEST_FLAG_PREFIX_ "print_time=0@D\n"
6334233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Don't print the elapsed time of each test.\n"
6335233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "output=xml@Y[@G:@YDIRECTORY_PATH@G"
6336233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_PATH_SEP_ "@Y|@G:@YFILE_PATH]@D\n"
6337233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Generate an XML report in the given directory or with the given file\n"
6338233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      name. @YFILE_PATH@D defaults to @Gtest_details.xml@D.\n"
6339233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_CAN_STREAM_RESULTS_
6340233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "stream_result_to=@YHOST@G:@YPORT@D\n"
6341233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Stream test results to the given server.\n"
6342233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_CAN_STREAM_RESULTS_
6343233d2500723e5594f3e7c70896ffeeef32b9c950ywan"\n"
6344233d2500723e5594f3e7c70896ffeeef32b9c950ywan"Assertion Behavior:\n"
6345233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS
6346233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n"
6347233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Set the default death test style.\n"
6348233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS
6349233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "break_on_failure@D\n"
6350233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Turn assertion failures into debugger break-points.\n"
6351233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "throw_on_failure@D\n"
6352233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Turn assertion failures into C++ exceptions.\n"
6353233d2500723e5594f3e7c70896ffeeef32b9c950ywan"  @G--" GTEST_FLAG_PREFIX_ "catch_exceptions=0@D\n"
6354233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      Do not report exceptions as test failures. Instead, allow them\n"
6355233d2500723e5594f3e7c70896ffeeef32b9c950ywan"      to crash the program or throw a pop-up (on Windows).\n"
6356233d2500723e5594f3e7c70896ffeeef32b9c950ywan"\n"
6357233d2500723e5594f3e7c70896ffeeef32b9c950ywan"Except for @G--" GTEST_FLAG_PREFIX_ "list_tests@D, you can alternatively set "
6358233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "the corresponding\n"
6359233d2500723e5594f3e7c70896ffeeef32b9c950ywan"environment variable of a flag (all letters in upper-case). For example, to\n"
6360233d2500723e5594f3e7c70896ffeeef32b9c950ywan"disable colored text output, you can either specify @G--" GTEST_FLAG_PREFIX_
6361233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "color=no@D or set\n"
6362233d2500723e5594f3e7c70896ffeeef32b9c950ywan"the @G" GTEST_FLAG_PREFIX_UPPER_ "COLOR@D environment variable to @Gno@D.\n"
6363233d2500723e5594f3e7c70896ffeeef32b9c950ywan"\n"
6364233d2500723e5594f3e7c70896ffeeef32b9c950ywan"For more information, please read the " GTEST_NAME_ " documentation at\n"
6365233d2500723e5594f3e7c70896ffeeef32b9c950ywan"@G" GTEST_PROJECT_URL_ "@D. If you find a bug in " GTEST_NAME_ "\n"
6366233d2500723e5594f3e7c70896ffeeef32b9c950ywan"(not one in your own code or tests), please report it to\n"
6367233d2500723e5594f3e7c70896ffeeef32b9c950ywan"@G<" GTEST_DEV_EMAIL_ ">@D.\n";
6368233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6369233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses the command line for Google Test flags, without initializing
6370233d2500723e5594f3e7c70896ffeeef32b9c950ywan// other parts of Google Test.  The type parameter CharType can be
6371233d2500723e5594f3e7c70896ffeeef32b9c950ywan// instantiated to either char or wchar_t.
6372233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename CharType>
6373233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
6374233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 1; i < *argc; i++) {
6375233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string arg_string = StreamableToString(argv[i]);
6376233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* const arg = arg_string.c_str();
6377233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6378233d2500723e5594f3e7c70896ffeeef32b9c950ywan    using internal::ParseBoolFlag;
6379233d2500723e5594f3e7c70896ffeeef32b9c950ywan    using internal::ParseInt32Flag;
6380233d2500723e5594f3e7c70896ffeeef32b9c950ywan    using internal::ParseStringFlag;
6381233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6382233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Do we see a Google Test flag?
6383233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag,
6384233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      &GTEST_FLAG(also_run_disabled_tests)) ||
6385233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseBoolFlag(arg, kBreakOnFailureFlag,
6386233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      &GTEST_FLAG(break_on_failure)) ||
6387233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseBoolFlag(arg, kCatchExceptionsFlag,
6388233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      &GTEST_FLAG(catch_exceptions)) ||
6389233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseStringFlag(arg, kColorFlag, &GTEST_FLAG(color)) ||
6390233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseStringFlag(arg, kDeathTestStyleFlag,
6391233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        &GTEST_FLAG(death_test_style)) ||
6392233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseBoolFlag(arg, kDeathTestUseFork,
6393233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      &GTEST_FLAG(death_test_use_fork)) ||
6394233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseStringFlag(arg, kFilterFlag, &GTEST_FLAG(filter)) ||
6395233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseStringFlag(arg, kInternalRunDeathTestFlag,
6396233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        &GTEST_FLAG(internal_run_death_test)) ||
6397233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseBoolFlag(arg, kListTestsFlag, &GTEST_FLAG(list_tests)) ||
6398233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseStringFlag(arg, kOutputFlag, &GTEST_FLAG(output)) ||
6399233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseBoolFlag(arg, kPrintTimeFlag, &GTEST_FLAG(print_time)) ||
6400233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseInt32Flag(arg, kRandomSeedFlag, &GTEST_FLAG(random_seed)) ||
6401233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseInt32Flag(arg, kRepeatFlag, &GTEST_FLAG(repeat)) ||
6402233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseBoolFlag(arg, kShuffleFlag, &GTEST_FLAG(shuffle)) ||
6403233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseInt32Flag(arg, kStackTraceDepthFlag,
6404233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       &GTEST_FLAG(stack_trace_depth)) ||
6405233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseStringFlag(arg, kStreamResultToFlag,
6406233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        &GTEST_FLAG(stream_result_to)) ||
6407233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ParseBoolFlag(arg, kThrowOnFailureFlag,
6408233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      &GTEST_FLAG(throw_on_failure))
6409233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ) {
6410233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Yes.  Shift the remainder of the argv list left by one.  Note
6411233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // that argv has (*argc + 1) elements, the last one always being
6412233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // NULL.  The following loop moves the trailing NULL element as
6413233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // well.
6414233d2500723e5594f3e7c70896ffeeef32b9c950ywan      for (int j = i; j != *argc; j++) {
6415233d2500723e5594f3e7c70896ffeeef32b9c950ywan        argv[j] = argv[j + 1];
6416233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
6417233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6418233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Decrements the argument count.
6419233d2500723e5594f3e7c70896ffeeef32b9c950ywan      (*argc)--;
6420233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6421233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // We also need to decrement the iterator as we just removed
6422233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // an element.
6423233d2500723e5594f3e7c70896ffeeef32b9c950ywan      i--;
6424233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else if (arg_string == "--help" || arg_string == "-h" ||
6425233d2500723e5594f3e7c70896ffeeef32b9c950ywan               arg_string == "-?" || arg_string == "/?" ||
6426233d2500723e5594f3e7c70896ffeeef32b9c950ywan               HasGoogleTestFlagPrefix(arg)) {
6427233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Both help flag and unrecognized Google Test flags (excluding
6428233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // internal ones) trigger help display.
6429233d2500723e5594f3e7c70896ffeeef32b9c950ywan      g_help_flag = true;
6430233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
6431233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6432233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6433233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (g_help_flag) {
6434233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // We print the help here instead of in RUN_ALL_TESTS(), as the
6435233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // latter may not be called at all if the user is using Google
6436233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Test with another testing framework.
6437233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintColorEncoded(kColorEncodedHelpMessage);
6438233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6439233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6440233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6441233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses the command line for Google Test flags, without initializing
6442233d2500723e5594f3e7c70896ffeeef32b9c950ywan// other parts of Google Test.
6443233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ParseGoogleTestFlagsOnly(int* argc, char** argv) {
6444233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ParseGoogleTestFlagsOnlyImpl(argc, argv);
6445233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6446233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv) {
6447233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ParseGoogleTestFlagsOnlyImpl(argc, argv);
6448233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6449233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6450233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The internal implementation of InitGoogleTest().
6451233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6452233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The type parameter CharType can be instantiated to either char or
6453233d2500723e5594f3e7c70896ffeeef32b9c950ywan// wchar_t.
6454233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename CharType>
6455233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid InitGoogleTestImpl(int* argc, CharType** argv) {
6456233d2500723e5594f3e7c70896ffeeef32b9c950ywan  g_init_gtest_count++;
6457233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6458233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We don't want to run the initialization code twice.
6459233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (g_init_gtest_count != 1) return;
6460233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6461233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (*argc <= 0) return;
6462233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6463233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::g_executable_path = internal::StreamableToString(argv[0]);
6464233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6465233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
6466233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6467233d2500723e5594f3e7c70896ffeeef32b9c950ywan  g_argvs.clear();
6468233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; i != *argc; i++) {
6469233d2500723e5594f3e7c70896ffeeef32b9c950ywan    g_argvs.push_back(StreamableToString(argv[i]));
6470233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6471233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6472233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
6473233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6474233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ParseGoogleTestFlagsOnly(argc, argv);
6475233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetUnitTestImpl()->PostFlagParsingInit();
6476233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6477233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6478233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
6479233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6480233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Initializes Google Test.  This must be called before calling
6481233d2500723e5594f3e7c70896ffeeef32b9c950ywan// RUN_ALL_TESTS().  In particular, it parses a command line for the
6482233d2500723e5594f3e7c70896ffeeef32b9c950ywan// flags that Google Test recognizes.  Whenever a Google Test flag is
6483233d2500723e5594f3e7c70896ffeeef32b9c950ywan// seen, it is removed from argv, and *argc is decremented.
6484233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6485233d2500723e5594f3e7c70896ffeeef32b9c950ywan// No value is returned.  Instead, the Google Test flag variables are
6486233d2500723e5594f3e7c70896ffeeef32b9c950ywan// updated.
6487233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6488233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Calling the function for the second time has no user-visible effect.
6489233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid InitGoogleTest(int* argc, char** argv) {
6490233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::InitGoogleTestImpl(argc, argv);
6491233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6492233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6493233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This overloaded version can be used in Windows programs compiled in
6494233d2500723e5594f3e7c70896ffeeef32b9c950ywan// UNICODE mode.
6495233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid InitGoogleTest(int* argc, wchar_t** argv) {
6496233d2500723e5594f3e7c70896ffeeef32b9c950ywan  internal::InitGoogleTestImpl(argc, argv);
6497233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6498233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6499233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
6500233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2005, Google Inc.
6501233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
6502233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6503233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
6504233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
6505233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
6506233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6507233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
6508233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
6509233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
6510233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
6511233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
6512233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
6513233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
6514233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
6515233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
6516233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6517233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6518233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6519233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6520233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
6521233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
6522233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
6523233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
6524233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
6525233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6526233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
6527233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6528233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6529233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: wan@google.com (Zhanyong Wan), vladl@google.com (Vlad Losev)
6530233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
6531233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This file implements death tests.
6532233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6533233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6534233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
6535233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6536233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_MAC
6537233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  include <crt_externs.h>
6538233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_MAC
6539233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6540233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <errno.h>
6541233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <fcntl.h>
6542233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <limits.h>
6543233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6544233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_LINUX
6545233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  include <signal.h>
6546233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_LINUX
6547233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6548233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <stdarg.h>
6549233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6550233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
6551233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  include <windows.h>
6552233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
6553233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  include <sys/mman.h>
6554233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  include <sys/wait.h>
6555233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS
6556233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6557233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_QNX
6558233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  include <spawn.h>
6559233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_QNX
6560233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6561233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
6562233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6563233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6564233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Indicates that this translation unit is part of Google Test's
6565233d2500723e5594f3e7c70896ffeeef32b9c950ywan// implementation.  It must come before gtest-internal-inl.h is
6566233d2500723e5594f3e7c70896ffeeef32b9c950ywan// included, or there will be a compiler error.  This trick is to
6567233d2500723e5594f3e7c70896ffeeef32b9c950ywan// prevent a user from accidentally including gtest-internal-inl.h in
6568233d2500723e5594f3e7c70896ffeeef32b9c950ywan// his code.
6569233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_IMPLEMENTATION_ 1
6570233d2500723e5594f3e7c70896ffeeef32b9c950ywan#undef GTEST_IMPLEMENTATION_
6571233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6572233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
6573233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6574233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Constants.
6575233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6576233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The default death test style.
6577233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kDefaultDeathTestStyle[] = "fast";
6578233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6579233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_string_(
6580233d2500723e5594f3e7c70896ffeeef32b9c950ywan    death_test_style,
6581233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::StringFromGTestEnv("death_test_style", kDefaultDeathTestStyle),
6582233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Indicates how to run a death test in a forked child process: "
6583233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "\"threadsafe\" (child process re-executes the test binary "
6584233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "from the beginning, running only the specific death test) or "
6585233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "\"fast\" (child process runs the death test immediately "
6586233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "after forking).");
6587233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6588233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_bool_(
6589233d2500723e5594f3e7c70896ffeeef32b9c950ywan    death_test_use_fork,
6590233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::BoolFromGTestEnv("death_test_use_fork", false),
6591233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Instructs to use fork()/_exit() instead of clone() in death tests. "
6592233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Ignored and always uses fork() on POSIX systems where clone() is not "
6593233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "implemented. Useful when running under valgrind or similar tools if "
6594233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "those do not support clone(). Valgrind 3.3.1 will just fail if "
6595233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "it sees an unsupported combination of clone() flags. "
6596233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "It is not recommended to use this flag w/o valgrind though it will "
6597233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "work in 99% of the cases. Once valgrind is fixed, this flag will "
6598233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "most likely be removed.");
6599233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6600233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
6601233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_DEFINE_string_(
6602233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal_run_death_test, "",
6603233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "Indicates the file, line number, temporal index of "
6604233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "the single death test to run, and a file descriptor to "
6605233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "which a success code may be sent, all separated by "
6606233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "the '|' characters.  This flag is specified if and only if the current "
6607233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "process is a sub-process launched for running a thread-safe "
6608233d2500723e5594f3e7c70896ffeeef32b9c950ywan    "death test.  FOR INTERNAL USE ONLY.");
6609233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
6610233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6611233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
6612233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6613233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
6614233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6615233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Valid only for fast death tests. Indicates the code is running in the
6616233d2500723e5594f3e7c70896ffeeef32b9c950ywan// child process of a fast style death test.
6617233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool g_in_fast_death_test_child = false;
6618233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6619233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns a Boolean value indicating whether the caller is currently
6620233d2500723e5594f3e7c70896ffeeef32b9c950ywan// executing in the context of the death test child process.  Tools such as
6621233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Valgrind heap checkers may need this to modify their behavior in death
6622233d2500723e5594f3e7c70896ffeeef32b9c950ywan// tests.  IMPORTANT: This is an internal utility.  Using it may break the
6623233d2500723e5594f3e7c70896ffeeef32b9c950ywan// implementation of death tests.  User code MUST NOT use it.
6624233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool InDeathTestChild() {
6625233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
6626233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6627233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // On Windows, death tests are thread-safe regardless of the value of the
6628233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // death_test_style flag.
6629233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return !GTEST_FLAG(internal_run_death_test).empty();
6630233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6631233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
6632233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6633233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(death_test_style) == "threadsafe")
6634233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return !GTEST_FLAG(internal_run_death_test).empty();
6635233d2500723e5594f3e7c70896ffeeef32b9c950ywan  else
6636233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return g_in_fast_death_test_child;
6637233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
6638233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6639233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6640233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
6641233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6642233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ExitedWithCode constructor.
6643233d2500723e5594f3e7c70896ffeeef32b9c950ywanExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) {
6644233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6645233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6646233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ExitedWithCode function-call operator.
6647233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ExitedWithCode::operator()(int exit_status) const {
6648233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
6649233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6650233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return exit_status == exit_code_;
6651233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6652233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
6653233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6654233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_;
6655233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6656233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS
6657233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6658233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6659233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if !GTEST_OS_WINDOWS
6660233d2500723e5594f3e7c70896ffeeef32b9c950ywan// KilledBySignal constructor.
6661233d2500723e5594f3e7c70896ffeeef32b9c950ywanKilledBySignal::KilledBySignal(int signum) : signum_(signum) {
6662233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6663233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6664233d2500723e5594f3e7c70896ffeeef32b9c950ywan// KilledBySignal function-call operator.
6665233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool KilledBySignal::operator()(int exit_status) const {
6666233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_;
6667233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6668233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // !GTEST_OS_WINDOWS
6669233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6670233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
6671233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6672233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Utilities needed for death tests.
6673233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6674233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Generates a textual description of a given exit code, in the format
6675233d2500723e5594f3e7c70896ffeeef32b9c950ywan// specified by wait(2).
6676233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string ExitSummary(int exit_code) {
6677233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message m;
6678233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6679233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
6680233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6681233d2500723e5594f3e7c70896ffeeef32b9c950ywan  m << "Exited with exit status " << exit_code;
6682233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6683233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
6684233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6685233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (WIFEXITED(exit_code)) {
6686233d2500723e5594f3e7c70896ffeeef32b9c950ywan    m << "Exited with exit status " << WEXITSTATUS(exit_code);
6687233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (WIFSIGNALED(exit_code)) {
6688233d2500723e5594f3e7c70896ffeeef32b9c950ywan    m << "Terminated by signal " << WTERMSIG(exit_code);
6689233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6690233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  ifdef WCOREDUMP
6691233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (WCOREDUMP(exit_code)) {
6692233d2500723e5594f3e7c70896ffeeef32b9c950ywan    m << " (core dumped)";
6693233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6694233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  endif
6695233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS
6696233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6697233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return m.GetString();
6698233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6699233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6700233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true if exit_status describes a process that was terminated
6701233d2500723e5594f3e7c70896ffeeef32b9c950ywan// by a signal, or exited normally with a nonzero exit code.
6702233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ExitedUnsuccessfully(int exit_status) {
6703233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return !ExitedWithCode(0)(exit_status);
6704233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6705233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6706233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if !GTEST_OS_WINDOWS
6707233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Generates a textual failure message when a death test finds more than
6708233d2500723e5594f3e7c70896ffeeef32b9c950ywan// one thread running, or cannot determine the number of threads, prior
6709233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to executing the given statement.  It is the responsibility of the
6710233d2500723e5594f3e7c70896ffeeef32b9c950ywan// caller not to pass a thread_count of 1.
6711233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string DeathTestThreadWarning(size_t thread_count) {
6712233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message msg;
6713233d2500723e5594f3e7c70896ffeeef32b9c950ywan  msg << "Death tests use fork(), which is unsafe particularly"
6714233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << " in a threaded context. For this test, " << GTEST_NAME_ << " ";
6715233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (thread_count == 0)
6716233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << "couldn't detect the number of threads.";
6717233d2500723e5594f3e7c70896ffeeef32b9c950ywan  else
6718233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << "detected " << thread_count << " threads.";
6719233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return msg.GetString();
6720233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6721233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // !GTEST_OS_WINDOWS
6722233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6723233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Flag characters for reporting a death test that did not die.
6724233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kDeathTestLived = 'L';
6725233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kDeathTestReturned = 'R';
6726233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kDeathTestThrew = 'T';
6727233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char kDeathTestInternalError = 'I';
6728233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6729233d2500723e5594f3e7c70896ffeeef32b9c950ywan// An enumeration describing all of the possible ways that a death test can
6730233d2500723e5594f3e7c70896ffeeef32b9c950ywan// conclude.  DIED means that the process died while executing the test
6731233d2500723e5594f3e7c70896ffeeef32b9c950ywan// code; LIVED means that process lived beyond the end of the test code;
6732233d2500723e5594f3e7c70896ffeeef32b9c950ywan// RETURNED means that the test statement attempted to execute a return
6733233d2500723e5594f3e7c70896ffeeef32b9c950ywan// statement, which is not allowed; THREW means that the test statement
6734233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returned control by throwing an exception.  IN_PROGRESS means the test
6735233d2500723e5594f3e7c70896ffeeef32b9c950ywan// has not yet concluded.
6736233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(vladl@google.com): Unify names and possibly values for
6737233d2500723e5594f3e7c70896ffeeef32b9c950ywan// AbortReason, DeathTestOutcome, and flag characters above.
6738233d2500723e5594f3e7c70896ffeeef32b9c950ywanenum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW };
6739233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6740233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Routine for aborting the program which is safe to call from an
6741233d2500723e5594f3e7c70896ffeeef32b9c950ywan// exec-style death test child process, in which case the error
6742233d2500723e5594f3e7c70896ffeeef32b9c950ywan// message is propagated back to the parent process.  Otherwise, the
6743233d2500723e5594f3e7c70896ffeeef32b9c950ywan// message is simply printed to stderr.  In either case, the program
6744233d2500723e5594f3e7c70896ffeeef32b9c950ywan// then exits with status 1.
6745233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid DeathTestAbort(const std::string& message) {
6746233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // On a POSIX system, this function may be called from a threadsafe-style
6747233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // death test child process, which operates on a very small stack.  Use
6748233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the heap for any additional non-minuscule memory requirements.
6749233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const InternalRunDeathTestFlag* const flag =
6750233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GetUnitTestImpl()->internal_run_death_test_flag();
6751233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (flag != NULL) {
6752233d2500723e5594f3e7c70896ffeeef32b9c950ywan    FILE* parent = posix::FDOpen(flag->write_fd(), "w");
6753233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fputc(kDeathTestInternalError, parent);
6754233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fprintf(parent, "%s", message.c_str());
6755233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(parent);
6756233d2500723e5594f3e7c70896ffeeef32b9c950ywan    _exit(1);
6757233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
6758233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fprintf(stderr, "%s", message.c_str());
6759233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stderr);
6760233d2500723e5594f3e7c70896ffeeef32b9c950ywan    posix::Abort();
6761233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6762233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6763233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6764233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A replacement for CHECK that calls DeathTestAbort if the assertion
6765233d2500723e5594f3e7c70896ffeeef32b9c950ywan// fails.
6766233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_DEATH_TEST_CHECK_(expression) \
6767233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do { \
6768233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (!::testing::internal::IsTrue(expression)) { \
6769233d2500723e5594f3e7c70896ffeeef32b9c950ywan      DeathTestAbort( \
6770233d2500723e5594f3e7c70896ffeeef32b9c950ywan          ::std::string("CHECK failed: File ") + __FILE__ +  ", line " \
6771233d2500723e5594f3e7c70896ffeeef32b9c950ywan          + ::testing::internal::StreamableToString(__LINE__) + ": " \
6772233d2500723e5594f3e7c70896ffeeef32b9c950ywan          + #expression); \
6773233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } \
6774233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (::testing::internal::AlwaysFalse())
6775233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6776233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for
6777233d2500723e5594f3e7c70896ffeeef32b9c950ywan// evaluating any system call that fulfills two conditions: it must return
6778233d2500723e5594f3e7c70896ffeeef32b9c950ywan// -1 on failure, and set errno to EINTR when it is interrupted and
6779233d2500723e5594f3e7c70896ffeeef32b9c950ywan// should be tried again.  The macro expands to a loop that repeatedly
6780233d2500723e5594f3e7c70896ffeeef32b9c950ywan// evaluates the expression as long as it evaluates to -1 and sets
6781233d2500723e5594f3e7c70896ffeeef32b9c950ywan// errno to EINTR.  If the expression evaluates to -1 but errno is
6782233d2500723e5594f3e7c70896ffeeef32b9c950ywan// something other than EINTR, DeathTestAbort is called.
6783233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \
6784233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do { \
6785233d2500723e5594f3e7c70896ffeeef32b9c950ywan    int gtest_retval; \
6786233d2500723e5594f3e7c70896ffeeef32b9c950ywan    do { \
6787233d2500723e5594f3e7c70896ffeeef32b9c950ywan      gtest_retval = (expression); \
6788233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } while (gtest_retval == -1 && errno == EINTR); \
6789233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (gtest_retval == -1) { \
6790233d2500723e5594f3e7c70896ffeeef32b9c950ywan      DeathTestAbort( \
6791233d2500723e5594f3e7c70896ffeeef32b9c950ywan          ::std::string("CHECK failed: File ") + __FILE__ + ", line " \
6792233d2500723e5594f3e7c70896ffeeef32b9c950ywan          + ::testing::internal::StreamableToString(__LINE__) + ": " \
6793233d2500723e5594f3e7c70896ffeeef32b9c950ywan          + #expression + " != -1"); \
6794233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } \
6795233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (::testing::internal::AlwaysFalse())
6796233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6797233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the message describing the last system error in errno.
6798233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string GetLastErrnoDescription() {
6799233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return errno == 0 ? "" : posix::StrError(errno);
6800233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6801233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6802233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This is called from a death test parent process to read a failure
6803233d2500723e5594f3e7c70896ffeeef32b9c950ywan// message from the death test child process and log it with the FATAL
6804233d2500723e5594f3e7c70896ffeeef32b9c950ywan// severity. On Windows, the message is read from a pipe handle. On other
6805233d2500723e5594f3e7c70896ffeeef32b9c950ywan// platforms, it is read from a file descriptor.
6806233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void FailFromInternalError(int fd) {
6807233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message error;
6808233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char buffer[256];
6809233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int num_read;
6810233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6811233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do {
6812233d2500723e5594f3e7c70896ffeeef32b9c950ywan    while ((num_read = posix::Read(fd, buffer, 255)) > 0) {
6813233d2500723e5594f3e7c70896ffeeef32b9c950ywan      buffer[num_read] = '\0';
6814233d2500723e5594f3e7c70896ffeeef32b9c950ywan      error << buffer;
6815233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
6816233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (num_read == -1 && errno == EINTR);
6817233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6818233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (num_read == 0) {
6819233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOG_(FATAL) << error.GetString();
6820233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
6821233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const int last_error = errno;
6822233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOG_(FATAL) << "Error while reading death test internal: "
6823233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << GetLastErrnoDescription() << " [" << last_error << "]";
6824233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6825233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6826233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6827233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Death test constructor.  Increments the running death test count
6828233d2500723e5594f3e7c70896ffeeef32b9c950ywan// for the current test.
6829233d2500723e5594f3e7c70896ffeeef32b9c950ywanDeathTest::DeathTest() {
6830233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TestInfo* const info = GetUnitTestImpl()->current_test_info();
6831233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (info == NULL) {
6832233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort("Cannot run a death test outside of a TEST or "
6833233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   "TEST_F construct");
6834233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6835233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6836233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6837233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates and returns a death test by dispatching to the current
6838233d2500723e5594f3e7c70896ffeeef32b9c950ywan// death test factory.
6839233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool DeathTest::Create(const char* statement, const RE* regex,
6840233d2500723e5594f3e7c70896ffeeef32b9c950ywan                       const char* file, int line, DeathTest** test) {
6841233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return GetUnitTestImpl()->death_test_factory()->Create(
6842233d2500723e5594f3e7c70896ffeeef32b9c950ywan      statement, regex, file, line, test);
6843233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6844233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6845233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* DeathTest::LastMessage() {
6846233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return last_death_test_message_.c_str();
6847233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6848233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6849233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid DeathTest::set_last_death_test_message(const std::string& message) {
6850233d2500723e5594f3e7c70896ffeeef32b9c950ywan  last_death_test_message_ = message;
6851233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6852233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6853233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string DeathTest::last_death_test_message_;
6854233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6855233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Provides cross platform implementation for some death functionality.
6856233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass DeathTestImpl : public DeathTest {
6857233d2500723e5594f3e7c70896ffeeef32b9c950ywan protected:
6858233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DeathTestImpl(const char* a_statement, const RE* a_regex)
6859233d2500723e5594f3e7c70896ffeeef32b9c950ywan      : statement_(a_statement),
6860233d2500723e5594f3e7c70896ffeeef32b9c950ywan        regex_(a_regex),
6861233d2500723e5594f3e7c70896ffeeef32b9c950ywan        spawned_(false),
6862233d2500723e5594f3e7c70896ffeeef32b9c950ywan        status_(-1),
6863233d2500723e5594f3e7c70896ffeeef32b9c950ywan        outcome_(IN_PROGRESS),
6864233d2500723e5594f3e7c70896ffeeef32b9c950ywan        read_fd_(-1),
6865233d2500723e5594f3e7c70896ffeeef32b9c950ywan        write_fd_(-1) {}
6866233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6867233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // read_fd_ is expected to be closed and cleared by a derived class.
6868233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ~DeathTestImpl() { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); }
6869233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6870233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void Abort(AbortReason reason);
6871233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual bool Passed(bool status_ok);
6872233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6873233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* statement() const { return statement_; }
6874233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const RE* regex() const { return regex_; }
6875233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool spawned() const { return spawned_; }
6876233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_spawned(bool is_spawned) { spawned_ = is_spawned; }
6877233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int status() const { return status_; }
6878233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_status(int a_status) { status_ = a_status; }
6879233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DeathTestOutcome outcome() const { return outcome_; }
6880233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; }
6881233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int read_fd() const { return read_fd_; }
6882233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_read_fd(int fd) { read_fd_ = fd; }
6883233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int write_fd() const { return write_fd_; }
6884233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_write_fd(int fd) { write_fd_ = fd; }
6885233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6886233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Called in the parent process only. Reads the result code of the death
6887233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // test child process via a pipe, interprets it to set the outcome_
6888233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // member, and closes read_fd_.  Outputs diagnostics and terminates in
6889233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // case of unexpected codes.
6890233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void ReadAndInterpretStatusByte();
6891233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6892233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
6893233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The textual content of the code this object is testing.  This class
6894233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // doesn't own this string and should not attempt to delete it.
6895233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const statement_;
6896233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The regular expression which test output must match.  DeathTestImpl
6897233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // doesn't own this object and should not attempt to delete it.
6898233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const RE* const regex_;
6899233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // True if the death test child process has been successfully spawned.
6900233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool spawned_;
6901233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The exit status of the child process.
6902233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int status_;
6903233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // How the death test concluded.
6904233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DeathTestOutcome outcome_;
6905233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Descriptor to the read end of the pipe to the child process.  It is
6906233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // always -1 in the child process.  The child keeps its write end of the
6907233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // pipe in write_fd_.
6908233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int read_fd_;
6909233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Descriptor to the child's write end of the pipe to the parent process.
6910233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // It is always -1 in the parent process.  The parent keeps its end of the
6911233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // pipe in read_fd_.
6912233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int write_fd_;
6913233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
6914233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6915233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Called in the parent process only. Reads the result code of the death
6916233d2500723e5594f3e7c70896ffeeef32b9c950ywan// test child process via a pipe, interprets it to set the outcome_
6917233d2500723e5594f3e7c70896ffeeef32b9c950ywan// member, and closes read_fd_.  Outputs diagnostics and terminates in
6918233d2500723e5594f3e7c70896ffeeef32b9c950ywan// case of unexpected codes.
6919233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid DeathTestImpl::ReadAndInterpretStatusByte() {
6920233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char flag;
6921233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int bytes_read;
6922233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6923233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The read() here blocks until data is available (signifying the
6924233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // failure of the death test) or until the pipe is closed (signifying
6925233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // its success), so it's okay to call this in the parent before
6926233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the child process has exited.
6927233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do {
6928233d2500723e5594f3e7c70896ffeeef32b9c950ywan    bytes_read = posix::Read(read_fd(), &flag, 1);
6929233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (bytes_read == -1 && errno == EINTR);
6930233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6931233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (bytes_read == 0) {
6932233d2500723e5594f3e7c70896ffeeef32b9c950ywan    set_outcome(DIED);
6933233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (bytes_read == 1) {
6934233d2500723e5594f3e7c70896ffeeef32b9c950ywan    switch (flag) {
6935233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case kDeathTestReturned:
6936233d2500723e5594f3e7c70896ffeeef32b9c950ywan        set_outcome(RETURNED);
6937233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
6938233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case kDeathTestThrew:
6939233d2500723e5594f3e7c70896ffeeef32b9c950ywan        set_outcome(THREW);
6940233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
6941233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case kDeathTestLived:
6942233d2500723e5594f3e7c70896ffeeef32b9c950ywan        set_outcome(LIVED);
6943233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
6944233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case kDeathTestInternalError:
6945233d2500723e5594f3e7c70896ffeeef32b9c950ywan        FailFromInternalError(read_fd());  // Does not return.
6946233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
6947233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default:
6948233d2500723e5594f3e7c70896ffeeef32b9c950ywan        GTEST_LOG_(FATAL) << "Death test child process reported "
6949233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          << "unexpected status byte ("
6950233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          << static_cast<unsigned int>(flag) << ")";
6951233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
6952233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
6953233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOG_(FATAL) << "Read from death test child process failed: "
6954233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << GetLastErrnoDescription();
6955233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6956233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd()));
6957233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_read_fd(-1);
6958233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6959233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6960233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Signals that the death test code which should have exited, didn't.
6961233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Should be called only in a death test child process.
6962233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Writes a status byte to the child's status file descriptor, then
6963233d2500723e5594f3e7c70896ffeeef32b9c950ywan// calls _exit(1).
6964233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid DeathTestImpl::Abort(AbortReason reason) {
6965233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The parent process considers the death test to be a failure if
6966233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it finds any data in our pipe.  So, here we write a single flag byte
6967233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // to the pipe, then exit.
6968233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char status_ch =
6969233d2500723e5594f3e7c70896ffeeef32b9c950ywan      reason == TEST_DID_NOT_DIE ? kDeathTestLived :
6970233d2500723e5594f3e7c70896ffeeef32b9c950ywan      reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned;
6971233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6972233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1));
6973233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We are leaking the descriptor here because on some platforms (i.e.,
6974233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // when built as Windows DLL), destructors of global objects will still
6975233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // run after calling _exit(). On such systems, write_fd_ will be
6976233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // indirectly closed from the destructor of UnitTestImpl, causing double
6977233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // close if it is also closed here. On debug configurations, double close
6978233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // may assert. As there are no in-process buffers to flush here, we are
6979233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // relying on the OS to close the descriptor after the process terminates
6980233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // when the destructors are not run.
6981233d2500723e5594f3e7c70896ffeeef32b9c950ywan  _exit(1);  // Exits w/o any normal exit hooks (we were supposed to crash)
6982233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
6983233d2500723e5594f3e7c70896ffeeef32b9c950ywan
6984233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns an indented copy of stderr output for a death test.
6985233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This makes distinguishing death test output lines from regular log lines
6986233d2500723e5594f3e7c70896ffeeef32b9c950ywan// much easier.
6987233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic ::std::string FormatDeathTestOutput(const ::std::string& output) {
6988233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::string ret;
6989233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t at = 0; ; ) {
6990233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const size_t line_end = output.find('\n', at);
6991233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ret += "[  DEATH   ] ";
6992233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (line_end == ::std::string::npos) {
6993233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ret += output.substr(at);
6994233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
6995233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
6996233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ret += output.substr(at, line_end + 1 - at);
6997233d2500723e5594f3e7c70896ffeeef32b9c950ywan    at = line_end + 1;
6998233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
6999233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ret;
7000233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7001233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7002233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Assesses the success or failure of a death test, using both private
7003233d2500723e5594f3e7c70896ffeeef32b9c950ywan// members which have previously been set, and one argument:
7004233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7005233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Private data members:
7006233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   outcome:  An enumeration describing how the death test
7007233d2500723e5594f3e7c70896ffeeef32b9c950ywan//             concluded: DIED, LIVED, THREW, or RETURNED.  The death test
7008233d2500723e5594f3e7c70896ffeeef32b9c950ywan//             fails in the latter three cases.
7009233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   status:   The exit status of the child process. On *nix, it is in the
7010233d2500723e5594f3e7c70896ffeeef32b9c950ywan//             in the format specified by wait(2). On Windows, this is the
7011233d2500723e5594f3e7c70896ffeeef32b9c950ywan//             value supplied to the ExitProcess() API or a numeric code
7012233d2500723e5594f3e7c70896ffeeef32b9c950ywan//             of the exception that terminated the program.
7013233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   regex:    A regular expression object to be applied to
7014233d2500723e5594f3e7c70896ffeeef32b9c950ywan//             the test's captured standard error output; the death test
7015233d2500723e5594f3e7c70896ffeeef32b9c950ywan//             fails if it does not match.
7016233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7017233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Argument:
7018233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   status_ok: true if exit_status is acceptable in the context of
7019233d2500723e5594f3e7c70896ffeeef32b9c950ywan//              this particular death test, which fails if it is false
7020233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7021233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff all of the above conditions are met.  Otherwise, the
7022233d2500723e5594f3e7c70896ffeeef32b9c950ywan// first failing condition, in the order given above, is the one that is
7023233d2500723e5594f3e7c70896ffeeef32b9c950ywan// reported. Also sets the last death test message string.
7024233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool DeathTestImpl::Passed(bool status_ok) {
7025233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!spawned())
7026233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
7027233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7028233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string error_message = GetCapturedStderr();
7029233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7030233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool success = false;
7031233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message buffer;
7032233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7033233d2500723e5594f3e7c70896ffeeef32b9c950ywan  buffer << "Death test: " << statement() << "\n";
7034233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (outcome()) {
7035233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case LIVED:
7036233d2500723e5594f3e7c70896ffeeef32b9c950ywan      buffer << "    Result: failed to die.\n"
7037233d2500723e5594f3e7c70896ffeeef32b9c950ywan             << " Error msg:\n" << FormatDeathTestOutput(error_message);
7038233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
7039233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case THREW:
7040233d2500723e5594f3e7c70896ffeeef32b9c950ywan      buffer << "    Result: threw an exception.\n"
7041233d2500723e5594f3e7c70896ffeeef32b9c950ywan             << " Error msg:\n" << FormatDeathTestOutput(error_message);
7042233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
7043233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case RETURNED:
7044233d2500723e5594f3e7c70896ffeeef32b9c950ywan      buffer << "    Result: illegal return in test statement.\n"
7045233d2500723e5594f3e7c70896ffeeef32b9c950ywan             << " Error msg:\n" << FormatDeathTestOutput(error_message);
7046233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
7047233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case DIED:
7048233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (status_ok) {
7049233d2500723e5594f3e7c70896ffeeef32b9c950ywan        const bool matched = RE::PartialMatch(error_message.c_str(), *regex());
7050233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if (matched) {
7051233d2500723e5594f3e7c70896ffeeef32b9c950ywan          success = true;
7052233d2500723e5594f3e7c70896ffeeef32b9c950ywan        } else {
7053233d2500723e5594f3e7c70896ffeeef32b9c950ywan          buffer << "    Result: died but not with expected error.\n"
7054233d2500723e5594f3e7c70896ffeeef32b9c950ywan                 << "  Expected: " << regex()->pattern() << "\n"
7055233d2500723e5594f3e7c70896ffeeef32b9c950ywan                 << "Actual msg:\n" << FormatDeathTestOutput(error_message);
7056233d2500723e5594f3e7c70896ffeeef32b9c950ywan        }
7057233d2500723e5594f3e7c70896ffeeef32b9c950ywan      } else {
7058233d2500723e5594f3e7c70896ffeeef32b9c950ywan        buffer << "    Result: died but not with expected exit code:\n"
7059233d2500723e5594f3e7c70896ffeeef32b9c950ywan               << "            " << ExitSummary(status()) << "\n"
7060233d2500723e5594f3e7c70896ffeeef32b9c950ywan               << "Actual msg:\n" << FormatDeathTestOutput(error_message);
7061233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
7062233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
7063233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case IN_PROGRESS:
7064233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:
7065233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTEST_LOG_(FATAL)
7066233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << "DeathTest::Passed somehow called before conclusion of test";
7067233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7068233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7069233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DeathTest::set_last_death_test_message(buffer.GetString());
7070233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return success;
7071233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7072233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7073233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
7074233d2500723e5594f3e7c70896ffeeef32b9c950ywan// WindowsDeathTest implements death tests on Windows. Due to the
7075233d2500723e5594f3e7c70896ffeeef32b9c950ywan// specifics of starting new processes on Windows, death tests there are
7076233d2500723e5594f3e7c70896ffeeef32b9c950ywan// always threadsafe, and Google Test considers the
7077233d2500723e5594f3e7c70896ffeeef32b9c950ywan// --gtest_death_test_style=fast setting to be equivalent to
7078233d2500723e5594f3e7c70896ffeeef32b9c950ywan// --gtest_death_test_style=threadsafe there.
7079233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7080233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A few implementation notes:  Like the Linux version, the Windows
7081233d2500723e5594f3e7c70896ffeeef32b9c950ywan// implementation uses pipes for child-to-parent communication. But due to
7082233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the specifics of pipes on Windows, some extra steps are required:
7083233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7084233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 1. The parent creates a communication pipe and stores handles to both
7085233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    ends of it.
7086233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 2. The parent starts the child and provides it with the information
7087233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    necessary to acquire the handle to the write end of the pipe.
7088233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 3. The child acquires the write end of the pipe and signals the parent
7089233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    using a Windows event.
7090233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 4. Now the parent can release the write end of the pipe on its side. If
7091233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    this is done before step 3, the object's reference count goes down to
7092233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    0 and it is destroyed, preventing the child from acquiring it. The
7093233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    parent now has to release it, or read operations on the read end of
7094233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    the pipe will not return when the child terminates.
7095233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 5. The parent reads child's output through the pipe (outcome code and
7096233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    any possible error messages) from the pipe, and its stderr and then
7097233d2500723e5594f3e7c70896ffeeef32b9c950ywan//    determines whether to fail the test.
7098233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7099233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Note: to distinguish Win32 API calls from the local method and function
7100233d2500723e5594f3e7c70896ffeeef32b9c950ywan// calls, the former are explicitly resolved in the global namespace.
7101233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7102233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass WindowsDeathTest : public DeathTestImpl {
7103233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
7104233d2500723e5594f3e7c70896ffeeef32b9c950ywan  WindowsDeathTest(const char* a_statement,
7105233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   const RE* a_regex,
7106233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   const char* file,
7107233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   int line)
7108233d2500723e5594f3e7c70896ffeeef32b9c950ywan      : DeathTestImpl(a_statement, a_regex), file_(file), line_(line) {}
7109233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7110233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // All of these virtual functions are inherited from DeathTest.
7111233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual int Wait();
7112233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual TestRole AssumeRole();
7113233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7114233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
7115233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The name of the file in which the death test is located.
7116233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const file_;
7117233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The line number on which the death test is located.
7118233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int line_;
7119233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Handle to the write end of the pipe to the child process.
7120233d2500723e5594f3e7c70896ffeeef32b9c950ywan  AutoHandle write_handle_;
7121233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Child process handle.
7122233d2500723e5594f3e7c70896ffeeef32b9c950ywan  AutoHandle child_handle_;
7123233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Event the child process uses to signal the parent that it has
7124233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // acquired the handle to the write end of the pipe. After seeing this
7125233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // event the parent can release its own handles to make sure its
7126233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // ReadFile() calls return when the child terminates.
7127233d2500723e5594f3e7c70896ffeeef32b9c950ywan  AutoHandle event_handle_;
7128233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
7129233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7130233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Waits for the child in a death test to exit, returning its exit
7131233d2500723e5594f3e7c70896ffeeef32b9c950ywan// status, or 0 if no child process exists.  As a side effect, sets the
7132233d2500723e5594f3e7c70896ffeeef32b9c950ywan// outcome data member.
7133233d2500723e5594f3e7c70896ffeeef32b9c950ywanint WindowsDeathTest::Wait() {
7134233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!spawned())
7135233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return 0;
7136233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7137233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Wait until the child either signals that it has acquired the write end
7138233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // of the pipe or it dies.
7139233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() };
7140233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (::WaitForMultipleObjects(2,
7141233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   wait_handles,
7142233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   FALSE,  // Waits for any of the handles.
7143233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   INFINITE)) {
7144233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case WAIT_OBJECT_0:
7145233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case WAIT_OBJECT_0 + 1:
7146233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
7147233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:
7148233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTEST_DEATH_TEST_CHECK_(false);  // Should not get here.
7149233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7150233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7151233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The child has acquired the write end of the pipe or exited.
7152233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We release the handle on our side and continue.
7153233d2500723e5594f3e7c70896ffeeef32b9c950ywan  write_handle_.Reset();
7154233d2500723e5594f3e7c70896ffeeef32b9c950ywan  event_handle_.Reset();
7155233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7156233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ReadAndInterpretStatusByte();
7157233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7158233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Waits for the child process to exit if it haven't already. This
7159233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // returns immediately if the child has already exited, regardless of
7160233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // whether previous calls to WaitForMultipleObjects synchronized on this
7161233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // handle or not.
7162233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(
7163233d2500723e5594f3e7c70896ffeeef32b9c950ywan      WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(),
7164233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                             INFINITE));
7165233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DWORD status_code;
7166233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(
7167233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE);
7168233d2500723e5594f3e7c70896ffeeef32b9c950ywan  child_handle_.Reset();
7169233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_status(static_cast<int>(status_code));
7170233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return status();
7171233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7172233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7173233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The AssumeRole process for a Windows death test.  It creates a child
7174233d2500723e5594f3e7c70896ffeeef32b9c950ywan// process with the same executable as the current process to run the
7175233d2500723e5594f3e7c70896ffeeef32b9c950ywan// death test.  The child process is given the --gtest_filter and
7176233d2500723e5594f3e7c70896ffeeef32b9c950ywan// --gtest_internal_run_death_test flags such that it knows to run the
7177233d2500723e5594f3e7c70896ffeeef32b9c950ywan// current death test only.
7178233d2500723e5594f3e7c70896ffeeef32b9c950ywanDeathTest::TestRole WindowsDeathTest::AssumeRole() {
7179233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const UnitTestImpl* const impl = GetUnitTestImpl();
7180233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const InternalRunDeathTestFlag* const flag =
7181233d2500723e5594f3e7c70896ffeeef32b9c950ywan      impl->internal_run_death_test_flag();
7182233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestInfo* const info = impl->current_test_info();
7183233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int death_test_index = info->result()->death_test_count();
7184233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7185233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (flag != NULL) {
7186233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // ParseInternalRunDeathTestFlag() has performed all the necessary
7187233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // processing.
7188233d2500723e5594f3e7c70896ffeeef32b9c950ywan    set_write_fd(flag->write_fd());
7189233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return EXECUTE_TEST;
7190233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7191233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7192233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // WindowsDeathTest uses an anonymous pipe to communicate results of
7193233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // a death test.
7194233d2500723e5594f3e7c70896ffeeef32b9c950ywan  SECURITY_ATTRIBUTES handles_are_inheritable = {
7195233d2500723e5594f3e7c70896ffeeef32b9c950ywan    sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
7196233d2500723e5594f3e7c70896ffeeef32b9c950ywan  HANDLE read_handle, write_handle;
7197233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(
7198233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable,
7199233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   0)  // Default buffer size.
7200233d2500723e5594f3e7c70896ffeeef32b9c950ywan      != FALSE);
7201233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_read_fd(::_open_osfhandle(reinterpret_cast<intptr_t>(read_handle),
7202233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                O_RDONLY));
7203233d2500723e5594f3e7c70896ffeeef32b9c950ywan  write_handle_.Reset(write_handle);
7204233d2500723e5594f3e7c70896ffeeef32b9c950ywan  event_handle_.Reset(::CreateEvent(
7205233d2500723e5594f3e7c70896ffeeef32b9c950ywan      &handles_are_inheritable,
7206233d2500723e5594f3e7c70896ffeeef32b9c950ywan      TRUE,    // The event will automatically reset to non-signaled state.
7207233d2500723e5594f3e7c70896ffeeef32b9c950ywan      FALSE,   // The initial state is non-signalled.
7208233d2500723e5594f3e7c70896ffeeef32b9c950ywan      NULL));  // The even is unnamed.
7209233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != NULL);
7210233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string filter_flag =
7211233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::string("--") + GTEST_FLAG_PREFIX_ + kFilterFlag + "=" +
7212233d2500723e5594f3e7c70896ffeeef32b9c950ywan      info->test_case_name() + "." + info->name();
7213233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string internal_flag =
7214233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag +
7215233d2500723e5594f3e7c70896ffeeef32b9c950ywan      "=" + file_ + "|" + StreamableToString(line_) + "|" +
7216233d2500723e5594f3e7c70896ffeeef32b9c950ywan      StreamableToString(death_test_index) + "|" +
7217233d2500723e5594f3e7c70896ffeeef32b9c950ywan      StreamableToString(static_cast<unsigned int>(::GetCurrentProcessId())) +
7218233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // size_t has the same width as pointers on both 32-bit and 64-bit
7219233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Windows platforms.
7220233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx.
7221233d2500723e5594f3e7c70896ffeeef32b9c950ywan      "|" + StreamableToString(reinterpret_cast<size_t>(write_handle)) +
7222233d2500723e5594f3e7c70896ffeeef32b9c950ywan      "|" + StreamableToString(reinterpret_cast<size_t>(event_handle_.Get()));
7223233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7224233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char executable_path[_MAX_PATH + 1];  // NOLINT
7225233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(
7226233d2500723e5594f3e7c70896ffeeef32b9c950ywan      _MAX_PATH + 1 != ::GetModuleFileNameA(NULL,
7227233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                            executable_path,
7228233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                            _MAX_PATH));
7229233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7230233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string command_line =
7231233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::string(::GetCommandLineA()) + " " + filter_flag + " \"" +
7232233d2500723e5594f3e7c70896ffeeef32b9c950ywan      internal_flag + "\"";
7233233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7234233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DeathTest::set_last_death_test_message("");
7235233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7236233d2500723e5594f3e7c70896ffeeef32b9c950ywan  CaptureStderr();
7237233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Flush the log buffers since the log streams are shared with the child.
7238233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FlushInfoLog();
7239233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7240233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The child process will share the standard handles with the parent.
7241233d2500723e5594f3e7c70896ffeeef32b9c950ywan  STARTUPINFOA startup_info;
7242233d2500723e5594f3e7c70896ffeeef32b9c950ywan  memset(&startup_info, 0, sizeof(STARTUPINFO));
7243233d2500723e5594f3e7c70896ffeeef32b9c950ywan  startup_info.dwFlags = STARTF_USESTDHANDLES;
7244233d2500723e5594f3e7c70896ffeeef32b9c950ywan  startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE);
7245233d2500723e5594f3e7c70896ffeeef32b9c950ywan  startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
7246233d2500723e5594f3e7c70896ffeeef32b9c950ywan  startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
7247233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7248233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PROCESS_INFORMATION process_info;
7249233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(::CreateProcessA(
7250233d2500723e5594f3e7c70896ffeeef32b9c950ywan      executable_path,
7251233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const_cast<char*>(command_line.c_str()),
7252233d2500723e5594f3e7c70896ffeeef32b9c950ywan      NULL,   // Retuned process handle is not inheritable.
7253233d2500723e5594f3e7c70896ffeeef32b9c950ywan      NULL,   // Retuned thread handle is not inheritable.
7254233d2500723e5594f3e7c70896ffeeef32b9c950ywan      TRUE,   // Child inherits all inheritable handles (for write_handle_).
7255233d2500723e5594f3e7c70896ffeeef32b9c950ywan      0x0,    // Default creation flags.
7256233d2500723e5594f3e7c70896ffeeef32b9c950ywan      NULL,   // Inherit the parent's environment.
7257233d2500723e5594f3e7c70896ffeeef32b9c950ywan      UnitTest::GetInstance()->original_working_dir(),
7258233d2500723e5594f3e7c70896ffeeef32b9c950ywan      &startup_info,
7259233d2500723e5594f3e7c70896ffeeef32b9c950ywan      &process_info) != FALSE);
7260233d2500723e5594f3e7c70896ffeeef32b9c950ywan  child_handle_.Reset(process_info.hProcess);
7261233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::CloseHandle(process_info.hThread);
7262233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_spawned(true);
7263233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return OVERSEE_TEST;
7264233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7265233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else  // We are not on Windows.
7266233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7267233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ForkingDeathTest provides implementations for most of the abstract
7268233d2500723e5594f3e7c70896ffeeef32b9c950ywan// methods of the DeathTest interface.  Only the AssumeRole method is
7269233d2500723e5594f3e7c70896ffeeef32b9c950ywan// left undefined.
7270233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass ForkingDeathTest : public DeathTestImpl {
7271233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
7272233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ForkingDeathTest(const char* statement, const RE* regex);
7273233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7274233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // All of these virtual functions are inherited from DeathTest.
7275233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual int Wait();
7276233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7277233d2500723e5594f3e7c70896ffeeef32b9c950ywan protected:
7278233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; }
7279233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7280233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
7281233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // PID of child process during death test; 0 in the child process itself.
7282233d2500723e5594f3e7c70896ffeeef32b9c950ywan  pid_t child_pid_;
7283233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
7284233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7285233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Constructs a ForkingDeathTest.
7286233d2500723e5594f3e7c70896ffeeef32b9c950ywanForkingDeathTest::ForkingDeathTest(const char* a_statement, const RE* a_regex)
7287233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : DeathTestImpl(a_statement, a_regex),
7288233d2500723e5594f3e7c70896ffeeef32b9c950ywan      child_pid_(-1) {}
7289233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7290233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Waits for the child in a death test to exit, returning its exit
7291233d2500723e5594f3e7c70896ffeeef32b9c950ywan// status, or 0 if no child process exists.  As a side effect, sets the
7292233d2500723e5594f3e7c70896ffeeef32b9c950ywan// outcome data member.
7293233d2500723e5594f3e7c70896ffeeef32b9c950ywanint ForkingDeathTest::Wait() {
7294233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!spawned())
7295233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return 0;
7296233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7297233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ReadAndInterpretStatusByte();
7298233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7299233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int status_value;
7300233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0));
7301233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_status(status_value);
7302233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return status_value;
7303233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7304233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7305233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A concrete death test class that forks, then immediately runs the test
7306233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the child process.
7307233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass NoExecDeathTest : public ForkingDeathTest {
7308233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
7309233d2500723e5594f3e7c70896ffeeef32b9c950ywan  NoExecDeathTest(const char* a_statement, const RE* a_regex) :
7310233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ForkingDeathTest(a_statement, a_regex) { }
7311233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual TestRole AssumeRole();
7312233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
7313233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7314233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The AssumeRole process for a fork-and-run death test.  It implements a
7315233d2500723e5594f3e7c70896ffeeef32b9c950ywan// straightforward fork, with a simple pipe to transmit the status byte.
7316233d2500723e5594f3e7c70896ffeeef32b9c950ywanDeathTest::TestRole NoExecDeathTest::AssumeRole() {
7317233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t thread_count = GetThreadCount();
7318233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (thread_count != 1) {
7319233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count);
7320233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7321233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7322233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int pipe_fd[2];
7323233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);
7324233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7325233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DeathTest::set_last_death_test_message("");
7326233d2500723e5594f3e7c70896ffeeef32b9c950ywan  CaptureStderr();
7327233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // When we fork the process below, the log file buffers are copied, but the
7328233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // file descriptors are shared.  We flush all log files here so that closing
7329233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the file descriptors in the child process doesn't throw off the
7330233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // synchronization between descriptors and buffers in the parent process.
7331233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // This is as close to the fork as possible to avoid a race condition in case
7332233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // there are multiple threads running before the death test, and another
7333233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // thread writes to the log file.
7334233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FlushInfoLog();
7335233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7336233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const pid_t child_pid = fork();
7337233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(child_pid != -1);
7338233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_child_pid(child_pid);
7339233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (child_pid == 0) {
7340233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0]));
7341233d2500723e5594f3e7c70896ffeeef32b9c950ywan    set_write_fd(pipe_fd[1]);
7342233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Redirects all logging to stderr in the child process to prevent
7343233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // concurrent writes to the log files.  We capture stderr in the parent
7344233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // process and append the child process' output to a log.
7345233d2500723e5594f3e7c70896ffeeef32b9c950ywan    LogToStderr();
7346233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Event forwarding to the listeners of event listener API mush be shut
7347233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // down in death test subprocesses.
7348233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GetUnitTestImpl()->listeners()->SuppressEventForwarding();
7349233d2500723e5594f3e7c70896ffeeef32b9c950ywan    g_in_fast_death_test_child = true;
7350233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return EXECUTE_TEST;
7351233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
7352233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));
7353233d2500723e5594f3e7c70896ffeeef32b9c950ywan    set_read_fd(pipe_fd[0]);
7354233d2500723e5594f3e7c70896ffeeef32b9c950ywan    set_spawned(true);
7355233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return OVERSEE_TEST;
7356233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7357233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7358233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7359233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A concrete death test class that forks and re-executes the main
7360233d2500723e5594f3e7c70896ffeeef32b9c950ywan// program from the beginning, with command-line flags set that cause
7361233d2500723e5594f3e7c70896ffeeef32b9c950ywan// only this specific death test to be run.
7362233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass ExecDeathTest : public ForkingDeathTest {
7363233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
7364233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ExecDeathTest(const char* a_statement, const RE* a_regex,
7365233d2500723e5594f3e7c70896ffeeef32b9c950ywan                const char* file, int line) :
7366233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ForkingDeathTest(a_statement, a_regex), file_(file), line_(line) { }
7367233d2500723e5594f3e7c70896ffeeef32b9c950ywan  virtual TestRole AssumeRole();
7368233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
7369233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static ::std::vector<testing::internal::string>
7370233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetArgvsForDeathTestChildProcess() {
7371233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::std::vector<testing::internal::string> args = GetInjectableArgvs();
7372233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return args;
7373233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7374233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The name of the file in which the death test is located.
7375233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const file_;
7376233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The line number on which the death test is located.
7377233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int line_;
7378233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
7379233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7380233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Utility class for accumulating command-line arguments.
7381233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass Arguments {
7382233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
7383233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Arguments() {
7384233d2500723e5594f3e7c70896ffeeef32b9c950ywan    args_.push_back(NULL);
7385233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7386233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7387233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ~Arguments() {
7388233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();
7389233d2500723e5594f3e7c70896ffeeef32b9c950ywan         ++i) {
7390233d2500723e5594f3e7c70896ffeeef32b9c950ywan      free(*i);
7391233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
7392233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7393233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void AddArgument(const char* argument) {
7394233d2500723e5594f3e7c70896ffeeef32b9c950ywan    args_.insert(args_.end() - 1, posix::StrDup(argument));
7395233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7396233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7397233d2500723e5594f3e7c70896ffeeef32b9c950ywan  template <typename Str>
7398233d2500723e5594f3e7c70896ffeeef32b9c950ywan  void AddArguments(const ::std::vector<Str>& arguments) {
7399233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (typename ::std::vector<Str>::const_iterator i = arguments.begin();
7400233d2500723e5594f3e7c70896ffeeef32b9c950ywan         i != arguments.end();
7401233d2500723e5594f3e7c70896ffeeef32b9c950ywan         ++i) {
7402233d2500723e5594f3e7c70896ffeeef32b9c950ywan      args_.insert(args_.end() - 1, posix::StrDup(i->c_str()));
7403233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
7404233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7405233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* const* Argv() {
7406233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return &args_[0];
7407233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7408233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7409233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
7410233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::vector<char*> args_;
7411233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
7412233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7413233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A struct that encompasses the arguments to the child process of a
7414233d2500723e5594f3e7c70896ffeeef32b9c950ywan// threadsafe-style death test process.
7415233d2500723e5594f3e7c70896ffeeef32b9c950ywanstruct ExecDeathTestArgs {
7416233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* const* argv;  // Command-line arguments for the child's call to exec
7417233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int close_fd;       // File descriptor to close; the read end of a pipe
7418233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
7419233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7420233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  if GTEST_OS_MAC
7421233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline char** GetEnviron() {
7422233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // When Google Test is built as a framework on MacOS X, the environ variable
7423233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // is unavailable. Apple's documentation (man environ) recommends using
7424233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // _NSGetEnviron() instead.
7425233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return *_NSGetEnviron();
7426233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7427233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  else
7428233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Some POSIX platforms expect you to declare environ. extern "C" makes
7429233d2500723e5594f3e7c70896ffeeef32b9c950ywan// it reside in the global namespace.
7430233d2500723e5594f3e7c70896ffeeef32b9c950ywanextern "C" char** environ;
7431233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline char** GetEnviron() { return environ; }
7432233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  endif  // GTEST_OS_MAC
7433233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7434233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  if !GTEST_OS_QNX
7435233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The main function for a threadsafe-style death test child process.
7436233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This function is called in a clone()-ed process and thus must avoid
7437233d2500723e5594f3e7c70896ffeeef32b9c950ywan// any potentially unsafe operations like malloc or libc functions.
7438233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic int ExecDeathTestChildMain(void* child_arg) {
7439233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ExecDeathTestArgs* const args = static_cast<ExecDeathTestArgs*>(child_arg);
7440233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd));
7441233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7442233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We need to execute the test program in the same environment where
7443233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it was originally invoked.  Therefore we change to the original
7444233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // working directory first.
7445233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const original_dir =
7446233d2500723e5594f3e7c70896ffeeef32b9c950ywan      UnitTest::GetInstance()->original_working_dir();
7447233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We can safely call chdir() as it's a direct system call.
7448233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (chdir(original_dir) != 0) {
7449233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort(std::string("chdir(\"") + original_dir + "\") failed: " +
7450233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   GetLastErrnoDescription());
7451233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return EXIT_FAILURE;
7452233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7453233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7454233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We can safely call execve() as it's a direct system call.  We
7455233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // cannot use execvp() as it's a libc function and thus potentially
7456233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // unsafe.  Since execve() doesn't search the PATH, the user must
7457233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // invoke the test program via a valid path that contains at least
7458233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // one path separator.
7459233d2500723e5594f3e7c70896ffeeef32b9c950ywan  execve(args->argv[0], args->argv, GetEnviron());
7460233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DeathTestAbort(std::string("execve(") + args->argv[0] + ", ...) in " +
7461233d2500723e5594f3e7c70896ffeeef32b9c950ywan                 original_dir + " failed: " +
7462233d2500723e5594f3e7c70896ffeeef32b9c950ywan                 GetLastErrnoDescription());
7463233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return EXIT_FAILURE;
7464233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7465233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  endif  // !GTEST_OS_QNX
7466233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7467233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Two utility routines that together determine the direction the stack
7468233d2500723e5594f3e7c70896ffeeef32b9c950ywan// grows.
7469233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This could be accomplished more elegantly by a single recursive
7470233d2500723e5594f3e7c70896ffeeef32b9c950ywan// function, but we want to guard against the unlikely possibility of
7471233d2500723e5594f3e7c70896ffeeef32b9c950ywan// a smart compiler optimizing the recursion away.
7472233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7473233d2500723e5594f3e7c70896ffeeef32b9c950ywan// GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining
7474233d2500723e5594f3e7c70896ffeeef32b9c950ywan// StackLowerThanAddress into StackGrowsDown, which then doesn't give
7475233d2500723e5594f3e7c70896ffeeef32b9c950ywan// correct answer.
7476233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid StackLowerThanAddress(const void* ptr, bool* result) GTEST_NO_INLINE_;
7477233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid StackLowerThanAddress(const void* ptr, bool* result) {
7478233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int dummy;
7479233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *result = (&dummy < ptr);
7480233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7481233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7482233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool StackGrowsDown() {
7483233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int dummy;
7484233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool result;
7485233d2500723e5594f3e7c70896ffeeef32b9c950ywan  StackLowerThanAddress(&dummy, &result);
7486233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result;
7487233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7488233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7489233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Spawns a child process with the same executable as the current process in
7490233d2500723e5594f3e7c70896ffeeef32b9c950ywan// a thread-safe manner and instructs it to run the death test.  The
7491233d2500723e5594f3e7c70896ffeeef32b9c950ywan// implementation uses fork(2) + exec.  On systems where clone(2) is
7492233d2500723e5594f3e7c70896ffeeef32b9c950ywan// available, it is used instead, being slightly more thread-safe.  On QNX,
7493233d2500723e5594f3e7c70896ffeeef32b9c950ywan// fork supports only single-threaded environments, so this function uses
7494233d2500723e5594f3e7c70896ffeeef32b9c950ywan// spawn(2) there instead.  The function dies with an error message if
7495233d2500723e5594f3e7c70896ffeeef32b9c950ywan// anything goes wrong.
7496233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {
7497233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ExecDeathTestArgs args = { argv, close_fd };
7498233d2500723e5594f3e7c70896ffeeef32b9c950ywan  pid_t child_pid = -1;
7499233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7500233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  if GTEST_OS_QNX
7501233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Obtains the current directory and sets it to be closed in the child
7502233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // process.
7503233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int cwd_fd = open(".", O_RDONLY);
7504233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(cwd_fd != -1);
7505233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(cwd_fd, F_SETFD, FD_CLOEXEC));
7506233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We need to execute the test program in the same environment where
7507233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it was originally invoked.  Therefore we change to the original
7508233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // working directory first.
7509233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const original_dir =
7510233d2500723e5594f3e7c70896ffeeef32b9c950ywan      UnitTest::GetInstance()->original_working_dir();
7511233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We can safely call chdir() as it's a direct system call.
7512233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (chdir(original_dir) != 0) {
7513233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort(std::string("chdir(\"") + original_dir + "\") failed: " +
7514233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   GetLastErrnoDescription());
7515233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return EXIT_FAILURE;
7516233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7517233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7518233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int fd_flags;
7519233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Set close_fd to be closed after spawn.
7520233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(fd_flags = fcntl(close_fd, F_GETFD));
7521233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(close_fd, F_SETFD,
7522233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                        fd_flags | FD_CLOEXEC));
7523233d2500723e5594f3e7c70896ffeeef32b9c950ywan  struct inheritance inherit = {0};
7524233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // spawn is a system call.
7525233d2500723e5594f3e7c70896ffeeef32b9c950ywan  child_pid = spawn(args.argv[0], 0, NULL, &inherit, args.argv, GetEnviron());
7526233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Restores the current working directory.
7527233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(fchdir(cwd_fd) != -1);
7528233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(close(cwd_fd));
7529233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7530233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  else   // GTEST_OS_QNX
7531233d2500723e5594f3e7c70896ffeeef32b9c950ywan#   if GTEST_OS_LINUX
7532233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // When a SIGPROF signal is received while fork() or clone() are executing,
7533233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // the process may hang. To avoid this, we ignore SIGPROF here and re-enable
7534233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it after the call to fork()/clone() is complete.
7535233d2500723e5594f3e7c70896ffeeef32b9c950ywan  struct sigaction saved_sigprof_action;
7536233d2500723e5594f3e7c70896ffeeef32b9c950ywan  struct sigaction ignore_sigprof_action;
7537233d2500723e5594f3e7c70896ffeeef32b9c950ywan  memset(&ignore_sigprof_action, 0, sizeof(ignore_sigprof_action));
7538233d2500723e5594f3e7c70896ffeeef32b9c950ywan  sigemptyset(&ignore_sigprof_action.sa_mask);
7539233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ignore_sigprof_action.sa_handler = SIG_IGN;
7540233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(sigaction(
7541233d2500723e5594f3e7c70896ffeeef32b9c950ywan      SIGPROF, &ignore_sigprof_action, &saved_sigprof_action));
7542233d2500723e5594f3e7c70896ffeeef32b9c950ywan#   endif  // GTEST_OS_LINUX
7543233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7544233d2500723e5594f3e7c70896ffeeef32b9c950ywan#   if GTEST_HAS_CLONE
7545233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool use_fork = GTEST_FLAG(death_test_use_fork);
7546233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7547233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!use_fork) {
7548233d2500723e5594f3e7c70896ffeeef32b9c950ywan    static const bool stack_grows_down = StackGrowsDown();
7549233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const size_t stack_size = getpagesize();
7550233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead.
7551233d2500723e5594f3e7c70896ffeeef32b9c950ywan    void* const stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,
7552233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             MAP_ANON | MAP_PRIVATE, -1, 0);
7553233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED);
7554233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7555233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Maximum stack alignment in bytes:  For a downward-growing stack, this
7556233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // amount is subtracted from size of the stack space to get an address
7557233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // that is within the stack space and is aligned on all systems we care
7558233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // about.  As far as I know there is no ABI with stack alignment greater
7559233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // than 64.  We assume stack and stack_size already have alignment of
7560233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // kMaxStackAlignment.
7561233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const size_t kMaxStackAlignment = 64;
7562233d2500723e5594f3e7c70896ffeeef32b9c950ywan    void* const stack_top =
7563233d2500723e5594f3e7c70896ffeeef32b9c950ywan        static_cast<char*>(stack) +
7564233d2500723e5594f3e7c70896ffeeef32b9c950ywan            (stack_grows_down ? stack_size - kMaxStackAlignment : 0);
7565233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_DEATH_TEST_CHECK_(stack_size > kMaxStackAlignment &&
7566233d2500723e5594f3e7c70896ffeeef32b9c950ywan        reinterpret_cast<intptr_t>(stack_top) % kMaxStackAlignment == 0);
7567233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7568233d2500723e5594f3e7c70896ffeeef32b9c950ywan    child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args);
7569233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7570233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1);
7571233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7572233d2500723e5594f3e7c70896ffeeef32b9c950ywan#   else
7573233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool use_fork = true;
7574233d2500723e5594f3e7c70896ffeeef32b9c950ywan#   endif  // GTEST_HAS_CLONE
7575233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7576233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (use_fork && (child_pid = fork()) == 0) {
7577233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ExecDeathTestChildMain(&args);
7578233d2500723e5594f3e7c70896ffeeef32b9c950ywan      _exit(0);
7579233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7580233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  endif  // GTEST_OS_QNX
7581233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  if GTEST_OS_LINUX
7582233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(
7583233d2500723e5594f3e7c70896ffeeef32b9c950ywan      sigaction(SIGPROF, &saved_sigprof_action, NULL));
7584233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  endif  // GTEST_OS_LINUX
7585233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7586233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(child_pid != -1);
7587233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return child_pid;
7588233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7589233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7590233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The AssumeRole process for a fork-and-exec death test.  It re-executes the
7591233d2500723e5594f3e7c70896ffeeef32b9c950ywan// main program from the beginning, setting the --gtest_filter
7592233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and --gtest_internal_run_death_test flags to cause only the current
7593233d2500723e5594f3e7c70896ffeeef32b9c950ywan// death test to be re-run.
7594233d2500723e5594f3e7c70896ffeeef32b9c950ywanDeathTest::TestRole ExecDeathTest::AssumeRole() {
7595233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const UnitTestImpl* const impl = GetUnitTestImpl();
7596233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const InternalRunDeathTestFlag* const flag =
7597233d2500723e5594f3e7c70896ffeeef32b9c950ywan      impl->internal_run_death_test_flag();
7598233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const TestInfo* const info = impl->current_test_info();
7599233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int death_test_index = info->result()->death_test_count();
7600233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7601233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (flag != NULL) {
7602233d2500723e5594f3e7c70896ffeeef32b9c950ywan    set_write_fd(flag->write_fd());
7603233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return EXECUTE_TEST;
7604233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7605233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7606233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int pipe_fd[2];
7607233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);
7608233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Clear the close-on-exec flag on the write end of the pipe, lest
7609233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it be closed when the child process does an exec:
7610233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1);
7611233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7612233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string filter_flag =
7613233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::string("--") + GTEST_FLAG_PREFIX_ + kFilterFlag + "="
7614233d2500723e5594f3e7c70896ffeeef32b9c950ywan      + info->test_case_name() + "." + info->name();
7615233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string internal_flag =
7616233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + "="
7617233d2500723e5594f3e7c70896ffeeef32b9c950ywan      + file_ + "|" + StreamableToString(line_) + "|"
7618233d2500723e5594f3e7c70896ffeeef32b9c950ywan      + StreamableToString(death_test_index) + "|"
7619233d2500723e5594f3e7c70896ffeeef32b9c950ywan      + StreamableToString(pipe_fd[1]);
7620233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Arguments args;
7621233d2500723e5594f3e7c70896ffeeef32b9c950ywan  args.AddArguments(GetArgvsForDeathTestChildProcess());
7622233d2500723e5594f3e7c70896ffeeef32b9c950ywan  args.AddArgument(filter_flag.c_str());
7623233d2500723e5594f3e7c70896ffeeef32b9c950ywan  args.AddArgument(internal_flag.c_str());
7624233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7625233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DeathTest::set_last_death_test_message("");
7626233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7627233d2500723e5594f3e7c70896ffeeef32b9c950ywan  CaptureStderr();
7628233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // See the comment in NoExecDeathTest::AssumeRole for why the next line
7629233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // is necessary.
7630233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FlushInfoLog();
7631233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7632233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]);
7633233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));
7634233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_child_pid(child_pid);
7635233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_read_fd(pipe_fd[0]);
7636233d2500723e5594f3e7c70896ffeeef32b9c950ywan  set_spawned(true);
7637233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return OVERSEE_TEST;
7638233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7639233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7640233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // !GTEST_OS_WINDOWS
7641233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7642233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Creates a concrete DeathTest-derived class that depends on the
7643233d2500723e5594f3e7c70896ffeeef32b9c950ywan// --gtest_death_test_style flag, and sets the pointer pointed to
7644233d2500723e5594f3e7c70896ffeeef32b9c950ywan// by the "test" argument to its address.  If the test should be
7645233d2500723e5594f3e7c70896ffeeef32b9c950ywan// skipped, sets that pointer to NULL.  Returns true, unless the
7646233d2500723e5594f3e7c70896ffeeef32b9c950ywan// flag is set to an invalid value.
7647233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool DefaultDeathTestFactory::Create(const char* statement, const RE* regex,
7648233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     const char* file, int line,
7649233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     DeathTest** test) {
7650233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UnitTestImpl* const impl = GetUnitTestImpl();
7651233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const InternalRunDeathTestFlag* const flag =
7652233d2500723e5594f3e7c70896ffeeef32b9c950ywan      impl->internal_run_death_test_flag();
7653233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int death_test_index = impl->current_test_info()
7654233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ->increment_death_test_count();
7655233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7656233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (flag != NULL) {
7657233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (death_test_index > flag->index()) {
7658233d2500723e5594f3e7c70896ffeeef32b9c950ywan      DeathTest::set_last_death_test_message(
7659233d2500723e5594f3e7c70896ffeeef32b9c950ywan          "Death test count (" + StreamableToString(death_test_index)
7660233d2500723e5594f3e7c70896ffeeef32b9c950ywan          + ") somehow exceeded expected maximum ("
7661233d2500723e5594f3e7c70896ffeeef32b9c950ywan          + StreamableToString(flag->index()) + ")");
7662233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return false;
7663233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
7664233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7665233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (!(flag->file() == file && flag->line() == line &&
7666233d2500723e5594f3e7c70896ffeeef32b9c950ywan          flag->index() == death_test_index)) {
7667233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *test = NULL;
7668233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return true;
7669233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
7670233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7671233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7672233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
7673233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7674233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(death_test_style) == "threadsafe" ||
7675233d2500723e5594f3e7c70896ffeeef32b9c950ywan      GTEST_FLAG(death_test_style) == "fast") {
7676233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *test = new WindowsDeathTest(statement, regex, file, line);
7677233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7678233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7679233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
7680233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7681233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(death_test_style) == "threadsafe") {
7682233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *test = new ExecDeathTest(statement, regex, file, line);
7683233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else if (GTEST_FLAG(death_test_style) == "fast") {
7684233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *test = new NoExecDeathTest(statement, regex);
7685233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7686233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7687233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS
7688233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7689233d2500723e5594f3e7c70896ffeeef32b9c950ywan  else {  // NOLINT - this is more readable than unbalanced brackets inside #if.
7690233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTest::set_last_death_test_message(
7691233d2500723e5594f3e7c70896ffeeef32b9c950ywan        "Unknown death test style \"" + GTEST_FLAG(death_test_style)
7692233d2500723e5594f3e7c70896ffeeef32b9c950ywan        + "\" encountered");
7693233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
7694233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7695233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7696233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return true;
7697233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7698233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7699233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Splits a given string on a given delimiter, populating a given
7700233d2500723e5594f3e7c70896ffeeef32b9c950ywan// vector with the fields.  GTEST_HAS_DEATH_TEST implies that we have
7701233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ::std::string, so we can use it here.
7702233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void SplitString(const ::std::string& str, char delimiter,
7703233d2500723e5594f3e7c70896ffeeef32b9c950ywan                        ::std::vector< ::std::string>* dest) {
7704233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::vector< ::std::string> parsed;
7705233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::string::size_type pos = 0;
7706233d2500723e5594f3e7c70896ffeeef32b9c950ywan  while (::testing::internal::AlwaysTrue()) {
7707233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const ::std::string::size_type colon = str.find(delimiter, pos);
7708233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (colon == ::std::string::npos) {
7709233d2500723e5594f3e7c70896ffeeef32b9c950ywan      parsed.push_back(str.substr(pos));
7710233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
7711233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
7712233d2500723e5594f3e7c70896ffeeef32b9c950ywan      parsed.push_back(str.substr(pos, colon - pos));
7713233d2500723e5594f3e7c70896ffeeef32b9c950ywan      pos = colon + 1;
7714233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
7715233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7716233d2500723e5594f3e7c70896ffeeef32b9c950ywan  dest->swap(parsed);
7717233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7718233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7719233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
7720233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Recreates the pipe and event handles from the provided parameters,
7721233d2500723e5594f3e7c70896ffeeef32b9c950ywan// signals the event, and returns a file descriptor wrapped around the pipe
7722233d2500723e5594f3e7c70896ffeeef32b9c950ywan// handle. This function is called in the child process only.
7723233d2500723e5594f3e7c70896ffeeef32b9c950ywanint GetStatusFileDescriptor(unsigned int parent_process_id,
7724233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            size_t write_handle_as_size_t,
7725233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            size_t event_handle_as_size_t) {
7726233d2500723e5594f3e7c70896ffeeef32b9c950ywan  AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE,
7727233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                   FALSE,  // Non-inheritable.
7728233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                   parent_process_id));
7729233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) {
7730233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort("Unable to open parent process " +
7731233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   StreamableToString(parent_process_id));
7732233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7733233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7734233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TODO(vladl@google.com): Replace the following check with a
7735233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // compile-time assertion when available.
7736233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t));
7737233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7738233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const HANDLE write_handle =
7739233d2500723e5594f3e7c70896ffeeef32b9c950ywan      reinterpret_cast<HANDLE>(write_handle_as_size_t);
7740233d2500723e5594f3e7c70896ffeeef32b9c950ywan  HANDLE dup_write_handle;
7741233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7742233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The newly initialized handle is accessible only in in the parent
7743233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // process. To obtain one accessible within the child, we need to use
7744233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // DuplicateHandle.
7745233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!::DuplicateHandle(parent_process_handle.Get(), write_handle,
7746233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         ::GetCurrentProcess(), &dup_write_handle,
7747233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         0x0,    // Requested privileges ignored since
7748233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                 // DUPLICATE_SAME_ACCESS is used.
7749233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         FALSE,  // Request non-inheritable handler.
7750233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         DUPLICATE_SAME_ACCESS)) {
7751233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort("Unable to duplicate the pipe handle " +
7752233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   StreamableToString(write_handle_as_size_t) +
7753233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   " from the parent process " +
7754233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   StreamableToString(parent_process_id));
7755233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7756233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7757233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const HANDLE event_handle = reinterpret_cast<HANDLE>(event_handle_as_size_t);
7758233d2500723e5594f3e7c70896ffeeef32b9c950ywan  HANDLE dup_event_handle;
7759233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7760233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!::DuplicateHandle(parent_process_handle.Get(), event_handle,
7761233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         ::GetCurrentProcess(), &dup_event_handle,
7762233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         0x0,
7763233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         FALSE,
7764233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         DUPLICATE_SAME_ACCESS)) {
7765233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort("Unable to duplicate the event handle " +
7766233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   StreamableToString(event_handle_as_size_t) +
7767233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   " from the parent process " +
7768233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   StreamableToString(parent_process_id));
7769233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7770233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7771233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int write_fd =
7772233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ::_open_osfhandle(reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND);
7773233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (write_fd == -1) {
7774233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort("Unable to convert pipe handle " +
7775233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   StreamableToString(write_handle_as_size_t) +
7776233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   " to a file descriptor");
7777233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7778233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7779233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Signals the parent that the write end of the pipe has been acquired
7780233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // so the parent can release its own write end.
7781233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::SetEvent(dup_event_handle);
7782233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7783233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return write_fd;
7784233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7785233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS
7786233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7787233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns a newly created InternalRunDeathTestFlag object with fields
7788233d2500723e5594f3e7c70896ffeeef32b9c950ywan// initialized from the GTEST_FLAG(internal_run_death_test) flag if
7789233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the flag is specified; otherwise returns NULL.
7790233d2500723e5594f3e7c70896ffeeef32b9c950ywanInternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {
7791233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (GTEST_FLAG(internal_run_death_test) == "") return NULL;
7792233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7793233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we
7794233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // can use it here.
7795233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int line = -1;
7796233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int index = -1;
7797233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::vector< ::std::string> fields;
7798233d2500723e5594f3e7c70896ffeeef32b9c950ywan  SplitString(GTEST_FLAG(internal_run_death_test).c_str(), '|', &fields);
7799233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int write_fd = -1;
7800233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7801233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
7802233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7803233d2500723e5594f3e7c70896ffeeef32b9c950ywan  unsigned int parent_process_id = 0;
7804233d2500723e5594f3e7c70896ffeeef32b9c950ywan  size_t write_handle_as_size_t = 0;
7805233d2500723e5594f3e7c70896ffeeef32b9c950ywan  size_t event_handle_as_size_t = 0;
7806233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7807233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (fields.size() != 6
7808233d2500723e5594f3e7c70896ffeeef32b9c950ywan      || !ParseNaturalNumber(fields[1], &line)
7809233d2500723e5594f3e7c70896ffeeef32b9c950ywan      || !ParseNaturalNumber(fields[2], &index)
7810233d2500723e5594f3e7c70896ffeeef32b9c950ywan      || !ParseNaturalNumber(fields[3], &parent_process_id)
7811233d2500723e5594f3e7c70896ffeeef32b9c950ywan      || !ParseNaturalNumber(fields[4], &write_handle_as_size_t)
7812233d2500723e5594f3e7c70896ffeeef32b9c950ywan      || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) {
7813233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort("Bad --gtest_internal_run_death_test flag: " +
7814233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   GTEST_FLAG(internal_run_death_test));
7815233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7816233d2500723e5594f3e7c70896ffeeef32b9c950ywan  write_fd = GetStatusFileDescriptor(parent_process_id,
7817233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     write_handle_as_size_t,
7818233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                     event_handle_as_size_t);
7819233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
7820233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7821233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (fields.size() != 4
7822233d2500723e5594f3e7c70896ffeeef32b9c950ywan      || !ParseNaturalNumber(fields[1], &line)
7823233d2500723e5594f3e7c70896ffeeef32b9c950ywan      || !ParseNaturalNumber(fields[2], &index)
7824233d2500723e5594f3e7c70896ffeeef32b9c950ywan      || !ParseNaturalNumber(fields[3], &write_fd)) {
7825233d2500723e5594f3e7c70896ffeeef32b9c950ywan    DeathTestAbort("Bad --gtest_internal_run_death_test flag: "
7826233d2500723e5594f3e7c70896ffeeef32b9c950ywan        + GTEST_FLAG(internal_run_death_test));
7827233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7828233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7829233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS
7830233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7831233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return new InternalRunDeathTestFlag(fields[0], line, index, write_fd);
7832233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7833233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7834233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
7835233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7836233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
7837233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7838233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
7839233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2008, Google Inc.
7840233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
7841233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7842233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
7843233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
7844233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
7845233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7846233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
7847233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
7848233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
7849233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
7850233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
7851233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
7852233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
7853233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
7854233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
7855233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7856233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7857233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7858233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7859233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7860233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7861233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
7862233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
7863233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7864233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7865233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
7866233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7867233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
7868233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Authors: keith.ray@gmail.com (Keith Ray)
7869233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7870233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7871233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stdlib.h>
7872233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7873233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE
7874233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <windows.h>
7875233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_WINDOWS
7876233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <direct.h>
7877233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <io.h>
7878233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_SYMBIAN
7879233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Symbian OpenC has PATH_MAX in sys/syslimits.h
7880233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/syslimits.h>
7881233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
7882233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <limits.h>
7883233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <climits>  // Some Linux distributions define PATH_MAX here.
7884233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE
7885233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7886233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
7887233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_PATH_MAX_ _MAX_PATH
7888233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif defined(PATH_MAX)
7889233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_PATH_MAX_ PATH_MAX
7890233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif defined(_XOPEN_PATH_MAX)
7891233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_PATH_MAX_ _XOPEN_PATH_MAX
7892233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
7893233d2500723e5594f3e7c70896ffeeef32b9c950ywan# define GTEST_PATH_MAX_ _POSIX_PATH_MAX
7894233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
7895233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7896233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7897233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
7898233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
7899233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7900233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
7901233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On Windows, '\\' is the standard path separator, but many tools and the
7902233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Windows API also accept '/' as an alternate path separator. Unless otherwise
7903233d2500723e5594f3e7c70896ffeeef32b9c950ywan// noted, a file path can contain either kind of path separators, or a mixture
7904233d2500723e5594f3e7c70896ffeeef32b9c950ywan// of them.
7905233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kPathSeparator = '\\';
7906233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kAlternatePathSeparator = '/';
7907233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kPathSeparatorString[] = "\\";
7908233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kAlternatePathSeparatorString[] = "/";
7909233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS_MOBILE
7910233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Windows CE doesn't have a current directory. You should not use
7911233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the current directory in tests on Windows CE, but this at least
7912233d2500723e5594f3e7c70896ffeeef32b9c950ywan// provides a reasonable fallback.
7913233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kCurrentDirectoryString[] = "\\";
7914233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Windows CE doesn't define INVALID_FILE_ATTRIBUTES
7915233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst DWORD kInvalidFileAttributes = 0xffffffff;
7916233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
7917233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kCurrentDirectoryString[] = ".\\";
7918233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS_MOBILE
7919233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
7920233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kPathSeparator = '/';
7921233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kPathSeparatorString[] = "/";
7922233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kCurrentDirectoryString[] = "./";
7923233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS
7924233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7925233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns whether the given character is a valid path separator.
7926233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic bool IsPathSeparator(char c) {
7927233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_ALT_PATH_SEP_
7928233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (c == kPathSeparator) || (c == kAlternatePathSeparator);
7929233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
7930233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return c == kPathSeparator;
7931233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
7932233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7933233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7934233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the current working directory, or "" if unsuccessful.
7935233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath FilePath::GetCurrentDir() {
7936233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE
7937233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Windows CE doesn't have a current directory, so we just return
7938233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // something reasonable.
7939233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return FilePath(kCurrentDirectoryString);
7940233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_WINDOWS
7941233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
7942233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
7943233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
7944233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
7945233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return FilePath(getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
7946233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE
7947233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7948233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7949233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns a copy of the FilePath with the case-insensitive extension removed.
7950233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
7951233d2500723e5594f3e7c70896ffeeef32b9c950ywan// FilePath("dir/file"). If a case-insensitive extension is not
7952233d2500723e5594f3e7c70896ffeeef32b9c950ywan// found, returns a copy of the original FilePath.
7953233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath FilePath::RemoveExtension(const char* extension) const {
7954233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string dot_extension = std::string(".") + extension;
7955233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (String::EndsWithCaseInsensitive(pathname_, dot_extension)) {
7956233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return FilePath(pathname_.substr(
7957233d2500723e5594f3e7c70896ffeeef32b9c950ywan        0, pathname_.length() - dot_extension.length()));
7958233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7959233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return *this;
7960233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7961233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7962233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns a pointer to the last occurence of a valid path separator in
7963233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the FilePath. On Windows, for example, both '/' and '\' are valid path
7964233d2500723e5594f3e7c70896ffeeef32b9c950ywan// separators. Returns NULL if no path separator was found.
7965233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* FilePath::FindLastPathSeparator() const {
7966233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const last_sep = strrchr(c_str(), kPathSeparator);
7967233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_ALT_PATH_SEP_
7968233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator);
7969233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Comparing two pointers of which only one is NULL is undefined.
7970233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (last_alt_sep != NULL &&
7971233d2500723e5594f3e7c70896ffeeef32b9c950ywan      (last_sep == NULL || last_alt_sep > last_sep)) {
7972233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return last_alt_sep;
7973233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
7974233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
7975233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return last_sep;
7976233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7977233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7978233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns a copy of the FilePath with the directory part removed.
7979233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Example: FilePath("path/to/file").RemoveDirectoryName() returns
7980233d2500723e5594f3e7c70896ffeeef32b9c950ywan// FilePath("file"). If there is no directory part ("just_a_file"), it returns
7981233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the FilePath unmodified. If there is no file part ("just_a_dir/") it
7982233d2500723e5594f3e7c70896ffeeef32b9c950ywan// returns an empty FilePath ("").
7983233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On Windows platform, '\' is the path separator, otherwise it is '/'.
7984233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath FilePath::RemoveDirectoryName() const {
7985233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const last_sep = FindLastPathSeparator();
7986233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return last_sep ? FilePath(last_sep + 1) : *this;
7987233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
7988233d2500723e5594f3e7c70896ffeeef32b9c950ywan
7989233d2500723e5594f3e7c70896ffeeef32b9c950ywan// RemoveFileName returns the directory path with the filename removed.
7990233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
7991233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If the FilePath is "a_file" or "/a_file", RemoveFileName returns
7992233d2500723e5594f3e7c70896ffeeef32b9c950ywan// FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
7993233d2500723e5594f3e7c70896ffeeef32b9c950ywan// not have a file, like "just/a/dir/", it returns the FilePath unmodified.
7994233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On Windows platform, '\' is the path separator, otherwise it is '/'.
7995233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath FilePath::RemoveFileName() const {
7996233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const last_sep = FindLastPathSeparator();
7997233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string dir;
7998233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (last_sep) {
7999233d2500723e5594f3e7c70896ffeeef32b9c950ywan    dir = std::string(c_str(), last_sep + 1 - c_str());
8000233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
8001233d2500723e5594f3e7c70896ffeeef32b9c950ywan    dir = kCurrentDirectoryString;
8002233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8003233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return FilePath(dir);
8004233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8005233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8006233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helper functions for naming files in a directory for xml output.
8007233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8008233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Given directory = "dir", base_name = "test", number = 0,
8009233d2500723e5594f3e7c70896ffeeef32b9c950ywan// extension = "xml", returns "dir/test.xml". If number is greater
8010233d2500723e5594f3e7c70896ffeeef32b9c950ywan// than zero (e.g., 12), returns "dir/test_12.xml".
8011233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On Windows platform, uses \ as the separator rather than /.
8012233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath FilePath::MakeFileName(const FilePath& directory,
8013233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                const FilePath& base_name,
8014233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                int number,
8015233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                const char* extension) {
8016233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string file;
8017233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (number == 0) {
8018233d2500723e5594f3e7c70896ffeeef32b9c950ywan    file = base_name.string() + "." + extension;
8019233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
8020233d2500723e5594f3e7c70896ffeeef32b9c950ywan    file = base_name.string() + "_" + StreamableToString(number)
8021233d2500723e5594f3e7c70896ffeeef32b9c950ywan        + "." + extension;
8022233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8023233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ConcatPaths(directory, FilePath(file));
8024233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8025233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8026233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml".
8027233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On Windows, uses \ as the separator rather than /.
8028233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath FilePath::ConcatPaths(const FilePath& directory,
8029233d2500723e5594f3e7c70896ffeeef32b9c950ywan                               const FilePath& relative_path) {
8030233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (directory.IsEmpty())
8031233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return relative_path;
8032233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const FilePath dir(directory.RemoveTrailingPathSeparator());
8033233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return FilePath(dir.string() + kPathSeparator + relative_path.string());
8034233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8035233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8036233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true if pathname describes something findable in the file-system,
8037233d2500723e5594f3e7c70896ffeeef32b9c950ywan// either a file, directory, or whatever.
8038233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool FilePath::FileOrDirectoryExists() const {
8039233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE
8040233d2500723e5594f3e7c70896ffeeef32b9c950ywan  LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
8041233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const DWORD attributes = GetFileAttributes(unicode);
8042233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete [] unicode;
8043233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return attributes != kInvalidFileAttributes;
8044233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8045233d2500723e5594f3e7c70896ffeeef32b9c950ywan  posix::StatStruct file_stat;
8046233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return posix::Stat(pathname_.c_str(), &file_stat) == 0;
8047233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE
8048233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8049233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8050233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true if pathname describes a directory in the file-system
8051233d2500723e5594f3e7c70896ffeeef32b9c950ywan// that exists.
8052233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool FilePath::DirectoryExists() const {
8053233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool result = false;
8054233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
8055233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Don't strip off trailing separator if path is a root directory on
8056233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Windows (like "C:\\").
8057233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const FilePath& path(IsRootDirectory() ? *this :
8058233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                           RemoveTrailingPathSeparator());
8059233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8060233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const FilePath& path(*this);
8061233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
8062233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8063233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE
8064233d2500723e5594f3e7c70896ffeeef32b9c950ywan  LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
8065233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const DWORD attributes = GetFileAttributes(unicode);
8066233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete [] unicode;
8067233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if ((attributes != kInvalidFileAttributes) &&
8068233d2500723e5594f3e7c70896ffeeef32b9c950ywan      (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
8069233d2500723e5594f3e7c70896ffeeef32b9c950ywan    result = true;
8070233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8071233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8072233d2500723e5594f3e7c70896ffeeef32b9c950ywan  posix::StatStruct file_stat;
8073233d2500723e5594f3e7c70896ffeeef32b9c950ywan  result = posix::Stat(path.c_str(), &file_stat) == 0 &&
8074233d2500723e5594f3e7c70896ffeeef32b9c950ywan      posix::IsDir(file_stat);
8075233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE
8076233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8077233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result;
8078233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8079233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8080233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true if pathname describes a root directory. (Windows has one
8081233d2500723e5594f3e7c70896ffeeef32b9c950ywan// root directory per disk drive.)
8082233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool FilePath::IsRootDirectory() const {
8083233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
8084233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TODO(wan@google.com): on Windows a network share like
8085233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // \\server\share can be a root directory, although it cannot be the
8086233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // current directory.  Handle this properly.
8087233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return pathname_.length() == 3 && IsAbsolutePath();
8088233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8089233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]);
8090233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
8091233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8092233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8093233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true if pathname describes an absolute path.
8094233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool FilePath::IsAbsolutePath() const {
8095233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const name = pathname_.c_str();
8096233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS
8097233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return pathname_.length() >= 3 &&
8098233d2500723e5594f3e7c70896ffeeef32b9c950ywan     ((name[0] >= 'a' && name[0] <= 'z') ||
8099233d2500723e5594f3e7c70896ffeeef32b9c950ywan      (name[0] >= 'A' && name[0] <= 'Z')) &&
8100233d2500723e5594f3e7c70896ffeeef32b9c950ywan     name[1] == ':' &&
8101233d2500723e5594f3e7c70896ffeeef32b9c950ywan     IsPathSeparator(name[2]);
8102233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8103233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsPathSeparator(name[0]);
8104233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
8105233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8106233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8107233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns a pathname for a file that does not currently exist. The pathname
8108233d2500723e5594f3e7c70896ffeeef32b9c950ywan// will be directory/base_name.extension or
8109233d2500723e5594f3e7c70896ffeeef32b9c950ywan// directory/base_name_<number>.extension if directory/base_name.extension
8110233d2500723e5594f3e7c70896ffeeef32b9c950ywan// already exists. The number will be incremented until a pathname is found
8111233d2500723e5594f3e7c70896ffeeef32b9c950ywan// that does not already exist.
8112233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
8113233d2500723e5594f3e7c70896ffeeef32b9c950ywan// There could be a race condition if two or more processes are calling this
8114233d2500723e5594f3e7c70896ffeeef32b9c950ywan// function at the same time -- they could both pick the same filename.
8115233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath FilePath::GenerateUniqueFileName(const FilePath& directory,
8116233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          const FilePath& base_name,
8117233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                          const char* extension) {
8118233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FilePath full_pathname;
8119233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int number = 0;
8120233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do {
8121233d2500723e5594f3e7c70896ffeeef32b9c950ywan    full_pathname.Set(MakeFileName(directory, base_name, number++, extension));
8122233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (full_pathname.FileOrDirectoryExists());
8123233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return full_pathname;
8124233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8125233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8126233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true if FilePath ends with a path separator, which indicates that
8127233d2500723e5594f3e7c70896ffeeef32b9c950ywan// it is intended to represent a directory. Returns false otherwise.
8128233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This does NOT check that a directory (or file) actually exists.
8129233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool FilePath::IsDirectory() const {
8130233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return !pathname_.empty() &&
8131233d2500723e5594f3e7c70896ffeeef32b9c950ywan         IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]);
8132233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8133233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8134233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Create directories so that path exists. Returns true if successful or if
8135233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the directories already exist; returns false if unable to create directories
8136233d2500723e5594f3e7c70896ffeeef32b9c950ywan// for any reason.
8137233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool FilePath::CreateDirectoriesRecursively() const {
8138233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!this->IsDirectory()) {
8139233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
8140233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8141233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8142233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (pathname_.length() == 0 || this->DirectoryExists()) {
8143233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return true;
8144233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8145233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8146233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());
8147233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return parent.CreateDirectoriesRecursively() && this->CreateFolder();
8148233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8149233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8150233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Create the directory so that path exists. Returns true if successful or
8151233d2500723e5594f3e7c70896ffeeef32b9c950ywan// if the directory already exists; returns false if unable to create the
8152233d2500723e5594f3e7c70896ffeeef32b9c950ywan// directory for any reason, including if the parent directory does not
8153233d2500723e5594f3e7c70896ffeeef32b9c950ywan// exist. Not named "CreateDirectory" because that's a macro on Windows.
8154233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool FilePath::CreateFolder() const {
8155233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE
8156233d2500723e5594f3e7c70896ffeeef32b9c950ywan  FilePath removed_sep(this->RemoveTrailingPathSeparator());
8157233d2500723e5594f3e7c70896ffeeef32b9c950ywan  LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
8158233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int result = CreateDirectory(unicode, NULL) ? 0 : -1;
8159233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete [] unicode;
8160233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_WINDOWS
8161233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int result = _mkdir(pathname_.c_str());
8162233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8163233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int result = mkdir(pathname_.c_str(), 0777);
8164233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE
8165233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8166233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (result == -1) {
8167233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return this->DirectoryExists();  // An error is OK if the directory exists.
8168233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8169233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return true;  // No error.
8170233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8171233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8172233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If input name has a trailing separator character, remove it and return the
8173233d2500723e5594f3e7c70896ffeeef32b9c950ywan// name, otherwise return the name string unmodified.
8174233d2500723e5594f3e7c70896ffeeef32b9c950ywan// On Windows platform, uses \ as the separator, other platforms use /.
8175233d2500723e5594f3e7c70896ffeeef32b9c950ywanFilePath FilePath::RemoveTrailingPathSeparator() const {
8176233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsDirectory()
8177233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ? FilePath(pathname_.substr(0, pathname_.length() - 1))
8178233d2500723e5594f3e7c70896ffeeef32b9c950ywan      : *this;
8179233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8180233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8181233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Removes any redundant separators that might be in the pathname.
8182233d2500723e5594f3e7c70896ffeeef32b9c950ywan// For example, "bar///foo" becomes "bar/foo". Does not eliminate other
8183233d2500723e5594f3e7c70896ffeeef32b9c950ywan// redundancies that might be in a pathname involving "." or "..".
8184233d2500723e5594f3e7c70896ffeeef32b9c950ywan// TODO(wan@google.com): handle Windows network shares (e.g. \\server\share).
8185233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid FilePath::Normalize() {
8186233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (pathname_.c_str() == NULL) {
8187233d2500723e5594f3e7c70896ffeeef32b9c950ywan    pathname_ = "";
8188233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
8189233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8190233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* src = pathname_.c_str();
8191233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* const dest = new char[pathname_.length() + 1];
8192233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* dest_ptr = dest;
8193233d2500723e5594f3e7c70896ffeeef32b9c950ywan  memset(dest_ptr, 0, pathname_.length() + 1);
8194233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8195233d2500723e5594f3e7c70896ffeeef32b9c950ywan  while (*src != '\0') {
8196233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *dest_ptr = *src;
8197233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (!IsPathSeparator(*src)) {
8198233d2500723e5594f3e7c70896ffeeef32b9c950ywan      src++;
8199233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
8200233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_ALT_PATH_SEP_
8201233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (*dest_ptr == kAlternatePathSeparator) {
8202233d2500723e5594f3e7c70896ffeeef32b9c950ywan        *dest_ptr = kPathSeparator;
8203233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
8204233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif
8205233d2500723e5594f3e7c70896ffeeef32b9c950ywan      while (IsPathSeparator(*src))
8206233d2500723e5594f3e7c70896ffeeef32b9c950ywan        src++;
8207233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
8208233d2500723e5594f3e7c70896ffeeef32b9c950ywan    dest_ptr++;
8209233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8210233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *dest_ptr = '\0';
8211233d2500723e5594f3e7c70896ffeeef32b9c950ywan  pathname_ = dest;
8212233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete[] dest;
8213233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8214233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8215233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
8216233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
8217233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2008, Google Inc.
8218233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
8219233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
8220233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
8221233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
8222233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
8223233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
8224233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
8225233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
8226233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
8227233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
8228233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
8229233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
8230233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
8231233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
8232233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
8233233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
8234233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
8235233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8236233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8237233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8238233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8239233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8240233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8241233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8242233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8243233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8244233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8245233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
8246233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: wan@google.com (Zhanyong Wan)
8247233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8248233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8249233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <limits.h>
8250233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stdlib.h>
8251233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stdio.h>
8252233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <string.h>
8253233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8254233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE
8255233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <windows.h>  // For TerminateProcess()
8256233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_WINDOWS
8257233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <io.h>
8258233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/stat.h>
8259233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8260233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <unistd.h>
8261233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE
8262233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8263233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_MAC
8264233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <mach/mach_init.h>
8265233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <mach/task.h>
8266233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <mach/vm_map.h>
8267233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_MAC
8268233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8269233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_QNX
8270233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <devctl.h>
8271233d2500723e5594f3e7c70896ffeeef32b9c950ywan# include <sys/procfs.h>
8272233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_QNX
8273233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8274233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8275233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Indicates that this translation unit is part of Google Test's
8276233d2500723e5594f3e7c70896ffeeef32b9c950ywan// implementation.  It must come before gtest-internal-inl.h is
8277233d2500723e5594f3e7c70896ffeeef32b9c950ywan// included, or there will be a compiler error.  This trick is to
8278233d2500723e5594f3e7c70896ffeeef32b9c950ywan// prevent a user from accidentally including gtest-internal-inl.h in
8279233d2500723e5594f3e7c70896ffeeef32b9c950ywan// his code.
8280233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_IMPLEMENTATION_ 1
8281233d2500723e5594f3e7c70896ffeeef32b9c950ywan#undef GTEST_IMPLEMENTATION_
8282233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8283233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
8284233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
8285233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8286233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if defined(_MSC_VER) || defined(__BORLANDC__)
8287233d2500723e5594f3e7c70896ffeeef32b9c950ywan// MSVC and C++Builder do not provide a definition of STDERR_FILENO.
8288233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst int kStdOutFileno = 1;
8289233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst int kStdErrFileno = 2;
8290233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8291233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst int kStdOutFileno = STDOUT_FILENO;
8292233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst int kStdErrFileno = STDERR_FILENO;
8293233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // _MSC_VER
8294233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8295233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_MAC
8296233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8297233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the number of threads running in the process, or 0 to indicate that
8298233d2500723e5594f3e7c70896ffeeef32b9c950ywan// we cannot detect it.
8299233d2500723e5594f3e7c70896ffeeef32b9c950ywansize_t GetThreadCount() {
8300233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const task_t task = mach_task_self();
8301233d2500723e5594f3e7c70896ffeeef32b9c950ywan  mach_msg_type_number_t thread_count;
8302233d2500723e5594f3e7c70896ffeeef32b9c950ywan  thread_act_array_t thread_list;
8303233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const kern_return_t status = task_threads(task, &thread_list, &thread_count);
8304233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (status == KERN_SUCCESS) {
8305233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // task_threads allocates resources in thread_list and we need to free them
8306233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // to avoid leaks.
8307233d2500723e5594f3e7c70896ffeeef32b9c950ywan    vm_deallocate(task,
8308233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  reinterpret_cast<vm_address_t>(thread_list),
8309233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  sizeof(thread_t) * thread_count);
8310233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return static_cast<size_t>(thread_count);
8311233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
8312233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return 0;
8313233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8314233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8315233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8316233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_OS_QNX
8317233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8318233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the number of threads running in the process, or 0 to indicate that
8319233d2500723e5594f3e7c70896ffeeef32b9c950ywan// we cannot detect it.
8320233d2500723e5594f3e7c70896ffeeef32b9c950ywansize_t GetThreadCount() {
8321233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int fd = open("/proc/self/as", O_RDONLY);
8322233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (fd < 0) {
8323233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return 0;
8324233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8325233d2500723e5594f3e7c70896ffeeef32b9c950ywan  procfs_info process_info;
8326233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int status =
8327233d2500723e5594f3e7c70896ffeeef32b9c950ywan      devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), NULL);
8328233d2500723e5594f3e7c70896ffeeef32b9c950ywan  close(fd);
8329233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (status == EOK) {
8330233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return static_cast<size_t>(process_info.num_threads);
8331233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
8332233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return 0;
8333233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8334233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8335233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8336233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8337233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8338233d2500723e5594f3e7c70896ffeeef32b9c950ywansize_t GetThreadCount() {
8339233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // There's no portable way to detect the number of threads, so we just
8340233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // return 0 to indicate that we cannot detect it.
8341233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return 0;
8342233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8343233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8344233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_MAC
8345233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8346233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_USES_POSIX_RE
8347233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8348233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Implements RE.  Currently only needed for death tests.
8349233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8350233d2500723e5594f3e7c70896ffeeef32b9c950ywanRE::~RE() {
8351233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (is_valid_) {
8352233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // regfree'ing an invalid regex might crash because the content
8353233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // of the regex is undefined. Since the regex's are essentially
8354233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // the same, one cannot be valid (or invalid) without the other
8355233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // being so too.
8356233d2500723e5594f3e7c70896ffeeef32b9c950ywan    regfree(&partial_regex_);
8357233d2500723e5594f3e7c70896ffeeef32b9c950ywan    regfree(&full_regex_);
8358233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8359233d2500723e5594f3e7c70896ffeeef32b9c950ywan  free(const_cast<char*>(pattern_));
8360233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8361233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8362233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff regular expression re matches the entire str.
8363233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool RE::FullMatch(const char* str, const RE& re) {
8364233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!re.is_valid_) return false;
8365233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8366233d2500723e5594f3e7c70896ffeeef32b9c950ywan  regmatch_t match;
8367233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return regexec(&re.full_regex_, str, 1, &match, 0) == 0;
8368233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8369233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8370233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff regular expression re matches a substring of str
8371233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (including str itself).
8372233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool RE::PartialMatch(const char* str, const RE& re) {
8373233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!re.is_valid_) return false;
8374233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8375233d2500723e5594f3e7c70896ffeeef32b9c950ywan  regmatch_t match;
8376233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return regexec(&re.partial_regex_, str, 1, &match, 0) == 0;
8377233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8378233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8379233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Initializes an RE from its string representation.
8380233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid RE::Init(const char* regex) {
8381233d2500723e5594f3e7c70896ffeeef32b9c950ywan  pattern_ = posix::StrDup(regex);
8382233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8383233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Reserves enough bytes to hold the regular expression used for a
8384233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // full match.
8385233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t full_regex_len = strlen(regex) + 10;
8386233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* const full_pattern = new char[full_regex_len];
8387233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8388233d2500723e5594f3e7c70896ffeeef32b9c950ywan  snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
8389233d2500723e5594f3e7c70896ffeeef32b9c950ywan  is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0;
8390233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We want to call regcomp(&partial_regex_, ...) even if the
8391233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // previous expression returns false.  Otherwise partial_regex_ may
8392233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // not be properly initialized can may cause trouble when it's
8393233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // freed.
8394233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
8395233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Some implementation of POSIX regex (e.g. on at least some
8396233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // versions of Cygwin) doesn't accept the empty string as a valid
8397233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // regex.  We change it to an equivalent form "()" to be safe.
8398233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (is_valid_) {
8399233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* const partial_regex = (*regex == '\0') ? "()" : regex;
8400233d2500723e5594f3e7c70896ffeeef32b9c950ywan    is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0;
8401233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8402233d2500723e5594f3e7c70896ffeeef32b9c950ywan  EXPECT_TRUE(is_valid_)
8403233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "Regular expression \"" << regex
8404233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << "\" is not a valid POSIX Extended regular expression.";
8405233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8406233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete[] full_pattern;
8407233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8408233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8409233d2500723e5594f3e7c70896ffeeef32b9c950ywan#elif GTEST_USES_SIMPLE_RE
8410233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8411233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff ch appears anywhere in str (excluding the
8412233d2500723e5594f3e7c70896ffeeef32b9c950ywan// terminating '\0' character).
8413233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsInSet(char ch, const char* str) {
8414233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ch != '\0' && strchr(str, ch) != NULL;
8415233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8416233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8417233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff ch belongs to the given classification.  Unlike
8418233d2500723e5594f3e7c70896ffeeef32b9c950ywan// similar functions in <ctype.h>, these aren't affected by the
8419233d2500723e5594f3e7c70896ffeeef32b9c950ywan// current locale.
8420233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; }
8421233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsAsciiPunct(char ch) {
8422233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return IsInSet(ch, "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~");
8423233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8424233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsRepeat(char ch) { return IsInSet(ch, "?*+"); }
8425233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsAsciiWhiteSpace(char ch) { return IsInSet(ch, " \f\n\r\t\v"); }
8426233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsAsciiWordChar(char ch) {
8427233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') ||
8428233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ('0' <= ch && ch <= '9') || ch == '_';
8429233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8430233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8431233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff "\\c" is a supported escape sequence.
8432233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool IsValidEscape(char c) {
8433233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (IsAsciiPunct(c) || IsInSet(c, "dDfnrsStvwW"));
8434233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8435233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8436233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff the given atom (specified by escaped and pattern)
8437233d2500723e5594f3e7c70896ffeeef32b9c950ywan// matches ch.  The result is undefined if the atom is invalid.
8438233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool AtomMatchesChar(bool escaped, char pattern_char, char ch) {
8439233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (escaped) {  // "\\p" where p is pattern_char.
8440233d2500723e5594f3e7c70896ffeeef32b9c950ywan    switch (pattern_char) {
8441233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'd': return IsAsciiDigit(ch);
8442233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'D': return !IsAsciiDigit(ch);
8443233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'f': return ch == '\f';
8444233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'n': return ch == '\n';
8445233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'r': return ch == '\r';
8446233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 's': return IsAsciiWhiteSpace(ch);
8447233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'S': return !IsAsciiWhiteSpace(ch);
8448233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 't': return ch == '\t';
8449233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'v': return ch == '\v';
8450233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'w': return IsAsciiWordChar(ch);
8451233d2500723e5594f3e7c70896ffeeef32b9c950ywan      case 'W': return !IsAsciiWordChar(ch);
8452233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
8453233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return IsAsciiPunct(pattern_char) && pattern_char == ch;
8454233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8455233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8456233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (pattern_char == '.' && ch != '\n') || pattern_char == ch;
8457233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8458233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8459233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Helper function used by ValidateRegex() to format error messages.
8460233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string FormatRegexSyntaxError(const char* regex, int index) {
8461233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return (Message() << "Syntax error at index " << index
8462233d2500723e5594f3e7c70896ffeeef32b9c950ywan          << " in simple regular expression \"" << regex << "\": ").GetString();
8463233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8464233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8465233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Generates non-fatal failures and returns false if regex is invalid;
8466233d2500723e5594f3e7c70896ffeeef32b9c950ywan// otherwise returns true.
8467233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ValidateRegex(const char* regex) {
8468233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (regex == NULL) {
8469233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // TODO(wan@google.com): fix the source file location in the
8470233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // assertion failures to match where the regex is used in user
8471233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // code.
8472233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ADD_FAILURE() << "NULL is not a valid simple regular expression.";
8473233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
8474233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8475233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8476233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool is_valid = true;
8477233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8478233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // True iff ?, *, or + can follow the previous atom.
8479233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool prev_repeatable = false;
8480233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (int i = 0; regex[i]; i++) {
8481233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (regex[i] == '\\') {  // An escape sequence
8482233d2500723e5594f3e7c70896ffeeef32b9c950ywan      i++;
8483233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (regex[i] == '\0') {
8484233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
8485233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << "'\\' cannot appear at the end.";
8486233d2500723e5594f3e7c70896ffeeef32b9c950ywan        return false;
8487233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
8488233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8489233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (!IsValidEscape(regex[i])) {
8490233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
8491233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << "invalid escape sequence \"\\" << regex[i] << "\".";
8492233d2500723e5594f3e7c70896ffeeef32b9c950ywan        is_valid = false;
8493233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
8494233d2500723e5594f3e7c70896ffeeef32b9c950ywan      prev_repeatable = true;
8495233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {  // Not an escape sequence.
8496233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const char ch = regex[i];
8497233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8498233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (ch == '^' && i > 0) {
8499233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
8500233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << "'^' can only appear at the beginning.";
8501233d2500723e5594f3e7c70896ffeeef32b9c950ywan        is_valid = false;
8502233d2500723e5594f3e7c70896ffeeef32b9c950ywan      } else if (ch == '$' && regex[i + 1] != '\0') {
8503233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
8504233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << "'$' can only appear at the end.";
8505233d2500723e5594f3e7c70896ffeeef32b9c950ywan        is_valid = false;
8506233d2500723e5594f3e7c70896ffeeef32b9c950ywan      } else if (IsInSet(ch, "()[]{}|")) {
8507233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
8508233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << "'" << ch << "' is unsupported.";
8509233d2500723e5594f3e7c70896ffeeef32b9c950ywan        is_valid = false;
8510233d2500723e5594f3e7c70896ffeeef32b9c950ywan      } else if (IsRepeat(ch) && !prev_repeatable) {
8511233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
8512233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << "'" << ch << "' can only follow a repeatable token.";
8513233d2500723e5594f3e7c70896ffeeef32b9c950ywan        is_valid = false;
8514233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
8515233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8516233d2500723e5594f3e7c70896ffeeef32b9c950ywan      prev_repeatable = !IsInSet(ch, "^$?*+");
8517233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
8518233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8519233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8520233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return is_valid;
8521233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8522233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8523233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Matches a repeated regex atom followed by a valid simple regular
8524233d2500723e5594f3e7c70896ffeeef32b9c950ywan// expression.  The regex atom is defined as c if escaped is false,
8525233d2500723e5594f3e7c70896ffeeef32b9c950ywan// or \c otherwise.  repeat is the repetition meta character (?, *,
8526233d2500723e5594f3e7c70896ffeeef32b9c950ywan// or +).  The behavior is undefined if str contains too many
8527233d2500723e5594f3e7c70896ffeeef32b9c950ywan// characters to be indexable by size_t, in which case the test will
8528233d2500723e5594f3e7c70896ffeeef32b9c950ywan// probably time out anyway.  We are fine with this limitation as
8529233d2500723e5594f3e7c70896ffeeef32b9c950ywan// std::string has it too.
8530233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool MatchRepetitionAndRegexAtHead(
8531233d2500723e5594f3e7c70896ffeeef32b9c950ywan    bool escaped, char c, char repeat, const char* regex,
8532233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* str) {
8533233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t min_count = (repeat == '+') ? 1 : 0;
8534233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t max_count = (repeat == '?') ? 1 :
8535233d2500723e5594f3e7c70896ffeeef32b9c950ywan      static_cast<size_t>(-1) - 1;
8536233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We cannot call numeric_limits::max() as it conflicts with the
8537233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // max() macro on Windows.
8538233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8539233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i <= max_count; ++i) {
8540233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // We know that the atom matches each of the first i characters in str.
8541233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
8542233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // We have enough matches at the head, and the tail matches too.
8543233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Since we only care about *whether* the pattern matches str
8544233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // (as opposed to *how* it matches), there is no need to find a
8545233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // greedy match.
8546233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return true;
8547233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
8548233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i]))
8549233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return false;
8550233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8551233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return false;
8552233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8553233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8554233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff regex matches a prefix of str.  regex must be a
8555233d2500723e5594f3e7c70896ffeeef32b9c950ywan// valid simple regular expression and not start with "^", or the
8556233d2500723e5594f3e7c70896ffeeef32b9c950ywan// result is undefined.
8557233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool MatchRegexAtHead(const char* regex, const char* str) {
8558233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (*regex == '\0')  // An empty regex matches a prefix of anything.
8559233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return true;
8560233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8561233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // "$" only matches the end of a string.  Note that regex being
8562233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // valid guarantees that there's nothing after "$" in it.
8563233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (*regex == '$')
8564233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return *str == '\0';
8565233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8566233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Is the first thing in regex an escape sequence?
8567233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const bool escaped = *regex == '\\';
8568233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (escaped)
8569233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ++regex;
8570233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (IsRepeat(regex[1])) {
8571233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so
8572233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // here's an indirect recursion.  It terminates as the regex gets
8573233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // shorter in each recursion.
8574233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return MatchRepetitionAndRegexAtHead(
8575233d2500723e5594f3e7c70896ffeeef32b9c950ywan        escaped, regex[0], regex[1], regex + 2, str);
8576233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
8577233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // regex isn't empty, isn't "$", and doesn't start with a
8578233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // repetition.  We match the first atom of regex with the first
8579233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // character of str and recurse.
8580233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) &&
8581233d2500723e5594f3e7c70896ffeeef32b9c950ywan        MatchRegexAtHead(regex + 1, str + 1);
8582233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8583233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8584233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8585233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff regex matches any substring of str.  regex must be
8586233d2500723e5594f3e7c70896ffeeef32b9c950ywan// a valid simple regular expression, or the result is undefined.
8587233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
8588233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The algorithm is recursive, but the recursion depth doesn't exceed
8589233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the regex length, so we won't need to worry about running out of
8590233d2500723e5594f3e7c70896ffeeef32b9c950ywan// stack space normally.  In rare cases the time complexity can be
8591233d2500723e5594f3e7c70896ffeeef32b9c950ywan// exponential with respect to the regex length + the string length,
8592233d2500723e5594f3e7c70896ffeeef32b9c950ywan// but usually it's must faster (often close to linear).
8593233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool MatchRegexAnywhere(const char* regex, const char* str) {
8594233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (regex == NULL || str == NULL)
8595233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
8596233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8597233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (*regex == '^')
8598233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return MatchRegexAtHead(regex + 1, str);
8599233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8600233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // A successful match can be anywhere in str.
8601233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do {
8602233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (MatchRegexAtHead(regex, str))
8603233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return true;
8604233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (*str++ != '\0');
8605233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return false;
8606233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8607233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8608233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Implements the RE class.
8609233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8610233d2500723e5594f3e7c70896ffeeef32b9c950ywanRE::~RE() {
8611233d2500723e5594f3e7c70896ffeeef32b9c950ywan  free(const_cast<char*>(pattern_));
8612233d2500723e5594f3e7c70896ffeeef32b9c950ywan  free(const_cast<char*>(full_pattern_));
8613233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8614233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8615233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff regular expression re matches the entire str.
8616233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool RE::FullMatch(const char* str, const RE& re) {
8617233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str);
8618233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8619233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8620233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true iff regular expression re matches a substring of str
8621233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (including str itself).
8622233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool RE::PartialMatch(const char* str, const RE& re) {
8623233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str);
8624233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8625233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8626233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Initializes an RE from its string representation.
8627233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid RE::Init(const char* regex) {
8628233d2500723e5594f3e7c70896ffeeef32b9c950ywan  pattern_ = full_pattern_ = NULL;
8629233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (regex != NULL) {
8630233d2500723e5594f3e7c70896ffeeef32b9c950ywan    pattern_ = posix::StrDup(regex);
8631233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8632233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8633233d2500723e5594f3e7c70896ffeeef32b9c950ywan  is_valid_ = ValidateRegex(regex);
8634233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!is_valid_) {
8635233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // No need to calculate the full pattern when the regex is invalid.
8636233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
8637233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8638233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8639233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t len = strlen(regex);
8640233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Reserves enough bytes to hold the regular expression used for a
8641233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // full match: we need space to prepend a '^', append a '$', and
8642233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // terminate the string with '\0'.
8643233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* buffer = static_cast<char*>(malloc(len + 3));
8644233d2500723e5594f3e7c70896ffeeef32b9c950ywan  full_pattern_ = buffer;
8645233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8646233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (*regex != '^')
8647233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *buffer++ = '^';  // Makes sure full_pattern_ starts with '^'.
8648233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8649233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // We don't use snprintf or strncpy, as they trigger a warning when
8650233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // compiled with VC++ 8.0.
8651233d2500723e5594f3e7c70896ffeeef32b9c950ywan  memcpy(buffer, regex, len);
8652233d2500723e5594f3e7c70896ffeeef32b9c950ywan  buffer += len;
8653233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8654233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (len == 0 || regex[len - 1] != '$')
8655233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *buffer++ = '$';  // Makes sure full_pattern_ ends with '$'.
8656233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8657233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *buffer = '\0';
8658233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8659233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8660233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_USES_POSIX_RE
8661233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8662233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char kUnknownFile[] = "unknown file";
8663233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8664233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats a source file path and a line number as they would appear
8665233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in an error message from the compiler used to compile this code.
8666233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ ::std::string FormatFileLocation(const char* file, int line) {
8667233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string file_name(file == NULL ? kUnknownFile : file);
8668233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8669233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (line < 0) {
8670233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return file_name + ":";
8671233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8672233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifdef _MSC_VER
8673233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return file_name + "(" + StreamableToString(line) + "):";
8674233d2500723e5594f3e7c70896ffeeef32b9c950ywan#else
8675233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return file_name + ":" + StreamableToString(line) + ":";
8676233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // _MSC_VER
8677233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8678233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8679233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Formats a file location for compiler-independent XML output.
8680233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Although this function is not platform dependent, we put it next to
8681233d2500723e5594f3e7c70896ffeeef32b9c950ywan// FormatFileLocation in order to contrast the two functions.
8682233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Note that FormatCompilerIndependentFileLocation() does NOT append colon
8683233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to the file location it produces, unlike FormatFileLocation().
8684233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTEST_API_ ::std::string FormatCompilerIndependentFileLocation(
8685233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* file, int line) {
8686233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string file_name(file == NULL ? kUnknownFile : file);
8687233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8688233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (line < 0)
8689233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return file_name;
8690233d2500723e5594f3e7c70896ffeeef32b9c950ywan  else
8691233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return file_name + ":" + StreamableToString(line);
8692233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8693233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8694233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8695233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line)
8696233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : severity_(severity) {
8697233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const marker =
8698233d2500723e5594f3e7c70896ffeeef32b9c950ywan      severity == GTEST_INFO ?    "[  INFO ]" :
8699233d2500723e5594f3e7c70896ffeeef32b9c950ywan      severity == GTEST_WARNING ? "[WARNING]" :
8700233d2500723e5594f3e7c70896ffeeef32b9c950ywan      severity == GTEST_ERROR ?   "[ ERROR ]" : "[ FATAL ]";
8701233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetStream() << ::std::endl << marker << " "
8702233d2500723e5594f3e7c70896ffeeef32b9c950ywan              << FormatFileLocation(file, line).c_str() << ": ";
8703233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8704233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8705233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
8706233d2500723e5594f3e7c70896ffeeef32b9c950ywanGTestLog::~GTestLog() {
8707233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetStream() << ::std::endl;
8708233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (severity_ == GTEST_FATAL) {
8709233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stderr);
8710233d2500723e5594f3e7c70896ffeeef32b9c950ywan    posix::Abort();
8711233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8712233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8713233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Disable Microsoft deprecation warnings for POSIX functions called from
8714233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this class (creat, dup, dup2, and close)
8715233d2500723e5594f3e7c70896ffeeef32b9c950ywan#ifdef _MSC_VER
8716233d2500723e5594f3e7c70896ffeeef32b9c950ywan# pragma warning(push)
8717233d2500723e5594f3e7c70896ffeeef32b9c950ywan# pragma warning(disable: 4996)
8718233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // _MSC_VER
8719233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8720233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_STREAM_REDIRECTION
8721233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8722233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Object that captures an output stream (stdout/stderr).
8723233d2500723e5594f3e7c70896ffeeef32b9c950ywanclass CapturedStream {
8724233d2500723e5594f3e7c70896ffeeef32b9c950ywan public:
8725233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The ctor redirects the stream to a temporary file.
8726233d2500723e5594f3e7c70896ffeeef32b9c950ywan  explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
8727233d2500723e5594f3e7c70896ffeeef32b9c950ywan# if GTEST_OS_WINDOWS
8728233d2500723e5594f3e7c70896ffeeef32b9c950ywan    char temp_dir_path[MAX_PATH + 1] = { '\0' };  // NOLINT
8729233d2500723e5594f3e7c70896ffeeef32b9c950ywan    char temp_file_path[MAX_PATH + 1] = { '\0' };  // NOLINT
8730233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8731233d2500723e5594f3e7c70896ffeeef32b9c950ywan    ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path);
8732233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const UINT success = ::GetTempFileNameA(temp_dir_path,
8733233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                            "gtest_redir",
8734233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                            0,  // Generate unique file name.
8735233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                            temp_file_path);
8736233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_CHECK_(success != 0)
8737233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << "Unable to create a temporary file in " << temp_dir_path;
8738233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE);
8739233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_CHECK_(captured_fd != -1) << "Unable to open temporary file "
8740233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    << temp_file_path;
8741233d2500723e5594f3e7c70896ffeeef32b9c950ywan    filename_ = temp_file_path;
8742233d2500723e5594f3e7c70896ffeeef32b9c950ywan# else
8743233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // There's no guarantee that a test has write access to the current
8744233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // directory, so we create the temporary file in the /tmp directory
8745233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // instead. We use /tmp on most systems, and /sdcard on Android.
8746233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // That's because Android doesn't have /tmp.
8747233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  if GTEST_OS_LINUX_ANDROID
8748233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Note: Android applications are expected to call the framework's
8749233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Context.getExternalStorageDirectory() method through JNI to get
8750233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // the location of the world-writable SD Card directory. However,
8751233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // this requires a Context handle, which cannot be retrieved
8752233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // globally from native code. Doing so also precludes running the
8753233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // code as part of a regular standalone executable, which doesn't
8754233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // run in a Dalvik process (e.g. when running it through 'adb shell').
8755233d2500723e5594f3e7c70896ffeeef32b9c950ywan    //
8756233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // The location /sdcard is directly accessible from native code
8757233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // and is the only location (unofficially) supported by the Android
8758233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // team. It's generally a symlink to the real SD Card mount point
8759233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // which can be /mnt/sdcard, /mnt/sdcard0, /system/media/sdcard, or
8760233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // other OEM-customized locations. Never rely on these, and always
8761233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // use /sdcard.
8762233d2500723e5594f3e7c70896ffeeef32b9c950ywan    char name_template[] = "/sdcard/gtest_captured_stream.XXXXXX";
8763233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  else
8764233d2500723e5594f3e7c70896ffeeef32b9c950ywan    char name_template[] = "/tmp/captured_stream.XXXXXX";
8765233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  endif  // GTEST_OS_LINUX_ANDROID
8766233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const int captured_fd = mkstemp(name_template);
8767233d2500723e5594f3e7c70896ffeeef32b9c950ywan    filename_ = name_template;
8768233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // GTEST_OS_WINDOWS
8769233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(NULL);
8770233d2500723e5594f3e7c70896ffeeef32b9c950ywan    dup2(captured_fd, fd_);
8771233d2500723e5594f3e7c70896ffeeef32b9c950ywan    close(captured_fd);
8772233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8773233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8774233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ~CapturedStream() {
8775233d2500723e5594f3e7c70896ffeeef32b9c950ywan    remove(filename_.c_str());
8776233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8777233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8778233d2500723e5594f3e7c70896ffeeef32b9c950ywan  std::string GetCapturedString() {
8779233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (uncaptured_fd_ != -1) {
8780233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Restores the original stream.
8781233d2500723e5594f3e7c70896ffeeef32b9c950ywan      fflush(NULL);
8782233d2500723e5594f3e7c70896ffeeef32b9c950ywan      dup2(uncaptured_fd_, fd_);
8783233d2500723e5594f3e7c70896ffeeef32b9c950ywan      close(uncaptured_fd_);
8784233d2500723e5594f3e7c70896ffeeef32b9c950ywan      uncaptured_fd_ = -1;
8785233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
8786233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8787233d2500723e5594f3e7c70896ffeeef32b9c950ywan    FILE* const file = posix::FOpen(filename_.c_str(), "r");
8788233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string content = ReadEntireFile(file);
8789233d2500723e5594f3e7c70896ffeeef32b9c950ywan    posix::FClose(file);
8790233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return content;
8791233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8792233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8793233d2500723e5594f3e7c70896ffeeef32b9c950ywan private:
8794233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Reads the entire content of a file as an std::string.
8795233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static std::string ReadEntireFile(FILE* file);
8796233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8797233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Returns the size (in bytes) of a file.
8798233d2500723e5594f3e7c70896ffeeef32b9c950ywan  static size_t GetFileSize(FILE* file);
8799233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8800233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int fd_;  // A stream to capture.
8801233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int uncaptured_fd_;
8802233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Name of the temporary file holding the stderr output.
8803233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::string filename_;
8804233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8805233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GTEST_DISALLOW_COPY_AND_ASSIGN_(CapturedStream);
8806233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
8807233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8808233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the size (in bytes) of a file.
8809233d2500723e5594f3e7c70896ffeeef32b9c950ywansize_t CapturedStream::GetFileSize(FILE* file) {
8810233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fseek(file, 0, SEEK_END);
8811233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return static_cast<size_t>(ftell(file));
8812233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8813233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8814233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Reads the entire content of a file as a string.
8815233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string CapturedStream::ReadEntireFile(FILE* file) {
8816233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t file_size = GetFileSize(file);
8817233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* const buffer = new char[file_size];
8818233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8819233d2500723e5594f3e7c70896ffeeef32b9c950ywan  size_t bytes_last_read = 0;  // # of bytes read in the last fread()
8820233d2500723e5594f3e7c70896ffeeef32b9c950ywan  size_t bytes_read = 0;       // # of bytes read so far
8821233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8822233d2500723e5594f3e7c70896ffeeef32b9c950ywan  fseek(file, 0, SEEK_SET);
8823233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8824233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Keeps reading the file until we cannot read further or the
8825233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // pre-determined file size is reached.
8826233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do {
8827233d2500723e5594f3e7c70896ffeeef32b9c950ywan    bytes_last_read = fread(buffer+bytes_read, 1, file_size-bytes_read, file);
8828233d2500723e5594f3e7c70896ffeeef32b9c950ywan    bytes_read += bytes_last_read;
8829233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (bytes_last_read > 0 && bytes_read < file_size);
8830233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8831233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string content(buffer, bytes_read);
8832233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete[] buffer;
8833233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8834233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return content;
8835233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8836233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8837233d2500723e5594f3e7c70896ffeeef32b9c950ywan# ifdef _MSC_VER
8838233d2500723e5594f3e7c70896ffeeef32b9c950ywan#  pragma warning(pop)
8839233d2500723e5594f3e7c70896ffeeef32b9c950ywan# endif  // _MSC_VER
8840233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8841233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic CapturedStream* g_captured_stderr = NULL;
8842233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic CapturedStream* g_captured_stdout = NULL;
8843233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8844233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Starts capturing an output stream (stdout/stderr).
8845233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid CaptureStream(int fd, const char* stream_name, CapturedStream** stream) {
8846233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (*stream != NULL) {
8847233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_LOG_(FATAL) << "Only one " << stream_name
8848233d2500723e5594f3e7c70896ffeeef32b9c950ywan                      << " capturer can exist at a time.";
8849233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8850233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *stream = new CapturedStream(fd);
8851233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8852233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8853233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Stops capturing the output stream and returns the captured string.
8854233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string GetCapturedStream(CapturedStream** captured_stream) {
8855233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string content = (*captured_stream)->GetCapturedString();
8856233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8857233d2500723e5594f3e7c70896ffeeef32b9c950ywan  delete *captured_stream;
8858233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *captured_stream = NULL;
8859233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8860233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return content;
8861233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8862233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8863233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Starts capturing stdout.
8864233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid CaptureStdout() {
8865233d2500723e5594f3e7c70896ffeeef32b9c950ywan  CaptureStream(kStdOutFileno, "stdout", &g_captured_stdout);
8866233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8867233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8868233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Starts capturing stderr.
8869233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid CaptureStderr() {
8870233d2500723e5594f3e7c70896ffeeef32b9c950ywan  CaptureStream(kStdErrFileno, "stderr", &g_captured_stderr);
8871233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8872233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8873233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Stops capturing stdout and returns the captured string.
8874233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string GetCapturedStdout() {
8875233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return GetCapturedStream(&g_captured_stdout);
8876233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8877233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8878233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Stops capturing stderr and returns the captured string.
8879233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string GetCapturedStderr() {
8880233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return GetCapturedStream(&g_captured_stderr);
8881233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8882233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8883233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_STREAM_REDIRECTION
8884233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8885233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_DEATH_TEST
8886233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8887233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A copy of all command line arguments.  Set by InitGoogleTest().
8888233d2500723e5594f3e7c70896ffeeef32b9c950ywan::std::vector<testing::internal::string> g_argvs;
8889233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8890233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const ::std::vector<testing::internal::string>* g_injected_test_argvs =
8891233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                        NULL;  // Owned.
8892233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8893233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid SetInjectableArgvs(const ::std::vector<testing::internal::string>* argvs) {
8894233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (g_injected_test_argvs != argvs)
8895233d2500723e5594f3e7c70896ffeeef32b9c950ywan    delete g_injected_test_argvs;
8896233d2500723e5594f3e7c70896ffeeef32b9c950ywan  g_injected_test_argvs = argvs;
8897233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8898233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8899233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst ::std::vector<testing::internal::string>& GetInjectableArgvs() {
8900233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (g_injected_test_argvs != NULL) {
8901233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return *g_injected_test_argvs;
8902233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8903233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return g_argvs;
8904233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8905233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_DEATH_TEST
8906233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8907233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_OS_WINDOWS_MOBILE
8908233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace posix {
8909233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid Abort() {
8910233d2500723e5594f3e7c70896ffeeef32b9c950ywan  DebugBreak();
8911233d2500723e5594f3e7c70896ffeeef32b9c950ywan  TerminateProcess(GetCurrentProcess(), 1);
8912233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8913233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace posix
8914233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_OS_WINDOWS_MOBILE
8915233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8916233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the name of the environment variable corresponding to the
8917233d2500723e5594f3e7c70896ffeeef32b9c950ywan// given flag.  For example, FlagToEnvVar("foo") will return
8918233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "GTEST_FOO" in the open-source version.
8919233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic std::string FlagToEnvVar(const char* flag) {
8920233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string full_flag =
8921233d2500723e5594f3e7c70896ffeeef32b9c950ywan      (Message() << GTEST_FLAG_PREFIX_ << flag).GetString();
8922233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8923233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message env_var;
8924233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i != full_flag.length(); i++) {
8925233d2500723e5594f3e7c70896ffeeef32b9c950ywan    env_var << ToUpper(full_flag.c_str()[i]);
8926233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8927233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8928233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return env_var.GetString();
8929233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8930233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8931233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Parses 'str' for a 32-bit signed integer.  If successful, writes
8932233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the result to *value and returns true; otherwise leaves *value
8933233d2500723e5594f3e7c70896ffeeef32b9c950ywan// unchanged and returns false.
8934233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool ParseInt32(const Message& src_text, const char* str, Int32* value) {
8935233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Parses the environment variable as a decimal integer.
8936233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char* end = NULL;
8937233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const long long_value = strtol(str, &end, 10);  // NOLINT
8938233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8939233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Has strtol() consumed all characters in the string?
8940233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (*end != '\0') {
8941233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // No - an invalid character was encountered.
8942233d2500723e5594f3e7c70896ffeeef32b9c950ywan    Message msg;
8943233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << "WARNING: " << src_text
8944233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << " is expected to be a 32-bit integer, but actually"
8945233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << " has value \"" << str << "\".\n";
8946233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("%s", msg.GetString().c_str());
8947233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stdout);
8948233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
8949233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8950233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8951233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Is the parsed value in the range of an Int32?
8952233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const Int32 result = static_cast<Int32>(long_value);
8953233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (long_value == LONG_MAX || long_value == LONG_MIN ||
8954233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // The parsed value overflows as a long.  (strtol() returns
8955233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // LONG_MAX or LONG_MIN when the input overflows.)
8956233d2500723e5594f3e7c70896ffeeef32b9c950ywan      result != long_value
8957233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // The parsed value overflows as an Int32.
8958233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ) {
8959233d2500723e5594f3e7c70896ffeeef32b9c950ywan    Message msg;
8960233d2500723e5594f3e7c70896ffeeef32b9c950ywan    msg << "WARNING: " << src_text
8961233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << " is expected to be a 32-bit integer, but actually"
8962233d2500723e5594f3e7c70896ffeeef32b9c950ywan        << " has value " << str << ", which overflows.\n";
8963233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("%s", msg.GetString().c_str());
8964233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stdout);
8965233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return false;
8966233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8967233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8968233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *value = result;
8969233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return true;
8970233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8971233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8972233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Reads and returns the Boolean environment variable corresponding to
8973233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the given flag; if it's not set, returns default_value.
8974233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
8975233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The value is considered true iff it's not "0".
8976233d2500723e5594f3e7c70896ffeeef32b9c950ywanbool BoolFromGTestEnv(const char* flag, bool default_value) {
8977233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string env_var = FlagToEnvVar(flag);
8978233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const string_value = posix::GetEnv(env_var.c_str());
8979233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return string_value == NULL ?
8980233d2500723e5594f3e7c70896ffeeef32b9c950ywan      default_value : strcmp(string_value, "0") != 0;
8981233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
8982233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8983233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Reads and returns a 32-bit integer stored in the environment
8984233d2500723e5594f3e7c70896ffeeef32b9c950ywan// variable corresponding to the given flag; if it isn't set or
8985233d2500723e5594f3e7c70896ffeeef32b9c950ywan// doesn't represent a valid 32-bit integer, returns default_value.
8986233d2500723e5594f3e7c70896ffeeef32b9c950ywanInt32 Int32FromGTestEnv(const char* flag, Int32 default_value) {
8987233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string env_var = FlagToEnvVar(flag);
8988233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const string_value = posix::GetEnv(env_var.c_str());
8989233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (string_value == NULL) {
8990233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // The environment variable is not set.
8991233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return default_value;
8992233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
8993233d2500723e5594f3e7c70896ffeeef32b9c950ywan
8994233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Int32 result = default_value;
8995233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!ParseInt32(Message() << "Environment variable " << env_var,
8996233d2500723e5594f3e7c70896ffeeef32b9c950ywan                  string_value, &result)) {
8997233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("The default value %s is used.\n",
8998233d2500723e5594f3e7c70896ffeeef32b9c950ywan           (Message() << default_value).GetString().c_str());
8999233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stdout);
9000233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return default_value;
9001233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9002233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9003233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return result;
9004233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9005233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9006233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Reads and returns the string environment variable corresponding to
9007233d2500723e5594f3e7c70896ffeeef32b9c950ywan// the given flag; if it's not set, returns default_value.
9008233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* StringFromGTestEnv(const char* flag, const char* default_value) {
9009233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string env_var = FlagToEnvVar(flag);
9010233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const value = posix::GetEnv(env_var.c_str());
9011233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return value == NULL ? default_value : value;
9012233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9013233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9014233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
9015233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
9016233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2007, Google Inc.
9017233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
9018233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9019233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
9020233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
9021233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
9022233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9023233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
9024233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
9025233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
9026233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
9027233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
9028233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
9029233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
9030233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
9031233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
9032233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9033233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9034233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
9035233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9036233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9037233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9038233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9039233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
9040233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
9041233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9042233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
9043233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9044233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9045233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: wan@google.com (Zhanyong Wan)
9046233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9047233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Google Test - The Google C++ Testing Framework
9048233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9049233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This file implements a universal value printer that can print a
9050233d2500723e5594f3e7c70896ffeeef32b9c950ywan// value of any type T:
9051233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9052233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
9053233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9054233d2500723e5594f3e7c70896ffeeef32b9c950ywan// It uses the << operator when possible, and prints the bytes in the
9055233d2500723e5594f3e7c70896ffeeef32b9c950ywan// object otherwise.  A user can override its behavior for a class
9056233d2500723e5594f3e7c70896ffeeef32b9c950ywan// type Foo by defining either operator<<(::std::ostream&, const Foo&)
9057233d2500723e5594f3e7c70896ffeeef32b9c950ywan// or void PrintTo(const Foo&, ::std::ostream*) in the namespace that
9058233d2500723e5594f3e7c70896ffeeef32b9c950ywan// defines Foo.
9059233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9060233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <ctype.h>
9061233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <stdio.h>
9062233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <ostream>  // NOLINT
9063233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include <string>
9064233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9065233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
9066233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9067233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace {
9068233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9069233d2500723e5594f3e7c70896ffeeef32b9c950ywanusing ::std::ostream;
9070233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9071233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a segment of bytes in the given object.
9072233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
9073233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                size_t count, ostream* os) {
9074233d2500723e5594f3e7c70896ffeeef32b9c950ywan  char text[5] = "";
9075233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t i = 0; i != count; i++) {
9076233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const size_t j = start + i;
9077233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (i != 0) {
9078233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Organizes the bytes into groups of 2 for easy parsing by
9079233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // human.
9080233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if ((j % 2) == 0)
9081233d2500723e5594f3e7c70896ffeeef32b9c950ywan        *os << ' ';
9082233d2500723e5594f3e7c70896ffeeef32b9c950ywan      else
9083233d2500723e5594f3e7c70896ffeeef32b9c950ywan        *os << '-';
9084233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
9085233d2500723e5594f3e7c70896ffeeef32b9c950ywan    GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);
9086233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *os << text;
9087233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9088233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9089233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9090233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints the bytes in the given value to the given ostream.
9091233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
9092233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              ostream* os) {
9093233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Tells the user how big the object is.
9094233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << count << "-byte object <";
9095233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9096233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t kThreshold = 132;
9097233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const size_t kChunkSize = 64;
9098233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // If the object size is bigger than kThreshold, we'll have to omit
9099233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // some details by printing only the first and the last kChunkSize
9100233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // bytes.
9101233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // TODO(wan): let the user control the threshold using a flag.
9102233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (count < kThreshold) {
9103233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
9104233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
9105233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
9106233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *os << " ... ";
9107233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Rounds up to 2-byte boundary.
9108233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const size_t resume_pos = (count - kChunkSize + 1)/2*2;
9109233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
9110233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9111233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << ">";
9112233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9113233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9114233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace
9115233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9116233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal2 {
9117233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9118233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Delegates to PrintBytesInObjectToImpl() to print the bytes in the
9119233d2500723e5594f3e7c70896ffeeef32b9c950ywan// given object.  The delegation simplifies the implementation, which
9120233d2500723e5594f3e7c70896ffeeef32b9c950ywan// uses the << operator and thus is easier done outside of the
9121233d2500723e5594f3e7c70896ffeeef32b9c950ywan// ::testing::internal namespace, which contains a << operator that
9122233d2500723e5594f3e7c70896ffeeef32b9c950ywan// sometimes conflicts with the one in STL.
9123233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
9124233d2500723e5594f3e7c70896ffeeef32b9c950ywan                          ostream* os) {
9125233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintBytesInObjectToImpl(obj_bytes, count, os);
9126233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9127233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9128233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal2
9129233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9130233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
9131233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9132233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Depending on the value of a char (or wchar_t), we print it in one
9133233d2500723e5594f3e7c70896ffeeef32b9c950ywan// of three formats:
9134233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   - as is if it's a printable ASCII (e.g. 'a', '2', ' '),
9135233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   - as a hexidecimal escape sequence (e.g. '\x7F'), or
9136233d2500723e5594f3e7c70896ffeeef32b9c950ywan//   - as a special escape sequence (e.g. '\r', '\n').
9137233d2500723e5594f3e7c70896ffeeef32b9c950ywanenum CharFormat {
9138233d2500723e5594f3e7c70896ffeeef32b9c950ywan  kAsIs,
9139233d2500723e5594f3e7c70896ffeeef32b9c950ywan  kHexEscape,
9140233d2500723e5594f3e7c70896ffeeef32b9c950ywan  kSpecialEscape
9141233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
9142233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9143233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns true if c is a printable ASCII character.  We test the
9144233d2500723e5594f3e7c70896ffeeef32b9c950ywan// value of c directly instead of calling isprint(), which is buggy on
9145233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Windows Mobile.
9146233d2500723e5594f3e7c70896ffeeef32b9c950ywaninline bool IsPrintableAscii(wchar_t c) {
9147233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return 0x20 <= c && c <= 0x7E;
9148233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9149233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9150233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a wide or narrow char c as a character literal without the
9151233d2500723e5594f3e7c70896ffeeef32b9c950ywan// quotes, escaping it when necessary; returns how c was formatted.
9152233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The template argument UnsignedChar is the unsigned version of Char,
9153233d2500723e5594f3e7c70896ffeeef32b9c950ywan// which is the type of c.
9154233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename UnsignedChar, typename Char>
9155233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {
9156233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (static_cast<wchar_t>(c)) {
9157233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\0':
9158233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\0";
9159233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9160233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\'':
9161233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\'";
9162233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9163233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\\':
9164233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\\\";
9165233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9166233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\a':
9167233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\a";
9168233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9169233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\b':
9170233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\b";
9171233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9172233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\f':
9173233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\f";
9174233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9175233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\n':
9176233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\n";
9177233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9178233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\r':
9179233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\r";
9180233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9181233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\t':
9182233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\t";
9183233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9184233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\v':
9185233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\v";
9186233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
9187233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:
9188233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (IsPrintableAscii(c)) {
9189233d2500723e5594f3e7c70896ffeeef32b9c950ywan        *os << static_cast<char>(c);
9190233d2500723e5594f3e7c70896ffeeef32b9c950ywan        return kAsIs;
9191233d2500723e5594f3e7c70896ffeeef32b9c950ywan      } else {
9192233d2500723e5594f3e7c70896ffeeef32b9c950ywan        *os << "\\x" + String::FormatHexInt(static_cast<UnsignedChar>(c));
9193233d2500723e5594f3e7c70896ffeeef32b9c950ywan        return kHexEscape;
9194233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
9195233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9196233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return kSpecialEscape;
9197233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9198233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9199233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a wchar_t c as if it's part of a string literal, escaping it when
9200233d2500723e5594f3e7c70896ffeeef32b9c950ywan// necessary; returns how c was formatted.
9201233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) {
9202233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (c) {
9203233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'\'':
9204233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "'";
9205233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return kAsIs;
9206233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case L'"':
9207233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\\\"";
9208233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return kSpecialEscape;
9209233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:
9210233d2500723e5594f3e7c70896ffeeef32b9c950ywan      return PrintAsCharLiteralTo<wchar_t>(c, os);
9211233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9212233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9213233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9214233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a char c as if it's part of a string literal, escaping it when
9215233d2500723e5594f3e7c70896ffeeef32b9c950ywan// necessary; returns how c was formatted.
9216233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic CharFormat PrintAsStringLiteralTo(char c, ostream* os) {
9217233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return PrintAsStringLiteralTo(
9218233d2500723e5594f3e7c70896ffeeef32b9c950ywan      static_cast<wchar_t>(static_cast<unsigned char>(c)), os);
9219233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9220233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9221233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a wide or narrow character c and its code.  '\0' is printed
9222233d2500723e5594f3e7c70896ffeeef32b9c950ywan// as "'\\0'", other unprintable characters are also properly escaped
9223233d2500723e5594f3e7c70896ffeeef32b9c950ywan// using the standard C++ escape sequence.  The template argument
9224233d2500723e5594f3e7c70896ffeeef32b9c950ywan// UnsignedChar is the unsigned version of Char, which is the type of c.
9225233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename UnsignedChar, typename Char>
9226233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintCharAndCodeTo(Char c, ostream* os) {
9227233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // First, print c as a literal in the most readable form we can find.
9228233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << ((sizeof(c) > 1) ? "L'" : "'");
9229233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);
9230233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << "'";
9231233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9232233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // To aid user debugging, we also print c's code in decimal, unless
9233233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it's 0 (in which case c was printed as '\\0', making the code
9234233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // obvious).
9235233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (c == 0)
9236233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
9237233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << " (" << static_cast<int>(c);
9238233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9239233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // For more convenience, we print c's code again in hexidecimal,
9240233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // unless c was already printed in the form '\x##' or the code is in
9241233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // [1, 9].
9242233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (format == kHexEscape || (1 <= c && c <= 9)) {
9243233d2500723e5594f3e7c70896ffeeef32b9c950ywan    // Do nothing.
9244233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
9245233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *os << ", 0x" << String::FormatHexInt(static_cast<UnsignedChar>(c));
9246233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9247233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << ")";
9248233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9249233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9250233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintTo(unsigned char c, ::std::ostream* os) {
9251233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintCharAndCodeTo<unsigned char>(c, os);
9252233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9253233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintTo(signed char c, ::std::ostream* os) {
9254233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintCharAndCodeTo<unsigned char>(c, os);
9255233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9256233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9257233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a wchar_t as a symbol if it is printable or as its internal
9258233d2500723e5594f3e7c70896ffeeef32b9c950ywan// code otherwise and also as its code.  L'\0' is printed as "L'\\0'".
9259233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintTo(wchar_t wc, ostream* os) {
9260233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintCharAndCodeTo<wchar_t>(wc, os);
9261233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9262233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9263233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints the given array of characters to the ostream.  CharType must be either
9264233d2500723e5594f3e7c70896ffeeef32b9c950ywan// char or wchar_t.
9265233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The array starts at begin, the length is len, it may include '\0' characters
9266233d2500723e5594f3e7c70896ffeeef32b9c950ywan// and may not be NUL-terminated.
9267233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename CharType>
9268233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void PrintCharsAsStringTo(
9269233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const CharType* begin, size_t len, ostream* os) {
9270233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const kQuoteBegin = sizeof(CharType) == 1 ? "\"" : "L\"";
9271233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << kQuoteBegin;
9272233d2500723e5594f3e7c70896ffeeef32b9c950ywan  bool is_previous_hex = false;
9273233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (size_t index = 0; index < len; ++index) {
9274233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const CharType cur = begin[index];
9275233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (is_previous_hex && IsXDigit(cur)) {
9276233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Previous character is of '\x..' form and this character can be
9277233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // interpreted as another hexadecimal digit in its number. Break string to
9278233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // disambiguate.
9279233d2500723e5594f3e7c70896ffeeef32b9c950ywan      *os << "\" " << kQuoteBegin;
9280233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
9281233d2500723e5594f3e7c70896ffeeef32b9c950ywan    is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape;
9282233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9283233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << "\"";
9284233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9285233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9286233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a (const) char/wchar_t array of 'len' elements, starting at address
9287233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 'begin'.  CharType must be either char or wchar_t.
9288233d2500723e5594f3e7c70896ffeeef32b9c950ywantemplate <typename CharType>
9289233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void UniversalPrintCharArray(
9290233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const CharType* begin, size_t len, ostream* os) {
9291233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The code
9292233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //   const char kFoo[] = "foo";
9293233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // generates an array of 4, not 3, elements, with the last one being '\0'.
9294233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //
9295233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Therefore when printing a char array, we don't print the last element if
9296233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // it's '\0', such that the output matches the string literal as it's
9297233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // written in the source code.
9298233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (len > 0 && begin[len - 1] == '\0') {
9299233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintCharsAsStringTo(begin, len - 1, os);
9300233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return;
9301233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9302233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9303233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // If, however, the last element in the array is not '\0', e.g.
9304233d2500723e5594f3e7c70896ffeeef32b9c950ywan  //    const char kFoo[] = { 'f', 'o', 'o' };
9305233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // we must print the entire array.  We also print a message to indicate
9306233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // that the array is not NUL-terminated.
9307233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintCharsAsStringTo(begin, len, os);
9308233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *os << " (no terminating NUL)";
9309233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9310233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9311233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a (const) char array of 'len' elements, starting at address 'begin'.
9312233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UniversalPrintArray(const char* begin, size_t len, ostream* os) {
9313233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UniversalPrintCharArray(begin, len, os);
9314233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9315233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9316233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a (const) wchar_t array of 'len' elements, starting at address
9317233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 'begin'.
9318233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {
9319233d2500723e5594f3e7c70896ffeeef32b9c950ywan  UniversalPrintCharArray(begin, len, os);
9320233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9321233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9322233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints the given C string to the ostream.
9323233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintTo(const char* s, ostream* os) {
9324233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (s == NULL) {
9325233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *os << "NULL";
9326233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
9327233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *os << ImplicitCast_<const void*>(s) << " pointing to ";
9328233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintCharsAsStringTo(s, strlen(s), os);
9329233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9330233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9331233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9332233d2500723e5594f3e7c70896ffeeef32b9c950ywan// MSVC compiler can be configured to define whar_t as a typedef
9333233d2500723e5594f3e7c70896ffeeef32b9c950ywan// of unsigned short. Defining an overload for const wchar_t* in that case
9334233d2500723e5594f3e7c70896ffeeef32b9c950ywan// would cause pointers to unsigned shorts be printed as wide strings,
9335233d2500723e5594f3e7c70896ffeeef32b9c950ywan// possibly accessing more memory than intended and causing invalid
9336233d2500723e5594f3e7c70896ffeeef32b9c950ywan// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
9337233d2500723e5594f3e7c70896ffeeef32b9c950ywan// wchar_t is implemented as a native type.
9338233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
9339233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints the given wide C string to the ostream.
9340233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintTo(const wchar_t* s, ostream* os) {
9341233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (s == NULL) {
9342233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *os << "NULL";
9343233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } else {
9344233d2500723e5594f3e7c70896ffeeef32b9c950ywan    *os << ImplicitCast_<const void*>(s) << " pointing to ";
9345233d2500723e5594f3e7c70896ffeeef32b9c950ywan    PrintCharsAsStringTo(s, wcslen(s), os);
9346233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9347233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9348233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // wchar_t is native
9349233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9350233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a ::string object.
9351233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_GLOBAL_STRING
9352233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintStringTo(const ::string& s, ostream* os) {
9353233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintCharsAsStringTo(s.data(), s.size(), os);
9354233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9355233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_GLOBAL_STRING
9356233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9357233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintStringTo(const ::std::string& s, ostream* os) {
9358233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintCharsAsStringTo(s.data(), s.size(), os);
9359233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9360233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9361233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a ::wstring object.
9362233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_GLOBAL_WSTRING
9363233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintWideStringTo(const ::wstring& s, ostream* os) {
9364233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintCharsAsStringTo(s.data(), s.size(), os);
9365233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9366233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_GLOBAL_WSTRING
9367233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9368233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_STD_WSTRING
9369233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid PrintWideStringTo(const ::std::wstring& s, ostream* os) {
9370233d2500723e5594f3e7c70896ffeeef32b9c950ywan  PrintCharsAsStringTo(s.data(), s.size(), os);
9371233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9372233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_STD_WSTRING
9373233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9374233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
9375233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9376233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
9377233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2008, Google Inc.
9378233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All rights reserved.
9379233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9380233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
9381233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
9382233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
9383233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9384233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
9385233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
9386233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
9387233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
9388233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
9389233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
9390233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
9391233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
9392233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
9393233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9394233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9395233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
9396233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9397233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9398233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9399233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9400233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
9401233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
9402233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9403233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
9404233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9405233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9406233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: mheule@google.com (Markus Heule)
9407233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9408233d2500723e5594f3e7c70896ffeeef32b9c950ywan// The Google C++ Testing Framework (Google Test)
9409233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9410233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9411233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Indicates that this translation unit is part of Google Test's
9412233d2500723e5594f3e7c70896ffeeef32b9c950ywan// implementation.  It must come before gtest-internal-inl.h is
9413233d2500723e5594f3e7c70896ffeeef32b9c950ywan// included, or there will be a compiler error.  This trick is to
9414233d2500723e5594f3e7c70896ffeeef32b9c950ywan// prevent a user from accidentally including gtest-internal-inl.h in
9415233d2500723e5594f3e7c70896ffeeef32b9c950ywan// his code.
9416233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define GTEST_IMPLEMENTATION_ 1
9417233d2500723e5594f3e7c70896ffeeef32b9c950ywan#undef GTEST_IMPLEMENTATION_
9418233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9419233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
9420233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9421233d2500723e5594f3e7c70896ffeeef32b9c950ywanusing internal::GetUnitTestImpl;
9422233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9423233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Gets the summary of the failure message by omitting the stack trace
9424233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in it.
9425233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::string TestPartResult::ExtractSummary(const char* message) {
9426233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const char* const stack_trace = strstr(message, internal::kStackTraceMarker);
9427233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return stack_trace == NULL ? message :
9428233d2500723e5594f3e7c70896ffeeef32b9c950ywan      std::string(message, stack_trace);
9429233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9430233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9431233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Prints a TestPartResult object.
9432233d2500723e5594f3e7c70896ffeeef32b9c950ywanstd::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
9433233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return os
9434233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << result.file_name() << ":" << result.line_number() << ": "
9435233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << (result.type() == TestPartResult::kSuccess ? "Success" :
9436233d2500723e5594f3e7c70896ffeeef32b9c950ywan          result.type() == TestPartResult::kFatalFailure ? "Fatal failure" :
9437233d2500723e5594f3e7c70896ffeeef32b9c950ywan          "Non-fatal failure") << ":\n"
9438233d2500723e5594f3e7c70896ffeeef32b9c950ywan      << result.message() << std::endl;
9439233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9440233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9441233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Appends a TestPartResult to the array.
9442233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid TestPartResultArray::Append(const TestPartResult& result) {
9443233d2500723e5594f3e7c70896ffeeef32b9c950ywan  array_.push_back(result);
9444233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9445233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9446233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the TestPartResult at the given index (0-based).
9447233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
9448233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (index < 0 || index >= size()) {
9449233d2500723e5594f3e7c70896ffeeef32b9c950ywan    printf("\nInvalid index (%d) into TestPartResultArray.\n", index);
9450233d2500723e5594f3e7c70896ffeeef32b9c950ywan    internal::posix::Abort();
9451233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9452233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9453233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return array_[index];
9454233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9455233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9456233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Returns the number of TestPartResult objects in the array.
9457233d2500723e5594f3e7c70896ffeeef32b9c950ywanint TestPartResultArray::size() const {
9458233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return static_cast<int>(array_.size());
9459233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9460233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9461233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
9462233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9463233d2500723e5594f3e7c70896ffeeef32b9c950ywanHasNewFatalFailureHelper::HasNewFatalFailureHelper()
9464233d2500723e5594f3e7c70896ffeeef32b9c950ywan    : has_new_fatal_failure_(false),
9465233d2500723e5594f3e7c70896ffeeef32b9c950ywan      original_reporter_(GetUnitTestImpl()->
9466233d2500723e5594f3e7c70896ffeeef32b9c950ywan                         GetTestPartResultReporterForCurrentThread()) {
9467233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);
9468233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9469233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9470233d2500723e5594f3e7c70896ffeeef32b9c950ywanHasNewFatalFailureHelper::~HasNewFatalFailureHelper() {
9471233d2500723e5594f3e7c70896ffeeef32b9c950ywan  GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(
9472233d2500723e5594f3e7c70896ffeeef32b9c950ywan      original_reporter_);
9473233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9474233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9475233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid HasNewFatalFailureHelper::ReportTestPartResult(
9476233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const TestPartResult& result) {
9477233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (result.fatally_failed())
9478233d2500723e5594f3e7c70896ffeeef32b9c950ywan    has_new_fatal_failure_ = true;
9479233d2500723e5594f3e7c70896ffeeef32b9c950ywan  original_reporter_->ReportTestPartResult(result);
9480233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9481233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9482233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
9483233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9484233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
9485233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Copyright 2008 Google Inc.
9486233d2500723e5594f3e7c70896ffeeef32b9c950ywan// All Rights Reserved.
9487233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9488233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Redistribution and use in source and binary forms, with or without
9489233d2500723e5594f3e7c70896ffeeef32b9c950ywan// modification, are permitted provided that the following conditions are
9490233d2500723e5594f3e7c70896ffeeef32b9c950ywan// met:
9491233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9492233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions of source code must retain the above copyright
9493233d2500723e5594f3e7c70896ffeeef32b9c950ywan// notice, this list of conditions and the following disclaimer.
9494233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Redistributions in binary form must reproduce the above
9495233d2500723e5594f3e7c70896ffeeef32b9c950ywan// copyright notice, this list of conditions and the following disclaimer
9496233d2500723e5594f3e7c70896ffeeef32b9c950ywan// in the documentation and/or other materials provided with the
9497233d2500723e5594f3e7c70896ffeeef32b9c950ywan// distribution.
9498233d2500723e5594f3e7c70896ffeeef32b9c950ywan//     * Neither the name of Google Inc. nor the names of its
9499233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contributors may be used to endorse or promote products derived from
9500233d2500723e5594f3e7c70896ffeeef32b9c950ywan// this software without specific prior written permission.
9501233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9502233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9503233d2500723e5594f3e7c70896ffeeef32b9c950ywan// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
9504233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9505233d2500723e5594f3e7c70896ffeeef32b9c950ywan// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9506233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9507233d2500723e5594f3e7c70896ffeeef32b9c950ywan// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9508233d2500723e5594f3e7c70896ffeeef32b9c950ywan// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
9509233d2500723e5594f3e7c70896ffeeef32b9c950ywan// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
9510233d2500723e5594f3e7c70896ffeeef32b9c950ywan// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
9511233d2500723e5594f3e7c70896ffeeef32b9c950ywan// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
9512233d2500723e5594f3e7c70896ffeeef32b9c950ywan// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9513233d2500723e5594f3e7c70896ffeeef32b9c950ywan//
9514233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Author: wan@google.com (Zhanyong Wan)
9515233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9516233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9517233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace testing {
9518233d2500723e5594f3e7c70896ffeeef32b9c950ywannamespace internal {
9519233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9520233d2500723e5594f3e7c70896ffeeef32b9c950ywan#if GTEST_HAS_TYPED_TEST_P
9521233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9522233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Skips to the first non-space char in str. Returns an empty string if str
9523233d2500723e5594f3e7c70896ffeeef32b9c950ywan// contains only whitespace characters.
9524233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const char* SkipSpaces(const char* str) {
9525233d2500723e5594f3e7c70896ffeeef32b9c950ywan  while (IsSpace(*str))
9526233d2500723e5594f3e7c70896ffeeef32b9c950ywan    str++;
9527233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return str;
9528233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9529233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9530233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Verifies that registered_tests match the test names in
9531233d2500723e5594f3e7c70896ffeeef32b9c950ywan// defined_test_names_; returns registered_tests if successful, or
9532233d2500723e5594f3e7c70896ffeeef32b9c950ywan// aborts the program otherwise.
9533233d2500723e5594f3e7c70896ffeeef32b9c950ywanconst char* TypedTestCasePState::VerifyRegisteredTestNames(
9534233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const char* file, int line, const char* registered_tests) {
9535233d2500723e5594f3e7c70896ffeeef32b9c950ywan  typedef ::std::set<const char*>::const_iterator DefinedTestIter;
9536233d2500723e5594f3e7c70896ffeeef32b9c950ywan  registered_ = true;
9537233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9538233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Skip initial whitespace in registered_tests since some
9539233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // preprocessors prefix stringizied literals with whitespace.
9540233d2500723e5594f3e7c70896ffeeef32b9c950ywan  registered_tests = SkipSpaces(registered_tests);
9541233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9542233d2500723e5594f3e7c70896ffeeef32b9c950ywan  Message errors;
9543233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ::std::set<std::string> tests;
9544233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (const char* names = registered_tests; names != NULL;
9545233d2500723e5594f3e7c70896ffeeef32b9c950ywan       names = SkipComma(names)) {
9546233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const std::string name = GetPrefixUntilComma(names);
9547233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (tests.count(name) != 0) {
9548233d2500723e5594f3e7c70896ffeeef32b9c950ywan      errors << "Test " << name << " is listed more than once.\n";
9549233d2500723e5594f3e7c70896ffeeef32b9c950ywan      continue;
9550233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
9551233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9552233d2500723e5594f3e7c70896ffeeef32b9c950ywan    bool found = false;
9553233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (DefinedTestIter it = defined_test_names_.begin();
9554233d2500723e5594f3e7c70896ffeeef32b9c950ywan         it != defined_test_names_.end();
9555233d2500723e5594f3e7c70896ffeeef32b9c950ywan         ++it) {
9556233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (name == *it) {
9557233d2500723e5594f3e7c70896ffeeef32b9c950ywan        found = true;
9558233d2500723e5594f3e7c70896ffeeef32b9c950ywan        break;
9559233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
9560233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
9561233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9562233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (found) {
9563233d2500723e5594f3e7c70896ffeeef32b9c950ywan      tests.insert(name);
9564233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else {
9565233d2500723e5594f3e7c70896ffeeef32b9c950ywan      errors << "No test named " << name
9566233d2500723e5594f3e7c70896ffeeef32b9c950ywan             << " can be found in this test case.\n";
9567233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
9568233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9569233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9570233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (DefinedTestIter it = defined_test_names_.begin();
9571233d2500723e5594f3e7c70896ffeeef32b9c950ywan       it != defined_test_names_.end();
9572233d2500723e5594f3e7c70896ffeeef32b9c950ywan       ++it) {
9573233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (tests.count(*it) == 0) {
9574233d2500723e5594f3e7c70896ffeeef32b9c950ywan      errors << "You forgot to list test " << *it << ".\n";
9575233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
9576233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9577233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9578233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const std::string& errors_str = errors.GetString();
9579233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (errors_str != "") {
9580233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(),
9581233d2500723e5594f3e7c70896ffeeef32b9c950ywan            errors_str.c_str());
9582233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fflush(stderr);
9583233d2500723e5594f3e7c70896ffeeef32b9c950ywan    posix::Abort();
9584233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
9585233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9586233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return registered_tests;
9587233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
9588233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9589233d2500723e5594f3e7c70896ffeeef32b9c950ywan#endif  // GTEST_HAS_TYPED_TEST_P
9590233d2500723e5594f3e7c70896ffeeef32b9c950ywan
9591233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace internal
9592233d2500723e5594f3e7c70896ffeeef32b9c950ywan}  // namespace testing
9593