1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #include "gtest/internal/gtest-tuple.h"
33 #include "gtest/gtest.h"
37 using ::std::tr1::get;
38 using ::std::tr1::make_tuple;
39 using ::std::tr1::tuple;
40 using ::std::tr1::tuple_element;
41 using ::std::tr1::tuple_size;
42 using ::testing::StaticAssertTypeEq;
44 // Tests that tuple_element<K, tuple<T0, T1, ..., TN> >::type returns TK.
45 TEST(tuple_element_Test, ReturnsElementType) {
46 StaticAssertTypeEq<int, tuple_element<0, tuple<int, char> >::type>();
47 StaticAssertTypeEq<int&, tuple_element<1, tuple<double, int&> >::type>();
48 StaticAssertTypeEq<bool, tuple_element<2, tuple<double, int, bool> >::type>();
51 // Tests that tuple_size<T>::value gives the number of fields in tuple
53 TEST(tuple_size_Test, ReturnsNumberOfFields) {
54 EXPECT_EQ(0, +tuple_size<tuple<> >::value);
55 EXPECT_EQ(1, +tuple_size<tuple<void*> >::value);
56 EXPECT_EQ(1, +tuple_size<tuple<char> >::value);
57 EXPECT_EQ(1, +(tuple_size<tuple<tuple<int, double> > >::value));
58 EXPECT_EQ(2, +(tuple_size<tuple<int&, const char> >::value));
59 EXPECT_EQ(3, +(tuple_size<tuple<char*, void, const bool&> >::value));
62 // Tests comparing a tuple with itself.
63 TEST(ComparisonTest, ComparesWithSelf) {
64 const tuple<int, char, bool> a(5, 'a', false);
70 // Tests comparing two tuples with the same value.
71 TEST(ComparisonTest, ComparesEqualTuples) {
72 const tuple<int, bool> a(5, true), b(5, true);
78 // Tests comparing two different tuples that have no reference fields.
79 TEST(ComparisonTest, ComparesUnequalTuplesWithoutReferenceFields) {
80 typedef tuple<const int, char> FooTuple;
82 const FooTuple a(0, 'x');
83 const FooTuple b(1, 'a');
88 const FooTuple c(1, 'b');
94 // Tests comparing two different tuples that have reference fields.
95 TEST(ComparisonTest, ComparesUnequalTuplesWithReferenceFields) {
96 typedef tuple<int&, const char&> FooTuple;
100 const FooTuple a(i, ch);
103 const FooTuple b(j, ch);
106 EXPECT_FALSE(a == b);
109 const char ch2 = 'b';
110 const FooTuple c(j, ch2);
113 EXPECT_FALSE(b == c);
116 // Tests that a tuple field with a reference type is an alias of the
117 // variable it's supposed to reference.
118 TEST(ReferenceFieldTest, IsAliasOfReferencedVariable) {
120 tuple<bool, int&> t(true, n);
123 EXPECT_EQ(n, get<1>(t))
124 << "Changing a underlying variable should update the reference field.";
126 // Makes sure that the implementation doesn't do anything funny with
127 // the & operator for the return type of get<>().
128 EXPECT_EQ(&n, &(get<1>(t)))
129 << "The address of a reference field should equal the address of "
130 << "the underlying variable.";
134 << "Changing a reference field should update the underlying variable.";
137 // Tests that tuple's default constructor default initializes each field.
138 // This test needs to compile without generating warnings.
139 TEST(TupleConstructorTest, DefaultConstructorDefaultInitializesEachField) {
140 // The TR1 report requires that tuple's default constructor default
141 // initializes each field, even if it's a primitive type. If the
142 // implementation forgets to do this, this test will catch it by
143 // generating warnings about using uninitialized variables (assuming
144 // a decent compiler).
150 EXPECT_EQ(0, get<0>(b1));
152 tuple<int, double> a2, b2;
154 EXPECT_EQ(0, get<0>(b2));
155 EXPECT_EQ(0.0, get<1>(b2));
157 tuple<double, char, bool*> a3, b3;
159 EXPECT_EQ(0.0, get<0>(b3));
160 EXPECT_EQ('\0', get<1>(b3));
161 EXPECT_TRUE(get<2>(b3) == NULL);
163 tuple<int, int, int, int, int, int, int, int, int, int> a10, b10;
165 EXPECT_EQ(0, get<0>(b10));
166 EXPECT_EQ(0, get<1>(b10));
167 EXPECT_EQ(0, get<2>(b10));
168 EXPECT_EQ(0, get<3>(b10));
169 EXPECT_EQ(0, get<4>(b10));
170 EXPECT_EQ(0, get<5>(b10));
171 EXPECT_EQ(0, get<6>(b10));
172 EXPECT_EQ(0, get<7>(b10));
173 EXPECT_EQ(0, get<8>(b10));
174 EXPECT_EQ(0, get<9>(b10));
177 // Tests constructing a tuple from its fields.
178 TEST(TupleConstructorTest, ConstructsFromFields) {
182 EXPECT_EQ(&n, &(get<0>(a)));
184 // Non-reference fields.
185 tuple<int, char> b(5, 'a');
186 EXPECT_EQ(5, get<0>(b));
187 EXPECT_EQ('a', get<1>(b));
189 // Const reference field.
191 tuple<bool, const int&> c(true, m);
192 EXPECT_TRUE(get<0>(c));
193 EXPECT_EQ(&m, &(get<1>(c)));
196 // Tests tuple's copy constructor.
197 TEST(TupleConstructorTest, CopyConstructor) {
198 tuple<double, bool> a(0.0, true);
199 tuple<double, bool> b(a);
201 EXPECT_DOUBLE_EQ(0.0, get<0>(b));
202 EXPECT_TRUE(get<1>(b));
205 // Tests constructing a tuple from another tuple that has a compatible
206 // but different type.
207 TEST(TupleConstructorTest, ConstructsFromDifferentTupleType) {
208 tuple<int, int, char> a(0, 1, 'a');
209 tuple<double, long, int> b(a);
211 EXPECT_DOUBLE_EQ(0.0, get<0>(b));
212 EXPECT_EQ(1, get<1>(b));
213 EXPECT_EQ('a', get<2>(b));
216 // Tests constructing a 2-tuple from an std::pair.
217 TEST(TupleConstructorTest, ConstructsFromPair) {
218 ::std::pair<int, char> a(1, 'a');
219 tuple<int, char> b(a);
220 tuple<int, const char&> c(a);
223 // Tests assigning a tuple to another tuple with the same type.
224 TEST(TupleAssignmentTest, AssignsToSameTupleType) {
225 const tuple<int, long> a(5, 7L);
228 EXPECT_EQ(5, get<0>(b));
229 EXPECT_EQ(7L, get<1>(b));
232 // Tests assigning a tuple to another tuple with a different but
234 TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
235 const tuple<int, long, bool> a(1, 7L, true);
236 tuple<long, int, bool> b;
238 EXPECT_EQ(1L, get<0>(b));
239 EXPECT_EQ(7, get<1>(b));
240 EXPECT_TRUE(get<2>(b));
243 // Tests assigning an std::pair to a 2-tuple.
244 TEST(TupleAssignmentTest, AssignsFromPair) {
245 const ::std::pair<int, bool> a(5, true);
248 EXPECT_EQ(5, get<0>(b));
249 EXPECT_TRUE(get<1>(b));
253 EXPECT_EQ(5L, get<0>(c));
254 EXPECT_TRUE(get<1>(c));
257 // A fixture for testing big tuples.
258 class BigTupleTest : public testing::Test {
260 typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
263 a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
264 b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
269 // Tests constructing big tuples.
270 TEST_F(BigTupleTest, Construction) {
275 // Tests that get<N>(t) returns the N-th (0-based) field of tuple t.
276 TEST_F(BigTupleTest, get) {
277 EXPECT_EQ(1, get<0>(a_));
278 EXPECT_EQ(2, get<9>(a_));
280 // Tests that get() works on a const tuple too.
281 const BigTuple a(a_);
282 EXPECT_EQ(1, get<0>(a));
283 EXPECT_EQ(2, get<9>(a));
286 // Tests comparing big tuples.
287 TEST_F(BigTupleTest, Comparisons) {
288 EXPECT_TRUE(a_ == a_);
289 EXPECT_FALSE(a_ != a_);
291 EXPECT_TRUE(a_ != b_);
292 EXPECT_FALSE(a_ == b_);
295 TEST(MakeTupleTest, WorksForScalarTypes) {
297 a = make_tuple(true, 5);
298 EXPECT_TRUE(get<0>(a));
299 EXPECT_EQ(5, get<1>(a));
301 tuple<char, int, long> b;
302 b = make_tuple('a', 'b', 5);
303 EXPECT_EQ('a', get<0>(b));
304 EXPECT_EQ('b', get<1>(b));
305 EXPECT_EQ(5, get<2>(b));
308 TEST(MakeTupleTest, WorksForPointers) {
309 int a[] = { 1, 2, 3, 4 };
310 const char* const str = "hi";
313 tuple<const char*, int*> t;
314 t = make_tuple(str, p);
315 EXPECT_EQ(str, get<0>(t));
316 EXPECT_EQ(p, get<1>(t));