[12746] | 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@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 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).
|
---|
| 146 |
|
---|
| 147 | tuple<> empty;
|
---|
| 148 |
|
---|
| 149 | tuple<int> a1, b1;
|
---|
| 150 | b1 = a1;
|
---|
| 151 | EXPECT_EQ(0, get<0>(b1));
|
---|
| 152 |
|
---|
| 153 | tuple<int, double> a2, b2;
|
---|
| 154 | b2 = a2;
|
---|
| 155 | EXPECT_EQ(0, get<0>(b2));
|
---|
| 156 | EXPECT_EQ(0.0, get<1>(b2));
|
---|
| 157 |
|
---|
| 158 | tuple<double, char, bool*> a3, b3;
|
---|
| 159 | b3 = a3;
|
---|
| 160 | EXPECT_EQ(0.0, get<0>(b3));
|
---|
| 161 | EXPECT_EQ('\0', get<1>(b3));
|
---|
| 162 | EXPECT_TRUE(get<2>(b3) == NULL);
|
---|
| 163 |
|
---|
| 164 | tuple<int, int, int, int, int, int, int, int, int, int> a10, b10;
|
---|
| 165 | b10 = a10;
|
---|
| 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));
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | // Tests constructing a tuple from its fields.
|
---|
| 179 | TEST(TupleConstructorTest, ConstructsFromFields) {
|
---|
| 180 | int n = 1;
|
---|
| 181 | // Reference field.
|
---|
| 182 | tuple<int&> a(n);
|
---|
| 183 | EXPECT_EQ(&n, &(get<0>(a)));
|
---|
| 184 |
|
---|
| 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));
|
---|
| 189 |
|
---|
| 190 | // Const reference field.
|
---|
| 191 | const int m = 2;
|
---|
| 192 | tuple<bool, const int&> c(true, m);
|
---|
| 193 | EXPECT_TRUE(get<0>(c));
|
---|
| 194 | EXPECT_EQ(&m, &(get<1>(c)));
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | // Tests tuple's copy constructor.
|
---|
| 198 | TEST(TupleConstructorTest, CopyConstructor) {
|
---|
| 199 | tuple<double, bool> a(0.0, true);
|
---|
| 200 | tuple<double, bool> b(a);
|
---|
| 201 |
|
---|
| 202 | EXPECT_DOUBLE_EQ(0.0, get<0>(b));
|
---|
| 203 | EXPECT_TRUE(get<1>(b));
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 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);
|
---|
| 211 |
|
---|
| 212 | EXPECT_DOUBLE_EQ(0.0, get<0>(b));
|
---|
| 213 | EXPECT_EQ(1, get<1>(b));
|
---|
| 214 | EXPECT_EQ('a', get<2>(b));
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 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);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | // Tests assigning a tuple to another tuple with the same type.
|
---|
| 225 | TEST(TupleAssignmentTest, AssignsToSameTupleType) {
|
---|
| 226 | const tuple<int, long> a(5, 7L);
|
---|
| 227 | tuple<int, long> b;
|
---|
| 228 | b = a;
|
---|
| 229 | EXPECT_EQ(5, get<0>(b));
|
---|
| 230 | EXPECT_EQ(7L, get<1>(b));
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | // Tests assigning a tuple to another tuple with a different but
|
---|
| 234 | // compatible type.
|
---|
| 235 | TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
|
---|
| 236 | const tuple<int, long, bool> a(1, 7L, true);
|
---|
| 237 | tuple<long, int, bool> b;
|
---|
| 238 | b = a;
|
---|
| 239 | EXPECT_EQ(1L, get<0>(b));
|
---|
| 240 | EXPECT_EQ(7, get<1>(b));
|
---|
| 241 | EXPECT_TRUE(get<2>(b));
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | // Tests assigning an std::pair to a 2-tuple.
|
---|
| 245 | TEST(TupleAssignmentTest, AssignsFromPair) {
|
---|
| 246 | const ::std::pair<int, bool> a(5, true);
|
---|
| 247 | tuple<int, bool> b;
|
---|
| 248 | b = a;
|
---|
| 249 | EXPECT_EQ(5, get<0>(b));
|
---|
| 250 | EXPECT_TRUE(get<1>(b));
|
---|
| 251 |
|
---|
| 252 | tuple<long, bool> c;
|
---|
| 253 | c = a;
|
---|
| 254 | EXPECT_EQ(5L, get<0>(c));
|
---|
| 255 | EXPECT_TRUE(get<1>(c));
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | // A fixture for testing big tuples.
|
---|
| 259 | class BigTupleTest : public testing::Test {
|
---|
| 260 | protected:
|
---|
| 261 | typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
|
---|
| 262 |
|
---|
| 263 | BigTupleTest() :
|
---|
| 264 | a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
|
---|
| 265 | b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
|
---|
| 266 |
|
---|
| 267 | BigTuple a_, b_;
|
---|
| 268 | };
|
---|
| 269 |
|
---|
| 270 | // Tests constructing big tuples.
|
---|
| 271 | TEST_F(BigTupleTest, Construction) {
|
---|
| 272 | BigTuple a;
|
---|
| 273 | BigTuple b(b_);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 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_));
|
---|
| 280 |
|
---|
| 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));
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | // Tests comparing big tuples.
|
---|
| 288 | TEST_F(BigTupleTest, Comparisons) {
|
---|
| 289 | EXPECT_TRUE(a_ == a_);
|
---|
| 290 | EXPECT_FALSE(a_ != a_);
|
---|
| 291 |
|
---|
| 292 | EXPECT_TRUE(a_ != b_);
|
---|
| 293 | EXPECT_FALSE(a_ == b_);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | TEST(MakeTupleTest, WorksForScalarTypes) {
|
---|
| 297 | tuple<bool, int> a;
|
---|
| 298 | a = make_tuple(true, 5);
|
---|
| 299 | EXPECT_TRUE(get<0>(a));
|
---|
| 300 | EXPECT_EQ(5, get<1>(a));
|
---|
| 301 |
|
---|
| 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));
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | TEST(MakeTupleTest, WorksForPointers) {
|
---|
| 310 | int a[] = { 1, 2, 3, 4 };
|
---|
| 311 | const char* const str = "hi";
|
---|
| 312 | int* const p = a;
|
---|
| 313 |
|
---|
| 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));
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | } // namespace
|
---|