shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2007, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file tests the internal utilities. |
| 35 | |
| 36 | #include <gmock/internal/gmock-internal-utils.h> |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 37 | #include <stdlib.h> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 38 | #include <map> |
| 39 | #include <string> |
| 40 | #include <sstream> |
| 41 | #include <vector> |
| 42 | #include <gmock/gmock.h> |
| 43 | #include <gmock/internal/gmock-port.h> |
| 44 | #include <gtest/gtest.h> |
| 45 | #include <gtest/gtest-spi.h> |
| 46 | |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 47 | #if GTEST_OS_CYGWIN |
| 48 | #include <sys/types.h> // For ssize_t. NOLINT |
| 49 | #endif |
| 50 | |
zhanyong.wan | 0ea67f8 | 2009-08-14 04:50:02 +0000 | [diff] [blame^] | 51 | class ProtocolMessage; |
| 52 | |
| 53 | namespace proto2 { |
| 54 | class Message; |
| 55 | } // namespace proto2 |
| 56 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 57 | namespace testing { |
| 58 | namespace internal { |
| 59 | |
| 60 | namespace { |
| 61 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 62 | using ::std::tr1::make_tuple; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 63 | using ::std::tr1::tuple; |
| 64 | |
zhanyong.wan | ce198ff | 2009-02-12 01:34:27 +0000 | [diff] [blame] | 65 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) { |
| 66 | EXPECT_EQ("", ConvertIdentifierNameToWords("")); |
| 67 | EXPECT_EQ("", ConvertIdentifierNameToWords("_")); |
| 68 | EXPECT_EQ("", ConvertIdentifierNameToWords("__")); |
| 69 | } |
| 70 | |
| 71 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) { |
| 72 | EXPECT_EQ("1", ConvertIdentifierNameToWords("_1")); |
| 73 | EXPECT_EQ("2", ConvertIdentifierNameToWords("2_")); |
| 74 | EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_")); |
| 75 | EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56")); |
| 76 | } |
| 77 | |
| 78 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) { |
| 79 | EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord")); |
| 80 | EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar")); |
| 81 | EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_")); |
| 82 | EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_")); |
| 83 | EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar")); |
| 84 | } |
| 85 | |
| 86 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) { |
| 87 | EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar")); |
| 88 | EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_")); |
| 89 | EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_")); |
| 90 | EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar")); |
| 91 | } |
| 92 | |
| 93 | TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) { |
| 94 | EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123")); |
| 95 | EXPECT_EQ("chapter 11 section 1", |
| 96 | ConvertIdentifierNameToWords("_Chapter11Section_1_")); |
| 97 | } |
| 98 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 99 | // Tests that CompileAssertTypesEqual compiles when the type arguments are |
| 100 | // equal. |
| 101 | TEST(CompileAssertTypesEqual, CompilesWhenTypesAreEqual) { |
| 102 | CompileAssertTypesEqual<void, void>(); |
| 103 | CompileAssertTypesEqual<int*, int*>(); |
| 104 | } |
| 105 | |
| 106 | // Tests that RemoveReference does not affect non-reference types. |
| 107 | TEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) { |
| 108 | CompileAssertTypesEqual<int, RemoveReference<int>::type>(); |
| 109 | CompileAssertTypesEqual<const char, RemoveReference<const char>::type>(); |
| 110 | } |
| 111 | |
| 112 | // Tests that RemoveReference removes reference from reference types. |
| 113 | TEST(RemoveReferenceTest, RemovesReference) { |
| 114 | CompileAssertTypesEqual<int, RemoveReference<int&>::type>(); |
| 115 | CompileAssertTypesEqual<const char, RemoveReference<const char&>::type>(); |
| 116 | } |
| 117 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 118 | // Tests GMOCK_REMOVE_REFERENCE_. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 119 | |
| 120 | template <typename T1, typename T2> |
| 121 | void TestGMockRemoveReference() { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 122 | CompileAssertTypesEqual<T1, GMOCK_REMOVE_REFERENCE_(T2)>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST(RemoveReferenceTest, MacroVersion) { |
| 126 | TestGMockRemoveReference<int, int>(); |
| 127 | TestGMockRemoveReference<const char, const char&>(); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | // Tests that RemoveConst does not affect non-const types. |
| 132 | TEST(RemoveConstTest, DoesNotAffectNonConstType) { |
| 133 | CompileAssertTypesEqual<int, RemoveConst<int>::type>(); |
| 134 | CompileAssertTypesEqual<char&, RemoveConst<char&>::type>(); |
| 135 | } |
| 136 | |
| 137 | // Tests that RemoveConst removes const from const types. |
| 138 | TEST(RemoveConstTest, RemovesConst) { |
| 139 | CompileAssertTypesEqual<int, RemoveConst<const int>::type>(); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 140 | CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>(); |
| 141 | CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 142 | } |
| 143 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 144 | // Tests GMOCK_REMOVE_CONST_. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 145 | |
| 146 | template <typename T1, typename T2> |
| 147 | void TestGMockRemoveConst() { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 148 | CompileAssertTypesEqual<T1, GMOCK_REMOVE_CONST_(T2)>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST(RemoveConstTest, MacroVersion) { |
| 152 | TestGMockRemoveConst<int, int>(); |
| 153 | TestGMockRemoveConst<double&, double&>(); |
| 154 | TestGMockRemoveConst<char, const char>(); |
| 155 | } |
| 156 | |
| 157 | // Tests that AddReference does not affect reference types. |
| 158 | TEST(AddReferenceTest, DoesNotAffectReferenceType) { |
| 159 | CompileAssertTypesEqual<int&, AddReference<int&>::type>(); |
| 160 | CompileAssertTypesEqual<const char&, AddReference<const char&>::type>(); |
| 161 | } |
| 162 | |
| 163 | // Tests that AddReference adds reference to non-reference types. |
| 164 | TEST(AddReferenceTest, AddsReference) { |
| 165 | CompileAssertTypesEqual<int&, AddReference<int>::type>(); |
| 166 | CompileAssertTypesEqual<const char&, AddReference<const char>::type>(); |
| 167 | } |
| 168 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 169 | // Tests GMOCK_ADD_REFERENCE_. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 170 | |
| 171 | template <typename T1, typename T2> |
| 172 | void TestGMockAddReference() { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 173 | CompileAssertTypesEqual<T1, GMOCK_ADD_REFERENCE_(T2)>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | TEST(AddReferenceTest, MacroVersion) { |
| 177 | TestGMockAddReference<int&, int>(); |
| 178 | TestGMockAddReference<const char&, const char&>(); |
| 179 | } |
| 180 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 181 | // Tests GMOCK_REFERENCE_TO_CONST_. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 182 | |
| 183 | template <typename T1, typename T2> |
| 184 | void TestGMockReferenceToConst() { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 185 | CompileAssertTypesEqual<T1, GMOCK_REFERENCE_TO_CONST_(T2)>(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST(GMockReferenceToConstTest, Works) { |
| 189 | TestGMockReferenceToConst<const char&, char>(); |
| 190 | TestGMockReferenceToConst<const int&, const int>(); |
| 191 | TestGMockReferenceToConst<const double&, double>(); |
| 192 | TestGMockReferenceToConst<const string&, const string&>(); |
| 193 | } |
| 194 | |
| 195 | TEST(PointeeOfTest, WorksForSmartPointers) { |
| 196 | CompileAssertTypesEqual<const char, |
| 197 | PointeeOf<internal::linked_ptr<const char> >::type>(); |
| 198 | } |
| 199 | |
| 200 | TEST(PointeeOfTest, WorksForRawPointers) { |
| 201 | CompileAssertTypesEqual<int, PointeeOf<int*>::type>(); |
| 202 | CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>(); |
| 203 | CompileAssertTypesEqual<void, PointeeOf<void*>::type>(); |
| 204 | } |
| 205 | |
| 206 | TEST(GetRawPointerTest, WorksForSmartPointers) { |
| 207 | const char* const raw_p4 = new const char('a'); // NOLINT |
| 208 | const internal::linked_ptr<const char> p4(raw_p4); |
| 209 | EXPECT_EQ(raw_p4, GetRawPointer(p4)); |
| 210 | } |
| 211 | |
| 212 | TEST(GetRawPointerTest, WorksForRawPointers) { |
| 213 | int* p = NULL; |
| 214 | EXPECT_EQ(NULL, GetRawPointer(p)); |
| 215 | int n = 1; |
| 216 | EXPECT_EQ(&n, GetRawPointer(&n)); |
| 217 | } |
| 218 | |
| 219 | class Base {}; |
| 220 | class Derived : public Base {}; |
| 221 | |
| 222 | // Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant. |
| 223 | TEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 224 | GMOCK_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true); |
| 225 | GMOCK_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value), |
| 226 | const_false); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can |
| 230 | // be implicitly converted to T2. |
| 231 | TEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) { |
| 232 | EXPECT_TRUE((ImplicitlyConvertible<int, double>::value)); |
| 233 | EXPECT_TRUE((ImplicitlyConvertible<double, int>::value)); |
| 234 | EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value)); |
| 235 | EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value)); |
| 236 | EXPECT_TRUE((ImplicitlyConvertible<Derived&, const Base&>::value)); |
| 237 | EXPECT_TRUE((ImplicitlyConvertible<const Base, Base>::value)); |
| 238 | } |
| 239 | |
| 240 | // Tests that ImplicitlyConvertible<T1, T2>::value is false when T1 |
| 241 | // cannot be implicitly converted to T2. |
| 242 | TEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) { |
| 243 | EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value)); |
| 244 | EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value)); |
| 245 | EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value)); |
| 246 | EXPECT_FALSE((ImplicitlyConvertible<Base&, Derived&>::value)); |
| 247 | } |
| 248 | |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 249 | // Tests KindOf<T>. |
| 250 | |
| 251 | TEST(KindOfTest, Bool) { |
| 252 | EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT |
| 253 | } |
| 254 | |
| 255 | TEST(KindOfTest, Integer) { |
| 256 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT |
| 257 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT |
| 258 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT |
| 259 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT |
| 260 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT |
| 261 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT |
| 262 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT |
| 263 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT |
| 264 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT |
| 265 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT |
| 266 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64)); // NOLINT |
| 267 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64)); // NOLINT |
| 268 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT |
| 269 | #if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN |
| 270 | // ssize_t is not defined on Windows and possibly some other OSes. |
| 271 | EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | TEST(KindOfTest, FloatingPoint) { |
| 276 | EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT |
| 277 | EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT |
| 278 | EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT |
| 279 | } |
| 280 | |
| 281 | TEST(KindOfTest, Other) { |
| 282 | EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT |
| 283 | EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT |
| 284 | EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT |
| 285 | } |
| 286 | |
| 287 | // Tests LosslessArithmeticConvertible<T, U>. |
| 288 | |
| 289 | TEST(LosslessArithmeticConvertibleTest, BoolToBool) { |
| 290 | EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value)); |
| 291 | } |
| 292 | |
| 293 | TEST(LosslessArithmeticConvertibleTest, BoolToInteger) { |
| 294 | EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value)); |
| 295 | EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value)); |
| 296 | EXPECT_TRUE( |
| 297 | (LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT |
| 298 | } |
| 299 | |
| 300 | TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) { |
| 301 | EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value)); |
| 302 | EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value)); |
| 303 | } |
| 304 | |
| 305 | TEST(LosslessArithmeticConvertibleTest, IntegerToBool) { |
| 306 | EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value)); |
| 307 | EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value)); |
| 308 | } |
| 309 | |
| 310 | TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) { |
| 311 | // Unsigned => larger signed is fine. |
| 312 | EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value)); |
| 313 | |
| 314 | // Unsigned => larger unsigned is fine. |
| 315 | EXPECT_TRUE( |
| 316 | (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT |
| 317 | |
| 318 | // Signed => unsigned is not fine. |
| 319 | EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT |
| 320 | EXPECT_FALSE((LosslessArithmeticConvertible< |
| 321 | signed char, unsigned int>::value)); // NOLINT |
| 322 | |
| 323 | // Same size and same signedness: fine too. |
| 324 | EXPECT_TRUE((LosslessArithmeticConvertible< |
| 325 | unsigned char, unsigned char>::value)); |
| 326 | EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value)); |
| 327 | EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value)); |
| 328 | EXPECT_TRUE((LosslessArithmeticConvertible< |
| 329 | unsigned long, unsigned long>::value)); // NOLINT |
| 330 | |
| 331 | // Same size, different signedness: not fine. |
| 332 | EXPECT_FALSE((LosslessArithmeticConvertible< |
| 333 | unsigned char, signed char>::value)); |
| 334 | EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value)); |
| 335 | EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value)); |
| 336 | |
| 337 | // Larger size => smaller size is not fine. |
| 338 | EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT |
| 339 | EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value)); |
| 340 | EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value)); |
| 341 | } |
| 342 | |
| 343 | TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) { |
| 344 | // Integers cannot be losslessly converted to floating-points, as |
| 345 | // the format of the latter is implementation-defined. |
| 346 | EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value)); |
| 347 | EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value)); |
| 348 | EXPECT_FALSE((LosslessArithmeticConvertible< |
| 349 | short, long double>::value)); // NOLINT |
| 350 | } |
| 351 | |
| 352 | TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) { |
| 353 | EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value)); |
| 354 | EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value)); |
| 355 | } |
| 356 | |
| 357 | TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) { |
| 358 | EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT |
| 359 | EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value)); |
| 360 | EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value)); |
| 361 | } |
| 362 | |
| 363 | TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) { |
| 364 | // Smaller size => larger size is fine. |
| 365 | EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value)); |
| 366 | EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value)); |
| 367 | EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value)); |
| 368 | |
| 369 | // Same size: fine. |
| 370 | EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value)); |
| 371 | EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value)); |
| 372 | |
| 373 | // Larger size => smaller size is not fine. |
| 374 | EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value)); |
| 375 | if (sizeof(double) == sizeof(long double)) { // NOLINT |
| 376 | // In some implementations (e.g. MSVC), double and long double |
| 377 | // have the same size. |
| 378 | EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value)); |
| 379 | } else { |
| 380 | EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value)); |
| 381 | } |
| 382 | } |
| 383 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 384 | // Tests that IsAProtocolMessage<T>::value is a compile-time constant. |
| 385 | TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 386 | GMOCK_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value, const_true); |
| 387 | GMOCK_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Tests that IsAProtocolMessage<T>::value is true when T is |
| 391 | // ProtocolMessage or a sub-class of it. |
| 392 | TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { |
zhanyong.wan | 0ea67f8 | 2009-08-14 04:50:02 +0000 | [diff] [blame^] | 393 | EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 394 | EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); |
| 395 | #if GMOCK_HAS_PROTOBUF_ |
| 396 | EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); |
| 397 | #endif // GMOCK_HAS_PROTOBUF_ |
| 398 | } |
| 399 | |
| 400 | // Tests that IsAProtocolMessage<T>::value is false when T is neither |
| 401 | // ProtocolMessage nor a sub-class of it. |
| 402 | TEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) { |
| 403 | EXPECT_FALSE(IsAProtocolMessage<int>::value); |
| 404 | EXPECT_FALSE(IsAProtocolMessage<const Base>::value); |
| 405 | } |
| 406 | |
| 407 | // Tests IsContainerTest. |
| 408 | |
| 409 | class NonContainer {}; |
| 410 | |
| 411 | TEST(IsContainerTestTest, WorksForNonContainer) { |
| 412 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0))); |
| 413 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0))); |
| 414 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0))); |
| 415 | } |
| 416 | |
| 417 | TEST(IsContainerTestTest, WorksForContainer) { |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 418 | EXPECT_EQ(sizeof(IsContainer), |
| 419 | sizeof(IsContainerTest<std::vector<bool> >(0))); |
| 420 | EXPECT_EQ(sizeof(IsContainer), |
| 421 | sizeof(IsContainerTest<std::map<int, double> >(0))); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Tests the TupleMatches() template function. |
| 425 | |
| 426 | TEST(TupleMatchesTest, WorksForSize0) { |
| 427 | tuple<> matchers; |
| 428 | tuple<> values; |
| 429 | |
| 430 | EXPECT_TRUE(TupleMatches(matchers, values)); |
| 431 | } |
| 432 | |
| 433 | TEST(TupleMatchesTest, WorksForSize1) { |
| 434 | tuple<Matcher<int> > matchers(Eq(1)); |
| 435 | tuple<int> values1(1), |
| 436 | values2(2); |
| 437 | |
| 438 | EXPECT_TRUE(TupleMatches(matchers, values1)); |
| 439 | EXPECT_FALSE(TupleMatches(matchers, values2)); |
| 440 | } |
| 441 | |
| 442 | TEST(TupleMatchesTest, WorksForSize2) { |
| 443 | tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a')); |
| 444 | tuple<int, char> values1(1, 'a'), |
| 445 | values2(1, 'b'), |
| 446 | values3(2, 'a'), |
| 447 | values4(2, 'b'); |
| 448 | |
| 449 | EXPECT_TRUE(TupleMatches(matchers, values1)); |
| 450 | EXPECT_FALSE(TupleMatches(matchers, values2)); |
| 451 | EXPECT_FALSE(TupleMatches(matchers, values3)); |
| 452 | EXPECT_FALSE(TupleMatches(matchers, values4)); |
| 453 | } |
| 454 | |
| 455 | TEST(TupleMatchesTest, WorksForSize5) { |
| 456 | tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>, // NOLINT |
| 457 | Matcher<string> > |
| 458 | matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi")); |
| 459 | tuple<int, char, bool, long, string> // NOLINT |
| 460 | values1(1, 'a', true, 2L, "hi"), |
| 461 | values2(1, 'a', true, 2L, "hello"), |
| 462 | values3(2, 'a', true, 2L, "hi"); |
| 463 | |
| 464 | EXPECT_TRUE(TupleMatches(matchers, values1)); |
| 465 | EXPECT_FALSE(TupleMatches(matchers, values2)); |
| 466 | EXPECT_FALSE(TupleMatches(matchers, values3)); |
| 467 | } |
| 468 | |
| 469 | // Tests that Assert(true, ...) succeeds. |
| 470 | TEST(AssertTest, SucceedsOnTrue) { |
| 471 | Assert(true, __FILE__, __LINE__, "This should succeed."); |
| 472 | Assert(true, __FILE__, __LINE__); // This should succeed too. |
| 473 | } |
| 474 | |
zhanyong.wan | 652540a | 2009-02-23 23:37:29 +0000 | [diff] [blame] | 475 | #if GTEST_HAS_DEATH_TEST |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 476 | |
| 477 | // Tests that Assert(false, ...) generates a fatal failure. |
| 478 | TEST(AssertTest, FailsFatallyOnFalse) { |
| 479 | EXPECT_DEATH({ // NOLINT |
| 480 | Assert(false, __FILE__, __LINE__, "This should fail."); |
| 481 | }, ""); |
| 482 | |
| 483 | EXPECT_DEATH({ // NOLINT |
| 484 | Assert(false, __FILE__, __LINE__); |
| 485 | }, ""); |
| 486 | } |
| 487 | |
| 488 | #endif // GTEST_HAS_DEATH_TEST |
| 489 | |
| 490 | // Tests that Expect(true, ...) succeeds. |
| 491 | TEST(ExpectTest, SucceedsOnTrue) { |
| 492 | Expect(true, __FILE__, __LINE__, "This should succeed."); |
| 493 | Expect(true, __FILE__, __LINE__); // This should succeed too. |
| 494 | } |
| 495 | |
| 496 | // Tests that Expect(false, ...) generates a non-fatal failure. |
| 497 | TEST(ExpectTest, FailsNonfatallyOnFalse) { |
| 498 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 499 | Expect(false, __FILE__, __LINE__, "This should fail."); |
| 500 | }, "This should fail"); |
| 501 | |
| 502 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 503 | Expect(false, __FILE__, __LINE__); |
| 504 | }, "Expectation failed"); |
| 505 | } |
| 506 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 507 | // Tests LogIsVisible(). |
| 508 | |
| 509 | class LogIsVisibleTest : public ::testing::Test { |
| 510 | protected: |
zhanyong.wan | 81476f2 | 2009-06-22 23:30:47 +0000 | [diff] [blame] | 511 | virtual void SetUp() { |
| 512 | // The code needs to work when both ::string and ::std::string are |
| 513 | // defined and the flag is implemented as a |
| 514 | // testing::internal::String. In this case, without the call to |
| 515 | // c_str(), the compiler will complain that it cannot figure out |
| 516 | // whether the String flag should be converted to a ::string or an |
| 517 | // ::std::string before being assigned to original_verbose_. |
| 518 | original_verbose_ = GMOCK_FLAG(verbose).c_str(); |
| 519 | } |
| 520 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 521 | virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } |
| 522 | |
| 523 | string original_verbose_; |
| 524 | }; |
| 525 | |
| 526 | TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) { |
| 527 | GMOCK_FLAG(verbose) = kInfoVerbosity; |
| 528 | EXPECT_TRUE(LogIsVisible(INFO)); |
| 529 | EXPECT_TRUE(LogIsVisible(WARNING)); |
| 530 | } |
| 531 | |
| 532 | TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) { |
| 533 | GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 534 | EXPECT_FALSE(LogIsVisible(INFO)); |
| 535 | EXPECT_FALSE(LogIsVisible(WARNING)); |
| 536 | } |
| 537 | |
| 538 | TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) { |
| 539 | GMOCK_FLAG(verbose) = kWarningVerbosity; |
| 540 | EXPECT_FALSE(LogIsVisible(INFO)); |
| 541 | EXPECT_TRUE(LogIsVisible(WARNING)); |
| 542 | } |
| 543 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 544 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 545 | #if 0 |
| 546 | |
| 547 | // Tests the Log() function. |
| 548 | |
| 549 | // Verifies that Log() behaves correctly for the given verbosity level |
| 550 | // and log severity. |
| 551 | void TestLogWithSeverity(const string& verbosity, LogSeverity severity, |
| 552 | bool should_print) { |
| 553 | const string old_flag = GMOCK_FLAG(verbose); |
| 554 | GMOCK_FLAG(verbose) = verbosity; |
| 555 | CaptureTestStdout(); |
| 556 | Log(severity, "Test log.\n", 0); |
| 557 | if (should_print) { |
| 558 | EXPECT_PRED2(RE::FullMatch, |
| 559 | GetCapturedTestStdout(), |
| 560 | severity == WARNING ? |
| 561 | "\nGMOCK WARNING:\nTest log\\.\nStack trace:\n[\\s\\S]*" : |
| 562 | "\nTest log\\.\nStack trace:\n[\\s\\S]*"); |
| 563 | } else { |
| 564 | EXPECT_EQ("", GetCapturedTestStdout()); |
| 565 | } |
| 566 | GMOCK_FLAG(verbose) = old_flag; |
| 567 | } |
| 568 | |
| 569 | // Tests that when the stack_frames_to_skip parameter is negative, |
| 570 | // Log() doesn't include the stack trace in the output. |
| 571 | TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) { |
| 572 | GMOCK_FLAG(verbose) = kInfoVerbosity; |
| 573 | CaptureTestStdout(); |
| 574 | Log(INFO, "Test log.\n", -1); |
| 575 | EXPECT_EQ("\nTest log.\n", GetCapturedTestStdout()); |
| 576 | } |
| 577 | |
| 578 | // Tests that in opt mode, a positive stack_frames_to_skip argument is |
| 579 | // treated as 0. |
| 580 | TEST(LogTest, NoSkippingStackFrameInOptMode) { |
| 581 | CaptureTestStdout(); |
| 582 | Log(WARNING, "Test log.\n", 100); |
| 583 | const string log = GetCapturedTestStdout(); |
| 584 | #ifdef NDEBUG |
| 585 | // In opt mode, no stack frame should be skipped. |
| 586 | EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n" |
| 587 | "Test log\\.\n" |
| 588 | "Stack trace:\n" |
| 589 | ".+")); |
| 590 | #else |
| 591 | // In dbg mode, the stack frames should be skipped. |
| 592 | EXPECT_EQ("\nGMOCK WARNING:\n" |
| 593 | "Test log.\n" |
| 594 | "Stack trace:\n", log); |
| 595 | #endif // NDEBUG |
| 596 | } |
| 597 | |
| 598 | // Tests that all logs are printed when the value of the |
| 599 | // --gmock_verbose flag is "info". |
| 600 | TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) { |
| 601 | TestLogWithSeverity(kInfoVerbosity, INFO, true); |
| 602 | TestLogWithSeverity(kInfoVerbosity, WARNING, true); |
| 603 | } |
| 604 | |
| 605 | // Tests that only warnings are printed when the value of the |
| 606 | // --gmock_verbose flag is "warning". |
| 607 | TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) { |
| 608 | TestLogWithSeverity(kWarningVerbosity, INFO, false); |
| 609 | TestLogWithSeverity(kWarningVerbosity, WARNING, true); |
| 610 | } |
| 611 | |
| 612 | // Tests that no logs are printed when the value of the |
| 613 | // --gmock_verbose flag is "error". |
| 614 | TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) { |
| 615 | TestLogWithSeverity(kErrorVerbosity, INFO, false); |
| 616 | TestLogWithSeverity(kErrorVerbosity, WARNING, false); |
| 617 | } |
| 618 | |
| 619 | // Tests that only warnings are printed when the value of the |
| 620 | // --gmock_verbose flag is invalid. |
| 621 | TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) { |
| 622 | TestLogWithSeverity("invalid", INFO, false); |
| 623 | TestLogWithSeverity("invalid", WARNING, true); |
| 624 | } |
| 625 | |
| 626 | #endif // 0 |
| 627 | |
| 628 | TEST(TypeTraitsTest, true_type) { |
| 629 | EXPECT_TRUE(true_type::value); |
| 630 | } |
| 631 | |
| 632 | TEST(TypeTraitsTest, false_type) { |
| 633 | EXPECT_FALSE(false_type::value); |
| 634 | } |
| 635 | |
| 636 | TEST(TypeTraitsTest, is_reference) { |
| 637 | EXPECT_FALSE(is_reference<int>::value); |
| 638 | EXPECT_FALSE(is_reference<char*>::value); |
| 639 | EXPECT_TRUE(is_reference<const int&>::value); |
| 640 | } |
| 641 | |
| 642 | TEST(TypeTraitsTest, is_pointer) { |
| 643 | EXPECT_FALSE(is_pointer<int>::value); |
| 644 | EXPECT_FALSE(is_pointer<char&>::value); |
| 645 | EXPECT_TRUE(is_pointer<const int*>::value); |
| 646 | } |
| 647 | |
| 648 | TEST(TypeTraitsTest, type_equals) { |
| 649 | EXPECT_FALSE((type_equals<int, const int>::value)); |
| 650 | EXPECT_FALSE((type_equals<int, int&>::value)); |
| 651 | EXPECT_FALSE((type_equals<int, double>::value)); |
| 652 | EXPECT_TRUE((type_equals<char, char>::value)); |
| 653 | } |
| 654 | |
| 655 | TEST(TypeTraitsTest, remove_reference) { |
| 656 | EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value)); |
| 657 | EXPECT_TRUE((type_equals<const int, |
| 658 | remove_reference<const int&>::type>::value)); |
| 659 | EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value)); |
| 660 | EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value)); |
| 661 | } |
| 662 | |
| 663 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 664 | #if 0 |
| 665 | |
| 666 | // Verifies that Log() behaves correctly for the given verbosity level |
| 667 | // and log severity. |
| 668 | string GrabOutput(void(*logger)(), const char* verbosity) { |
| 669 | const string saved_flag = GMOCK_FLAG(verbose); |
| 670 | GMOCK_FLAG(verbose) = verbosity; |
| 671 | CaptureTestStdout(); |
| 672 | logger(); |
| 673 | GMOCK_FLAG(verbose) = saved_flag; |
| 674 | return GetCapturedTestStdout(); |
| 675 | } |
| 676 | |
| 677 | class DummyMock { |
| 678 | public: |
| 679 | MOCK_METHOD0(TestMethod, void()); |
| 680 | MOCK_METHOD1(TestMethodArg, void(int dummy)); |
| 681 | }; |
| 682 | |
| 683 | void ExpectCallLogger() { |
| 684 | DummyMock mock; |
| 685 | EXPECT_CALL(mock, TestMethod()); |
| 686 | mock.TestMethod(); |
| 687 | }; |
| 688 | |
| 689 | // Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info". |
| 690 | TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) { |
| 691 | EXPECT_THAT(GrabOutput(ExpectCallLogger, kInfoVerbosity), |
| 692 | HasSubstr("EXPECT_CALL(mock, TestMethod())")); |
| 693 | } |
| 694 | |
| 695 | // Verifies that EXPECT_CALL doesn't log |
| 696 | // if the --gmock_verbose flag is set to "warning". |
| 697 | TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) { |
| 698 | EXPECT_EQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity)); |
| 699 | } |
| 700 | |
| 701 | // Verifies that EXPECT_CALL doesn't log |
| 702 | // if the --gmock_verbose flag is set to "error". |
| 703 | TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) { |
| 704 | EXPECT_EQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity)); |
| 705 | } |
| 706 | |
| 707 | void OnCallLogger() { |
| 708 | DummyMock mock; |
| 709 | ON_CALL(mock, TestMethod()); |
| 710 | }; |
| 711 | |
| 712 | // Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info". |
| 713 | TEST(OnCallTest, LogsWhenVerbosityIsInfo) { |
| 714 | EXPECT_THAT(GrabOutput(OnCallLogger, kInfoVerbosity), |
| 715 | HasSubstr("ON_CALL(mock, TestMethod())")); |
| 716 | } |
| 717 | |
| 718 | // Verifies that ON_CALL doesn't log |
| 719 | // if the --gmock_verbose flag is set to "warning". |
| 720 | TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) { |
| 721 | EXPECT_EQ("", GrabOutput(OnCallLogger, kWarningVerbosity)); |
| 722 | } |
| 723 | |
| 724 | // Verifies that ON_CALL doesn't log if |
| 725 | // the --gmock_verbose flag is set to "error". |
| 726 | TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) { |
| 727 | EXPECT_EQ("", GrabOutput(OnCallLogger, kErrorVerbosity)); |
| 728 | } |
| 729 | |
| 730 | void OnCallAnyArgumentLogger() { |
| 731 | DummyMock mock; |
| 732 | ON_CALL(mock, TestMethodArg(_)); |
| 733 | } |
| 734 | |
| 735 | // Verifies that ON_CALL prints provided _ argument. |
| 736 | TEST(OnCallTest, LogsAnythingArgument) { |
| 737 | EXPECT_THAT(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity), |
| 738 | HasSubstr("ON_CALL(mock, TestMethodArg(_)")); |
| 739 | } |
| 740 | |
| 741 | #endif // 0 |
| 742 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 743 | // Tests ArrayEq(). |
| 744 | |
| 745 | TEST(ArrayEqTest, WorksForDegeneratedArrays) { |
| 746 | EXPECT_TRUE(ArrayEq(5, 5L)); |
| 747 | EXPECT_FALSE(ArrayEq('a', 0)); |
| 748 | } |
| 749 | |
| 750 | TEST(ArrayEqTest, WorksForOneDimensionalArrays) { |
| 751 | const int a[] = { 0, 1 }; |
| 752 | long b[] = { 0, 1 }; |
| 753 | EXPECT_TRUE(ArrayEq(a, b)); |
| 754 | EXPECT_TRUE(ArrayEq(a, 2, b)); |
| 755 | |
| 756 | b[0] = 2; |
| 757 | EXPECT_FALSE(ArrayEq(a, b)); |
| 758 | EXPECT_FALSE(ArrayEq(a, 1, b)); |
| 759 | } |
| 760 | |
| 761 | TEST(ArrayEqTest, WorksForTwoDimensionalArrays) { |
| 762 | const char a[][3] = { "hi", "lo" }; |
| 763 | const char b[][3] = { "hi", "lo" }; |
| 764 | const char c[][3] = { "hi", "li" }; |
| 765 | |
| 766 | EXPECT_TRUE(ArrayEq(a, b)); |
| 767 | EXPECT_TRUE(ArrayEq(a, 2, b)); |
| 768 | |
| 769 | EXPECT_FALSE(ArrayEq(a, c)); |
| 770 | EXPECT_FALSE(ArrayEq(a, 2, c)); |
| 771 | } |
| 772 | |
| 773 | // Tests ArrayAwareFind(). |
| 774 | |
| 775 | TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) { |
| 776 | const char a[] = "hello"; |
| 777 | EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o')); |
| 778 | EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x')); |
| 779 | } |
| 780 | |
| 781 | TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) { |
| 782 | int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; |
| 783 | const int b[2] = { 2, 3 }; |
| 784 | EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b)); |
| 785 | |
| 786 | const int c[2] = { 6, 7 }; |
| 787 | EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c)); |
| 788 | } |
| 789 | |
| 790 | // Tests CopyArray(). |
| 791 | |
| 792 | TEST(CopyArrayTest, WorksForDegeneratedArrays) { |
| 793 | int n = 0; |
| 794 | CopyArray('a', &n); |
| 795 | EXPECT_EQ('a', n); |
| 796 | } |
| 797 | |
| 798 | TEST(CopyArrayTest, WorksForOneDimensionalArrays) { |
| 799 | const char a[3] = "hi"; |
| 800 | int b[3]; |
| 801 | CopyArray(a, &b); |
| 802 | EXPECT_TRUE(ArrayEq(a, b)); |
| 803 | |
| 804 | int c[3]; |
| 805 | CopyArray(a, 3, c); |
| 806 | EXPECT_TRUE(ArrayEq(a, c)); |
| 807 | } |
| 808 | |
| 809 | TEST(CopyArrayTest, WorksForTwoDimensionalArrays) { |
| 810 | const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; |
| 811 | int b[2][3]; |
| 812 | CopyArray(a, &b); |
| 813 | EXPECT_TRUE(ArrayEq(a, b)); |
| 814 | |
| 815 | int c[2][3]; |
| 816 | CopyArray(a, 2, c); |
| 817 | EXPECT_TRUE(ArrayEq(a, c)); |
| 818 | } |
| 819 | |
| 820 | // Tests NativeArray. |
| 821 | |
| 822 | TEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) { |
| 823 | const int a[3] = { 0, 1, 2 }; |
| 824 | NativeArray<int> na(a, kReference); |
| 825 | EXPECT_EQ(3, na.size()); |
| 826 | EXPECT_EQ(a, na.begin()); |
| 827 | } |
| 828 | |
| 829 | TEST(NativeArrayTest, ConstructorFromTupleWorks) { |
| 830 | int a[3] = { 0, 1, 2 }; |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 831 | int* const p = a; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 832 | // Tests with a plain pointer. |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 833 | NativeArray<int> na(make_tuple(p, 3U), kReference); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 834 | EXPECT_EQ(a, na.begin()); |
| 835 | |
| 836 | const linked_ptr<char> b(new char); |
| 837 | *b = 'a'; |
| 838 | // Tests with a smart pointer. |
| 839 | NativeArray<char> nb(make_tuple(b, 1), kCopy); |
| 840 | EXPECT_NE(b.get(), nb.begin()); |
| 841 | EXPECT_EQ('a', nb.begin()[0]); |
| 842 | } |
| 843 | |
| 844 | TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) { |
| 845 | typedef int Array[2]; |
| 846 | Array* a = new Array[1]; |
| 847 | (*a)[0] = 0; |
| 848 | (*a)[1] = 1; |
| 849 | NativeArray<int> na(*a, kCopy); |
| 850 | EXPECT_NE(*a, na.begin()); |
| 851 | delete[] a; |
| 852 | EXPECT_EQ(0, na.begin()[0]); |
| 853 | EXPECT_EQ(1, na.begin()[1]); |
| 854 | |
| 855 | // We rely on the heap checker to verify that na deletes the copy of |
| 856 | // array. |
| 857 | } |
| 858 | |
| 859 | TEST(NativeArrayTest, TypeMembersAreCorrect) { |
| 860 | StaticAssertTypeEq<char, NativeArray<char>::value_type>(); |
| 861 | StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>(); |
| 862 | |
| 863 | StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>(); |
| 864 | StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>(); |
| 865 | } |
| 866 | |
| 867 | TEST(NativeArrayTest, MethodsWork) { |
| 868 | const int a[] = { 0, 1, 2 }; |
| 869 | NativeArray<int> na(a, kCopy); |
| 870 | ASSERT_EQ(3, na.size()); |
| 871 | EXPECT_EQ(3, na.end() - na.begin()); |
| 872 | |
| 873 | NativeArray<int>::const_iterator it = na.begin(); |
| 874 | EXPECT_EQ(0, *it); |
| 875 | ++it; |
| 876 | EXPECT_EQ(1, *it); |
| 877 | it++; |
| 878 | EXPECT_EQ(2, *it); |
| 879 | ++it; |
| 880 | EXPECT_EQ(na.end(), it); |
| 881 | |
| 882 | EXPECT_THAT(na, Eq(na)); |
| 883 | |
| 884 | NativeArray<int> na2(a, kReference); |
| 885 | EXPECT_THAT(na, Eq(na2)); |
| 886 | |
| 887 | const int b1[] = { 0, 1, 1 }; |
| 888 | const int b2[] = { 0, 1, 2, 3 }; |
| 889 | EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, kReference)))); |
| 890 | EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, kCopy)))); |
| 891 | } |
| 892 | |
| 893 | TEST(NativeArrayTest, WorksForTwoDimensionalArray) { |
| 894 | const char a[2][3] = { "hi", "lo" }; |
| 895 | NativeArray<char[3]> na(a, kReference); |
| 896 | ASSERT_EQ(2, na.size()); |
| 897 | EXPECT_EQ(a, na.begin()); |
| 898 | } |
| 899 | |
| 900 | // Tests StlContainerView. |
| 901 | |
| 902 | TEST(StlContainerViewTest, WorksForStlContainer) { |
| 903 | StaticAssertTypeEq<std::vector<int>, |
| 904 | StlContainerView<std::vector<int> >::type>(); |
| 905 | StaticAssertTypeEq<const std::vector<double>&, |
| 906 | StlContainerView<std::vector<double> >::const_reference>(); |
| 907 | |
| 908 | typedef std::vector<char> Chars; |
| 909 | Chars v1; |
| 910 | const Chars& v2(StlContainerView<Chars>::ConstReference(v1)); |
| 911 | EXPECT_EQ(&v1, &v2); |
| 912 | |
| 913 | v1.push_back('a'); |
| 914 | Chars v3 = StlContainerView<Chars>::Copy(v1); |
| 915 | EXPECT_THAT(v3, Eq(v3)); |
| 916 | } |
| 917 | |
| 918 | TEST(StlContainerViewTest, WorksForStaticNativeArray) { |
| 919 | StaticAssertTypeEq<NativeArray<int>, |
| 920 | StlContainerView<int[3]>::type>(); |
| 921 | StaticAssertTypeEq<NativeArray<double>, |
| 922 | StlContainerView<const double[4]>::type>(); |
| 923 | StaticAssertTypeEq<NativeArray<char[3]>, |
| 924 | StlContainerView<const char[2][3]>::type>(); |
| 925 | |
| 926 | StaticAssertTypeEq<const NativeArray<int>, |
| 927 | StlContainerView<int[2]>::const_reference>(); |
| 928 | |
| 929 | int a1[3] = { 0, 1, 2 }; |
| 930 | NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1); |
| 931 | EXPECT_EQ(3, a2.size()); |
| 932 | EXPECT_EQ(a1, a2.begin()); |
| 933 | |
| 934 | const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1); |
| 935 | ASSERT_EQ(3, a3.size()); |
| 936 | EXPECT_EQ(0, a3.begin()[0]); |
| 937 | EXPECT_EQ(1, a3.begin()[1]); |
| 938 | EXPECT_EQ(2, a3.begin()[2]); |
| 939 | |
| 940 | // Makes sure a1 and a3 aren't aliases. |
| 941 | a1[0] = 3; |
| 942 | EXPECT_EQ(0, a3.begin()[0]); |
| 943 | } |
| 944 | |
| 945 | TEST(StlContainerViewTest, WorksForDynamicNativeArray) { |
| 946 | StaticAssertTypeEq<NativeArray<int>, |
| 947 | StlContainerView<tuple<const int*, size_t> >::type>(); |
| 948 | StaticAssertTypeEq<NativeArray<double>, |
| 949 | StlContainerView<tuple<linked_ptr<double>, int> >::type>(); |
| 950 | |
| 951 | StaticAssertTypeEq<const NativeArray<int>, |
| 952 | StlContainerView<tuple<const int*, int> >::const_reference>(); |
| 953 | |
| 954 | int a1[3] = { 0, 1, 2 }; |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 955 | const int* const p1 = a1; |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 956 | NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >:: |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 957 | ConstReference(make_tuple(p1, 3)); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 958 | EXPECT_EQ(3, a2.size()); |
| 959 | EXPECT_EQ(a1, a2.begin()); |
| 960 | |
| 961 | const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >:: |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 962 | Copy(make_tuple(static_cast<int*>(a1), 3)); |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 963 | ASSERT_EQ(3, a3.size()); |
| 964 | EXPECT_EQ(0, a3.begin()[0]); |
| 965 | EXPECT_EQ(1, a3.begin()[1]); |
| 966 | EXPECT_EQ(2, a3.begin()[2]); |
| 967 | |
| 968 | // Makes sure a1 and a3 aren't aliases. |
| 969 | a1[0] = 3; |
| 970 | EXPECT_EQ(0, a3.begin()[0]); |
| 971 | } |
| 972 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 973 | } // namespace |
| 974 | } // namespace internal |
| 975 | } // namespace testing |