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