| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/template_util.h" | 5 #include "base/template_util.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 struct AStruct {}; | |
| 13 class AClass {}; | |
| 14 enum AnEnum {}; | |
| 15 | |
| 16 class Parent {}; | |
| 17 class Child : public Parent {}; | |
| 18 | |
| 19 // is_pointer<Type> | |
| 20 static_assert(!is_pointer<int>::value, "IsPointer"); | |
| 21 static_assert(!is_pointer<int&>::value, "IsPointer"); | |
| 22 static_assert(is_pointer<int*>::value, "IsPointer"); | |
| 23 static_assert(is_pointer<const int*>::value, "IsPointer"); | |
| 24 | |
| 25 // is_array<Type> | |
| 26 static_assert(!is_array<int>::value, "IsArray"); | |
| 27 static_assert(!is_array<int*>::value, "IsArray"); | |
| 28 static_assert(!is_array<int (*)[3]>::value, "IsArray"); | |
| 29 static_assert(is_array<int[]>::value, "IsArray"); | |
| 30 static_assert(is_array<const int[]>::value, "IsArray"); | |
| 31 static_assert(is_array<int[3]>::value, "IsArray"); | |
| 32 | |
| 33 // is_non_const_reference<Type> | 12 // is_non_const_reference<Type> |
| 34 static_assert(!is_non_const_reference<int>::value, "IsNonConstReference"); | 13 static_assert(!is_non_const_reference<int>::value, "IsNonConstReference"); |
| 35 static_assert(!is_non_const_reference<const int&>::value, | 14 static_assert(!is_non_const_reference<const int&>::value, |
| 36 "IsNonConstReference"); | 15 "IsNonConstReference"); |
| 37 static_assert(is_non_const_reference<int&>::value, "IsNonConstReference"); | 16 static_assert(is_non_const_reference<int&>::value, "IsNonConstReference"); |
| 38 | 17 |
| 39 // is_convertible<From, To> | |
| 40 | |
| 41 // Extra parens needed to make preprocessor macro parsing happy. Otherwise, | |
| 42 // it sees the equivalent of: | |
| 43 // | |
| 44 // (is_convertible < Child), (Parent > ::value) | |
| 45 // | |
| 46 // Silly C++. | |
| 47 static_assert((is_convertible<Child, Parent>::value), "IsConvertible"); | |
| 48 static_assert(!(is_convertible<Parent, Child>::value), "IsConvertible"); | |
| 49 static_assert(!(is_convertible<Parent, AStruct>::value), "IsConvertible"); | |
| 50 static_assert((is_convertible<int, double>::value), "IsConvertible"); | |
| 51 static_assert((is_convertible<int*, void*>::value), "IsConvertible"); | |
| 52 static_assert(!(is_convertible<void*, int*>::value), "IsConvertible"); | |
| 53 | |
| 54 // Array types are an easy corner case. Make sure to test that | |
| 55 // it does indeed compile. | |
| 56 static_assert(!(is_convertible<int[10], double>::value), "IsConvertible"); | |
| 57 static_assert(!(is_convertible<double, int[10]>::value), "IsConvertible"); | |
| 58 static_assert((is_convertible<int[10], int*>::value), "IsConvertible"); | |
| 59 | |
| 60 // is_same<Type1, Type2> | |
| 61 static_assert(!(is_same<Child, Parent>::value), "IsSame"); | |
| 62 static_assert(!(is_same<Parent, Child>::value), "IsSame"); | |
| 63 static_assert((is_same<Parent, Parent>::value), "IsSame"); | |
| 64 static_assert((is_same<int*, int*>::value), "IsSame"); | |
| 65 static_assert((is_same<int, int>::value), "IsSame"); | |
| 66 static_assert((is_same<void, void>::value), "IsSame"); | |
| 67 static_assert(!(is_same<int, double>::value), "IsSame"); | |
| 68 | |
| 69 // is_class<Type> | |
| 70 static_assert(is_class<AStruct>::value, "IsClass"); | |
| 71 static_assert(is_class<AClass>::value, "IsClass"); | |
| 72 static_assert(!is_class<AnEnum>::value, "IsClass"); | |
| 73 static_assert(!is_class<int>::value, "IsClass"); | |
| 74 static_assert(!is_class<char*>::value, "IsClass"); | |
| 75 static_assert(!is_class<int&>::value, "IsClass"); | |
| 76 static_assert(!is_class<char[3]>::value, "IsClass"); | |
| 77 | |
| 78 static_assert(!is_member_function_pointer<int>::value, | |
| 79 "IsMemberFunctionPointer"); | |
| 80 static_assert(!is_member_function_pointer<int*>::value, | |
| 81 "IsMemberFunctionPointer"); | |
| 82 static_assert(!is_member_function_pointer<void*>::value, | |
| 83 "IsMemberFunctionPointer"); | |
| 84 static_assert(!is_member_function_pointer<AStruct>::value, | |
| 85 "IsMemberFunctionPointer"); | |
| 86 static_assert(!is_member_function_pointer<AStruct*>::value, | |
| 87 "IsMemberFunctionPointer"); | |
| 88 static_assert(!is_member_function_pointer<void (*)()>::value, | |
| 89 "IsMemberFunctionPointer"); | |
| 90 static_assert(!is_member_function_pointer<int (*)(int)>::value, | |
| 91 "IsMemberFunctionPointer"); | |
| 92 static_assert(!is_member_function_pointer<int (*)(int, int)>::value, | |
| 93 "IsMemberFunctionPointer"); | |
| 94 | |
| 95 static_assert(is_member_function_pointer<void (AStruct::*)()>::value, | |
| 96 "IsMemberFunctionPointer"); | |
| 97 static_assert(is_member_function_pointer<void (AStruct::*)(int)>::value, | |
| 98 "IsMemberFunctionPointer"); | |
| 99 static_assert(is_member_function_pointer<int (AStruct::*)(int)>::value, | |
| 100 "IsMemberFunctionPointer"); | |
| 101 static_assert(is_member_function_pointer<int (AStruct::*)(int) const>::value, | |
| 102 "IsMemberFunctionPointer"); | |
| 103 static_assert(is_member_function_pointer<int (AStruct::*)(int, int)>::value, | |
| 104 "IsMemberFunctionPointer"); | |
| 105 | |
| 106 } // namespace | 18 } // namespace |
| 107 } // namespace base | 19 } // namespace base |
| OLD | NEW |