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.
30 // Author: wan@google.com (Zhanyong Wan)
32 #include "gtest/internal/gtest-tuple.h"
34 #include "gtest/gtest.h"
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;
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>();
52 // Tests that tuple_size<T>::value gives the number of fields in tuple
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));
63 // Tests comparing a tuple with itself.
64 TEST(ComparisonTest, ComparesWithSelf) {
65 const tuple<int, char, bool> a(5, 'a', false);
71 // Tests comparing two tuples with the same value.
72 TEST(ComparisonTest, ComparesEqualTuples) {
73 const tuple<int, bool> a(5, true), b(5, true);
79 // Tests comparing two different tuples that have no reference fields.
80 TEST(ComparisonTest, ComparesUnequalTuplesWithoutReferenceFields) {
81 typedef tuple<const int, char> FooTuple;
83 const FooTuple a(0, 'x');
84 const FooTuple b(1, 'a');
89 const FooTuple c(1, 'b');
95 // Tests comparing two different tuples that have reference fields.
96 TEST(ComparisonTest, ComparesUnequalTuplesWithReferenceFields) {
97 typedef tuple<int&, const char&> FooTuple;
101 const FooTuple a(i, ch);
104 const FooTuple b(j, ch);
107 EXPECT_FALSE(a == b);
110 const char ch2 = 'b';
111 const FooTuple c(j, ch2);
114 EXPECT_FALSE(b == c);
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) {
121 tuple<bool, int&> t(true, n);
124 EXPECT_EQ(n, get<1>(t))
125 << "Changing a underlying variable should update the reference field.";
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.";
135 << "Changing a reference field should update the underlying variable.";
138 // Tests that tuple's default constructor default initializes each field.
139 // This test needs to compile without generating warnings.
140 TEST(TupleConstructorTest, DefaultConstructorDefaultInitializesEachField) {
141 // The TR1 report requires that tuple's default constructor default
142 // initializes each field, even if it's a primitive type. If the
143 // implementation forgets to do this, this test will catch it by
144 // generating warnings about using uninitialized variables (assuming
145 // a decent compiler).
151 EXPECT_EQ(0, get<0>(b1));
153 tuple<int, double> a2, b2;
155 EXPECT_EQ(0, get<0>(b2));
156 EXPECT_EQ(0.0, get<1>(b2));
158 tuple<double, char, bool*> a3, b3;
160 EXPECT_EQ(0.0, get<0>(b3));
161 EXPECT_EQ('\0', get<1>(b3));
162 EXPECT_TRUE(get<2>(b3) == NULL);
164 tuple<int, int, int, int, int, int, int, int, int, int> a10, b10;
166 EXPECT_EQ(0, get<0>(b10));
167 EXPECT_EQ(0, get<1>(b10));
168 EXPECT_EQ(0, get<2>(b10));
169 EXPECT_EQ(0, get<3>(b10));
170 EXPECT_EQ(0, get<4>(b10));
171 EXPECT_EQ(0, get<5>(b10));
172 EXPECT_EQ(0, get<6>(b10));
173 EXPECT_EQ(0, get<7>(b10));
174 EXPECT_EQ(0, get<8>(b10));
175 EXPECT_EQ(0, get<9>(b10));
178 // Tests constructing a tuple from its fields.
179 TEST(TupleConstructorTest, ConstructsFromFields) {
183 EXPECT_EQ(&n, &(get<0>(a)));
185 // Non-reference fields.
186 tuple<int, char> b(5, 'a');
187 EXPECT_EQ(5, get<0>(b));
188 EXPECT_EQ('a', get<1>(b));
190 // Const reference field.
192 tuple<bool, const int&> c(true, m);
193 EXPECT_TRUE(get<0>(c));
194 EXPECT_EQ(&m, &(get<1>(c)));
197 // Tests tuple's copy constructor.
198 TEST(TupleConstructorTest, CopyConstructor) {
199 tuple<double, bool> a(0.0, true);
200 tuple<double, bool> b(a);
202 EXPECT_DOUBLE_EQ(0.0, get<0>(b));
203 EXPECT_TRUE(get<1>(b));
206 // Tests constructing a tuple from another tuple that has a compatible
207 // but different type.
208 TEST(TupleConstructorTest, ConstructsFromDifferentTupleType) {
209 tuple<int, int, char> a(0, 1, 'a');
210 tuple<double, long, int> b(a);
212 EXPECT_DOUBLE_EQ(0.0, get<0>(b));
213 EXPECT_EQ(1, get<1>(b));
214 EXPECT_EQ('a', get<2>(b));
217 // Tests constructing a 2-tuple from an std::pair.
218 TEST(TupleConstructorTest, ConstructsFromPair) {
219 ::std::pair<int, char> a(1, 'a');
220 tuple<int, char> b(a);
221 tuple<int, const char&> c(a);
224 // Tests assigning a tuple to another tuple with the same type.
225 TEST(TupleAssignmentTest, AssignsToSameTupleType) {
226 const tuple<int, long> a(5, 7L);
229 EXPECT_EQ(5, get<0>(b));
230 EXPECT_EQ(7L, get<1>(b));
233 // Tests assigning a tuple to another tuple with a different but
235 TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
236 const tuple<int, long, bool> a(1, 7L, true);
237 tuple<long, int, bool> b;
239 EXPECT_EQ(1L, get<0>(b));
240 EXPECT_EQ(7, get<1>(b));
241 EXPECT_TRUE(get<2>(b));
244 // Tests assigning an std::pair to a 2-tuple.
245 TEST(TupleAssignmentTest, AssignsFromPair) {
246 const ::std::pair<int, bool> a(5, true);
249 EXPECT_EQ(5, get<0>(b));
250 EXPECT_TRUE(get<1>(b));
254 EXPECT_EQ(5L, get<0>(c));
255 EXPECT_TRUE(get<1>(c));
258 // A fixture for testing big tuples.
259 class BigTupleTest : public testing::Test {
261 typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
264 a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
265 b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
270 // Tests constructing big tuples.
271 TEST_F(BigTupleTest, Construction) {
276 // Tests that get<N>(t) returns the N-th (0-based) field of tuple t.
277 TEST_F(BigTupleTest, get) {
278 EXPECT_EQ(1, get<0>(a_));
279 EXPECT_EQ(2, get<9>(a_));
281 // Tests that get() works on a const tuple too.
282 const BigTuple a(a_);
283 EXPECT_EQ(1, get<0>(a));
284 EXPECT_EQ(2, get<9>(a));
287 // Tests comparing big tuples.
288 TEST_F(BigTupleTest, Comparisons) {
289 EXPECT_TRUE(a_ == a_);
290 EXPECT_FALSE(a_ != a_);
292 EXPECT_TRUE(a_ != b_);
293 EXPECT_FALSE(a_ == b_);
296 TEST(MakeTupleTest, WorksForScalarTypes) {
298 a = make_tuple(true, 5);
299 EXPECT_TRUE(get<0>(a));
300 EXPECT_EQ(5, get<1>(a));
302 tuple<char, int, long> b;
303 b = make_tuple('a', 'b', 5);
304 EXPECT_EQ('a', get<0>(b));
305 EXPECT_EQ('b', get<1>(b));
306 EXPECT_EQ(5, get<2>(b));
309 TEST(MakeTupleTest, WorksForPointers) {
310 int a[] = { 1, 2, 3, 4 };
311 const char* const str = "hi";
314 tuple<const char*, int*> t;
315 t = make_tuple(str, p);
316 EXPECT_EQ(str, get<0>(t));
317 EXPECT_EQ(p, get<1>(t));