Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "base/template_util.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace base {
      9 namespace {
     10 
     11 struct AStruct {};
     12 class AClass {};
     13 enum AnEnum {};
     14 
     15 class Parent {};
     16 class Child : public Parent {};
     17 
     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);
     23 }
     24 
     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);
     32 }
     33 
     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);
     38 }
     39 
     40 TEST(TemplateUtilTest, IsConvertible) {
     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 
     51   EXPECT_TRUE( (is_convertible<int, double>::value) );
     52   EXPECT_TRUE( (is_convertible<int*, void*>::value) );
     53   EXPECT_FALSE( (is_convertible<void*, int*>::value) );
     54 }
     55 
     56 TEST(TemplateUtilTest, IsClass) {
     57   EXPECT_TRUE(is_class<AStruct>::value);
     58   EXPECT_TRUE(is_class<AClass>::value);
     59 
     60   EXPECT_FALSE(is_class<AnEnum>::value);
     61   EXPECT_FALSE(is_class<int>::value);
     62   EXPECT_FALSE(is_class<char*>::value);
     63   EXPECT_FALSE(is_class<int&>::value);
     64   EXPECT_FALSE(is_class<char[3]>::value);
     65 }
     66 
     67 }  // namespace
     68 }  // namespace base
     69