gmock-internal-utils_test.cc revision 0ea67f88aea208e7ef13c91b374b352679beab38
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; 214e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(NULL, GetRawPointer(p)); 215e35fdd936d133bf8a48de140a3c666897588a05shiqian int n = 1; 216e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(&n, GetRawPointer(&n)); 217e35fdd936d133bf8a48de140a3c666897588a05shiqian} 218e35fdd936d133bf8a48de140a3c666897588a05shiqian 219e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Base {}; 220e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Derived : public Base {}; 221e35fdd936d133bf8a48de140a3c666897588a05shiqian 222e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant. 223e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) { 224e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true); 225e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value), 226e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan const_false); 227e35fdd936d133bf8a48de140a3c666897588a05shiqian} 228e35fdd936d133bf8a48de140a3c666897588a05shiqian 229e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can 230e35fdd936d133bf8a48de140a3c666897588a05shiqian// be implicitly converted to T2. 231e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) { 232e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<int, double>::value)); 233e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<double, int>::value)); 234e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value)); 235e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value)); 236e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<Derived&, const Base&>::value)); 237e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((ImplicitlyConvertible<const Base, Base>::value)); 238e35fdd936d133bf8a48de140a3c666897588a05shiqian} 239e35fdd936d133bf8a48de140a3c666897588a05shiqian 240e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that ImplicitlyConvertible<T1, T2>::value is false when T1 241e35fdd936d133bf8a48de140a3c666897588a05shiqian// cannot be implicitly converted to T2. 242e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) { 243e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value)); 244e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value)); 245e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value)); 246e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((ImplicitlyConvertible<Base&, Derived&>::value)); 247e35fdd936d133bf8a48de140a3c666897588a05shiqian} 248e35fdd936d133bf8a48de140a3c666897588a05shiqian 24916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Tests KindOf<T>. 25016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 25116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Bool) { 25216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT 25316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 25416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 25516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Integer) { 25616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT 25716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT 25816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT 25916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT 26016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT 26116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT 26216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT 26316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT 26416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT 26516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT 26616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64)); // NOLINT 26716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64)); // NOLINT 26816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT 26916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN 27016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // ssize_t is not defined on Windows and possibly some other OSes. 27116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT 27216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#endif 27316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 27416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 27516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, FloatingPoint) { 27616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT 27716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT 27816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT 27916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 28016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(KindOfTest, Other) { 28216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT 28316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT 28416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT 28516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 28616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan// Tests LosslessArithmeticConvertible<T, U>. 28816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 28916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToBool) { 29016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value)); 29116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 29216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 29316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToInteger) { 29416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value)); 29516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value)); 29616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE( 29716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT 29816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 29916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 30016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) { 30116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value)); 30216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value)); 30316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 30416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 30516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToBool) { 30616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value)); 30716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value)); 30816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 30916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToInteger) { 31116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Unsigned => larger signed is fine. 31216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value)); 31316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Unsigned => larger unsigned is fine. 31516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE( 31616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT 31716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 31816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Signed => unsigned is not fine. 31916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT 32016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible< 32116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan signed char, unsigned int>::value)); // NOLINT 32216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 32316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Same size and same signedness: fine too. 32416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible< 32516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan unsigned char, unsigned char>::value)); 32616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value)); 32716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value)); 32816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible< 32916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan unsigned long, unsigned long>::value)); // NOLINT 33016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 33116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Same size, different signedness: not fine. 33216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible< 33316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan unsigned char, signed char>::value)); 33416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value)); 33516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value)); 33616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 33716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Larger size => smaller size is not fine. 33816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT 33916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value)); 34016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value)); 34116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 34216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 34316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) { 34416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Integers cannot be losslessly converted to floating-points, as 34516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // the format of the latter is implementation-defined. 34616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value)); 34716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value)); 34816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible< 34916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan short, long double>::value)); // NOLINT 35016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 35116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 35216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) { 35316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value)); 35416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value)); 35516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 35616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 35716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) { 35816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT 35916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value)); 36016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value)); 36116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 36216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 36316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wanTEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) { 36416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Smaller size => larger size is fine. 36516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value)); 36616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value)); 36716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value)); 36816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 36916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Same size: fine. 37016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value)); 37116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value)); 37216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 37316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // Larger size => smaller size is not fine. 37416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value)); 37516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan if (sizeof(double) == sizeof(long double)) { // NOLINT 37616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // In some implementations (e.g. MSVC), double and long double 37716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan // have the same size. 37816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value)); 37916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan } else { 38016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value)); 38116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan } 38216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan} 38316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan 384e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is a compile-time constant. 385e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { 386e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value, const_true); 387e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false); 388e35fdd936d133bf8a48de140a3c666897588a05shiqian} 389e35fdd936d133bf8a48de140a3c666897588a05shiqian 390e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is true when T is 391e35fdd936d133bf8a48de140a3c666897588a05shiqian// ProtocolMessage or a sub-class of it. 392e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { 3930ea67f88aea208e7ef13c91b374b352679beab38zhanyong.wan EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); 394e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); 395e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GMOCK_HAS_PROTOBUF_ 396e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); 397e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_HAS_PROTOBUF_ 398e35fdd936d133bf8a48de140a3c666897588a05shiqian} 399e35fdd936d133bf8a48de140a3c666897588a05shiqian 400e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that IsAProtocolMessage<T>::value is false when T is neither 401e35fdd936d133bf8a48de140a3c666897588a05shiqian// ProtocolMessage nor a sub-class of it. 402e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) { 403e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(IsAProtocolMessage<int>::value); 404e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(IsAProtocolMessage<const Base>::value); 405e35fdd936d133bf8a48de140a3c666897588a05shiqian} 406e35fdd936d133bf8a48de140a3c666897588a05shiqian 407e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests IsContainerTest. 408e35fdd936d133bf8a48de140a3c666897588a05shiqian 409e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NonContainer {}; 410e35fdd936d133bf8a48de140a3c666897588a05shiqian 411e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsContainerTestTest, WorksForNonContainer) { 412e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0))); 413e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0))); 414e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0))); 415e35fdd936d133bf8a48de140a3c666897588a05shiqian} 416e35fdd936d133bf8a48de140a3c666897588a05shiqian 417e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(IsContainerTestTest, WorksForContainer) { 41816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(sizeof(IsContainer), 41916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan sizeof(IsContainerTest<std::vector<bool> >(0))); 42016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan EXPECT_EQ(sizeof(IsContainer), 42116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan sizeof(IsContainerTest<std::map<int, double> >(0))); 422e35fdd936d133bf8a48de140a3c666897588a05shiqian} 423e35fdd936d133bf8a48de140a3c666897588a05shiqian 424e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests the TupleMatches() template function. 425e35fdd936d133bf8a48de140a3c666897588a05shiqian 426e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize0) { 427e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<> matchers; 428e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<> values; 429e35fdd936d133bf8a48de140a3c666897588a05shiqian 430e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(TupleMatches(matchers, values)); 431e35fdd936d133bf8a48de140a3c666897588a05shiqian} 432e35fdd936d133bf8a48de140a3c666897588a05shiqian 433e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize1) { 434e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<Matcher<int> > matchers(Eq(1)); 435e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<int> values1(1), 436e35fdd936d133bf8a48de140a3c666897588a05shiqian values2(2); 437e35fdd936d133bf8a48de140a3c666897588a05shiqian 438e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(TupleMatches(matchers, values1)); 439e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values2)); 440e35fdd936d133bf8a48de140a3c666897588a05shiqian} 441e35fdd936d133bf8a48de140a3c666897588a05shiqian 442e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize2) { 443e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a')); 444e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<int, char> values1(1, 'a'), 445e35fdd936d133bf8a48de140a3c666897588a05shiqian values2(1, 'b'), 446e35fdd936d133bf8a48de140a3c666897588a05shiqian values3(2, 'a'), 447e35fdd936d133bf8a48de140a3c666897588a05shiqian values4(2, 'b'); 448e35fdd936d133bf8a48de140a3c666897588a05shiqian 449e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(TupleMatches(matchers, values1)); 450e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values2)); 451e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values3)); 452e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values4)); 453e35fdd936d133bf8a48de140a3c666897588a05shiqian} 454e35fdd936d133bf8a48de140a3c666897588a05shiqian 455e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TupleMatchesTest, WorksForSize5) { 456e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>, // NOLINT 457e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher<string> > 458e35fdd936d133bf8a48de140a3c666897588a05shiqian matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi")); 459e35fdd936d133bf8a48de140a3c666897588a05shiqian tuple<int, char, bool, long, string> // NOLINT 460e35fdd936d133bf8a48de140a3c666897588a05shiqian values1(1, 'a', true, 2L, "hi"), 461e35fdd936d133bf8a48de140a3c666897588a05shiqian values2(1, 'a', true, 2L, "hello"), 462e35fdd936d133bf8a48de140a3c666897588a05shiqian values3(2, 'a', true, 2L, "hi"); 463e35fdd936d133bf8a48de140a3c666897588a05shiqian 464e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(TupleMatches(matchers, values1)); 465e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values2)); 466e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(TupleMatches(matchers, values3)); 467e35fdd936d133bf8a48de140a3c666897588a05shiqian} 468e35fdd936d133bf8a48de140a3c666897588a05shiqian 469e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Assert(true, ...) succeeds. 470e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AssertTest, SucceedsOnTrue) { 471e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(true, __FILE__, __LINE__, "This should succeed."); 472e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(true, __FILE__, __LINE__); // This should succeed too. 473e35fdd936d133bf8a48de140a3c666897588a05shiqian} 474e35fdd936d133bf8a48de140a3c666897588a05shiqian 475652540a278e56528e576f9ea77515f386698a326zhanyong.wan#if GTEST_HAS_DEATH_TEST 476e35fdd936d133bf8a48de140a3c666897588a05shiqian 477e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Assert(false, ...) generates a fatal failure. 478e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(AssertTest, FailsFatallyOnFalse) { 479e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_DEATH({ // NOLINT 480e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(false, __FILE__, __LINE__, "This should fail."); 481e35fdd936d133bf8a48de140a3c666897588a05shiqian }, ""); 482e35fdd936d133bf8a48de140a3c666897588a05shiqian 483e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_DEATH({ // NOLINT 484e35fdd936d133bf8a48de140a3c666897588a05shiqian Assert(false, __FILE__, __LINE__); 485e35fdd936d133bf8a48de140a3c666897588a05shiqian }, ""); 486e35fdd936d133bf8a48de140a3c666897588a05shiqian} 487e35fdd936d133bf8a48de140a3c666897588a05shiqian 488e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GTEST_HAS_DEATH_TEST 489e35fdd936d133bf8a48de140a3c666897588a05shiqian 490e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Expect(true, ...) succeeds. 491e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectTest, SucceedsOnTrue) { 492e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(true, __FILE__, __LINE__, "This should succeed."); 493e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(true, __FILE__, __LINE__); // This should succeed too. 494e35fdd936d133bf8a48de140a3c666897588a05shiqian} 495e35fdd936d133bf8a48de140a3c666897588a05shiqian 496e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that Expect(false, ...) generates a non-fatal failure. 497e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectTest, FailsNonfatallyOnFalse) { 498e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_NONFATAL_FAILURE({ // NOLINT 499e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(false, __FILE__, __LINE__, "This should fail."); 500e35fdd936d133bf8a48de140a3c666897588a05shiqian }, "This should fail"); 501e35fdd936d133bf8a48de140a3c666897588a05shiqian 502e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_NONFATAL_FAILURE({ // NOLINT 503e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(false, __FILE__, __LINE__); 504e35fdd936d133bf8a48de140a3c666897588a05shiqian }, "Expectation failed"); 505e35fdd936d133bf8a48de140a3c666897588a05shiqian} 506e35fdd936d133bf8a48de140a3c666897588a05shiqian 5079413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan// Tests LogIsVisible(). 5089413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5099413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanclass LogIsVisibleTest : public ::testing::Test { 5109413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan protected: 51181476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan virtual void SetUp() { 51281476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // The code needs to work when both ::string and ::std::string are 51381476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // defined and the flag is implemented as a 51481476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // testing::internal::String. In this case, without the call to 51581476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // c_str(), the compiler will complain that it cannot figure out 51681476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // whether the String flag should be converted to a ::string or an 51781476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan // ::std::string before being assigned to original_verbose_. 51881476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan original_verbose_ = GMOCK_FLAG(verbose).c_str(); 51981476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan } 52081476f2f9070186027e7337854bbcf693b85c6c6zhanyong.wan 5219413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } 5229413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5239413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan string original_verbose_; 5249413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan}; 5259413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5269413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) { 5279413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan GMOCK_FLAG(verbose) = kInfoVerbosity; 5289413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_TRUE(LogIsVisible(INFO)); 5299413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_TRUE(LogIsVisible(WARNING)); 5309413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan} 5319413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5329413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) { 5339413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan GMOCK_FLAG(verbose) = kErrorVerbosity; 5349413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_FALSE(LogIsVisible(INFO)); 5359413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_FALSE(LogIsVisible(WARNING)); 5369413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan} 5379413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 5389413f2ff615ae1b933580576183d316c4cb6376czhanyong.wanTEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) { 5399413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan GMOCK_FLAG(verbose) = kWarningVerbosity; 5409413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_FALSE(LogIsVisible(INFO)); 5419413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan EXPECT_TRUE(LogIsVisible(WARNING)); 5429413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan} 5439413f2ff615ae1b933580576183d316c4cb6376czhanyong.wan 544e35fdd936d133bf8a48de140a3c666897588a05shiqian// TODO(wan@google.com): find a way to re-enable these tests. 545e35fdd936d133bf8a48de140a3c666897588a05shiqian#if 0 546e35fdd936d133bf8a48de140a3c666897588a05shiqian 547e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests the Log() function. 548e35fdd936d133bf8a48de140a3c666897588a05shiqian 549e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that Log() behaves correctly for the given verbosity level 550e35fdd936d133bf8a48de140a3c666897588a05shiqian// and log severity. 551e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid TestLogWithSeverity(const string& verbosity, LogSeverity severity, 552e35fdd936d133bf8a48de140a3c666897588a05shiqian bool should_print) { 553e35fdd936d133bf8a48de140a3c666897588a05shiqian const string old_flag = GMOCK_FLAG(verbose); 554e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = verbosity; 555e35fdd936d133bf8a48de140a3c666897588a05shiqian CaptureTestStdout(); 556e35fdd936d133bf8a48de140a3c666897588a05shiqian Log(severity, "Test log.\n", 0); 557e35fdd936d133bf8a48de140a3c666897588a05shiqian if (should_print) { 558e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_PRED2(RE::FullMatch, 559e35fdd936d133bf8a48de140a3c666897588a05shiqian GetCapturedTestStdout(), 560e35fdd936d133bf8a48de140a3c666897588a05shiqian severity == WARNING ? 561e35fdd936d133bf8a48de140a3c666897588a05shiqian "\nGMOCK WARNING:\nTest log\\.\nStack trace:\n[\\s\\S]*" : 562e35fdd936d133bf8a48de140a3c666897588a05shiqian "\nTest log\\.\nStack trace:\n[\\s\\S]*"); 563e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 564e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ("", GetCapturedTestStdout()); 565e35fdd936d133bf8a48de140a3c666897588a05shiqian } 566e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = old_flag; 567e35fdd936d133bf8a48de140a3c666897588a05shiqian} 568e35fdd936d133bf8a48de140a3c666897588a05shiqian 569e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that when the stack_frames_to_skip parameter is negative, 570e35fdd936d133bf8a48de140a3c666897588a05shiqian// Log() doesn't include the stack trace in the output. 571e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) { 572e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = kInfoVerbosity; 573e35fdd936d133bf8a48de140a3c666897588a05shiqian CaptureTestStdout(); 574e35fdd936d133bf8a48de140a3c666897588a05shiqian Log(INFO, "Test log.\n", -1); 575e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ("\nTest log.\n", GetCapturedTestStdout()); 576e35fdd936d133bf8a48de140a3c666897588a05shiqian} 577e35fdd936d133bf8a48de140a3c666897588a05shiqian 578e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that in opt mode, a positive stack_frames_to_skip argument is 579e35fdd936d133bf8a48de140a3c666897588a05shiqian// treated as 0. 580e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoSkippingStackFrameInOptMode) { 581e35fdd936d133bf8a48de140a3c666897588a05shiqian CaptureTestStdout(); 582e35fdd936d133bf8a48de140a3c666897588a05shiqian Log(WARNING, "Test log.\n", 100); 583e35fdd936d133bf8a48de140a3c666897588a05shiqian const string log = GetCapturedTestStdout(); 584e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifdef NDEBUG 585e35fdd936d133bf8a48de140a3c666897588a05shiqian // In opt mode, no stack frame should be skipped. 586e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n" 587e35fdd936d133bf8a48de140a3c666897588a05shiqian "Test log\\.\n" 588e35fdd936d133bf8a48de140a3c666897588a05shiqian "Stack trace:\n" 589e35fdd936d133bf8a48de140a3c666897588a05shiqian ".+")); 590e35fdd936d133bf8a48de140a3c666897588a05shiqian#else 591e35fdd936d133bf8a48de140a3c666897588a05shiqian // In dbg mode, the stack frames should be skipped. 592e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ("\nGMOCK WARNING:\n" 593e35fdd936d133bf8a48de140a3c666897588a05shiqian "Test log.\n" 594e35fdd936d133bf8a48de140a3c666897588a05shiqian "Stack trace:\n", log); 595e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // NDEBUG 596e35fdd936d133bf8a48de140a3c666897588a05shiqian} 597e35fdd936d133bf8a48de140a3c666897588a05shiqian 598e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that all logs are printed when the value of the 599e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "info". 600e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) { 601e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kInfoVerbosity, INFO, true); 602e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kInfoVerbosity, WARNING, true); 603e35fdd936d133bf8a48de140a3c666897588a05shiqian} 604e35fdd936d133bf8a48de140a3c666897588a05shiqian 605e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that only warnings are printed when the value of the 606e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "warning". 607e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) { 608e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kWarningVerbosity, INFO, false); 609e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kWarningVerbosity, WARNING, true); 610e35fdd936d133bf8a48de140a3c666897588a05shiqian} 611e35fdd936d133bf8a48de140a3c666897588a05shiqian 612e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that no logs are printed when the value of the 613e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is "error". 614e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) { 615e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kErrorVerbosity, INFO, false); 616e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity(kErrorVerbosity, WARNING, false); 617e35fdd936d133bf8a48de140a3c666897588a05shiqian} 618e35fdd936d133bf8a48de140a3c666897588a05shiqian 619e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tests that only warnings are printed when the value of the 620e35fdd936d133bf8a48de140a3c666897588a05shiqian// --gmock_verbose flag is invalid. 621e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) { 622e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity("invalid", INFO, false); 623e35fdd936d133bf8a48de140a3c666897588a05shiqian TestLogWithSeverity("invalid", WARNING, true); 624e35fdd936d133bf8a48de140a3c666897588a05shiqian} 625e35fdd936d133bf8a48de140a3c666897588a05shiqian 626e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // 0 627e35fdd936d133bf8a48de140a3c666897588a05shiqian 628e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, true_type) { 629e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(true_type::value); 630e35fdd936d133bf8a48de140a3c666897588a05shiqian} 631e35fdd936d133bf8a48de140a3c666897588a05shiqian 632e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, false_type) { 633e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(false_type::value); 634e35fdd936d133bf8a48de140a3c666897588a05shiqian} 635e35fdd936d133bf8a48de140a3c666897588a05shiqian 636e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, is_reference) { 637e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(is_reference<int>::value); 638e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(is_reference<char*>::value); 639e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(is_reference<const int&>::value); 640e35fdd936d133bf8a48de140a3c666897588a05shiqian} 641e35fdd936d133bf8a48de140a3c666897588a05shiqian 642e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, is_pointer) { 643e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(is_pointer<int>::value); 644e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE(is_pointer<char&>::value); 645e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE(is_pointer<const int*>::value); 646e35fdd936d133bf8a48de140a3c666897588a05shiqian} 647e35fdd936d133bf8a48de140a3c666897588a05shiqian 648e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, type_equals) { 649e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((type_equals<int, const int>::value)); 650e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((type_equals<int, int&>::value)); 651e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_FALSE((type_equals<int, double>::value)); 652e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<char, char>::value)); 653e35fdd936d133bf8a48de140a3c666897588a05shiqian} 654e35fdd936d133bf8a48de140a3c666897588a05shiqian 655e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(TypeTraitsTest, remove_reference) { 656e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value)); 657e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<const int, 658e35fdd936d133bf8a48de140a3c666897588a05shiqian remove_reference<const int&>::type>::value)); 659e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value)); 660e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value)); 661e35fdd936d133bf8a48de140a3c666897588a05shiqian} 662e35fdd936d133bf8a48de140a3c666897588a05shiqian 663e35fdd936d133bf8a48de140a3c666897588a05shiqian// TODO(wan@google.com): find a way to re-enable these tests. 664e35fdd936d133bf8a48de140a3c666897588a05shiqian#if 0 665e35fdd936d133bf8a48de140a3c666897588a05shiqian 666e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that Log() behaves correctly for the given verbosity level 667e35fdd936d133bf8a48de140a3c666897588a05shiqian// and log severity. 668e35fdd936d133bf8a48de140a3c666897588a05shiqianstring GrabOutput(void(*logger)(), const char* verbosity) { 669e35fdd936d133bf8a48de140a3c666897588a05shiqian const string saved_flag = GMOCK_FLAG(verbose); 670e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = verbosity; 671e35fdd936d133bf8a48de140a3c666897588a05shiqian CaptureTestStdout(); 672e35fdd936d133bf8a48de140a3c666897588a05shiqian logger(); 673e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_FLAG(verbose) = saved_flag; 674e35fdd936d133bf8a48de140a3c666897588a05shiqian return GetCapturedTestStdout(); 675e35fdd936d133bf8a48de140a3c666897588a05shiqian} 676e35fdd936d133bf8a48de140a3c666897588a05shiqian 677e35fdd936d133bf8a48de140a3c666897588a05shiqianclass DummyMock { 678e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 679e35fdd936d133bf8a48de140a3c666897588a05shiqian MOCK_METHOD0(TestMethod, void()); 680e35fdd936d133bf8a48de140a3c666897588a05shiqian MOCK_METHOD1(TestMethodArg, void(int dummy)); 681e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 682e35fdd936d133bf8a48de140a3c666897588a05shiqian 683e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExpectCallLogger() { 684e35fdd936d133bf8a48de140a3c666897588a05shiqian DummyMock mock; 685e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_CALL(mock, TestMethod()); 686e35fdd936d133bf8a48de140a3c666897588a05shiqian mock.TestMethod(); 687e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 688e35fdd936d133bf8a48de140a3c666897588a05shiqian 689e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info". 690e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest, LogsWhenVerbosityIsInfo) { 691e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity), 692e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr("EXPECT_CALL(mock, TestMethod())")); 693e35fdd936d133bf8a48de140a3c666897588a05shiqian} 694e35fdd936d133bf8a48de140a3c666897588a05shiqian 695e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL doesn't log 696e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "warning". 697e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) { 698e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity)); 699e35fdd936d133bf8a48de140a3c666897588a05shiqian} 700e35fdd936d133bf8a48de140a3c666897588a05shiqian 701e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that EXPECT_CALL doesn't log 702e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "error". 703e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) { 704e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity)); 705e35fdd936d133bf8a48de140a3c666897588a05shiqian} 706e35fdd936d133bf8a48de140a3c666897588a05shiqian 707e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid OnCallLogger() { 708e35fdd936d133bf8a48de140a3c666897588a05shiqian DummyMock mock; 709e35fdd936d133bf8a48de140a3c666897588a05shiqian ON_CALL(mock, TestMethod()); 710e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 711e35fdd936d133bf8a48de140a3c666897588a05shiqian 712e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info". 713e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, LogsWhenVerbosityIsInfo) { 714e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity), 715e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr("ON_CALL(mock, TestMethod())")); 716e35fdd936d133bf8a48de140a3c666897588a05shiqian} 717e35fdd936d133bf8a48de140a3c666897588a05shiqian 718e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL doesn't log 719e35fdd936d133bf8a48de140a3c666897588a05shiqian// if the --gmock_verbose flag is set to "warning". 720e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) { 721e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ("", GrabOutput(OnCallLogger, kWarningVerbosity)); 722e35fdd936d133bf8a48de140a3c666897588a05shiqian} 723e35fdd936d133bf8a48de140a3c666897588a05shiqian 724e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL doesn't log if 725e35fdd936d133bf8a48de140a3c666897588a05shiqian// the --gmock_verbose flag is set to "error". 726e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, DoesNotLogWhenVerbosityIsError) { 727e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_EQ("", GrabOutput(OnCallLogger, kErrorVerbosity)); 728e35fdd936d133bf8a48de140a3c666897588a05shiqian} 729e35fdd936d133bf8a48de140a3c666897588a05shiqian 730e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid OnCallAnyArgumentLogger() { 731e35fdd936d133bf8a48de140a3c666897588a05shiqian DummyMock mock; 732e35fdd936d133bf8a48de140a3c666897588a05shiqian ON_CALL(mock, TestMethodArg(_)); 733e35fdd936d133bf8a48de140a3c666897588a05shiqian} 734e35fdd936d133bf8a48de140a3c666897588a05shiqian 735e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies that ON_CALL prints provided _ argument. 736e35fdd936d133bf8a48de140a3c666897588a05shiqianTEST(OnCallTest, LogsAnythingArgument) { 737e35fdd936d133bf8a48de140a3c666897588a05shiqian EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity), 738e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr("ON_CALL(mock, TestMethodArg(_)")); 739e35fdd936d133bf8a48de140a3c666897588a05shiqian} 740e35fdd936d133bf8a48de140a3c666897588a05shiqian 741e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // 0 742e35fdd936d133bf8a48de140a3c666897588a05shiqian 743b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests ArrayEq(). 744b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 745b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForDegeneratedArrays) { 746b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(5, 5L)); 747b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq('a', 0)); 748b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 749b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 750b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForOneDimensionalArrays) { 751b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int a[] = { 0, 1 }; 752b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan long b[] = { 0, 1 }; 753b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, b)); 754b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, 2, b)); 755b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 756b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan b[0] = 2; 757b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq(a, b)); 758b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq(a, 1, b)); 759b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 760b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 761b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayEqTest, WorksForTwoDimensionalArrays) { 762b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char a[][3] = { "hi", "lo" }; 763b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char b[][3] = { "hi", "lo" }; 764b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char c[][3] = { "hi", "li" }; 765b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 766b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, b)); 767b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, 2, b)); 768b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 769b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq(a, c)); 770b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_FALSE(ArrayEq(a, 2, c)); 771b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 772b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 773b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests ArrayAwareFind(). 774b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 775b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayAwareFindTest, WorksForOneDimensionalArray) { 776b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char a[] = "hello"; 777b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o')); 778b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x')); 779b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 780b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 781b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) { 782b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; 783b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int b[2] = { 2, 3 }; 784b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b)); 785b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 786b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int c[2] = { 6, 7 }; 787b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c)); 788b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 789b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 790b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests CopyArray(). 791b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 792b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForDegeneratedArrays) { 793b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int n = 0; 794b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray('a', &n); 795b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ('a', n); 796b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 797b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 798b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForOneDimensionalArrays) { 799b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char a[3] = "hi"; 800b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int b[3]; 801b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(a, &b); 802b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, b)); 803b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 804b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int c[3]; 805b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(a, 3, c); 806b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, c)); 807b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 808b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 809b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(CopyArrayTest, WorksForTwoDimensionalArrays) { 810b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; 811b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int b[2][3]; 812b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(a, &b); 813b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, b)); 814b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 815b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int c[2][3]; 816b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan CopyArray(a, 2, c); 817b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_TRUE(ArrayEq(a, c)); 818b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 819b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 820b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests NativeArray. 821b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 822b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) { 823b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int a[3] = { 0, 1, 2 }; 824b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int> na(a, kReference); 825b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(3, na.size()); 826b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a, na.begin()); 827b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 828b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 829b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, ConstructorFromTupleWorks) { 830b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int a[3] = { 0, 1, 2 }; 83190c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan int* const p = a; 832b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Tests with a plain pointer. 83390c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan NativeArray<int> na(make_tuple(p, 3U), kReference); 834b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a, na.begin()); 835b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 836b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const linked_ptr<char> b(new char); 837b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan *b = 'a'; 838b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Tests with a smart pointer. 839b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<char> nb(make_tuple(b, 1), kCopy); 840b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_NE(b.get(), nb.begin()); 841b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ('a', nb.begin()[0]); 842b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 843b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 844b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) { 845b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef int Array[2]; 846b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan Array* a = new Array[1]; 847b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan (*a)[0] = 0; 848b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan (*a)[1] = 1; 849b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int> na(*a, kCopy); 850b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_NE(*a, na.begin()); 851b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan delete[] a; 852b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, na.begin()[0]); 853b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(1, na.begin()[1]); 854b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 855b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // We rely on the heap checker to verify that na deletes the copy of 856b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // array. 857b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 858b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 859b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, TypeMembersAreCorrect) { 860b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<char, NativeArray<char>::value_type>(); 861b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>(); 862b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 863b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>(); 864b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>(); 865b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 866b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 867b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, MethodsWork) { 868b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int a[] = { 0, 1, 2 }; 869b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int> na(a, kCopy); 870b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ASSERT_EQ(3, na.size()); 871b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(3, na.end() - na.begin()); 872b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 873b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int>::const_iterator it = na.begin(); 874b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, *it); 875b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ++it; 876b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(1, *it); 877b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan it++; 878b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(2, *it); 879b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ++it; 880b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(na.end(), it); 881b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 882b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_THAT(na, Eq(na)); 883b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 884b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int> na2(a, kReference); 885b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_THAT(na, Eq(na2)); 886b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 887b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int b1[] = { 0, 1, 1 }; 888b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const int b2[] = { 0, 1, 2, 3 }; 889b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, kReference)))); 890b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, kCopy)))); 891b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 892b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 893b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(NativeArrayTest, WorksForTwoDimensionalArray) { 894b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const char a[2][3] = { "hi", "lo" }; 895b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<char[3]> na(a, kReference); 896b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ASSERT_EQ(2, na.size()); 897b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a, na.begin()); 898b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 899b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 900b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Tests StlContainerView. 901b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 902b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForStlContainer) { 903b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<std::vector<int>, 904b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<std::vector<int> >::type>(); 905b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const std::vector<double>&, 906b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<std::vector<double> >::const_reference>(); 907b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 908b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef std::vector<char> Chars; 909b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan Chars v1; 910b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const Chars& v2(StlContainerView<Chars>::ConstReference(v1)); 911b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(&v1, &v2); 912b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 913b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan v1.push_back('a'); 914b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan Chars v3 = StlContainerView<Chars>::Copy(v1); 915b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_THAT(v3, Eq(v3)); 916b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 917b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 918b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForStaticNativeArray) { 919b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<int>, 920b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<int[3]>::type>(); 921b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<double>, 922b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<const double[4]>::type>(); 923b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<char[3]>, 924b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<const char[2][3]>::type>(); 925b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 926b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const NativeArray<int>, 927b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<int[2]>::const_reference>(); 928b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 929b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int a1[3] = { 0, 1, 2 }; 930b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1); 931b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(3, a2.size()); 932b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a1, a2.begin()); 933b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 934b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1); 935b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ASSERT_EQ(3, a3.size()); 936b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, a3.begin()[0]); 937b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(1, a3.begin()[1]); 938b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(2, a3.begin()[2]); 939b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 940b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Makes sure a1 and a3 aren't aliases. 941b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan a1[0] = 3; 942b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, a3.begin()[0]); 943b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 944b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 945b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanTEST(StlContainerViewTest, WorksForDynamicNativeArray) { 946b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<int>, 947b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<tuple<const int*, size_t> >::type>(); 948b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<NativeArray<double>, 949b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<tuple<linked_ptr<double>, int> >::type>(); 950b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 951b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StaticAssertTypeEq<const NativeArray<int>, 952b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerView<tuple<const int*, int> >::const_reference>(); 953b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 954b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan int a1[3] = { 0, 1, 2 }; 95590c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan const int* const p1 = a1; 956b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >:: 95790c90f9250f19c3f3a5c2c0887c1d9e414afe41bzhanyong.wan ConstReference(make_tuple(p1, 3)); 958b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(3, a2.size()); 959b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(a1, a2.begin()); 960b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 961b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >:: 9622661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan Copy(make_tuple(static_cast<int*>(a1), 3)); 963b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan ASSERT_EQ(3, a3.size()); 964b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, a3.begin()[0]); 965b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(1, a3.begin()[1]); 966b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(2, a3.begin()[2]); 967b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 968b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Makes sure a1 and a3 aren't aliases. 969b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan a1[0] = 3; 970b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan EXPECT_EQ(0, a3.begin()[0]); 971b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 972b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 973e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace 974e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 975e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 976