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 "base/basictypes.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace base {
11namespace {
12
13struct AStruct {};
14class AClass {};
15enum AnEnum {};
16
17class Parent {};
18class Child : public Parent {};
19
20// is_pointer<Type>
21COMPILE_ASSERT(!is_pointer<int>::value, IsPointer);
22COMPILE_ASSERT(!is_pointer<int&>::value, IsPointer);
23COMPILE_ASSERT(is_pointer<int*>::value, IsPointer);
24COMPILE_ASSERT(is_pointer<const int*>::value, IsPointer);
25
26// is_array<Type>
27COMPILE_ASSERT(!is_array<int>::value, IsArray);
28COMPILE_ASSERT(!is_array<int*>::value, IsArray);
29COMPILE_ASSERT(!is_array<int(*)[3]>::value, IsArray);
30COMPILE_ASSERT(is_array<int[]>::value, IsArray);
31COMPILE_ASSERT(is_array<const int[]>::value, IsArray);
32COMPILE_ASSERT(is_array<int[3]>::value, IsArray);
33
34// is_non_const_reference<Type>
35COMPILE_ASSERT(!is_non_const_reference<int>::value, IsNonConstReference);
36COMPILE_ASSERT(!is_non_const_reference<const int&>::value, IsNonConstReference);
37COMPILE_ASSERT(is_non_const_reference<int&>::value, IsNonConstReference);
38
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++.
47COMPILE_ASSERT( (is_convertible<Child, Parent>::value), IsConvertible);
48COMPILE_ASSERT(!(is_convertible<Parent, Child>::value), IsConvertible);
49COMPILE_ASSERT(!(is_convertible<Parent, AStruct>::value), IsConvertible);
50COMPILE_ASSERT( (is_convertible<int, double>::value), IsConvertible);
51COMPILE_ASSERT( (is_convertible<int*, void*>::value), IsConvertible);
52COMPILE_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.
56COMPILE_ASSERT(!(is_convertible<int[10], double>::value), IsConvertible);
57COMPILE_ASSERT(!(is_convertible<double, int[10]>::value), IsConvertible);
58COMPILE_ASSERT( (is_convertible<int[10], int*>::value), IsConvertible);
59
60// is_same<Type1, Type2>
61COMPILE_ASSERT(!(is_same<Child, Parent>::value), IsSame);
62COMPILE_ASSERT(!(is_same<Parent, Child>::value), IsSame);
63COMPILE_ASSERT( (is_same<Parent, Parent>::value), IsSame);
64COMPILE_ASSERT( (is_same<int*, int*>::value), IsSame);
65COMPILE_ASSERT( (is_same<int, int>::value), IsSame);
66COMPILE_ASSERT( (is_same<void, void>::value), IsSame);
67COMPILE_ASSERT(!(is_same<int, double>::value), IsSame);
68
69
70// is_class<Type>
71COMPILE_ASSERT(is_class<AStruct>::value, IsClass);
72COMPILE_ASSERT(is_class<AClass>::value, IsClass);
73COMPILE_ASSERT(!is_class<AnEnum>::value, IsClass);
74COMPILE_ASSERT(!is_class<int>::value, IsClass);
75COMPILE_ASSERT(!is_class<char*>::value, IsClass);
76COMPILE_ASSERT(!is_class<int&>::value, IsClass);
77COMPILE_ASSERT(!is_class<char[3]>::value, IsClass);
78
79
80COMPILE_ASSERT(!is_member_function_pointer<int>::value,
81               IsMemberFunctionPointer);
82COMPILE_ASSERT(!is_member_function_pointer<int*>::value,
83               IsMemberFunctionPointer);
84COMPILE_ASSERT(!is_member_function_pointer<void*>::value,
85               IsMemberFunctionPointer);
86COMPILE_ASSERT(!is_member_function_pointer<AStruct>::value,
87               IsMemberFunctionPointer);
88COMPILE_ASSERT(!is_member_function_pointer<AStruct*>::value,
89               IsMemberFunctionPointer);
90COMPILE_ASSERT(!is_member_function_pointer<int(*)(int)>::value,
91               IsMemberFunctionPointer);
92COMPILE_ASSERT(!is_member_function_pointer<int(*)(int, int)>::value,
93               IsMemberFunctionPointer);
94
95COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)()>::value,
96               IsMemberFunctionPointer);
97COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)(int)>::value,
98               IsMemberFunctionPointer);
99COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int)>::value,
100               IsMemberFunctionPointer);
101COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int) const>::value,
102               IsMemberFunctionPointer);
103COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int, int)>::value,
104               IsMemberFunctionPointer);
105COMPILE_ASSERT(is_member_function_pointer<
106                 int (AStruct::*)(int, int) const>::value,
107               IsMemberFunctionPointer);
108COMPILE_ASSERT(is_member_function_pointer<
109                 int (AStruct::*)(int, int, int)>::value,
110               IsMemberFunctionPointer);
111COMPILE_ASSERT(is_member_function_pointer<
112                 int (AStruct::*)(int, int, int) const>::value,
113               IsMemberFunctionPointer);
114COMPILE_ASSERT(is_member_function_pointer<
115                 int (AStruct::*)(int, int, int, int)>::value,
116               IsMemberFunctionPointer);
117COMPILE_ASSERT(is_member_function_pointer<
118                 int (AStruct::*)(int, int, int, int) const>::value,
119               IsMemberFunctionPointer);
120
121// False because we don't have a specialization for 5 params yet.
122COMPILE_ASSERT(!is_member_function_pointer<
123                 int (AStruct::*)(int, int, int, int, int)>::value,
124               IsMemberFunctionPointer);
125COMPILE_ASSERT(!is_member_function_pointer<
126                 int (AStruct::*)(int, int, int, int, int) const>::value,
127               IsMemberFunctionPointer);
128
129}  // namespace
130}  // namespace base
131