gmock-internal-utils_test.cc revision 76c1c612e23d87874669faf0b1c8f74caa4a7eb4
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 510ea67f88aea208e7ef13c91b374b352679beab38zhanyong.wanclass ProtocolMessage; 520ea67f88aea208e7ef13c91b374b352679beab38zhanyong.wan 530ea67f88aea208e7ef13c91b374b352679beab38zhanyong.wannamespace proto2 { 540ea67f88aea208e7ef13c91b374b352679beab38zhanyong.wanclass Message; 550ea67f88aea208e7ef13c91b374b352679beab38zhanyong.wan} // namespace proto2 560ea67f88aea208e7ef13c91b374b352679beab38zhanyong.wan 57e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing { 58e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 59e35fdd936d133bf8a48de140a3c666897588a05shiqian 60e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace { 61e35fdd936d133bf8a48de140a3c666897588a05shiqian 62b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanusing ::std::tr1::make_tuple; 63e35fdd936d133bf8a48de140a3c666897588a05shiqianusing ::std::tr1::tuple; 64e35fdd936d133bf8a48de140a3c666897588a05shiqian 65ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) { 66ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("", ConvertIdentifierNameToWords("")); 67ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("", ConvertIdentifierNameToWords("_")); 68ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("", ConvertIdentifierNameToWords("__")); 69ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan} 70ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan 71ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) { 72ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("1", ConvertIdentifierNameToWords("_1")); 73ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("2", ConvertIdentifierNameToWords("2_")); 74ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_")); 75ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56")); 76ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan} 77ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan 78ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) { 79ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord")); 80ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar")); 81ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_")); 82ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_")); 83ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar")); 84ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan} 85ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan 86ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) { 87ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar")); 88ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_")); 89ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_")); 90ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar")); 91ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan} 92ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan 93ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wanTEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) { 94ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123")); 95ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan EXPECT_EQ("chapter 11 section 1", 96ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan ConvertIdentifierNameToWords("_Chapter11Section_1_")); 97ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan} 98ce198ff8997775d63b802615ee0cea5886ab82abzhanyong.wan 99e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that CompileAssertTypesEqual compiles when the type arguments are 100e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal. 101e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(CompileAssertTypesEqual, CompilesWhenTypesAreEqual) { 102e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<void, void>(); 103e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<int*, int*>(); 104e35fdd936d133bf8a48de140a3c666897588a05shiqian} 105e35fdd936d133bf8a48de140a3c666897588a05shiqian 106e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that RemoveReference does not affect non-reference types. 107e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) { 108e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<int, RemoveReference<int>::type>(); 109e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<const char, RemoveReference<const char>::type>(); 110e35fdd936d133bf8a48de140a3c666897588a05shiqian} 111e35fdd936d133bf8a48de140a3c666897588a05shiqian 112e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that RemoveReference removes reference from reference types. 113e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveReferenceTest, RemovesReference) { 114e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<int, RemoveReference<int&>::type>(); 115e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<const char, RemoveReference<const char&>::type>(); 116e35fdd936d133bf8a48de140a3c666897588a05shiqian} 117e35fdd936d133bf8a48de140a3c666897588a05shiqian 118e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan// Tests GMOCK_REMOVE_REFERENCE_. 119e35fdd936d133bf8a48de140a3c666897588a05shiqian 120e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2> 121e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestGMockRemoveReference() { 122e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan CompileAssertTypesEqual<T1, GMOCK_REMOVE_REFERENCE_(T2)>(); 123e35fdd936d133bf8a48de140a3c666897588a05shiqian} 124e35fdd936d133bf8a48de140a3c666897588a05shiqian 125e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveReferenceTest, MacroVersion) { 126e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockRemoveReference<int, int>(); 127e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockRemoveReference<const char, const char&>(); 128e35fdd936d133bf8a48de140a3c666897588a05shiqian} 129e35fdd936d133bf8a48de140a3c666897588a05shiqian 130e35fdd936d133bf8a48de140a3c666897588a05shiqian 131e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that RemoveConst does not affect non-const types. 132e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveConstTest, DoesNotAffectNonConstType) { 133e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<int, RemoveConst<int>::type>(); 134e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<char&, RemoveConst<char&>::type>(); 135e35fdd936d133bf8a48de140a3c666897588a05shiqian} 136e35fdd936d133bf8a48de140a3c666897588a05shiqian 137e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that RemoveConst removes const from const types. 138e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveConstTest, RemovesConst) { 139e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<int, RemoveConst<const int>::type>(); 140b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>(); 141b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>(); 142e35fdd936d133bf8a48de140a3c666897588a05shiqian} 143e35fdd936d133bf8a48de140a3c666897588a05shiqian 144e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan// Tests GMOCK_REMOVE_CONST_. 145e35fdd936d133bf8a48de140a3c666897588a05shiqian 146e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2> 147e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestGMockRemoveConst() { 148e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan CompileAssertTypesEqual<T1, GMOCK_REMOVE_CONST_(T2)>(); 149e35fdd936d133bf8a48de140a3c666897588a05shiqian} 150e35fdd936d133bf8a48de140a3c666897588a05shiqian 151e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(RemoveConstTest, MacroVersion) { 152e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockRemoveConst<int, int>(); 153e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockRemoveConst<double&, double&>(); 154e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockRemoveConst<char, const char>(); 155e35fdd936d133bf8a48de140a3c666897588a05shiqian} 156e35fdd936d133bf8a48de140a3c666897588a05shiqian 157e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that AddReference does not affect reference types. 158e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AddReferenceTest, DoesNotAffectReferenceType) { 159e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<int&, AddReference<int&>::type>(); 160e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<const char&, AddReference<const char&>::type>(); 161e35fdd936d133bf8a48de140a3c666897588a05shiqian} 162e35fdd936d133bf8a48de140a3c666897588a05shiqian 163e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that AddReference adds reference to non-reference types. 164e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AddReferenceTest, AddsReference) { 165e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<int&, AddReference<int>::type>(); 166e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<const char&, AddReference<const char>::type>(); 167e35fdd936d133bf8a48de140a3c666897588a05shiqian} 168e35fdd936d133bf8a48de140a3c666897588a05shiqian 169e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan// Tests GMOCK_ADD_REFERENCE_. 170e35fdd936d133bf8a48de140a3c666897588a05shiqian 171e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2> 172e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestGMockAddReference() { 173e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan CompileAssertTypesEqual<T1, GMOCK_ADD_REFERENCE_(T2)>(); 174e35fdd936d133bf8a48de140a3c666897588a05shiqian} 175e35fdd936d133bf8a48de140a3c666897588a05shiqian 176e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AddReferenceTest, MacroVersion) { 177e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockAddReference<int&, int>(); 178e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockAddReference<const char&, const char&>(); 179e35fdd936d133bf8a48de140a3c666897588a05shiqian} 180e35fdd936d133bf8a48de140a3c666897588a05shiqian 181e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan// Tests GMOCK_REFERENCE_TO_CONST_. 182e35fdd936d133bf8a48de140a3c666897588a05shiqian 183e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T1, typename T2> 184e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestGMockReferenceToConst() { 185e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan CompileAssertTypesEqual<T1, GMOCK_REFERENCE_TO_CONST_(T2)>(); 186e35fdd936d133bf8a48de140a3c666897588a05shiqian} 187e35fdd936d133bf8a48de140a3c666897588a05shiqian 188e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(GMockReferenceToConstTest, Works) { 189e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockReferenceToConst<const char&, char>(); 190e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockReferenceToConst<const int&, const int>(); 191e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockReferenceToConst<const double&, double>(); 192e35fdd936d133bf8a48de140a3c666897588a05shiqian TestGMockReferenceToConst<const string&, const string&>(); 193e35fdd936d133bf8a48de140a3c666897588a05shiqian} 194e35fdd936d133bf8a48de140a3c666897588a05shiqian 195e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(PointeeOfTest, WorksForSmartPointers) { 196e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<const char, 197e35fdd936d133bf8a48de140a3c666897588a05shiqian PointeeOf<internal::linked_ptr<const char> >::type>(); 198e35fdd936d133bf8a48de140a3c666897588a05shiqian} 199e35fdd936d133bf8a48de140a3c666897588a05shiqian 200e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(PointeeOfTest, WorksForRawPointers) { 201e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<int, PointeeOf<int*>::type>(); 202e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>(); 203e35fdd936d133bf8a48de140a3c666897588a05shiqian CompileAssertTypesEqual<void, PointeeOf<void*>::type>(); 204e35fdd936d133bf8a48de140a3c666897588a05shiqian} 205e35fdd936d133bf8a48de140a3c666897588a05shiqian 206e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(GetRawPointerTest, WorksForSmartPointers) { 207e35fdd936d133bf8a48de140a3c666897588a05shiqian const char* const raw_p4 = new const char('a'); // NOLINT 208e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::linked_ptr<const char> p4(raw_p4); 209e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(raw_p4, GetRawPointer(p4)); 210e35fdd936d133bf8a48de140a3c666897588a05shiqian} 211e35fdd936d133bf8a48de140a3c666897588a05shiqian 212e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(GetRawPointerTest, WorksForRawPointers) { 213e35fdd936d133bf8a48de140a3c666897588a05shiqian int* p = NULL; 21495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // Don't use EXPECT_EQ as no NULL-testing magic on Symbian. 21595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan EXPECT_TRUE(NULL == GetRawPointer(p)); 216e35fdd936d133bf8a48de140a3c666897588a05shiqian int n = 1; 217e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(&n, GetRawPointer(&n)); 218e35fdd936d133bf8a48de140a3c666897588a05shiqian} 219e35fdd936d133bf8a48de140a3c666897588a05shiqian 220e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Base {}; 221e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Derived : public Base {}; 222e35fdd936d133bf8a48de140a3c666897588a05shiqian 223e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant. 224e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) { 225e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true); 226e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value), 227e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan const_false); 228e35fdd936d133bf8a48de140a3c666897588a05shiqian} 229e35fdd936d133bf8a48de140a3c666897588a05shiqian 230e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can 231e35fdd936d133bf8a48de140a3c666897588a05shiqian// be implicitly converted to T2. 232e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) { 233e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<int, double>::value)); 234e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<double, int>::value)); 235e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value)); 236e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value)); 237e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<Derived&, const Base&>::value)); 238e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<const Base, Base>::value)); 239e35fdd936d133bf8a48de140a3c666897588a05shiqian} 240e35fdd936d133bf8a48de140a3c666897588a05shiqian 241e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is false when T1 242e35fdd936d133bf8a48de140a3c666897588a05shiqian// cannot be implicitly converted to T2. 243e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) { 244e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value)); 245e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value)); 246e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value)); 247e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((ImplicitlyConvertible<Base&, Derived&>::value)); 248e35fdd936d133bf8a48de140a3c666897588a05shiqian} 249e35fdd936d133bf8a48de140a3c666897588a05shiqian 25016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Tests KindOf<T>. 25116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 25216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Bool) { 25316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT 25416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 25516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 25616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Integer) { 25716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT 25816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT 25916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT 26016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT 26116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT 26216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT 26316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT 26416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT 26516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT 26616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT 26716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64)); // NOLINT 26816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64)); // NOLINT 26916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT 27016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN 27116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // ssize_t is not defined on Windows and possibly some other OSes. 27216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT 27316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#endif 27416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 27516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 27616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, FloatingPoint) { 27716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT 27816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT 27916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT 28016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 28116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Other) { 28316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT 28416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT 28516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT 28616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 28716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Tests LosslessArithmeticConvertible<T, U>. 28916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 29016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToBool) { 29116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value)); 29216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 29316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 29416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToInteger) { 29516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value)); 29616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value)); 29716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE( 29816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT 29916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 30016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 30116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) { 30216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value)); 30316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value)); 30416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 30516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 30616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToBool) { 30716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value)); 30816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value)); 30916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 31016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToInteger) { 31216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Unsigned => larger signed is fine. 31316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value)); 31416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Unsigned => larger unsigned is fine. 31616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE( 31716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT 31816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Signed => unsigned is not fine. 32016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT 32116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible< 32216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan signed char, unsigned int>::value)); // NOLINT 32316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 32416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Same size and same signedness: fine too. 32516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible< 32616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan unsigned char, unsigned char>::value)); 32716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value)); 32816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value)); 32916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible< 33016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan unsigned long, unsigned long>::value)); // NOLINT 33116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 33216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Same size, different signedness: not fine. 33316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible< 33416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan unsigned char, signed char>::value)); 33516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value)); 33616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value)); 33716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 33816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Larger size => smaller size is not fine. 33916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT 34016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value)); 34116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value)); 34216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 34316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 34416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) { 34516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Integers cannot be losslessly converted to floating-points, as 34616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // the format of the latter is implementation-defined. 34716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value)); 34816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value)); 34916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible< 35016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan short, long double>::value)); // NOLINT 35116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 35216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 35316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) { 35416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value)); 35516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value)); 35616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 35716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 35816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) { 35916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT 36016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value)); 36116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value)); 36216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 36316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 36416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) { 36516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Smaller size => larger size is fine. 36616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value)); 36716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value)); 36816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value)); 36916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 37016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Same size: fine. 37116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value)); 37216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value)); 37316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 37416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Larger size => smaller size is not fine. 37516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value)); 37616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan if (sizeof(double) == sizeof(long double)) { // NOLINT 37716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // In some implementations (e.g. MSVC), double and long double 37816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // have the same size. 37916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value)); 38016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan } else { 38116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value)); 38216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan } 38316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 38416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 385e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is a compile-time constant. 386e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { 387e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value, const_true); 388e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false); 389e35fdd936d133bf8a48de140a3c666897588a05shiqian} 390e35fdd936d133bf8a48de140a3c666897588a05shiqian 391e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is true when T is 392e35fdd936d133bf8a48de140a3c666897588a05shiqian// ProtocolMessage or a sub-class of it. 393e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { 3940ea67f88aea208e7ef13c91b374b352679beab38zhanyong.wan EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); 395e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); 396e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GMOCK_HAS_PROTOBUF_ 397e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); 398e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_HAS_PROTOBUF_ 399e35fdd936d133bf8a48de140a3c666897588a05shiqian} 400e35fdd936d133bf8a48de140a3c666897588a05shiqian 401e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is false when T is neither 402e35fdd936d133bf8a48de140a3c666897588a05shiqian// ProtocolMessage nor a sub-class of it. 403e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) { 404e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(IsAProtocolMessage<int>::value); 405e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(IsAProtocolMessage<const Base>::value); 406e35fdd936d133bf8a48de140a3c666897588a05shiqian} 407e35fdd936d133bf8a48de140a3c666897588a05shiqian 408e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests IsContainerTest. 409e35fdd936d133bf8a48de140a3c666897588a05shiqian 410e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NonContainer {}; 411e35fdd936d133bf8a48de140a3c666897588a05shiqian 412e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsContainerTestTest, WorksForNonContainer) { 413e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0))); 414e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0))); 415e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0))); 416e35fdd936d133bf8a48de140a3c666897588a05shiqian} 417e35fdd936d133bf8a48de140a3c666897588a05shiqian 418e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsContainerTestTest, WorksForContainer) { 41916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(sizeof(IsContainer), 42016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan sizeof(IsContainerTest<std::vector<bool> >(0))); 42116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(sizeof(IsContainer), 42216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan sizeof(IsContainerTest<std::map<int, double> >(0))); 423e35fdd936d133bf8a48de140a3c666897588a05shiqian} 424e35fdd936d133bf8a48de140a3c666897588a05shiqian 425e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests the TupleMatches() template function. 426e35fdd936d133bf8a48de140a3c666897588a05shiqian 427e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize0) { 428e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<> matchers; 429e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<> values; 430e35fdd936d133bf8a48de140a3c666897588a05shiqian 431e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(TupleMatches(matchers, values)); 432e35fdd936d133bf8a48de140a3c666897588a05shiqian} 433e35fdd936d133bf8a48de140a3c666897588a05shiqian 434e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize1) { 435e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<Matcher<int> > matchers(Eq(1)); 436e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<int> values1(1), 437e35fdd936d133bf8a48de140a3c666897588a05shiqian values2(2); 438e35fdd936d133bf8a48de140a3c666897588a05shiqian 439e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(TupleMatches(matchers, values1)); 440e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values2)); 441e35fdd936d133bf8a48de140a3c666897588a05shiqian} 442e35fdd936d133bf8a48de140a3c666897588a05shiqian 443e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize2) { 444e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a')); 445e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<int, char> values1(1, 'a'), 446e35fdd936d133bf8a48de140a3c666897588a05shiqian values2(1, 'b'), 447e35fdd936d133bf8a48de140a3c666897588a05shiqian values3(2, 'a'), 448e35fdd936d133bf8a48de140a3c666897588a05shiqian values4(2, 'b'); 449e35fdd936d133bf8a48de140a3c666897588a05shiqian 450e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(TupleMatches(matchers, values1)); 451e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values2)); 452e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values3)); 453e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values4)); 454e35fdd936d133bf8a48de140a3c666897588a05shiqian} 455e35fdd936d133bf8a48de140a3c666897588a05shiqian 456e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize5) { 457e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>, // NOLINT 458e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher<string> > 459e35fdd936d133bf8a48de140a3c666897588a05shiqian matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi")); 460e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<int, char, bool, long, string> // NOLINT 461e35fdd936d133bf8a48de140a3c666897588a05shiqian values1(1, 'a', true, 2L, "hi"), 462e35fdd936d133bf8a48de140a3c666897588a05shiqian values2(1, 'a', true, 2L, "hello"), 463e35fdd936d133bf8a48de140a3c666897588a05shiqian values3(2, 'a', true, 2L, "hi"); 464e35fdd936d133bf8a48de140a3c666897588a05shiqian 465e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(TupleMatches(matchers, values1)); 466e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values2)); 467e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values3)); 468e35fdd936d133bf8a48de140a3c666897588a05shiqian} 469e35fdd936d133bf8a48de140a3c666897588a05shiqian 470e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Assert(true, ...) succeeds. 471e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AssertTest, SucceedsOnTrue) { 472e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(true, __FILE__, __LINE__, "This should succeed."); 473e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(true, __FILE__, __LINE__); // This should succeed too. 474e35fdd936d133bf8a48de140a3c666897588a05shiqian} 475e35fdd936d133bf8a48de140a3c666897588a05shiqian 476e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Assert(false, ...) generates a fatal failure. 477e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AssertTest, FailsFatallyOnFalse) { 47804d6ed817e40f676225e0bf20ff1470d8f0c4a20zhanyong.wan EXPECT_DEATH_IF_SUPPORTED({ 479e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(false, __FILE__, __LINE__, "This should fail."); 480e35fdd936d133bf8a48de140a3c666897588a05shiqian }, ""); 481e35fdd936d133bf8a48de140a3c666897588a05shiqian 48204d6ed817e40f676225e0bf20ff1470d8f0c4a20zhanyong.wan EXPECT_DEATH_IF_SUPPORTED({ 483e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(false, __FILE__, __LINE__); 484e35fdd936d133bf8a48de140a3c666897588a05shiqian }, ""); 485e35fdd936d133bf8a48de140a3c666897588a05shiqian} 486e35fdd936d133bf8a48de140a3c666897588a05shiqian 487e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Expect(true, ...) succeeds. 488e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectTest, SucceedsOnTrue) { 489e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(true, __FILE__, __LINE__, "This should succeed."); 490e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(true, __FILE__, __LINE__); // This should succeed too. 491e35fdd936d133bf8a48de140a3c666897588a05shiqian} 492e35fdd936d133bf8a48de140a3c666897588a05shiqian 493e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Expect(false, ...) generates a non-fatal failure. 494e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectTest, FailsNonfatallyOnFalse) { 495e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_NONFATAL_FAILURE({ // NOLINT 496e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(false, __FILE__, __LINE__, "This should fail."); 497e35fdd936d133bf8a48de140a3c666897588a05shiqian }, "This should fail"); 498e35fdd936d133bf8a48de140a3c666897588a05shiqian 499e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_NONFATAL_FAILURE({ // NOLINT 500e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(false, __FILE__, __LINE__); 501e35fdd936d133bf8a48de140a3c666897588a05shiqian }, "Expectation failed"); 502e35fdd936d133bf8a48de140a3c666897588a05shiqian} 503e35fdd936d133bf8a48de140a3c666897588a05shiqian 5049413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan// Tests LogIsVisible(). 5059413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5069413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanclass LogIsVisibleTest : public ::testing::Test { 5079413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan protected: 50881476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan virtual void SetUp() { 50981476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // The code needs to work when both ::string and ::std::string are 51081476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // defined and the flag is implemented as a 51181476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // testing::internal::String. In this case, without the call to 51281476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // c_str(), the compiler will complain that it cannot figure out 51381476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // whether the String flag should be converted to a ::string or an 51481476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // ::std::string before being assigned to original_verbose_. 51581476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan original_verbose_ = GMOCK_FLAG(verbose).c_str(); 51681476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan } 51781476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan 5189413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } 5199413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5209413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan string original_verbose_; 5219413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan}; 5229413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5239413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) { 5249413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan GMOCK_FLAG(verbose) = kInfoVerbosity; 5259413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_TRUE(LogIsVisible(INFO)); 5269413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_TRUE(LogIsVisible(WARNING)); 5279413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan} 5289413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5299413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) { 5309413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan GMOCK_FLAG(verbose) = kErrorVerbosity; 5319413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_FALSE(LogIsVisible(INFO)); 5329413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_FALSE(LogIsVisible(WARNING)); 5339413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan} 5349413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5359413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) { 5369413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan GMOCK_FLAG(verbose) = kWarningVerbosity; 5379413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_FALSE(LogIsVisible(INFO)); 5389413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_TRUE(LogIsVisible(WARNING)); 5399413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan} 5409413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 541470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan#if GTEST_HAS_STREAM_REDIRECTION_ 542e35fdd936d133bf8a48de140a3c666897588a05shiqian 543e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests the Log() function. 544e35fdd936d133bf8a48de140a3c666897588a05shiqian 545e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that Log() behaves correctly for the given verbosity level 546e35fdd936d133bf8a48de140a3c666897588a05shiqian// and log severity. 547e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestLogWithSeverity(const string& verbosity, LogSeverity severity, 548e35fdd936d133bf8a48de140a3c666897588a05shiqian bool should_print) { 549e35fdd936d133bf8a48de140a3c666897588a05shiqian const string old_flag = GMOCK_FLAG(verbose); 550e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = verbosity; 551470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan CaptureStdout(); 552e35fdd936d133bf8a48de140a3c666897588a05shiqian Log(severity, "Test log.\n", 0); 553e35fdd936d133bf8a48de140a3c666897588a05shiqian if (should_print) { 554470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan EXPECT_THAT(GetCapturedStdout().c_str(), 555470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan ContainsRegex( 556470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan severity == WARNING ? 557470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan "^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" : 558470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan "^\nTest log\\.\nStack trace:\n")); 559e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 560470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan EXPECT_STREQ("", GetCapturedStdout().c_str()); 561e35fdd936d133bf8a48de140a3c666897588a05shiqian } 562e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = old_flag; 563e35fdd936d133bf8a48de140a3c666897588a05shiqian} 564e35fdd936d133bf8a48de140a3c666897588a05shiqian 565e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that when the stack_frames_to_skip parameter is negative, 566e35fdd936d133bf8a48de140a3c666897588a05shiqian// Log() doesn't include the stack trace in the output. 567e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) { 56876c1c612e23d87874669faf0b1c8f74caa4a7eb4vladlosev const string saved_flag = GMOCK_FLAG(verbose); 569e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = kInfoVerbosity; 570470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan CaptureStdout(); 571e35fdd936d133bf8a48de140a3c666897588a05shiqian Log(INFO, "Test log.\n", -1); 572470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str()); 57376c1c612e23d87874669faf0b1c8f74caa4a7eb4vladlosev GMOCK_FLAG(verbose) = saved_flag; 574e35fdd936d133bf8a48de140a3c666897588a05shiqian} 575e35fdd936d133bf8a48de140a3c666897588a05shiqian 576e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that in opt mode, a positive stack_frames_to_skip argument is 577e35fdd936d133bf8a48de140a3c666897588a05shiqian// treated as 0. 578e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoSkippingStackFrameInOptMode) { 579470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan CaptureStdout(); 580e35fdd936d133bf8a48de140a3c666897588a05shiqian Log(WARNING, "Test log.\n", 100); 581470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan const String log = GetCapturedStdout(); 582470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan#if defined(NDEBUG) && GTEST_GOOGLE3_MODE_ 583e35fdd936d133bf8a48de140a3c666897588a05shiqian // In opt mode, no stack frame should be skipped. 584e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n" 585e35fdd936d133bf8a48de140a3c666897588a05shiqian "Test log\\.\n" 586e35fdd936d133bf8a48de140a3c666897588a05shiqian "Stack trace:\n" 587e35fdd936d133bf8a48de140a3c666897588a05shiqian ".+")); 588e35fdd936d133bf8a48de140a3c666897588a05shiqian#else 589e35fdd936d133bf8a48de140a3c666897588a05shiqian // In dbg mode, the stack frames should be skipped. 590470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan EXPECT_STREQ("\nGMOCK WARNING:\n" 591470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan "Test log.\n" 592470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan "Stack trace:\n", log.c_str()); 593470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan#endif 594e35fdd936d133bf8a48de140a3c666897588a05shiqian} 595e35fdd936d133bf8a48de140a3c666897588a05shiqian 596e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that all logs are printed when the value of the 597e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "info". 598e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) { 599e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kInfoVerbosity, INFO, true); 600e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kInfoVerbosity, WARNING, true); 601e35fdd936d133bf8a48de140a3c666897588a05shiqian} 602e35fdd936d133bf8a48de140a3c666897588a05shiqian 603e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that only warnings are printed when the value of the 604e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "warning". 605e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) { 606e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kWarningVerbosity, INFO, false); 607e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kWarningVerbosity, WARNING, true); 608e35fdd936d133bf8a48de140a3c666897588a05shiqian} 609e35fdd936d133bf8a48de140a3c666897588a05shiqian 610e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that no logs are printed when the value of the 611e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "error". 612e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) { 613e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kErrorVerbosity, INFO, false); 614e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kErrorVerbosity, WARNING, false); 615e35fdd936d133bf8a48de140a3c666897588a05shiqian} 616e35fdd936d133bf8a48de140a3c666897588a05shiqian 617e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that only warnings are printed when the value of the 618e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is invalid. 619e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) { 620e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity("invalid", INFO, false); 621e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity("invalid", WARNING, true); 622e35fdd936d133bf8a48de140a3c666897588a05shiqian} 623e35fdd936d133bf8a48de140a3c666897588a05shiqian 624470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan#endif // GTEST_HAS_STREAM_REDIRECTION_ 625e35fdd936d133bf8a48de140a3c666897588a05shiqian 626e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, true_type) { 627e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(true_type::value); 628e35fdd936d133bf8a48de140a3c666897588a05shiqian} 629e35fdd936d133bf8a48de140a3c666897588a05shiqian 630e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, false_type) { 631e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(false_type::value); 632e35fdd936d133bf8a48de140a3c666897588a05shiqian} 633e35fdd936d133bf8a48de140a3c666897588a05shiqian 634e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, is_reference) { 635e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(is_reference<int>::value); 636e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(is_reference<char*>::value); 637e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(is_reference<const int&>::value); 638e35fdd936d133bf8a48de140a3c666897588a05shiqian} 639e35fdd936d133bf8a48de140a3c666897588a05shiqian 640e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, is_pointer) { 641e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(is_pointer<int>::value); 642e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(is_pointer<char&>::value); 643e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(is_pointer<const int*>::value); 644e35fdd936d133bf8a48de140a3c666897588a05shiqian} 645e35fdd936d133bf8a48de140a3c666897588a05shiqian 646e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, type_equals) { 647e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((type_equals<int, const int>::value)); 648e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((type_equals<int, int&>::value)); 649e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((type_equals<int, double>::value)); 650e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<char, char>::value)); 651e35fdd936d133bf8a48de140a3c666897588a05shiqian} 652e35fdd936d133bf8a48de140a3c666897588a05shiqian 653e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, remove_reference) { 654e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value)); 655e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<const int, 656e35fdd936d133bf8a48de140a3c666897588a05shiqian remove_reference<const int&>::type>::value)); 657e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value)); 658e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value)); 659e35fdd936d133bf8a48de140a3c666897588a05shiqian} 660e35fdd936d133bf8a48de140a3c666897588a05shiqian 661470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan#if GTEST_HAS_STREAM_REDIRECTION_ 662e35fdd936d133bf8a48de140a3c666897588a05shiqian 663e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that Log() behaves correctly for the given verbosity level 664e35fdd936d133bf8a48de140a3c666897588a05shiqian// and log severity. 665470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wanString GrabOutput(void(*logger)(), const char* verbosity) { 666e35fdd936d133bf8a48de140a3c666897588a05shiqian const string saved_flag = GMOCK_FLAG(verbose); 667e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = verbosity; 668470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan CaptureStdout(); 669e35fdd936d133bf8a48de140a3c666897588a05shiqian logger(); 670e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = saved_flag; 671470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan return GetCapturedStdout(); 672e35fdd936d133bf8a48de140a3c666897588a05shiqian} 673e35fdd936d133bf8a48de140a3c666897588a05shiqian 674e35fdd936d133bf8a48de140a3c666897588a05shiqianclass DummyMock { 675e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 676e35fdd936d133bf8a48de140a3c666897588a05shiqian MOCK_METHOD0(TestMethod, void()); 677e35fdd936d133bf8a48de140a3c666897588a05shiqian MOCK_METHOD1(TestMethodArg, void(int dummy)); 678e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 679e35fdd936d133bf8a48de140a3c666897588a05shiqian 680e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExpectCallLogger() { 681e35fdd936d133bf8a48de140a3c666897588a05shiqian DummyMock mock; 682e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_CALL(mock, TestMethod()); 683e35fdd936d133bf8a48de140a3c666897588a05shiqian mock.TestMethod(); 684e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 685e35fdd936d133bf8a48de140a3c666897588a05shiqian 686e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info". 687e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest, LogsWhenVerbosityIsInfo) { 688e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity), 689e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr("EXPECT_CALL(mock, TestMethod())")); 690e35fdd936d133bf8a48de140a3c666897588a05shiqian} 691e35fdd936d133bf8a48de140a3c666897588a05shiqian 692e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL doesn't log 693e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "warning". 694e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) { 695470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str()); 696e35fdd936d133bf8a48de140a3c666897588a05shiqian} 697e35fdd936d133bf8a48de140a3c666897588a05shiqian 698e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL doesn't log 699e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "error". 700e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) { 701470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str()); 702e35fdd936d133bf8a48de140a3c666897588a05shiqian} 703e35fdd936d133bf8a48de140a3c666897588a05shiqian 704e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid OnCallLogger() { 705e35fdd936d133bf8a48de140a3c666897588a05shiqian DummyMock mock; 706e35fdd936d133bf8a48de140a3c666897588a05shiqian ON_CALL(mock, TestMethod()); 707e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 708e35fdd936d133bf8a48de140a3c666897588a05shiqian 709e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info". 710e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, LogsWhenVerbosityIsInfo) { 711e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity), 712e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr("ON_CALL(mock, TestMethod())")); 713e35fdd936d133bf8a48de140a3c666897588a05shiqian} 714e35fdd936d133bf8a48de140a3c666897588a05shiqian 715e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL doesn't log 716e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "warning". 717e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) { 718470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str()); 719e35fdd936d133bf8a48de140a3c666897588a05shiqian} 720e35fdd936d133bf8a48de140a3c666897588a05shiqian 721e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL doesn't log if 722e35fdd936d133bf8a48de140a3c666897588a05shiqian// the --gmock_verbose flag is set to "error". 723e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, DoesNotLogWhenVerbosityIsError) { 724470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str()); 725e35fdd936d133bf8a48de140a3c666897588a05shiqian} 726e35fdd936d133bf8a48de140a3c666897588a05shiqian 727e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid OnCallAnyArgumentLogger() { 728e35fdd936d133bf8a48de140a3c666897588a05shiqian DummyMock mock; 729e35fdd936d133bf8a48de140a3c666897588a05shiqian ON_CALL(mock, TestMethodArg(_)); 730e35fdd936d133bf8a48de140a3c666897588a05shiqian} 731e35fdd936d133bf8a48de140a3c666897588a05shiqian 732e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL prints provided _ argument. 733e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, LogsAnythingArgument) { 734e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity), 735e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr("ON_CALL(mock, TestMethodArg(_)")); 736e35fdd936d133bf8a48de140a3c666897588a05shiqian} 737e35fdd936d133bf8a48de140a3c666897588a05shiqian 738470df42bad6a78531f0ec51e43a194f3e26c4f4dzhanyong.wan#endif // GTEST_HAS_STREAM_REDIRECTION_ 739e35fdd936d133bf8a48de140a3c666897588a05shiqian 740b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests ArrayEq(). 741b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 742b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForDegeneratedArrays) { 743b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(5, 5L)); 744b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq('a', 0)); 745b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 746b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 747b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForOneDimensionalArrays) { 748b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int a[] = { 0, 1 }; 749b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan long b[] = { 0, 1 }; 750b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, b)); 751b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, 2, b)); 752b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 753b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan b[0] = 2; 754b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq(a, b)); 755b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq(a, 1, b)); 756b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 757b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 758b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForTwoDimensionalArrays) { 759b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char a[][3] = { "hi", "lo" }; 760b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char b[][3] = { "hi", "lo" }; 761b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char c[][3] = { "hi", "li" }; 762b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 763b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, b)); 764b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, 2, b)); 765b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 766b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq(a, c)); 767b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq(a, 2, c)); 768b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 769b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 770b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests ArrayAwareFind(). 771b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 772b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayAwareFindTest, WorksForOneDimensionalArray) { 773b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char a[] = "hello"; 774b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o')); 775b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x')); 776b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 777b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 778b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) { 779b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; 780b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int b[2] = { 2, 3 }; 781b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b)); 782b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 783b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int c[2] = { 6, 7 }; 784b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c)); 785b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 786b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 787b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests CopyArray(). 788b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 789b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForDegeneratedArrays) { 790b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int n = 0; 791b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray('a', &n); 792b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ('a', n); 793b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 794b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 795b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForOneDimensionalArrays) { 796b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char a[3] = "hi"; 797b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int b[3]; 798b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(a, &b); 799b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, b)); 800b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 801b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int c[3]; 802b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(a, 3, c); 803b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, c)); 804b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 805b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 806b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForTwoDimensionalArrays) { 807b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; 808b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int b[2][3]; 809b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(a, &b); 810b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, b)); 811b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 812b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int c[2][3]; 813b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(a, 2, c); 814b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, c)); 815b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 816b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 817b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests NativeArray. 818b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 8194bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wanTEST(NativeArrayTest, ConstructorFromArrayWorks) { 820b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int a[3] = { 0, 1, 2 }; 8214bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan NativeArray<int> na(a, 3, kReference); 82232de5f53763125925e078498250f7e73a88de9edzhanyong.wan EXPECT_EQ(3U, na.size()); 823b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a, na.begin()); 824b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 825b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 826b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) { 827b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef int Array[2]; 828b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan Array* a = new Array[1]; 829b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan (*a)[0] = 0; 830b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan (*a)[1] = 1; 8314bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan NativeArray<int> na(*a, 2, kCopy); 832b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_NE(*a, na.begin()); 833b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan delete[] a; 834b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, na.begin()[0]); 835b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(1, na.begin()[1]); 836b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 837b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // We rely on the heap checker to verify that na deletes the copy of 838b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // array. 839b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 840b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 841b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, TypeMembersAreCorrect) { 842b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<char, NativeArray<char>::value_type>(); 843b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>(); 844b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 845b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>(); 846b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>(); 847b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 848b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 849b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, MethodsWork) { 8504bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan const int a[3] = { 0, 1, 2 }; 8514bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan NativeArray<int> na(a, 3, kCopy); 85232de5f53763125925e078498250f7e73a88de9edzhanyong.wan ASSERT_EQ(3U, na.size()); 853b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(3, na.end() - na.begin()); 854b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 855b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int>::const_iterator it = na.begin(); 856b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, *it); 857b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ++it; 858b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(1, *it); 859b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan it++; 860b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(2, *it); 861b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ++it; 862b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(na.end(), it); 863b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 864b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_THAT(na, Eq(na)); 865b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 8664bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan NativeArray<int> na2(a, 3, kReference); 867b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_THAT(na, Eq(na2)); 868b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 8694bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan const int b1[3] = { 0, 1, 1 }; 8704bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan const int b2[4] = { 0, 1, 2, 3 }; 8714bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, 3, kReference)))); 8724bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, 4, kCopy)))); 873b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 874b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 875b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, WorksForTwoDimensionalArray) { 876b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char a[2][3] = { "hi", "lo" }; 8774bd79e4f25a86f3a2a99f6af06cc43cdacc55853zhanyong.wan NativeArray<char[3]> na(a, 2, kReference); 87832de5f53763125925e078498250f7e73a88de9edzhanyong.wan ASSERT_EQ(2U, na.size()); 879b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a, na.begin()); 880b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 881b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 882b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests StlContainerView. 883b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 884b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForStlContainer) { 885b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<std::vector<int>, 886b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<std::vector<int> >::type>(); 887b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const std::vector<double>&, 888b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<std::vector<double> >::const_reference>(); 889b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 890b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef std::vector<char> Chars; 891b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan Chars v1; 892b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const Chars& v2(StlContainerView<Chars>::ConstReference(v1)); 893b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(&v1, &v2); 894b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 895b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan v1.push_back('a'); 896b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan Chars v3 = StlContainerView<Chars>::Copy(v1); 897b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_THAT(v3, Eq(v3)); 898b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 899b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 900b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForStaticNativeArray) { 901b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<int>, 902b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<int[3]>::type>(); 903b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<double>, 904b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<const double[4]>::type>(); 905b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<char[3]>, 906b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<const char[2][3]>::type>(); 907b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 908b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const NativeArray<int>, 909b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<int[2]>::const_reference>(); 910b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 911b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int a1[3] = { 0, 1, 2 }; 912b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1); 91332de5f53763125925e078498250f7e73a88de9edzhanyong.wan EXPECT_EQ(3U, a2.size()); 914b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a1, a2.begin()); 915b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 916b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1); 91732de5f53763125925e078498250f7e73a88de9edzhanyong.wan ASSERT_EQ(3U, a3.size()); 918b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, a3.begin()[0]); 919b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(1, a3.begin()[1]); 920b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(2, a3.begin()[2]); 921b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 922b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Makes sure a1 and a3 aren't aliases. 923b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan a1[0] = 3; 924b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, a3.begin()[0]); 925b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 926b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 927b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForDynamicNativeArray) { 928b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<int>, 929b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<tuple<const int*, size_t> >::type>(); 930b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<double>, 931b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<tuple<linked_ptr<double>, int> >::type>(); 932b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 933b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const NativeArray<int>, 934b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<tuple<const int*, int> >::const_reference>(); 935b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 936b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int a1[3] = { 0, 1, 2 }; 93790c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan const int* const p1 = a1; 938b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >:: 93990c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan ConstReference(make_tuple(p1, 3)); 94032de5f53763125925e078498250f7e73a88de9edzhanyong.wan EXPECT_EQ(3U, a2.size()); 941b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a1, a2.begin()); 942b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 943b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >:: 9442661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan Copy(make_tuple(static_cast<int*>(a1), 3)); 94532de5f53763125925e078498250f7e73a88de9edzhanyong.wan ASSERT_EQ(3U, a3.size()); 946b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, a3.begin()[0]); 947b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(1, a3.begin()[1]); 948b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(2, a3.begin()[2]); 949b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 950b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Makes sure a1 and a3 aren't aliases. 951b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan a1[0] = 3; 952b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, a3.begin()[0]); 953b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 954b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 955e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace 956e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 957e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 958