Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 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/template_util.h" |
| 6 | |
Vitaly Buka | 8750b27 | 2015-08-18 18:39:08 -0700 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 9 | #include "base/basictypes.h" |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 10 | |
| 11 | namespace base { |
| 12 | namespace { |
| 13 | |
| 14 | struct AStruct {}; |
| 15 | class AClass {}; |
| 16 | enum AnEnum {}; |
| 17 | |
| 18 | class Parent {}; |
| 19 | class Child : public Parent {}; |
| 20 | |
Vitaly Buka | 8750b27 | 2015-08-18 18:39:08 -0700 | [diff] [blame] | 21 | using std::is_pointer; |
| 22 | using std::is_array; |
| 23 | using std::is_convertible; |
| 24 | using std::is_same; |
| 25 | using std::is_class; |
| 26 | using std::is_member_function_pointer; |
| 27 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 28 | // is_pointer<Type> |
| 29 | COMPILE_ASSERT(!is_pointer<int>::value, IsPointer); |
| 30 | COMPILE_ASSERT(!is_pointer<int&>::value, IsPointer); |
| 31 | COMPILE_ASSERT(is_pointer<int*>::value, IsPointer); |
| 32 | COMPILE_ASSERT(is_pointer<const int*>::value, IsPointer); |
| 33 | |
| 34 | // is_array<Type> |
| 35 | COMPILE_ASSERT(!is_array<int>::value, IsArray); |
| 36 | COMPILE_ASSERT(!is_array<int*>::value, IsArray); |
| 37 | COMPILE_ASSERT(!is_array<int(*)[3]>::value, IsArray); |
| 38 | COMPILE_ASSERT(is_array<int[]>::value, IsArray); |
| 39 | COMPILE_ASSERT(is_array<const int[]>::value, IsArray); |
| 40 | COMPILE_ASSERT(is_array<int[3]>::value, IsArray); |
| 41 | |
| 42 | // is_non_const_reference<Type> |
| 43 | COMPILE_ASSERT(!is_non_const_reference<int>::value, IsNonConstReference); |
| 44 | COMPILE_ASSERT(!is_non_const_reference<const int&>::value, IsNonConstReference); |
| 45 | COMPILE_ASSERT(is_non_const_reference<int&>::value, IsNonConstReference); |
| 46 | |
| 47 | // is_convertible<From, To> |
| 48 | |
| 49 | // Extra parens needed to make preprocessor macro parsing happy. Otherwise, |
| 50 | // it sees the equivalent of: |
| 51 | // |
| 52 | // (is_convertible < Child), (Parent > ::value) |
| 53 | // |
| 54 | // Silly C++. |
| 55 | COMPILE_ASSERT( (is_convertible<Child, Parent>::value), IsConvertible); |
| 56 | COMPILE_ASSERT(!(is_convertible<Parent, Child>::value), IsConvertible); |
| 57 | COMPILE_ASSERT(!(is_convertible<Parent, AStruct>::value), IsConvertible); |
| 58 | COMPILE_ASSERT( (is_convertible<int, double>::value), IsConvertible); |
| 59 | COMPILE_ASSERT( (is_convertible<int*, void*>::value), IsConvertible); |
| 60 | COMPILE_ASSERT(!(is_convertible<void*, int*>::value), IsConvertible); |
| 61 | |
| 62 | // Array types are an easy corner case. Make sure to test that |
| 63 | // it does indeed compile. |
| 64 | COMPILE_ASSERT(!(is_convertible<int[10], double>::value), IsConvertible); |
| 65 | COMPILE_ASSERT(!(is_convertible<double, int[10]>::value), IsConvertible); |
| 66 | COMPILE_ASSERT( (is_convertible<int[10], int*>::value), IsConvertible); |
| 67 | |
| 68 | // is_same<Type1, Type2> |
| 69 | COMPILE_ASSERT(!(is_same<Child, Parent>::value), IsSame); |
| 70 | COMPILE_ASSERT(!(is_same<Parent, Child>::value), IsSame); |
| 71 | COMPILE_ASSERT( (is_same<Parent, Parent>::value), IsSame); |
| 72 | COMPILE_ASSERT( (is_same<int*, int*>::value), IsSame); |
| 73 | COMPILE_ASSERT( (is_same<int, int>::value), IsSame); |
| 74 | COMPILE_ASSERT( (is_same<void, void>::value), IsSame); |
| 75 | COMPILE_ASSERT(!(is_same<int, double>::value), IsSame); |
| 76 | |
| 77 | |
| 78 | // is_class<Type> |
| 79 | COMPILE_ASSERT(is_class<AStruct>::value, IsClass); |
| 80 | COMPILE_ASSERT(is_class<AClass>::value, IsClass); |
| 81 | COMPILE_ASSERT(!is_class<AnEnum>::value, IsClass); |
| 82 | COMPILE_ASSERT(!is_class<int>::value, IsClass); |
| 83 | COMPILE_ASSERT(!is_class<char*>::value, IsClass); |
| 84 | COMPILE_ASSERT(!is_class<int&>::value, IsClass); |
| 85 | COMPILE_ASSERT(!is_class<char[3]>::value, IsClass); |
| 86 | |
| 87 | |
| 88 | COMPILE_ASSERT(!is_member_function_pointer<int>::value, |
| 89 | IsMemberFunctionPointer); |
| 90 | COMPILE_ASSERT(!is_member_function_pointer<int*>::value, |
| 91 | IsMemberFunctionPointer); |
| 92 | COMPILE_ASSERT(!is_member_function_pointer<void*>::value, |
| 93 | IsMemberFunctionPointer); |
| 94 | COMPILE_ASSERT(!is_member_function_pointer<AStruct>::value, |
| 95 | IsMemberFunctionPointer); |
| 96 | COMPILE_ASSERT(!is_member_function_pointer<AStruct*>::value, |
| 97 | IsMemberFunctionPointer); |
| 98 | COMPILE_ASSERT(!is_member_function_pointer<void(*)()>::value, |
| 99 | IsMemberFunctionPointer); |
| 100 | COMPILE_ASSERT(!is_member_function_pointer<int(*)(int)>::value, |
| 101 | IsMemberFunctionPointer); |
| 102 | COMPILE_ASSERT(!is_member_function_pointer<int(*)(int, int)>::value, |
| 103 | IsMemberFunctionPointer); |
| 104 | |
| 105 | COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)()>::value, |
| 106 | IsMemberFunctionPointer); |
| 107 | COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)(int)>::value, |
| 108 | IsMemberFunctionPointer); |
| 109 | COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int)>::value, |
| 110 | IsMemberFunctionPointer); |
| 111 | COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int) const>::value, |
| 112 | IsMemberFunctionPointer); |
| 113 | COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int, int)>::value, |
| 114 | IsMemberFunctionPointer); |
| 115 | |
| 116 | } // namespace |
| 117 | } // namespace base |