WebKit roll 109146:109201
[chromium-blink-merge.git] / base / template_util_unittest.cc
blobb330a013f6f7ccc222a2be74c320a2559c05cec0
1 // Copyright (c) 2011 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"
6 #include "testing/gtest/include/gtest/gtest.h"
8 namespace base {
9 namespace {
11 struct AStruct {};
12 class AClass {};
13 enum AnEnum {};
15 class Parent {};
16 class Child : public Parent {};
18 TEST(TemplateUtilTest, IsPointer) {
19 EXPECT_FALSE(is_pointer<int>::value);
20 EXPECT_FALSE(is_pointer<int&>::value);
21 EXPECT_TRUE(is_pointer<int*>::value);
22 EXPECT_TRUE(is_pointer<const int*>::value);
25 TEST(TemplateUtilTest, IsArray) {
26 EXPECT_FALSE(is_array<int>::value);
27 EXPECT_FALSE(is_array<int*>::value);
28 EXPECT_FALSE(is_array<int(*)[3]>::value);
29 EXPECT_TRUE(is_array<int[]>::value);
30 EXPECT_TRUE(is_array<const int[]>::value);
31 EXPECT_TRUE(is_array<int[3]>::value);
34 TEST(TemplateUtilTest, IsNonConstReference) {
35 EXPECT_FALSE(is_non_const_reference<int>::value);
36 EXPECT_FALSE(is_non_const_reference<const int&>::value);
37 EXPECT_TRUE(is_non_const_reference<int&>::value);
40 TEST(TemplateUtilTest, IsConvertible) {
41 // Extra parens needed to make EXPECT_*'s parsing happy. Otherwise,
42 // it sees the equivalent of
44 // EXPECT_TRUE( (is_convertible < Child), (Parent > ::value));
46 // Silly C++.
47 EXPECT_TRUE( (is_convertible<Child, Parent>::value) );
48 EXPECT_FALSE( (is_convertible<Parent, Child>::value) );
49 EXPECT_FALSE( (is_convertible<Parent, AStruct>::value) );
51 EXPECT_TRUE( (is_convertible<int, double>::value) );
52 EXPECT_TRUE( (is_convertible<int*, void*>::value) );
53 EXPECT_FALSE( (is_convertible<void*, int*>::value) );
55 // Array types are an easy corner case. Make sure to test that
56 // it does indeed compile.
57 EXPECT_FALSE( (is_convertible<int[10], double>::value) );
58 EXPECT_FALSE( (is_convertible<double, int[10]>::value) );
59 EXPECT_TRUE( (is_convertible<int[10], int*>::value) );
62 TEST(TemplateUtilTest, IsSame) {
63 EXPECT_FALSE( (is_same<Child, Parent>::value) );
64 EXPECT_FALSE( (is_same<Parent, Child>::value) );
65 EXPECT_TRUE( (is_same<Parent, Parent>::value) );
67 EXPECT_TRUE( (is_same<int*, int*>::value) );
68 EXPECT_TRUE( (is_same<int, int>::value) );
69 EXPECT_TRUE( (is_same<void, void>::value) );
70 EXPECT_FALSE( (is_same<int, double>::value) );
73 TEST(TemplateUtilTest, IsClass) {
74 EXPECT_TRUE(is_class<AStruct>::value);
75 EXPECT_TRUE(is_class<AClass>::value);
77 EXPECT_FALSE(is_class<AnEnum>::value);
78 EXPECT_FALSE(is_class<int>::value);
79 EXPECT_FALSE(is_class<char*>::value);
80 EXPECT_FALSE(is_class<int&>::value);
81 EXPECT_FALSE(is_class<char[3]>::value);
84 } // namespace
85 } // namespace base