gmock-internal-utils_test.cc revision 90c90f9250f19c3f3a5c2c0887c1d9e414afe41b
1e35fdd936d133bf8a48de140a3c666897588a05shiqian// Copyright 2007, Google Inc.
2e35fdd936d133bf8a48de140a3c666897588a05shiqian// All rights reserved.
3e35fdd936d133bf8a48de140a3c666897588a05shiqian//
4e35fdd936d133bf8a48de140a3c666897588a05shiqian// Redistribution and use in source and binary forms, with or without
5e35fdd936d133bf8a48de140a3c666897588a05shiqian// modification, are permitted provided that the following conditions are
6e35fdd936d133bf8a48de140a3c666897588a05shiqian// met:
7e35fdd936d133bf8a48de140a3c666897588a05shiqian//
8e35fdd936d133bf8a48de140a3c666897588a05shiqian//     * Redistributions of source code must retain the above copyright
9e35fdd936d133bf8a48de140a3c666897588a05shiqian// notice, this list of conditions and the following disclaimer.
10e35fdd936d133bf8a48de140a3c666897588a05shiqian//     * Redistributions in binary form must reproduce the above
11e35fdd936d133bf8a48de140a3c666897588a05shiqian// copyright notice, this list of conditions and the following disclaimer
12e35fdd936d133bf8a48de140a3c666897588a05shiqian// in the documentation and/or other materials provided with the
13e35fdd936d133bf8a48de140a3c666897588a05shiqian// distribution.
14e35fdd936d133bf8a48de140a3c666897588a05shiqian//     * Neither the name of Google Inc. nor the names of its
15e35fdd936d133bf8a48de140a3c666897588a05shiqian// contributors may be used to endorse or promote products derived from
16e35fdd936d133bf8a48de140a3c666897588a05shiqian// this software without specific prior written permission.
17e35fdd936d133bf8a48de140a3c666897588a05shiqian//
18e35fdd936d133bf8a48de140a3c666897588a05shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19e35fdd936d133bf8a48de140a3c666897588a05shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21e35fdd936d133bf8a48de140a3c666897588a05shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22e35fdd936d133bf8a48de140a3c666897588a05shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23e35fdd936d133bf8a48de140a3c666897588a05shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e35fdd936d133bf8a48de140a3c666897588a05shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e35fdd936d133bf8a48de140a3c666897588a05shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e35fdd936d133bf8a48de140a3c666897588a05shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28e35fdd936d133bf8a48de140a3c666897588a05shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e35fdd936d133bf8a48de140a3c666897588a05shiqian//
30e35fdd936d133bf8a48de140a3c666897588a05shiqian// Author: wan@google.com (Zhanyong Wan)
31e35fdd936d133bf8a48de140a3c666897588a05shiqian
32e35fdd936d133bf8a48de140a3c666897588a05shiqian// Google Mock - a framework for writing C++ mock classes.
33e35fdd936d133bf8a48de140a3c666897588a05shiqian//
34e35fdd936d133bf8a48de140a3c666897588a05shiqian// This file tests the internal utilities.
35e35fdd936d133bf8a48de140a3c666897588a05shiqian
36e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-internal-utils.h>
3716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#include <stdlib.h>
38e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <map>
39e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <string>
40e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <sstream>
41e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <vector>
42e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/gmock.h>
43e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-port.h>
44e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gtest/gtest.h>
45e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gtest/gtest-spi.h>
46e35fdd936d133bf8a48de140a3c666897588a05shiqian
4716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#if GTEST_OS_CYGWIN
4816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#include <sys/types.h>  // For ssize_t. NOLINT
4916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#endif
5016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
51e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing {
52e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
53e35fdd936d133bf8a48de140a3c666897588a05shiqian
54e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace {
55e35fdd936d133bf8a48de140a3c666897588a05shiqian
56b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanusing ::std::tr1::make_tuple;
57e35fdd936d133bf8a48de140a3c666897588a05shiqianusing ::std::tr1::tuple;
58e35fdd936d133bf8a48de140a3c666897588a05shiqian
59ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
60ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("", ConvertIdentifierNameToWords(""));
61ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("", ConvertIdentifierNameToWords("_"));
62ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("", ConvertIdentifierNameToWords("__"));
63ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan}
64ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan
65ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) {
66ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("1", ConvertIdentifierNameToWords("_1"));
67ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("2", ConvertIdentifierNameToWords("2_"));
68ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_"));
69ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56"));
70ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan}
71ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan
72ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) {
73ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord"));
74ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar"));
75ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_"));
76ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_"));
77ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar"));
78ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan}
79ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan
80ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) {
81ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar"));
82ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_"));
83ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_"));
84ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar"));
85ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan}
86ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan
87ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) {
88ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123"));
89ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan  EXPECT_EQ("chapter 11 section 1",
90ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan            ConvertIdentifierNameToWords("_Chapter11Section_1_"));
91ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan}
92ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan
93e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that CompileAssertTypesEqual compiles when the type arguments are
94e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal.
95e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(CompileAssertTypesEqual, CompilesWhenTypesAreEqual) {
96e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<void, void>();
97e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<int*, int*>();
98e35fdd936d133bf8a48de140a3c666897588a05shiqian}
99e35fdd936d133bf8a48de140a3c666897588a05shiqian
100e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that RemoveReference does not affect non-reference types.
101e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) {
102e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<int, RemoveReference<int>::type>();
103e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<const char, RemoveReference<const char>::type>();
104e35fdd936d133bf8a48de140a3c666897588a05shiqian}
105e35fdd936d133bf8a48de140a3c666897588a05shiqian
106e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that RemoveReference removes reference from reference types.
107e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveReferenceTest, RemovesReference) {
108e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<int, RemoveReference<int&>::type>();
109e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<const char, RemoveReference<const char&>::type>();
110e35fdd936d133bf8a48de140a3c666897588a05shiqian}
111e35fdd936d133bf8a48de140a3c666897588a05shiqian
112e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan// Tests GMOCK_REMOVE_REFERENCE_.
113e35fdd936d133bf8a48de140a3c666897588a05shiqian
114e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2>
115e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestGMockRemoveReference() {
116e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  CompileAssertTypesEqual<T1, GMOCK_REMOVE_REFERENCE_(T2)>();
117e35fdd936d133bf8a48de140a3c666897588a05shiqian}
118e35fdd936d133bf8a48de140a3c666897588a05shiqian
119e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveReferenceTest, MacroVersion) {
120e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockRemoveReference<int, int>();
121e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockRemoveReference<const char, const char&>();
122e35fdd936d133bf8a48de140a3c666897588a05shiqian}
123e35fdd936d133bf8a48de140a3c666897588a05shiqian
124e35fdd936d133bf8a48de140a3c666897588a05shiqian
125e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that RemoveConst does not affect non-const types.
126e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveConstTest, DoesNotAffectNonConstType) {
127e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<int, RemoveConst<int>::type>();
128e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<char&, RemoveConst<char&>::type>();
129e35fdd936d133bf8a48de140a3c666897588a05shiqian}
130e35fdd936d133bf8a48de140a3c666897588a05shiqian
131e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that RemoveConst removes const from const types.
132e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveConstTest, RemovesConst) {
133e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<int, RemoveConst<const int>::type>();
134b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>();
135b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>();
136e35fdd936d133bf8a48de140a3c666897588a05shiqian}
137e35fdd936d133bf8a48de140a3c666897588a05shiqian
138e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan// Tests GMOCK_REMOVE_CONST_.
139e35fdd936d133bf8a48de140a3c666897588a05shiqian
140e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2>
141e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestGMockRemoveConst() {
142e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  CompileAssertTypesEqual<T1, GMOCK_REMOVE_CONST_(T2)>();
143e35fdd936d133bf8a48de140a3c666897588a05shiqian}
144e35fdd936d133bf8a48de140a3c666897588a05shiqian
145e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveConstTest, MacroVersion) {
146e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockRemoveConst<int, int>();
147e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockRemoveConst<double&, double&>();
148e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockRemoveConst<char, const char>();
149e35fdd936d133bf8a48de140a3c666897588a05shiqian}
150e35fdd936d133bf8a48de140a3c666897588a05shiqian
151e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that AddReference does not affect reference types.
152e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AddReferenceTest, DoesNotAffectReferenceType) {
153e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<int&, AddReference<int&>::type>();
154e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<const char&, AddReference<const char&>::type>();
155e35fdd936d133bf8a48de140a3c666897588a05shiqian}
156e35fdd936d133bf8a48de140a3c666897588a05shiqian
157e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that AddReference adds reference to non-reference types.
158e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AddReferenceTest, AddsReference) {
159e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<int&, AddReference<int>::type>();
160e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<const char&, AddReference<const char>::type>();
161e35fdd936d133bf8a48de140a3c666897588a05shiqian}
162e35fdd936d133bf8a48de140a3c666897588a05shiqian
163e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan// Tests GMOCK_ADD_REFERENCE_.
164e35fdd936d133bf8a48de140a3c666897588a05shiqian
165e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2>
166e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestGMockAddReference() {
167e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  CompileAssertTypesEqual<T1, GMOCK_ADD_REFERENCE_(T2)>();
168e35fdd936d133bf8a48de140a3c666897588a05shiqian}
169e35fdd936d133bf8a48de140a3c666897588a05shiqian
170e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AddReferenceTest, MacroVersion) {
171e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockAddReference<int&, int>();
172e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockAddReference<const char&, const char&>();
173e35fdd936d133bf8a48de140a3c666897588a05shiqian}
174e35fdd936d133bf8a48de140a3c666897588a05shiqian
175e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan// Tests GMOCK_REFERENCE_TO_CONST_.
176e35fdd936d133bf8a48de140a3c666897588a05shiqian
177e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2>
178e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestGMockReferenceToConst() {
179e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  CompileAssertTypesEqual<T1, GMOCK_REFERENCE_TO_CONST_(T2)>();
180e35fdd936d133bf8a48de140a3c666897588a05shiqian}
181e35fdd936d133bf8a48de140a3c666897588a05shiqian
182e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(GMockReferenceToConstTest, Works) {
183e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockReferenceToConst<const char&, char>();
184e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockReferenceToConst<const int&, const int>();
185e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockReferenceToConst<const double&, double>();
186e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestGMockReferenceToConst<const string&, const string&>();
187e35fdd936d133bf8a48de140a3c666897588a05shiqian}
188e35fdd936d133bf8a48de140a3c666897588a05shiqian
189e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(PointeeOfTest, WorksForSmartPointers) {
190e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<const char,
191e35fdd936d133bf8a48de140a3c666897588a05shiqian      PointeeOf<internal::linked_ptr<const char> >::type>();
192e35fdd936d133bf8a48de140a3c666897588a05shiqian}
193e35fdd936d133bf8a48de140a3c666897588a05shiqian
194e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(PointeeOfTest, WorksForRawPointers) {
195e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<int, PointeeOf<int*>::type>();
196e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>();
197e35fdd936d133bf8a48de140a3c666897588a05shiqian  CompileAssertTypesEqual<void, PointeeOf<void*>::type>();
198e35fdd936d133bf8a48de140a3c666897588a05shiqian}
199e35fdd936d133bf8a48de140a3c666897588a05shiqian
200e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(GetRawPointerTest, WorksForSmartPointers) {
201e35fdd936d133bf8a48de140a3c666897588a05shiqian  const char* const raw_p4 = new const char('a');  // NOLINT
202e35fdd936d133bf8a48de140a3c666897588a05shiqian  const internal::linked_ptr<const char> p4(raw_p4);
203e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ(raw_p4, GetRawPointer(p4));
204e35fdd936d133bf8a48de140a3c666897588a05shiqian}
205e35fdd936d133bf8a48de140a3c666897588a05shiqian
206e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(GetRawPointerTest, WorksForRawPointers) {
207e35fdd936d133bf8a48de140a3c666897588a05shiqian  int* p = NULL;
208e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ(NULL, GetRawPointer(p));
209e35fdd936d133bf8a48de140a3c666897588a05shiqian  int n = 1;
210e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ(&n, GetRawPointer(&n));
211e35fdd936d133bf8a48de140a3c666897588a05shiqian}
212e35fdd936d133bf8a48de140a3c666897588a05shiqian
213e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Base {};
214e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Derived : public Base {};
215e35fdd936d133bf8a48de140a3c666897588a05shiqian
216e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant.
217e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) {
218e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  GMOCK_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true);
219e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  GMOCK_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value),
220e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        const_false);
221e35fdd936d133bf8a48de140a3c666897588a05shiqian}
222e35fdd936d133bf8a48de140a3c666897588a05shiqian
223e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can
224e35fdd936d133bf8a48de140a3c666897588a05shiqian// be implicitly converted to T2.
225e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) {
226e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((ImplicitlyConvertible<int, double>::value));
227e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((ImplicitlyConvertible<double, int>::value));
228e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value));
229e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value));
230e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((ImplicitlyConvertible<Derived&, const Base&>::value));
231e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((ImplicitlyConvertible<const Base, Base>::value));
232e35fdd936d133bf8a48de140a3c666897588a05shiqian}
233e35fdd936d133bf8a48de140a3c666897588a05shiqian
234e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is false when T1
235e35fdd936d133bf8a48de140a3c666897588a05shiqian// cannot be implicitly converted to T2.
236e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) {
237e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value));
238e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value));
239e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value));
240e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE((ImplicitlyConvertible<Base&, Derived&>::value));
241e35fdd936d133bf8a48de140a3c666897588a05shiqian}
242e35fdd936d133bf8a48de140a3c666897588a05shiqian
24316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Tests KindOf<T>.
24416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
24516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Bool) {
24616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool));  // NOLINT
24716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
24816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
24916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Integer) {
25016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char));  // NOLINT
25116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char));  // NOLINT
25216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char));  // NOLINT
25316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short));  // NOLINT
25416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short));  // NOLINT
25516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int));  // NOLINT
25616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int));  // NOLINT
25716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long));  // NOLINT
25816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long));  // NOLINT
25916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t));  // NOLINT
26016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64));  // NOLINT
26116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64));  // NOLINT
26216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t));  // NOLINT
26316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
26416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // ssize_t is not defined on Windows and possibly some other OSes.
26516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t));  // NOLINT
26616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#endif
26716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
26816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
26916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, FloatingPoint) {
27016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float));  // NOLINT
27116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double));  // NOLINT
27216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double));  // NOLINT
27316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
27416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
27516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Other) {
27616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*));  // NOLINT
27716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**));  // NOLINT
27816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base));  // NOLINT
27916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
28016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
28116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Tests LosslessArithmeticConvertible<T, U>.
28216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
28316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToBool) {
28416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
28516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
28616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
28716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToInteger) {
28816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value));
28916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value));
29016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE(
29116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan      (LosslessArithmeticConvertible<bool, unsigned long>::value));  // NOLINT
29216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
29316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
29416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
29516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
29616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
29716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
29816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
29916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
30016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
30116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
30216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
30316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
30416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToInteger) {
30516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Unsigned => larger signed is fine.
30616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value));
30716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
30816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Unsigned => larger unsigned is fine.
30916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE(
31016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan      (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT
31116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
31216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Signed => unsigned is not fine.
31316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT
31416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<
31516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan      signed char, unsigned int>::value));  // NOLINT
31616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
31716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Same size and same signedness: fine too.
31816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<
31916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan               unsigned char, unsigned char>::value));
32016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value));
32116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value));
32216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<
32316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan               unsigned long, unsigned long>::value));  // NOLINT
32416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
32516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Same size, different signedness: not fine.
32616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<
32716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan                unsigned char, signed char>::value));
32816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value));
32916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value));
33016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
33116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Larger size => smaller size is not fine.
33216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value));  // NOLINT
33316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value));
33416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value));
33516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
33616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
33716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) {
33816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Integers cannot be losslessly converted to floating-points, as
33916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // the format of the latter is implementation-defined.
34016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value));
34116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value));
34216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<
34316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan                short, long double>::value));  // NOLINT
34416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
34516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
34616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
34716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
34816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
34916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
35016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
35116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) {
35216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value));  // NOLINT
35316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value));
35416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value));
35516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
35616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
35716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) {
35816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Smaller size => larger size is fine.
35916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value));
36016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value));
36116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value));
36216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
36316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Same size: fine.
36416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value));
36516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value));
36616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
36716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // Larger size => smaller size is not fine.
36816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value));
36916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  if (sizeof(double) == sizeof(long double)) {  // NOLINT
37016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan    // In some implementations (e.g. MSVC), double and long double
37116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan    // have the same size.
37216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan    EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value));
37316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  } else {
37416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan    EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value));
37516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  }
37616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan}
37716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan
378e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is a compile-time constant.
379e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) {
380e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  GMOCK_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value, const_true);
381e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  GMOCK_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false);
382e35fdd936d133bf8a48de140a3c666897588a05shiqian}
383e35fdd936d133bf8a48de140a3c666897588a05shiqian
384e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is true when T is
385e35fdd936d133bf8a48de140a3c666897588a05shiqian// ProtocolMessage or a sub-class of it.
386e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) {
387e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value);
388e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GMOCK_HAS_PROTOBUF_
389e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value);
390e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GMOCK_HAS_PROTOBUF_
391e35fdd936d133bf8a48de140a3c666897588a05shiqian}
392e35fdd936d133bf8a48de140a3c666897588a05shiqian
393e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is false when T is neither
394e35fdd936d133bf8a48de140a3c666897588a05shiqian// ProtocolMessage nor a sub-class of it.
395e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) {
396e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(IsAProtocolMessage<int>::value);
397e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(IsAProtocolMessage<const Base>::value);
398e35fdd936d133bf8a48de140a3c666897588a05shiqian}
399e35fdd936d133bf8a48de140a3c666897588a05shiqian
400e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests IsContainerTest.
401e35fdd936d133bf8a48de140a3c666897588a05shiqian
402e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NonContainer {};
403e35fdd936d133bf8a48de140a3c666897588a05shiqian
404e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsContainerTestTest, WorksForNonContainer) {
405e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0)));
406e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0)));
407e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0)));
408e35fdd936d133bf8a48de140a3c666897588a05shiqian}
409e35fdd936d133bf8a48de140a3c666897588a05shiqian
410e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsContainerTestTest, WorksForContainer) {
41116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(sizeof(IsContainer),
41216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan            sizeof(IsContainerTest<std::vector<bool> >(0)));
41316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  EXPECT_EQ(sizeof(IsContainer),
41416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan            sizeof(IsContainerTest<std::map<int, double> >(0)));
415e35fdd936d133bf8a48de140a3c666897588a05shiqian}
416e35fdd936d133bf8a48de140a3c666897588a05shiqian
417e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests the TupleMatches() template function.
418e35fdd936d133bf8a48de140a3c666897588a05shiqian
419e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize0) {
420e35fdd936d133bf8a48de140a3c666897588a05shiqian  tuple<> matchers;
421e35fdd936d133bf8a48de140a3c666897588a05shiqian  tuple<> values;
422e35fdd936d133bf8a48de140a3c666897588a05shiqian
423e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(TupleMatches(matchers, values));
424e35fdd936d133bf8a48de140a3c666897588a05shiqian}
425e35fdd936d133bf8a48de140a3c666897588a05shiqian
426e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize1) {
427e35fdd936d133bf8a48de140a3c666897588a05shiqian  tuple<Matcher<int> > matchers(Eq(1));
428e35fdd936d133bf8a48de140a3c666897588a05shiqian  tuple<int> values1(1),
429e35fdd936d133bf8a48de140a3c666897588a05shiqian      values2(2);
430e35fdd936d133bf8a48de140a3c666897588a05shiqian
431e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(TupleMatches(matchers, values1));
432e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(TupleMatches(matchers, values2));
433e35fdd936d133bf8a48de140a3c666897588a05shiqian}
434e35fdd936d133bf8a48de140a3c666897588a05shiqian
435e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize2) {
436e35fdd936d133bf8a48de140a3c666897588a05shiqian  tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a'));
437e35fdd936d133bf8a48de140a3c666897588a05shiqian  tuple<int, char> values1(1, 'a'),
438e35fdd936d133bf8a48de140a3c666897588a05shiqian      values2(1, 'b'),
439e35fdd936d133bf8a48de140a3c666897588a05shiqian      values3(2, 'a'),
440e35fdd936d133bf8a48de140a3c666897588a05shiqian      values4(2, 'b');
441e35fdd936d133bf8a48de140a3c666897588a05shiqian
442e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(TupleMatches(matchers, values1));
443e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(TupleMatches(matchers, values2));
444e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(TupleMatches(matchers, values3));
445e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(TupleMatches(matchers, values4));
446e35fdd936d133bf8a48de140a3c666897588a05shiqian}
447e35fdd936d133bf8a48de140a3c666897588a05shiqian
448e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize5) {
449e35fdd936d133bf8a48de140a3c666897588a05shiqian  tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>,  // NOLINT
450e35fdd936d133bf8a48de140a3c666897588a05shiqian      Matcher<string> >
451e35fdd936d133bf8a48de140a3c666897588a05shiqian      matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
452e35fdd936d133bf8a48de140a3c666897588a05shiqian  tuple<int, char, bool, long, string>  // NOLINT
453e35fdd936d133bf8a48de140a3c666897588a05shiqian      values1(1, 'a', true, 2L, "hi"),
454e35fdd936d133bf8a48de140a3c666897588a05shiqian      values2(1, 'a', true, 2L, "hello"),
455e35fdd936d133bf8a48de140a3c666897588a05shiqian      values3(2, 'a', true, 2L, "hi");
456e35fdd936d133bf8a48de140a3c666897588a05shiqian
457e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(TupleMatches(matchers, values1));
458e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(TupleMatches(matchers, values2));
459e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(TupleMatches(matchers, values3));
460e35fdd936d133bf8a48de140a3c666897588a05shiqian}
461e35fdd936d133bf8a48de140a3c666897588a05shiqian
462e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Assert(true, ...) succeeds.
463e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AssertTest, SucceedsOnTrue) {
464e35fdd936d133bf8a48de140a3c666897588a05shiqian  Assert(true, __FILE__, __LINE__, "This should succeed.");
465e35fdd936d133bf8a48de140a3c666897588a05shiqian  Assert(true, __FILE__, __LINE__);  // This should succeed too.
466e35fdd936d133bf8a48de140a3c666897588a05shiqian}
467e35fdd936d133bf8a48de140a3c666897588a05shiqian
468652540a278e56528e576f9ea77515f386698a326zhanyong.wan#if GTEST_HAS_DEATH_TEST
469e35fdd936d133bf8a48de140a3c666897588a05shiqian
470e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Assert(false, ...) generates a fatal failure.
471e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AssertTest, FailsFatallyOnFalse) {
472e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_DEATH({  // NOLINT
473e35fdd936d133bf8a48de140a3c666897588a05shiqian    Assert(false, __FILE__, __LINE__, "This should fail.");
474e35fdd936d133bf8a48de140a3c666897588a05shiqian  }, "");
475e35fdd936d133bf8a48de140a3c666897588a05shiqian
476e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_DEATH({  // NOLINT
477e35fdd936d133bf8a48de140a3c666897588a05shiqian    Assert(false, __FILE__, __LINE__);
478e35fdd936d133bf8a48de140a3c666897588a05shiqian  }, "");
479e35fdd936d133bf8a48de140a3c666897588a05shiqian}
480e35fdd936d133bf8a48de140a3c666897588a05shiqian
481e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GTEST_HAS_DEATH_TEST
482e35fdd936d133bf8a48de140a3c666897588a05shiqian
483e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Expect(true, ...) succeeds.
484e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectTest, SucceedsOnTrue) {
485e35fdd936d133bf8a48de140a3c666897588a05shiqian  Expect(true, __FILE__, __LINE__, "This should succeed.");
486e35fdd936d133bf8a48de140a3c666897588a05shiqian  Expect(true, __FILE__, __LINE__);  // This should succeed too.
487e35fdd936d133bf8a48de140a3c666897588a05shiqian}
488e35fdd936d133bf8a48de140a3c666897588a05shiqian
489e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Expect(false, ...) generates a non-fatal failure.
490e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectTest, FailsNonfatallyOnFalse) {
491e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_NONFATAL_FAILURE({  // NOLINT
492e35fdd936d133bf8a48de140a3c666897588a05shiqian    Expect(false, __FILE__, __LINE__, "This should fail.");
493e35fdd936d133bf8a48de140a3c666897588a05shiqian  }, "This should fail");
494e35fdd936d133bf8a48de140a3c666897588a05shiqian
495e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_NONFATAL_FAILURE({  // NOLINT
496e35fdd936d133bf8a48de140a3c666897588a05shiqian    Expect(false, __FILE__, __LINE__);
497e35fdd936d133bf8a48de140a3c666897588a05shiqian  }, "Expectation failed");
498e35fdd936d133bf8a48de140a3c666897588a05shiqian}
499e35fdd936d133bf8a48de140a3c666897588a05shiqian
5009413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan// Tests LogIsVisible().
5019413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan
5029413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanclass LogIsVisibleTest : public ::testing::Test {
5039413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan protected:
5049413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); }
5059413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; }
5069413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan
5079413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  string original_verbose_;
5089413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan};
5099413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan
5109413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) {
5119413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  GMOCK_FLAG(verbose) = kInfoVerbosity;
5129413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  EXPECT_TRUE(LogIsVisible(INFO));
5139413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  EXPECT_TRUE(LogIsVisible(WARNING));
5149413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan}
5159413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan
5169413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) {
5179413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  GMOCK_FLAG(verbose) = kErrorVerbosity;
5189413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  EXPECT_FALSE(LogIsVisible(INFO));
5199413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  EXPECT_FALSE(LogIsVisible(WARNING));
5209413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan}
5219413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan
5229413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
5239413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  GMOCK_FLAG(verbose) = kWarningVerbosity;
5249413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  EXPECT_FALSE(LogIsVisible(INFO));
5259413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan  EXPECT_TRUE(LogIsVisible(WARNING));
5269413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan}
5279413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan
528e35fdd936d133bf8a48de140a3c666897588a05shiqian// TODO(wan@google.com): find a way to re-enable these tests.
529e35fdd936d133bf8a48de140a3c666897588a05shiqian#if 0
530e35fdd936d133bf8a48de140a3c666897588a05shiqian
531e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests the Log() function.
532e35fdd936d133bf8a48de140a3c666897588a05shiqian
533e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that Log() behaves correctly for the given verbosity level
534e35fdd936d133bf8a48de140a3c666897588a05shiqian// and log severity.
535e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestLogWithSeverity(const string& verbosity, LogSeverity severity,
536e35fdd936d133bf8a48de140a3c666897588a05shiqian                         bool should_print) {
537e35fdd936d133bf8a48de140a3c666897588a05shiqian  const string old_flag = GMOCK_FLAG(verbose);
538e35fdd936d133bf8a48de140a3c666897588a05shiqian  GMOCK_FLAG(verbose) = verbosity;
539e35fdd936d133bf8a48de140a3c666897588a05shiqian  CaptureTestStdout();
540e35fdd936d133bf8a48de140a3c666897588a05shiqian  Log(severity, "Test log.\n", 0);
541e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (should_print) {
542e35fdd936d133bf8a48de140a3c666897588a05shiqian    EXPECT_PRED2(RE::FullMatch,
543e35fdd936d133bf8a48de140a3c666897588a05shiqian                 GetCapturedTestStdout(),
544e35fdd936d133bf8a48de140a3c666897588a05shiqian                 severity == WARNING ?
545e35fdd936d133bf8a48de140a3c666897588a05shiqian                 "\nGMOCK WARNING:\nTest log\\.\nStack trace:\n[\\s\\S]*" :
546e35fdd936d133bf8a48de140a3c666897588a05shiqian                 "\nTest log\\.\nStack trace:\n[\\s\\S]*");
547e35fdd936d133bf8a48de140a3c666897588a05shiqian  } else {
548e35fdd936d133bf8a48de140a3c666897588a05shiqian    EXPECT_EQ("", GetCapturedTestStdout());
549e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
550e35fdd936d133bf8a48de140a3c666897588a05shiqian  GMOCK_FLAG(verbose) = old_flag;
551e35fdd936d133bf8a48de140a3c666897588a05shiqian}
552e35fdd936d133bf8a48de140a3c666897588a05shiqian
553e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that when the stack_frames_to_skip parameter is negative,
554e35fdd936d133bf8a48de140a3c666897588a05shiqian// Log() doesn't include the stack trace in the output.
555e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
556e35fdd936d133bf8a48de140a3c666897588a05shiqian  GMOCK_FLAG(verbose) = kInfoVerbosity;
557e35fdd936d133bf8a48de140a3c666897588a05shiqian  CaptureTestStdout();
558e35fdd936d133bf8a48de140a3c666897588a05shiqian  Log(INFO, "Test log.\n", -1);
559e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ("\nTest log.\n", GetCapturedTestStdout());
560e35fdd936d133bf8a48de140a3c666897588a05shiqian}
561e35fdd936d133bf8a48de140a3c666897588a05shiqian
562e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that in opt mode, a positive stack_frames_to_skip argument is
563e35fdd936d133bf8a48de140a3c666897588a05shiqian// treated as 0.
564e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoSkippingStackFrameInOptMode) {
565e35fdd936d133bf8a48de140a3c666897588a05shiqian  CaptureTestStdout();
566e35fdd936d133bf8a48de140a3c666897588a05shiqian  Log(WARNING, "Test log.\n", 100);
567e35fdd936d133bf8a48de140a3c666897588a05shiqian  const string log = GetCapturedTestStdout();
568e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifdef NDEBUG
569e35fdd936d133bf8a48de140a3c666897588a05shiqian  // In opt mode, no stack frame should be skipped.
570e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n"
571e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 "Test log\\.\n"
572e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 "Stack trace:\n"
573e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 ".+"));
574e35fdd936d133bf8a48de140a3c666897588a05shiqian#else
575e35fdd936d133bf8a48de140a3c666897588a05shiqian  // In dbg mode, the stack frames should be skipped.
576e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ("\nGMOCK WARNING:\n"
577e35fdd936d133bf8a48de140a3c666897588a05shiqian            "Test log.\n"
578e35fdd936d133bf8a48de140a3c666897588a05shiqian            "Stack trace:\n", log);
579e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // NDEBUG
580e35fdd936d133bf8a48de140a3c666897588a05shiqian}
581e35fdd936d133bf8a48de140a3c666897588a05shiqian
582e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that all logs are printed when the value of the
583e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "info".
584e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) {
585e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestLogWithSeverity(kInfoVerbosity, INFO, true);
586e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestLogWithSeverity(kInfoVerbosity, WARNING, true);
587e35fdd936d133bf8a48de140a3c666897588a05shiqian}
588e35fdd936d133bf8a48de140a3c666897588a05shiqian
589e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that only warnings are printed when the value of the
590e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "warning".
591e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) {
592e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestLogWithSeverity(kWarningVerbosity, INFO, false);
593e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestLogWithSeverity(kWarningVerbosity, WARNING, true);
594e35fdd936d133bf8a48de140a3c666897588a05shiqian}
595e35fdd936d133bf8a48de140a3c666897588a05shiqian
596e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that no logs are printed when the value of the
597e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "error".
598e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) {
599e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestLogWithSeverity(kErrorVerbosity, INFO, false);
600e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestLogWithSeverity(kErrorVerbosity, WARNING, false);
601e35fdd936d133bf8a48de140a3c666897588a05shiqian}
602e35fdd936d133bf8a48de140a3c666897588a05shiqian
603e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that only warnings are printed when the value of the
604e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is invalid.
605e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
606e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestLogWithSeverity("invalid", INFO, false);
607e35fdd936d133bf8a48de140a3c666897588a05shiqian  TestLogWithSeverity("invalid", WARNING, true);
608e35fdd936d133bf8a48de140a3c666897588a05shiqian}
609e35fdd936d133bf8a48de140a3c666897588a05shiqian
610e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // 0
611e35fdd936d133bf8a48de140a3c666897588a05shiqian
612e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, true_type) {
613e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(true_type::value);
614e35fdd936d133bf8a48de140a3c666897588a05shiqian}
615e35fdd936d133bf8a48de140a3c666897588a05shiqian
616e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, false_type) {
617e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(false_type::value);
618e35fdd936d133bf8a48de140a3c666897588a05shiqian}
619e35fdd936d133bf8a48de140a3c666897588a05shiqian
620e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, is_reference) {
621e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(is_reference<int>::value);
622e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(is_reference<char*>::value);
623e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(is_reference<const int&>::value);
624e35fdd936d133bf8a48de140a3c666897588a05shiqian}
625e35fdd936d133bf8a48de140a3c666897588a05shiqian
626e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, is_pointer) {
627e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(is_pointer<int>::value);
628e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE(is_pointer<char&>::value);
629e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE(is_pointer<const int*>::value);
630e35fdd936d133bf8a48de140a3c666897588a05shiqian}
631e35fdd936d133bf8a48de140a3c666897588a05shiqian
632e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, type_equals) {
633e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE((type_equals<int, const int>::value));
634e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE((type_equals<int, int&>::value));
635e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_FALSE((type_equals<int, double>::value));
636e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((type_equals<char, char>::value));
637e35fdd936d133bf8a48de140a3c666897588a05shiqian}
638e35fdd936d133bf8a48de140a3c666897588a05shiqian
639e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, remove_reference) {
640e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value));
641e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((type_equals<const int,
642e35fdd936d133bf8a48de140a3c666897588a05shiqian               remove_reference<const int&>::type>::value));
643e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value));
644e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value));
645e35fdd936d133bf8a48de140a3c666897588a05shiqian}
646e35fdd936d133bf8a48de140a3c666897588a05shiqian
647e35fdd936d133bf8a48de140a3c666897588a05shiqian// TODO(wan@google.com): find a way to re-enable these tests.
648e35fdd936d133bf8a48de140a3c666897588a05shiqian#if 0
649e35fdd936d133bf8a48de140a3c666897588a05shiqian
650e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that Log() behaves correctly for the given verbosity level
651e35fdd936d133bf8a48de140a3c666897588a05shiqian// and log severity.
652e35fdd936d133bf8a48de140a3c666897588a05shiqianstring GrabOutput(void(*logger)(), const char* verbosity) {
653e35fdd936d133bf8a48de140a3c666897588a05shiqian  const string saved_flag = GMOCK_FLAG(verbose);
654e35fdd936d133bf8a48de140a3c666897588a05shiqian  GMOCK_FLAG(verbose) = verbosity;
655e35fdd936d133bf8a48de140a3c666897588a05shiqian  CaptureTestStdout();
656e35fdd936d133bf8a48de140a3c666897588a05shiqian  logger();
657e35fdd936d133bf8a48de140a3c666897588a05shiqian  GMOCK_FLAG(verbose) = saved_flag;
658e35fdd936d133bf8a48de140a3c666897588a05shiqian  return GetCapturedTestStdout();
659e35fdd936d133bf8a48de140a3c666897588a05shiqian}
660e35fdd936d133bf8a48de140a3c666897588a05shiqian
661e35fdd936d133bf8a48de140a3c666897588a05shiqianclass DummyMock {
662e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
663e35fdd936d133bf8a48de140a3c666897588a05shiqian  MOCK_METHOD0(TestMethod, void());
664e35fdd936d133bf8a48de140a3c666897588a05shiqian  MOCK_METHOD1(TestMethodArg, void(int dummy));
665e35fdd936d133bf8a48de140a3c666897588a05shiqian};
666e35fdd936d133bf8a48de140a3c666897588a05shiqian
667e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExpectCallLogger() {
668e35fdd936d133bf8a48de140a3c666897588a05shiqian  DummyMock mock;
669e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_CALL(mock, TestMethod());
670e35fdd936d133bf8a48de140a3c666897588a05shiqian  mock.TestMethod();
671e35fdd936d133bf8a48de140a3c666897588a05shiqian};
672e35fdd936d133bf8a48de140a3c666897588a05shiqian
673e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
674e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
675e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity),
676e35fdd936d133bf8a48de140a3c666897588a05shiqian              HasSubstr("EXPECT_CALL(mock, TestMethod())"));
677e35fdd936d133bf8a48de140a3c666897588a05shiqian}
678e35fdd936d133bf8a48de140a3c666897588a05shiqian
679e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL doesn't log
680e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "warning".
681e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
682e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity));
683e35fdd936d133bf8a48de140a3c666897588a05shiqian}
684e35fdd936d133bf8a48de140a3c666897588a05shiqian
685e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL doesn't log
686e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "error".
687e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest,  DoesNotLogWhenVerbosityIsError) {
688e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity));
689e35fdd936d133bf8a48de140a3c666897588a05shiqian}
690e35fdd936d133bf8a48de140a3c666897588a05shiqian
691e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid OnCallLogger() {
692e35fdd936d133bf8a48de140a3c666897588a05shiqian  DummyMock mock;
693e35fdd936d133bf8a48de140a3c666897588a05shiqian  ON_CALL(mock, TestMethod());
694e35fdd936d133bf8a48de140a3c666897588a05shiqian};
695e35fdd936d133bf8a48de140a3c666897588a05shiqian
696e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
697e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, LogsWhenVerbosityIsInfo) {
698e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity),
699e35fdd936d133bf8a48de140a3c666897588a05shiqian              HasSubstr("ON_CALL(mock, TestMethod())"));
700e35fdd936d133bf8a48de140a3c666897588a05shiqian}
701e35fdd936d133bf8a48de140a3c666897588a05shiqian
702e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL doesn't log
703e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "warning".
704e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
705e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ("", GrabOutput(OnCallLogger, kWarningVerbosity));
706e35fdd936d133bf8a48de140a3c666897588a05shiqian}
707e35fdd936d133bf8a48de140a3c666897588a05shiqian
708e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL doesn't log if
709e35fdd936d133bf8a48de140a3c666897588a05shiqian// the --gmock_verbose flag is set to "error".
710e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
711e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_EQ("", GrabOutput(OnCallLogger, kErrorVerbosity));
712e35fdd936d133bf8a48de140a3c666897588a05shiqian}
713e35fdd936d133bf8a48de140a3c666897588a05shiqian
714e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid OnCallAnyArgumentLogger() {
715e35fdd936d133bf8a48de140a3c666897588a05shiqian  DummyMock mock;
716e35fdd936d133bf8a48de140a3c666897588a05shiqian  ON_CALL(mock, TestMethodArg(_));
717e35fdd936d133bf8a48de140a3c666897588a05shiqian}
718e35fdd936d133bf8a48de140a3c666897588a05shiqian
719e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL prints provided _ argument.
720e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, LogsAnythingArgument) {
721e35fdd936d133bf8a48de140a3c666897588a05shiqian  EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity),
722e35fdd936d133bf8a48de140a3c666897588a05shiqian              HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
723e35fdd936d133bf8a48de140a3c666897588a05shiqian}
724e35fdd936d133bf8a48de140a3c666897588a05shiqian
725e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // 0
726e35fdd936d133bf8a48de140a3c666897588a05shiqian
727b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests ArrayEq().
728b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
729b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForDegeneratedArrays) {
730b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(5, 5L));
731b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_FALSE(ArrayEq('a', 0));
732b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
733b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
734b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForOneDimensionalArrays) {
735b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const int a[] = { 0, 1 };
736b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  long b[] = { 0, 1 };
737b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(a, b));
738b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(a, 2, b));
739b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
740b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  b[0] = 2;
741b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_FALSE(ArrayEq(a, b));
742b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_FALSE(ArrayEq(a, 1, b));
743b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
744b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
745b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForTwoDimensionalArrays) {
746b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const char a[][3] = { "hi", "lo" };
747b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const char b[][3] = { "hi", "lo" };
748b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const char c[][3] = { "hi", "li" };
749b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
750b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(a, b));
751b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(a, 2, b));
752b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
753b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_FALSE(ArrayEq(a, c));
754b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_FALSE(ArrayEq(a, 2, c));
755b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
756b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
757b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests ArrayAwareFind().
758b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
759b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayAwareFindTest, WorksForOneDimensionalArray) {
760b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const char a[] = "hello";
761b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o'));
762b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x'));
763b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
764b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
765b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) {
766b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
767b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const int b[2] = { 2, 3 };
768b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b));
769b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
770b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const int c[2] = { 6, 7 };
771b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c));
772b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
773b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
774b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests CopyArray().
775b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
776b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForDegeneratedArrays) {
777b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int n = 0;
778b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  CopyArray('a', &n);
779b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ('a', n);
780b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
781b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
782b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForOneDimensionalArrays) {
783b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const char a[3] = "hi";
784b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int b[3];
785b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  CopyArray(a, &b);
786b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(a, b));
787b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
788b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int c[3];
789b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  CopyArray(a, 3, c);
790b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(a, c));
791b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
792b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
793b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForTwoDimensionalArrays) {
794b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } };
795b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int b[2][3];
796b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  CopyArray(a, &b);
797b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(a, b));
798b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
799b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int c[2][3];
800b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  CopyArray(a, 2, c);
801b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_TRUE(ArrayEq(a, c));
802b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
803b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
804b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests NativeArray.
805b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
806b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) {
807b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const int a[3] = { 0, 1, 2 };
808b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<int> na(a, kReference);
809b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(3, na.size());
810b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a, na.begin());
811b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
812b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
813b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, ConstructorFromTupleWorks) {
814b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int a[3] = { 0, 1, 2 };
81590c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan  int* const p = a;
816b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Tests with a plain pointer.
81790c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan  NativeArray<int> na(make_tuple(p, 3U), kReference);
818b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a, na.begin());
819b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
820b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const linked_ptr<char> b(new char);
821b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  *b = 'a';
822b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Tests with a smart pointer.
823b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<char> nb(make_tuple(b, 1), kCopy);
824b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_NE(b.get(), nb.begin());
825b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ('a', nb.begin()[0]);
826b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
827b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
828b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) {
829b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef int Array[2];
830b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  Array* a = new Array[1];
831b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  (*a)[0] = 0;
832b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  (*a)[1] = 1;
833b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<int> na(*a, kCopy);
834b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_NE(*a, na.begin());
835b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  delete[] a;
836b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(0, na.begin()[0]);
837b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(1, na.begin()[1]);
838b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
839b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // We rely on the heap checker to verify that na deletes the copy of
840b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // array.
841b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
842b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
843b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, TypeMembersAreCorrect) {
844b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<char, NativeArray<char>::value_type>();
845b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>();
846b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
847b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>();
848b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>();
849b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
850b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
851b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, MethodsWork) {
852b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const int a[] = { 0, 1, 2 };
853b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<int> na(a, kCopy);
854b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  ASSERT_EQ(3, na.size());
855b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(3, na.end() - na.begin());
856b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
857b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<int>::const_iterator it = na.begin();
858b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(0, *it);
859b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  ++it;
860b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(1, *it);
861b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  it++;
862b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(2, *it);
863b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  ++it;
864b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(na.end(), it);
865b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
866b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_THAT(na, Eq(na));
867b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
868b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<int> na2(a, kReference);
869b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_THAT(na, Eq(na2));
870b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
871b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const int b1[] = { 0, 1, 1 };
872b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const int b2[] = { 0, 1, 2, 3 };
873b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, kReference))));
874b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, kCopy))));
875b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
876b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
877b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, WorksForTwoDimensionalArray) {
878b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const char a[2][3] = { "hi", "lo" };
879b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<char[3]> na(a, kReference);
880b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  ASSERT_EQ(2, na.size());
881b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a, na.begin());
882b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
883b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
884b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests StlContainerView.
885b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
886b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForStlContainer) {
887b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<std::vector<int>,
888b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<std::vector<int> >::type>();
889b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<const std::vector<double>&,
890b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<std::vector<double> >::const_reference>();
891b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
892b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef std::vector<char> Chars;
893b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  Chars v1;
894b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const Chars& v2(StlContainerView<Chars>::ConstReference(v1));
895b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(&v1, &v2);
896b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
897b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  v1.push_back('a');
898b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  Chars v3 = StlContainerView<Chars>::Copy(v1);
899b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_THAT(v3, Eq(v3));
900b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
901b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
902b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForStaticNativeArray) {
903b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<NativeArray<int>,
904b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<int[3]>::type>();
905b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<NativeArray<double>,
906b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<const double[4]>::type>();
907b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<NativeArray<char[3]>,
908b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<const char[2][3]>::type>();
909b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
910b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<const NativeArray<int>,
911b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<int[2]>::const_reference>();
912b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
913b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int a1[3] = { 0, 1, 2 };
914b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
915b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(3, a2.size());
916b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a1, a2.begin());
917b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
918b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1);
919b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  ASSERT_EQ(3, a3.size());
920b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(0, a3.begin()[0]);
921b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(1, a3.begin()[1]);
922b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(2, a3.begin()[2]);
923b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
924b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Makes sure a1 and a3 aren't aliases.
925b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  a1[0] = 3;
926b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(0, a3.begin()[0]);
927b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
928b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
929b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForDynamicNativeArray) {
930b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<NativeArray<int>,
931b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<tuple<const int*, size_t> >::type>();
932b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<NativeArray<double>,
933b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<tuple<linked_ptr<double>, int> >::type>();
934b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
935b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  StaticAssertTypeEq<const NativeArray<int>,
936b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      StlContainerView<tuple<const int*, int> >::const_reference>();
937b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
938b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  int a1[3] = { 0, 1, 2 };
93990c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan  const int* const p1 = a1;
940b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >::
94190c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan      ConstReference(make_tuple(p1, 3));
942b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(3, a2.size());
943b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(a1, a2.begin());
944b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
945b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >::
9462661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan      Copy(make_tuple(static_cast<int*>(a1), 3));
947b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  ASSERT_EQ(3, a3.size());
948b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(0, a3.begin()[0]);
949b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(1, a3.begin()[1]);
950b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(2, a3.begin()[2]);
951b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
952b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Makes sure a1 and a3 aren't aliases.
953b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  a1[0] = 3;
954b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  EXPECT_EQ(0, a3.begin()[0]);
955b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
956b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
957e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace
958e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
959e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace testing
960