OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/template_util.h" | 5 #include "base/template_util.h" |
| 6 |
| 7 #include "base/basictypes.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
7 | 9 |
8 namespace base { | 10 namespace base { |
9 namespace { | 11 namespace { |
10 | 12 |
11 struct AStruct {}; | 13 struct AStruct {}; |
12 class AClass {}; | 14 class AClass {}; |
13 enum AnEnum {}; | 15 enum AnEnum {}; |
14 | 16 |
15 class Parent {}; | 17 class Parent {}; |
16 class Child : public Parent {}; | 18 class Child : public Parent {}; |
17 | 19 |
18 TEST(TemplateUtilTest, IsPointer) { | 20 // is_pointer<Type> |
19 EXPECT_FALSE(is_pointer<int>::value); | 21 COMPILE_ASSERT(!is_pointer<int>::value, IsPointer); |
20 EXPECT_FALSE(is_pointer<int&>::value); | 22 COMPILE_ASSERT(!is_pointer<int&>::value, IsPointer); |
21 EXPECT_TRUE(is_pointer<int*>::value); | 23 COMPILE_ASSERT(is_pointer<int*>::value, IsPointer); |
22 EXPECT_TRUE(is_pointer<const int*>::value); | 24 COMPILE_ASSERT(is_pointer<const int*>::value, IsPointer); |
23 } | |
24 | 25 |
25 TEST(TemplateUtilTest, IsArray) { | 26 // is_array<Type> |
26 EXPECT_FALSE(is_array<int>::value); | 27 COMPILE_ASSERT(!is_array<int>::value, IsArray); |
27 EXPECT_FALSE(is_array<int*>::value); | 28 COMPILE_ASSERT(!is_array<int*>::value, IsArray); |
28 EXPECT_FALSE(is_array<int(*)[3]>::value); | 29 COMPILE_ASSERT(!is_array<int(*)[3]>::value, IsArray); |
29 EXPECT_TRUE(is_array<int[]>::value); | 30 COMPILE_ASSERT(is_array<int[]>::value, IsArray); |
30 EXPECT_TRUE(is_array<const int[]>::value); | 31 COMPILE_ASSERT(is_array<const int[]>::value, IsArray); |
31 EXPECT_TRUE(is_array<int[3]>::value); | 32 COMPILE_ASSERT(is_array<int[3]>::value, IsArray); |
32 } | |
33 | 33 |
34 TEST(TemplateUtilTest, IsNonConstReference) { | 34 // is_non_const_reference<Type> |
35 EXPECT_FALSE(is_non_const_reference<int>::value); | 35 COMPILE_ASSERT(!is_non_const_reference<int>::value, IsNonConstReference); |
36 EXPECT_FALSE(is_non_const_reference<const int&>::value); | 36 COMPILE_ASSERT(!is_non_const_reference<const int&>::value, IsNonConstReference); |
37 EXPECT_TRUE(is_non_const_reference<int&>::value); | 37 COMPILE_ASSERT(is_non_const_reference<int&>::value, IsNonConstReference); |
38 } | |
39 | 38 |
40 TEST(TemplateUtilTest, IsConvertible) { | 39 // is_convertible<From, To> |
41 // Extra parens needed to make EXPECT_*'s parsing happy. Otherwise, | |
42 // it sees the equivalent of | |
43 // | |
44 // EXPECT_TRUE( (is_convertible < Child), (Parent > ::value)); | |
45 // | |
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) ); | |
50 | 40 |
51 EXPECT_TRUE( (is_convertible<int, double>::value) ); | 41 // Extra parens needed to make preprocessor macro parsing happy. Otherwise, |
52 EXPECT_TRUE( (is_convertible<int*, void*>::value) ); | 42 // it sees the equivalent of: |
53 EXPECT_FALSE( (is_convertible<void*, int*>::value) ); | 43 // |
| 44 // (is_convertible < Child), (Parent > ::value) |
| 45 // |
| 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 | 53 |
55 // Array types are an easy corner case. Make sure to test that | 54 // Array types are an easy corner case. Make sure to test that |
56 // it does indeed compile. | 55 // it does indeed compile. |
57 EXPECT_FALSE( (is_convertible<int[10], double>::value) ); | 56 COMPILE_ASSERT(!(is_convertible<int[10], double>::value), IsConvertible); |
58 EXPECT_FALSE( (is_convertible<double, int[10]>::value) ); | 57 COMPILE_ASSERT(!(is_convertible<double, int[10]>::value), IsConvertible); |
59 EXPECT_TRUE( (is_convertible<int[10], int*>::value) ); | 58 COMPILE_ASSERT( (is_convertible<int[10], int*>::value), IsConvertible); |
60 } | |
61 | 59 |
62 TEST(TemplateUtilTest, IsSame) { | 60 // is_same<Type1, Type2> |
63 EXPECT_FALSE( (is_same<Child, Parent>::value) ); | 61 COMPILE_ASSERT(!(is_same<Child, Parent>::value), IsSame); |
64 EXPECT_FALSE( (is_same<Parent, Child>::value) ); | 62 COMPILE_ASSERT(!(is_same<Parent, Child>::value), IsSame); |
65 EXPECT_TRUE( (is_same<Parent, Parent>::value) ); | 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); |
66 | 68 |
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) ); | |
71 } | |
72 | 69 |
73 TEST(TemplateUtilTest, IsClass) { | 70 // is_class<Type> |
74 EXPECT_TRUE(is_class<AStruct>::value); | 71 COMPILE_ASSERT(is_class<AStruct>::value, IsClass); |
75 EXPECT_TRUE(is_class<AClass>::value); | 72 COMPILE_ASSERT(is_class<AClass>::value, IsClass); |
76 | 73 COMPILE_ASSERT(!is_class<AnEnum>::value, IsClass); |
77 EXPECT_FALSE(is_class<AnEnum>::value); | 74 COMPILE_ASSERT(!is_class<int>::value, IsClass); |
78 EXPECT_FALSE(is_class<int>::value); | 75 COMPILE_ASSERT(!is_class<char*>::value, IsClass); |
79 EXPECT_FALSE(is_class<char*>::value); | 76 COMPILE_ASSERT(!is_class<int&>::value, IsClass); |
80 EXPECT_FALSE(is_class<int&>::value); | 77 COMPILE_ASSERT(!is_class<char[3]>::value, IsClass); |
81 EXPECT_FALSE(is_class<char[3]>::value); | |
82 } | |
83 | 78 |
84 } // namespace | 79 } // namespace |
85 } // namespace base | 80 } // namespace base |
OLD | NEW |