00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
00053
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
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
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
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
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
00118
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
00128
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
00139
00140 TEST(TupleConstructorTest, DefaultConstructorDefaultInitializesEachField) {
00141
00142
00143
00144
00145
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
00179 TEST(TupleConstructorTest, ConstructsFromFields) {
00180 int n = 1;
00181
00182 tuple<int&> a(n);
00183 EXPECT_EQ(&n, &(get<0>(a)));
00184
00185
00186 tuple<int, char> b(5, 'a');
00187 EXPECT_EQ(5, get<0>(b));
00188 EXPECT_EQ('a', get<1>(b));
00189
00190
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
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
00207
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
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
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
00234
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
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
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
00271 TEST_F(BigTupleTest, Construction) {
00272 BigTuple a;
00273 BigTuple b(b_);
00274 }
00275
00276
00277 TEST_F(BigTupleTest, get) {
00278 EXPECT_EQ(1, get<0>(a_));
00279 EXPECT_EQ(2, get<9>(a_));
00280
00281
00282 const BigTuple a(a_);
00283 EXPECT_EQ(1, get<0>(a));
00284 EXPECT_EQ(2, get<9>(a));
00285 }
00286
00287
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 }