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 
7 #include <gtest/gtest.h>
8 
9 
10 namespace base {
11 namespace {
12 
13 struct AStruct {};
14 class AClass {};
15 enum AnEnum {};
16 
17 class Parent {};
18 class Child : public Parent {};
19 
20 using std::is_pointer;
21 using std::is_array;
22 using std::is_convertible;
23 using std::is_same;
24 using std::is_class;
25 using std::is_member_function_pointer;
26 
27 // is_pointer<Type>
28 static_assert(!is_pointer<int>::value, "IsPointer");
29 static_assert(!is_pointer<int&>::value, "IsPointer");
30 static_assert(is_pointer<int*>::value, "IsPointer");
31 static_assert(is_pointer<const int*>::value, "IsPointer");
32 
33 // is_array<Type>
34 static_assert(!is_array<int>::value, "IsArray");
35 static_assert(!is_array<int*>::value, "IsArray");
36 static_assert(!is_array<int (*)[3]>::value, "IsArray");
37 static_assert(is_array<int[]>::value, "IsArray");
38 static_assert(is_array<const int[]>::value, "IsArray");
39 static_assert(is_array<int[3]>::value, "IsArray");
40 
41 // is_non_const_reference<Type>
42 static_assert(!is_non_const_reference<int>::value, "IsNonConstReference");
43 static_assert(!is_non_const_reference<const int&>::value,
44               "IsNonConstReference");
45 static_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 static_assert((is_convertible<Child, Parent>::value), "IsConvertible");
56 static_assert(!(is_convertible<Parent, Child>::value), "IsConvertible");
57 static_assert(!(is_convertible<Parent, AStruct>::value), "IsConvertible");
58 static_assert((is_convertible<int, double>::value), "IsConvertible");
59 static_assert((is_convertible<int*, void*>::value), "IsConvertible");
60 static_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 static_assert(!(is_convertible<int[10], double>::value), "IsConvertible");
65 static_assert(!(is_convertible<double, int[10]>::value), "IsConvertible");
66 static_assert((is_convertible<int[10], int*>::value), "IsConvertible");
67 
68 // is_same<Type1, Type2>
69 static_assert(!(is_same<Child, Parent>::value), "IsSame");
70 static_assert(!(is_same<Parent, Child>::value), "IsSame");
71 static_assert((is_same<Parent, Parent>::value), "IsSame");
72 static_assert((is_same<int*, int*>::value), "IsSame");
73 static_assert((is_same<int, int>::value), "IsSame");
74 static_assert((is_same<void, void>::value), "IsSame");
75 static_assert(!(is_same<int, double>::value), "IsSame");
76 
77 // is_class<Type>
78 static_assert(is_class<AStruct>::value, "IsClass");
79 static_assert(is_class<AClass>::value, "IsClass");
80 static_assert(!is_class<AnEnum>::value, "IsClass");
81 static_assert(!is_class<int>::value, "IsClass");
82 static_assert(!is_class<char*>::value, "IsClass");
83 static_assert(!is_class<int&>::value, "IsClass");
84 static_assert(!is_class<char[3]>::value, "IsClass");
85 
86 static_assert(!is_member_function_pointer<int>::value,
87               "IsMemberFunctionPointer");
88 static_assert(!is_member_function_pointer<int*>::value,
89               "IsMemberFunctionPointer");
90 static_assert(!is_member_function_pointer<void*>::value,
91               "IsMemberFunctionPointer");
92 static_assert(!is_member_function_pointer<AStruct>::value,
93               "IsMemberFunctionPointer");
94 static_assert(!is_member_function_pointer<AStruct*>::value,
95               "IsMemberFunctionPointer");
96 static_assert(!is_member_function_pointer<void (*)()>::value,
97               "IsMemberFunctionPointer");
98 static_assert(!is_member_function_pointer<int (*)(int)>::value,
99               "IsMemberFunctionPointer");
100 static_assert(!is_member_function_pointer<int (*)(int, int)>::value,
101               "IsMemberFunctionPointer");
102 
103 static_assert(is_member_function_pointer<void (AStruct::*)()>::value,
104               "IsMemberFunctionPointer");
105 static_assert(is_member_function_pointer<void (AStruct::*)(int)>::value,
106               "IsMemberFunctionPointer");
107 static_assert(is_member_function_pointer<int (AStruct::*)(int)>::value,
108               "IsMemberFunctionPointer");
109 static_assert(is_member_function_pointer<int (AStruct::*)(int) const>::value,
110               "IsMemberFunctionPointer");
111 static_assert(is_member_function_pointer<int (AStruct::*)(int, int)>::value,
112               "IsMemberFunctionPointer");
113 
114 }  // namespace
115 }  // namespace base
116