Home | History | Annotate | Download | only in test
      1 // Copyright 2007, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 //
     30 // Author: wan (at) google.com (Zhanyong Wan)
     31 
     32 #include <gtest/internal/gtest-tuple.h>
     33 #include <utility>
     34 #include <gtest/gtest.h>
     35 
     36 namespace {
     37 
     38 using ::std::tr1::get;
     39 using ::std::tr1::make_tuple;
     40 using ::std::tr1::tuple;
     41 using ::std::tr1::tuple_element;
     42 using ::std::tr1::tuple_size;
     43 using ::testing::StaticAssertTypeEq;
     44 
     45 // Tests that tuple_element<K, tuple<T0, T1, ..., TN> >::type returns TK.
     46 TEST(tuple_element_Test, ReturnsElementType) {
     47   StaticAssertTypeEq<int, tuple_element<0, tuple<int, char> >::type>();
     48   StaticAssertTypeEq<int&, tuple_element<1, tuple<double, int&> >::type>();
     49   StaticAssertTypeEq<bool, tuple_element<2, tuple<double, int, bool> >::type>();
     50 }
     51 
     52 // Tests that tuple_size<T>::value gives the number of fields in tuple
     53 // type T.
     54 TEST(tuple_size_Test, ReturnsNumberOfFields) {
     55   EXPECT_EQ(0, +tuple_size<tuple<> >::value);
     56   EXPECT_EQ(1, +tuple_size<tuple<void*> >::value);
     57   EXPECT_EQ(1, +tuple_size<tuple<char> >::value);
     58   EXPECT_EQ(1, +(tuple_size<tuple<tuple<int, double> > >::value));
     59   EXPECT_EQ(2, +(tuple_size<tuple<int&, const char> >::value));
     60   EXPECT_EQ(3, +(tuple_size<tuple<char*, void, const bool&> >::value));
     61 }
     62 
     63 // Tests comparing a tuple with itself.
     64 TEST(ComparisonTest, ComparesWithSelf) {
     65   const tuple<int, char, bool> a(5, 'a', false);
     66 
     67   EXPECT_TRUE(a == a);
     68   EXPECT_FALSE(a != a);
     69 }
     70 
     71 // Tests comparing two tuples with the same value.
     72 TEST(ComparisonTest, ComparesEqualTuples) {
     73   const tuple<int, bool> a(5, true), b(5, true);
     74 
     75   EXPECT_TRUE(a == b);
     76   EXPECT_FALSE(a != b);
     77 }
     78 
     79 // Tests comparing two different tuples that have no reference fields.
     80 TEST(ComparisonTest, ComparesUnequalTuplesWithoutReferenceFields) {
     81   typedef tuple<const int, char> FooTuple;
     82 
     83   const FooTuple a(0, 'x');
     84   const FooTuple b(1, 'a');
     85 
     86   EXPECT_TRUE(a != b);
     87   EXPECT_FALSE(a == b);
     88 
     89   const FooTuple c(1, 'b');
     90 
     91   EXPECT_TRUE(b != c);
     92   EXPECT_FALSE(b == c);
     93 }
     94 
     95 // Tests comparing two different tuples that have reference fields.
     96 TEST(ComparisonTest, ComparesUnequalTuplesWithReferenceFields) {
     97   typedef tuple<int&, const char&> FooTuple;
     98 
     99   int i = 5;
    100   const char ch = 'a';
    101   const FooTuple a(i, ch);
    102 
    103   int j = 6;
    104   const FooTuple b(j, ch);
    105 
    106   EXPECT_TRUE(a != b);
    107   EXPECT_FALSE(a == b);
    108 
    109   j = 5;
    110   const char ch2 = 'b';
    111   const FooTuple c(j, ch2);
    112 
    113   EXPECT_TRUE(b != c);
    114   EXPECT_FALSE(b == c);
    115 }
    116 
    117 // Tests that a tuple field with a reference type is an alias of the
    118 // variable it's supposed to reference.
    119 TEST(ReferenceFieldTest, IsAliasOfReferencedVariable) {
    120   int n = 0;
    121   tuple<bool, int&> t(true, n);
    122 
    123   n = 1;
    124   EXPECT_EQ(n, get<1>(t))
    125       << "Changing a underlying variable should update the reference field.";
    126 
    127   // Makes sure that the implementation doesn't do anything funny with
    128   // the & operator for the return type of get<>().
    129   EXPECT_EQ(&n, &(get<1>(t)))
    130       << "The address of a reference field should equal the address of "
    131       << "the underlying variable.";
    132 
    133   get<1>(t) = 2;
    134   EXPECT_EQ(2, n)
    135       << "Changing a reference field should update the underlying variable.";
    136 }
    137 
    138 // Tests tuple's default constructor.
    139 TEST(TupleConstructorTest, DefaultConstructor) {
    140   // We are just testing that the following compiles.
    141   tuple<> empty;
    142   tuple<int> one_field;
    143   tuple<double, char, bool*> three_fields;
    144 }
    145 
    146 // Tests constructing a tuple from its fields.
    147 TEST(TupleConstructorTest, ConstructsFromFields) {
    148   int n = 1;
    149   // Reference field.
    150   tuple<int&> a(n);
    151   EXPECT_EQ(&n, &(get<0>(a)));
    152 
    153   // Non-reference fields.
    154   tuple<int, char> b(5, 'a');
    155   EXPECT_EQ(5, get<0>(b));
    156   EXPECT_EQ('a', get<1>(b));
    157 
    158   // Const reference field.
    159   const int m = 2;
    160   tuple<bool, const int&> c(true, m);
    161   EXPECT_TRUE(get<0>(c));
    162   EXPECT_EQ(&m, &(get<1>(c)));
    163 }
    164 
    165 // Tests tuple's copy constructor.
    166 TEST(TupleConstructorTest, CopyConstructor) {
    167   tuple<double, bool> a(0.0, true);
    168   tuple<double, bool> b(a);
    169 
    170   EXPECT_DOUBLE_EQ(0.0, get<0>(b));
    171   EXPECT_TRUE(get<1>(b));
    172 }
    173 
    174 // Tests constructing a tuple from another tuple that has a compatible
    175 // but different type.
    176 TEST(TupleConstructorTest, ConstructsFromDifferentTupleType) {
    177   tuple<int, int, char> a(0, 1, 'a');
    178   tuple<double, long, int> b(a);
    179 
    180   EXPECT_DOUBLE_EQ(0.0, get<0>(b));
    181   EXPECT_EQ(1, get<1>(b));
    182   EXPECT_EQ('a', get<2>(b));
    183 }
    184 
    185 // Tests constructing a 2-tuple from an std::pair.
    186 TEST(TupleConstructorTest, ConstructsFromPair) {
    187   ::std::pair<int, char> a(1, 'a');
    188   tuple<int, char> b(a);
    189   tuple<int, const char&> c(a);
    190 }
    191 
    192 // Tests assigning a tuple to another tuple with the same type.
    193 TEST(TupleAssignmentTest, AssignsToSameTupleType) {
    194   const tuple<int, long> a(5, 7L);
    195   tuple<int, long> b;
    196   b = a;
    197   EXPECT_EQ(5, get<0>(b));
    198   EXPECT_EQ(7L, get<1>(b));
    199 }
    200 
    201 // Tests assigning a tuple to another tuple with a different but
    202 // compatible type.
    203 TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
    204   const tuple<int, long, bool> a(1, 7L, true);
    205   tuple<long, int, bool> b;
    206   b = a;
    207   EXPECT_EQ(1L, get<0>(b));
    208   EXPECT_EQ(7, get<1>(b));
    209   EXPECT_TRUE(get<2>(b));
    210 }
    211 
    212 // Tests assigning an std::pair to a 2-tuple.
    213 TEST(TupleAssignmentTest, AssignsFromPair) {
    214   const ::std::pair<int, bool> a(5, true);
    215   tuple<int, bool> b;
    216   b = a;
    217   EXPECT_EQ(5, get<0>(b));
    218   EXPECT_TRUE(get<1>(b));
    219 
    220   tuple<long, bool> c;
    221   c = a;
    222   EXPECT_EQ(5L, get<0>(c));
    223   EXPECT_TRUE(get<1>(c));
    224 }
    225 
    226 // A fixture for testing big tuples.
    227 class BigTupleTest : public testing::Test {
    228  protected:
    229   typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
    230 
    231   BigTupleTest() :
    232       a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
    233       b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
    234 
    235   BigTuple a_, b_;
    236 };
    237 
    238 // Tests constructing big tuples.
    239 TEST_F(BigTupleTest, Construction) {
    240   BigTuple a;
    241   BigTuple b(b_);
    242 }
    243 
    244 // Tests that get<N>(t) returns the N-th (0-based) field of tuple t.
    245 TEST_F(BigTupleTest, get) {
    246   EXPECT_EQ(1, get<0>(a_));
    247   EXPECT_EQ(2, get<9>(a_));
    248 
    249   // Tests that get() works on a const tuple too.
    250   const BigTuple a(a_);
    251   EXPECT_EQ(1, get<0>(a));
    252   EXPECT_EQ(2, get<9>(a));
    253 }
    254 
    255 // Tests comparing big tuples.
    256 TEST_F(BigTupleTest, Comparisons) {
    257   EXPECT_TRUE(a_ == a_);
    258   EXPECT_FALSE(a_ != a_);
    259 
    260   EXPECT_TRUE(a_ != b_);
    261   EXPECT_FALSE(a_ == b_);
    262 }
    263 
    264 TEST(MakeTupleTest, WorksForScalarTypes) {
    265   tuple<bool, int> a;
    266   a = make_tuple(true, 5);
    267   EXPECT_TRUE(get<0>(a));
    268   EXPECT_EQ(5, get<1>(a));
    269 
    270   tuple<char, int, long> b;
    271   b = make_tuple('a', 'b', 5);
    272   EXPECT_EQ('a', get<0>(b));
    273   EXPECT_EQ('b', get<1>(b));
    274   EXPECT_EQ(5, get<2>(b));
    275 }
    276 
    277 TEST(MakeTupleTest, WorksForPointers) {
    278   int a[] = { 1, 2, 3, 4 };
    279   const char* const str = "hi";
    280   int* const p = a;
    281 
    282   tuple<const char*, int*> t;
    283   t = make_tuple(str, p);
    284   EXPECT_EQ(str, get<0>(t));
    285   EXPECT_EQ(p, get<1>(t));
    286 }
    287 
    288 }  // namespace
    289