OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/nullptr.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/template_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 // Overloaded free function used to test pointers do not match nullptr_t. |
| 14 bool VoidParamMatchesNullptr(base::nullptr_t pointer) { |
| 15 return true; |
| 16 } |
| 17 |
| 18 bool VoidParamMatchesNullptr(void* pointer) { |
| 19 return false; |
| 20 } |
| 21 |
| 22 // Overloaded free function used to test integral constants do not |
| 23 // match nullptr_t. |
| 24 bool IntParamMatchesNullptr(base::nullptr_t param) { |
| 25 return true; |
| 26 } |
| 27 |
| 28 bool IntParamMatchesNullptr(intptr_t param) { |
| 29 return false; |
| 30 } |
| 31 |
| 32 template <typename T> struct TemplateMatchesNullptr : base::false_type {}; |
| 33 template <> struct TemplateMatchesNullptr<base::nullptr_t> : base::true_type {}; |
| 34 |
| 35 COMPILE_ASSERT(!TemplateMatchesNullptr<int>::value, MatchesNullptr); |
| 36 COMPILE_ASSERT(!TemplateMatchesNullptr<void*>::value, MatchesNullptr); |
| 37 COMPILE_ASSERT(TemplateMatchesNullptr<base::nullptr_t>::value, MatchesNullptr); |
| 38 |
| 39 template <typename T> |
| 40 bool TemplateMethodMatchesNullptr(T param) { |
| 41 return false; |
| 42 } |
| 43 |
| 44 template <> |
| 45 bool TemplateMethodMatchesNullptr<base::nullptr_t>(base::nullptr_t param) { |
| 46 return true; |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 TEST(NullptrTest, VoidParamsDontMatch) { |
| 52 void* dummy_param = NULL; |
| 53 EXPECT_FALSE(VoidParamMatchesNullptr(dummy_param)); |
| 54 EXPECT_TRUE(VoidParamMatchesNullptr(base::NullPtr)); |
| 55 } |
| 56 |
| 57 TEST(NullptrTest, IntParamsDontMatch) { |
| 58 int dummy_param = 0; |
| 59 EXPECT_FALSE(IntParamMatchesNullptr(dummy_param)); |
| 60 EXPECT_TRUE(IntParamMatchesNullptr(base::NullPtr)); |
| 61 } |
| 62 |
| 63 TEST(NullptrTest, TemplateMethodParamsDontMatch) { |
| 64 void* dummy_param = NULL; |
| 65 EXPECT_FALSE(TemplateMethodMatchesNullptr(dummy_param)); |
| 66 EXPECT_TRUE(TemplateMethodMatchesNullptr(base::NullPtr)); |
| 67 } |
OLD | NEW |