Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / base / template_util_unittest.cc
blob3ec3887154d89a4fd99033e9d5ca2bd77a2111ae
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.
5 #include "base/template_util.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace base {
11 namespace {
13 struct AStruct {};
14 class AClass {};
15 enum AnEnum {};
17 class Parent {};
18 class Child : public Parent {};
20 // is_pointer<Type>
21 COMPILE_ASSERT(!is_pointer<int>::value, IsPointer);
22 COMPILE_ASSERT(!is_pointer<int&>::value, IsPointer);
23 COMPILE_ASSERT(is_pointer<int*>::value, IsPointer);
24 COMPILE_ASSERT(is_pointer<const int*>::value, IsPointer);
26 // is_array<Type>
27 COMPILE_ASSERT(!is_array<int>::value, IsArray);
28 COMPILE_ASSERT(!is_array<int*>::value, IsArray);
29 COMPILE_ASSERT(!is_array<int(*)[3]>::value, IsArray);
30 COMPILE_ASSERT(is_array<int[]>::value, IsArray);
31 COMPILE_ASSERT(is_array<const int[]>::value, IsArray);
32 COMPILE_ASSERT(is_array<int[3]>::value, IsArray);
34 // is_non_const_reference<Type>
35 COMPILE_ASSERT(!is_non_const_reference<int>::value, IsNonConstReference);
36 COMPILE_ASSERT(!is_non_const_reference<const int&>::value, IsNonConstReference);
37 COMPILE_ASSERT(is_non_const_reference<int&>::value, IsNonConstReference);
39 // is_convertible<From, To>
41 // Extra parens needed to make preprocessor macro parsing happy. Otherwise,
42 // it sees the equivalent of:
44 // (is_convertible < Child), (Parent > ::value)
46 // Silly C++.
47 COMPILE_ASSERT( (is_convertible<Child, Parent>::value), IsConvertible);
48 COMPILE_ASSERT(!(is_convertible<Parent, Child>::value), IsConvertible);
49 COMPILE_ASSERT(!(is_convertible<Parent, AStruct>::value), IsConvertible);
50 COMPILE_ASSERT( (is_convertible<int, double>::value), IsConvertible);
51 COMPILE_ASSERT( (is_convertible<int*, void*>::value), IsConvertible);
52 COMPILE_ASSERT(!(is_convertible<void*, int*>::value), IsConvertible);
54 // Array types are an easy corner case. Make sure to test that
55 // it does indeed compile.
56 COMPILE_ASSERT(!(is_convertible<int[10], double>::value), IsConvertible);
57 COMPILE_ASSERT(!(is_convertible<double, int[10]>::value), IsConvertible);
58 COMPILE_ASSERT( (is_convertible<int[10], int*>::value), IsConvertible);
60 // is_same<Type1, Type2>
61 COMPILE_ASSERT(!(is_same<Child, Parent>::value), IsSame);
62 COMPILE_ASSERT(!(is_same<Parent, Child>::value), IsSame);
63 COMPILE_ASSERT( (is_same<Parent, Parent>::value), IsSame);
64 COMPILE_ASSERT( (is_same<int*, int*>::value), IsSame);
65 COMPILE_ASSERT( (is_same<int, int>::value), IsSame);
66 COMPILE_ASSERT( (is_same<void, void>::value), IsSame);
67 COMPILE_ASSERT(!(is_same<int, double>::value), IsSame);
70 // is_class<Type>
71 COMPILE_ASSERT(is_class<AStruct>::value, IsClass);
72 COMPILE_ASSERT(is_class<AClass>::value, IsClass);
73 COMPILE_ASSERT(!is_class<AnEnum>::value, IsClass);
74 COMPILE_ASSERT(!is_class<int>::value, IsClass);
75 COMPILE_ASSERT(!is_class<char*>::value, IsClass);
76 COMPILE_ASSERT(!is_class<int&>::value, IsClass);
77 COMPILE_ASSERT(!is_class<char[3]>::value, IsClass);
80 COMPILE_ASSERT(!is_member_function_pointer<int>::value,
81 IsMemberFunctionPointer);
82 COMPILE_ASSERT(!is_member_function_pointer<int*>::value,
83 IsMemberFunctionPointer);
84 COMPILE_ASSERT(!is_member_function_pointer<void*>::value,
85 IsMemberFunctionPointer);
86 COMPILE_ASSERT(!is_member_function_pointer<AStruct>::value,
87 IsMemberFunctionPointer);
88 COMPILE_ASSERT(!is_member_function_pointer<AStruct*>::value,
89 IsMemberFunctionPointer);
90 COMPILE_ASSERT(!is_member_function_pointer<void(*)()>::value,
91 IsMemberFunctionPointer);
92 COMPILE_ASSERT(!is_member_function_pointer<int(*)(int)>::value,
93 IsMemberFunctionPointer);
94 COMPILE_ASSERT(!is_member_function_pointer<int(*)(int, int)>::value,
95 IsMemberFunctionPointer);
97 COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)()>::value,
98 IsMemberFunctionPointer);
99 COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)(int)>::value,
100 IsMemberFunctionPointer);
101 COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int)>::value,
102 IsMemberFunctionPointer);
103 COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int) const>::value,
104 IsMemberFunctionPointer);
105 COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int, int)>::value,
106 IsMemberFunctionPointer);
108 } // namespace
109 } // namespace base