Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: base/template_util_unittest.cc

Issue 10565006: Switch template_utils_unittest to using COMPILE_ASSERT. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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);
20 EXPECT_FALSE(is_pointer<int&>::value);
21 EXPECT_TRUE(is_pointer<int*>::value);
22 EXPECT_TRUE(is_pointer<const int*>::value);
23 }
24 21
Jeffrey Yasskin 2012/06/18 21:36:00 Nit: I would skip the blank lines between the head
25 TEST(TemplateUtilTest, IsArray) { 22 COMPILE_ASSERT(!is_pointer<int>::value, IsPointer);
26 EXPECT_FALSE(is_array<int>::value); 23 COMPILE_ASSERT(!is_pointer<int&>::value, IsPointer);
27 EXPECT_FALSE(is_array<int*>::value); 24 COMPILE_ASSERT(is_pointer<int*>::value, IsPointer);
28 EXPECT_FALSE(is_array<int(*)[3]>::value); 25 COMPILE_ASSERT(is_pointer<const int*>::value, IsPointer);
29 EXPECT_TRUE(is_array<int[]>::value);
30 EXPECT_TRUE(is_array<const int[]>::value);
31 EXPECT_TRUE(is_array<int[3]>::value);
32 }
33 26
34 TEST(TemplateUtilTest, IsNonConstReference) { 27 // is_array<Type>
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);
38 }
39 28
40 TEST(TemplateUtilTest, IsConvertible) { 29 COMPILE_ASSERT(!is_array<int>::value, IsArray);
41 // Extra parens needed to make EXPECT_*'s parsing happy. Otherwise, 30 COMPILE_ASSERT(!is_array<int*>::value, IsArray);
42 // it sees the equivalent of 31 COMPILE_ASSERT(!is_array<int(*)[3]>::value, IsArray);
43 // 32 COMPILE_ASSERT(is_array<int[]>::value, IsArray);
44 // EXPECT_TRUE( (is_convertible < Child), (Parent > ::value)); 33 COMPILE_ASSERT(is_array<const int[]>::value, IsArray);
45 // 34 COMPILE_ASSERT(is_array<int[3]>::value, IsArray);
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 35
51 EXPECT_TRUE( (is_convertible<int, double>::value) ); 36 // is_non_const_reference<Type>
52 EXPECT_TRUE( (is_convertible<int*, void*>::value) );
53 EXPECT_FALSE( (is_convertible<void*, int*>::value) );
54 37
55 // Array types are an easy corner case. Make sure to test that 38 COMPILE_ASSERT(!is_non_const_reference<int>::value, IsNonConstReference);
56 // it does indeed compile. 39 COMPILE_ASSERT(!is_non_const_reference<const int&>::value, IsNonConstReference);
57 EXPECT_FALSE( (is_convertible<int[10], double>::value) ); 40 COMPILE_ASSERT(is_non_const_reference<int&>::value, IsNonConstReference);
58 EXPECT_FALSE( (is_convertible<double, int[10]>::value) );
59 EXPECT_TRUE( (is_convertible<int[10], int*>::value) );
60 }
61 41
62 TEST(TemplateUtilTest, IsSame) { 42 // is_convertible<From, To>
63 EXPECT_FALSE( (is_same<Child, Parent>::value) );
64 EXPECT_FALSE( (is_same<Parent, Child>::value) );
65 EXPECT_TRUE( (is_same<Parent, Parent>::value) );
66 43
67 EXPECT_TRUE( (is_same<int*, int*>::value) ); 44 // Extra parens needed to make preprocessor macro parsing happy. Otherwise,
68 EXPECT_TRUE( (is_same<int, int>::value) ); 45 // it sees the equivalent of:
69 EXPECT_TRUE( (is_same<void, void>::value) ); 46 //
70 EXPECT_FALSE( (is_same<int, double>::value) ); 47 // (is_convertible < Child), (Parent > ::value)
71 } 48 //
49 // Silly C++.
72 50
73 TEST(TemplateUtilTest, IsClass) { 51 COMPILE_ASSERT( (is_convertible<Child, Parent>::value), IsConvertible);
74 EXPECT_TRUE(is_class<AStruct>::value); 52 COMPILE_ASSERT(!(is_convertible<Parent, Child>::value), IsConvertible);
75 EXPECT_TRUE(is_class<AClass>::value); 53 COMPILE_ASSERT(!(is_convertible<Parent, AStruct>::value), IsConvertible);
54 COMPILE_ASSERT( (is_convertible<int, double>::value), IsConvertible);
55 COMPILE_ASSERT( (is_convertible<int*, void*>::value), IsConvertible);
56 COMPILE_ASSERT(!(is_convertible<void*, int*>::value), IsConvertible);
76 57
77 EXPECT_FALSE(is_class<AnEnum>::value); 58 // Array types are an easy corner case. Make sure to test that
78 EXPECT_FALSE(is_class<int>::value); 59 // it does indeed compile.
79 EXPECT_FALSE(is_class<char*>::value); 60 COMPILE_ASSERT(!(is_convertible<int[10], double>::value), IsConvertible);
80 EXPECT_FALSE(is_class<int&>::value); 61 COMPILE_ASSERT(!(is_convertible<double, int[10]>::value), IsConvertible);
81 EXPECT_FALSE(is_class<char[3]>::value); 62 COMPILE_ASSERT( (is_convertible<int[10], int*>::value), IsConvertible);
82 } 63
64 // is_same<Type1, Type2>
65
66 COMPILE_ASSERT(!(is_same<Child, Parent>::value), IsSame);
67 COMPILE_ASSERT(!(is_same<Parent, Child>::value), IsSame);
68 COMPILE_ASSERT( (is_same<Parent, Parent>::value), IsSame);
69 COMPILE_ASSERT( (is_same<int*, int*>::value), IsSame);
70 COMPILE_ASSERT( (is_same<int, int>::value), IsSame);
71 COMPILE_ASSERT( (is_same<void, void>::value), IsSame);
72 COMPILE_ASSERT(!(is_same<int, double>::value), IsSame);
73
74
75 // is_class<Type>
76
77 COMPILE_ASSERT(is_class<AStruct>::value, IsClass);
78 COMPILE_ASSERT(is_class<AClass>::value, IsClass);
79 COMPILE_ASSERT(!is_class<AnEnum>::value, IsClass);
80 COMPILE_ASSERT(!is_class<int>::value, IsClass);
81 COMPILE_ASSERT(!is_class<char*>::value, IsClass);
82 COMPILE_ASSERT(!is_class<int&>::value, IsClass);
83 COMPILE_ASSERT(!is_class<char[3]>::value, IsClass);
84
83 85
84 } // namespace 86 } // namespace
85 } // namespace base 87 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698