gtest-tuple_test.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 #include "gtest/internal/gtest-tuple.h"
00033 #include <utility>
00034 #include "gtest/gtest.h"
00035 
00036 namespace {
00037 
00038 using ::std::tr1::get;
00039 using ::std::tr1::make_tuple;
00040 using ::std::tr1::tuple;
00041 using ::std::tr1::tuple_element;
00042 using ::std::tr1::tuple_size;
00043 using ::testing::StaticAssertTypeEq;
00044 
00045 // Tests that tuple_element<K, tuple<T0, T1, ..., TN> >::type returns TK.
00046 TEST(tuple_element_Test, ReturnsElementType) {
00047   StaticAssertTypeEq<int, tuple_element<0, tuple<int, char> >::type>();
00048   StaticAssertTypeEq<int&, tuple_element<1, tuple<double, int&> >::type>();
00049   StaticAssertTypeEq<bool, tuple_element<2, tuple<double, int, bool> >::type>();
00050 }
00051 
00052 // Tests that tuple_size<T>::value gives the number of fields in tuple
00053 // type T.
00054 TEST(tuple_size_Test, ReturnsNumberOfFields) {
00055   EXPECT_EQ(0, +tuple_size<tuple<> >::value);
00056   EXPECT_EQ(1, +tuple_size<tuple<void*> >::value);
00057   EXPECT_EQ(1, +tuple_size<tuple<char> >::value);
00058   EXPECT_EQ(1, +(tuple_size<tuple<tuple<int, double> > >::value));
00059   EXPECT_EQ(2, +(tuple_size<tuple<int&, const char> >::value));
00060   EXPECT_EQ(3, +(tuple_size<tuple<char*, void, const bool&> >::value));
00061 }
00062 
00063 // Tests comparing a tuple with itself.
00064 TEST(ComparisonTest, ComparesWithSelf) {
00065   const tuple<int, char, bool> a(5, 'a', false);
00066 
00067   EXPECT_TRUE(a == a);
00068   EXPECT_FALSE(a != a);
00069 }
00070 
00071 // Tests comparing two tuples with the same value.
00072 TEST(ComparisonTest, ComparesEqualTuples) {
00073   const tuple<int, bool> a(5, true), b(5, true);
00074 
00075   EXPECT_TRUE(a == b);
00076   EXPECT_FALSE(a != b);
00077 }
00078 
00079 // Tests comparing two different tuples that have no reference fields.
00080 TEST(ComparisonTest, ComparesUnequalTuplesWithoutReferenceFields) {
00081   typedef tuple<const int, char> FooTuple;
00082 
00083   const FooTuple a(0, 'x');
00084   const FooTuple b(1, 'a');
00085 
00086   EXPECT_TRUE(a != b);
00087   EXPECT_FALSE(a == b);
00088 
00089   const FooTuple c(1, 'b');
00090 
00091   EXPECT_TRUE(b != c);
00092   EXPECT_FALSE(b == c);
00093 }
00094 
00095 // Tests comparing two different tuples that have reference fields.
00096 TEST(ComparisonTest, ComparesUnequalTuplesWithReferenceFields) {
00097   typedef tuple<int&, const char&> FooTuple;
00098 
00099   int i = 5;
00100   const char ch = 'a';
00101   const FooTuple a(i, ch);
00102 
00103   int j = 6;
00104   const FooTuple b(j, ch);
00105 
00106   EXPECT_TRUE(a != b);
00107   EXPECT_FALSE(a == b);
00108 
00109   j = 5;
00110   const char ch2 = 'b';
00111   const FooTuple c(j, ch2);
00112 
00113   EXPECT_TRUE(b != c);
00114   EXPECT_FALSE(b == c);
00115 }
00116 
00117 // Tests that a tuple field with a reference type is an alias of the
00118 // variable it's supposed to reference.
00119 TEST(ReferenceFieldTest, IsAliasOfReferencedVariable) {
00120   int n = 0;
00121   tuple<bool, int&> t(true, n);
00122 
00123   n = 1;
00124   EXPECT_EQ(n, get<1>(t))
00125       << "Changing a underlying variable should update the reference field.";
00126 
00127   // Makes sure that the implementation doesn't do anything funny with
00128   // the & operator for the return type of get<>().
00129   EXPECT_EQ(&n, &(get<1>(t)))
00130       << "The address of a reference field should equal the address of "
00131       << "the underlying variable.";
00132 
00133   get<1>(t) = 2;
00134   EXPECT_EQ(2, n)
00135       << "Changing a reference field should update the underlying variable.";
00136 }
00137 
00138 // Tests that tuple's default constructor default initializes each field.
00139 // This test needs to compile without generating warnings.
00140 TEST(TupleConstructorTest, DefaultConstructorDefaultInitializesEachField) {
00141   // The TR1 report requires that tuple's default constructor default
00142   // initializes each field, even if it's a primitive type.  If the
00143   // implementation forgets to do this, this test will catch it by
00144   // generating warnings about using uninitialized variables (assuming
00145   // a decent compiler).
00146 
00147   tuple<> empty;
00148 
00149   tuple<int> a1, b1;
00150   b1 = a1;
00151   EXPECT_EQ(0, get<0>(b1));
00152 
00153   tuple<int, double> a2, b2;
00154   b2 = a2;
00155   EXPECT_EQ(0, get<0>(b2));
00156   EXPECT_EQ(0.0, get<1>(b2));
00157 
00158   tuple<double, char, bool*> a3, b3;
00159   b3 = a3;
00160   EXPECT_EQ(0.0, get<0>(b3));
00161   EXPECT_EQ('\0', get<1>(b3));
00162   EXPECT_TRUE(get<2>(b3) == NULL);
00163 
00164   tuple<int, int, int, int, int, int, int, int, int, int> a10, b10;
00165   b10 = a10;
00166   EXPECT_EQ(0, get<0>(b10));
00167   EXPECT_EQ(0, get<1>(b10));
00168   EXPECT_EQ(0, get<2>(b10));
00169   EXPECT_EQ(0, get<3>(b10));
00170   EXPECT_EQ(0, get<4>(b10));
00171   EXPECT_EQ(0, get<5>(b10));
00172   EXPECT_EQ(0, get<6>(b10));
00173   EXPECT_EQ(0, get<7>(b10));
00174   EXPECT_EQ(0, get<8>(b10));
00175   EXPECT_EQ(0, get<9>(b10));
00176 }
00177 
00178 // Tests constructing a tuple from its fields.
00179 TEST(TupleConstructorTest, ConstructsFromFields) {
00180   int n = 1;
00181   // Reference field.
00182   tuple<int&> a(n);
00183   EXPECT_EQ(&n, &(get<0>(a)));
00184 
00185   // Non-reference fields.
00186   tuple<int, char> b(5, 'a');
00187   EXPECT_EQ(5, get<0>(b));
00188   EXPECT_EQ('a', get<1>(b));
00189 
00190   // Const reference field.
00191   const int m = 2;
00192   tuple<bool, const int&> c(true, m);
00193   EXPECT_TRUE(get<0>(c));
00194   EXPECT_EQ(&m, &(get<1>(c)));
00195 }
00196 
00197 // Tests tuple's copy constructor.
00198 TEST(TupleConstructorTest, CopyConstructor) {
00199   tuple<double, bool> a(0.0, true);
00200   tuple<double, bool> b(a);
00201 
00202   EXPECT_DOUBLE_EQ(0.0, get<0>(b));
00203   EXPECT_TRUE(get<1>(b));
00204 }
00205 
00206 // Tests constructing a tuple from another tuple that has a compatible
00207 // but different type.
00208 TEST(TupleConstructorTest, ConstructsFromDifferentTupleType) {
00209   tuple<int, int, char> a(0, 1, 'a');
00210   tuple<double, long, int> b(a);
00211 
00212   EXPECT_DOUBLE_EQ(0.0, get<0>(b));
00213   EXPECT_EQ(1, get<1>(b));
00214   EXPECT_EQ('a', get<2>(b));
00215 }
00216 
00217 // Tests constructing a 2-tuple from an std::pair.
00218 TEST(TupleConstructorTest, ConstructsFromPair) {
00219   ::std::pair<int, char> a(1, 'a');
00220   tuple<int, char> b(a);
00221   tuple<int, const char&> c(a);
00222 }
00223 
00224 // Tests assigning a tuple to another tuple with the same type.
00225 TEST(TupleAssignmentTest, AssignsToSameTupleType) {
00226   const tuple<int, long> a(5, 7L);
00227   tuple<int, long> b;
00228   b = a;
00229   EXPECT_EQ(5, get<0>(b));
00230   EXPECT_EQ(7L, get<1>(b));
00231 }
00232 
00233 // Tests assigning a tuple to another tuple with a different but
00234 // compatible type.
00235 TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
00236   const tuple<int, long, bool> a(1, 7L, true);
00237   tuple<long, int, bool> b;
00238   b = a;
00239   EXPECT_EQ(1L, get<0>(b));
00240   EXPECT_EQ(7, get<1>(b));
00241   EXPECT_TRUE(get<2>(b));
00242 }
00243 
00244 // Tests assigning an std::pair to a 2-tuple.
00245 TEST(TupleAssignmentTest, AssignsFromPair) {
00246   const ::std::pair<int, bool> a(5, true);
00247   tuple<int, bool> b;
00248   b = a;
00249   EXPECT_EQ(5, get<0>(b));
00250   EXPECT_TRUE(get<1>(b));
00251 
00252   tuple<long, bool> c;
00253   c = a;
00254   EXPECT_EQ(5L, get<0>(c));
00255   EXPECT_TRUE(get<1>(c));
00256 }
00257 
00258 // A fixture for testing big tuples.
00259 class BigTupleTest : public testing::Test {
00260  protected:
00261   typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
00262 
00263   BigTupleTest() :
00264       a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
00265       b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
00266 
00267   BigTuple a_, b_;
00268 };
00269 
00270 // Tests constructing big tuples.
00271 TEST_F(BigTupleTest, Construction) {
00272   BigTuple a;
00273   BigTuple b(b_);
00274 }
00275 
00276 // Tests that get<N>(t) returns the N-th (0-based) field of tuple t.
00277 TEST_F(BigTupleTest, get) {
00278   EXPECT_EQ(1, get<0>(a_));
00279   EXPECT_EQ(2, get<9>(a_));
00280 
00281   // Tests that get() works on a const tuple too.
00282   const BigTuple a(a_);
00283   EXPECT_EQ(1, get<0>(a));
00284   EXPECT_EQ(2, get<9>(a));
00285 }
00286 
00287 // Tests comparing big tuples.
00288 TEST_F(BigTupleTest, Comparisons) {
00289   EXPECT_TRUE(a_ == a_);
00290   EXPECT_FALSE(a_ != a_);
00291 
00292   EXPECT_TRUE(a_ != b_);
00293   EXPECT_FALSE(a_ == b_);
00294 }
00295 
00296 TEST(MakeTupleTest, WorksForScalarTypes) {
00297   tuple<bool, int> a;
00298   a = make_tuple(true, 5);
00299   EXPECT_TRUE(get<0>(a));
00300   EXPECT_EQ(5, get<1>(a));
00301 
00302   tuple<char, int, long> b;
00303   b = make_tuple('a', 'b', 5);
00304   EXPECT_EQ('a', get<0>(b));
00305   EXPECT_EQ('b', get<1>(b));
00306   EXPECT_EQ(5, get<2>(b));
00307 }
00308 
00309 TEST(MakeTupleTest, WorksForPointers) {
00310   int a[] = { 1, 2, 3, 4 };
00311   const char* const str = "hi";
00312   int* const p = a;
00313 
00314   tuple<const char*, int*> t;
00315   t = make_tuple(str, p);
00316   EXPECT_EQ(str, get<0>(t));
00317   EXPECT_EQ(p, get<1>(t));
00318 }
00319 
00320 }  // namespace


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:03