Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | // Copyright 2007, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Author: wan@google.com (Zhanyong Wan) |
michael@0 | 31 | |
michael@0 | 32 | #include "gtest/internal/gtest-tuple.h" |
michael@0 | 33 | #include <utility> |
michael@0 | 34 | #include "gtest/gtest.h" |
michael@0 | 35 | |
michael@0 | 36 | namespace { |
michael@0 | 37 | |
michael@0 | 38 | using ::std::tr1::get; |
michael@0 | 39 | using ::std::tr1::make_tuple; |
michael@0 | 40 | using ::std::tr1::tuple; |
michael@0 | 41 | using ::std::tr1::tuple_element; |
michael@0 | 42 | using ::std::tr1::tuple_size; |
michael@0 | 43 | using ::testing::StaticAssertTypeEq; |
michael@0 | 44 | |
michael@0 | 45 | // Tests that tuple_element<K, tuple<T0, T1, ..., TN> >::type returns TK. |
michael@0 | 46 | TEST(tuple_element_Test, ReturnsElementType) { |
michael@0 | 47 | StaticAssertTypeEq<int, tuple_element<0, tuple<int, char> >::type>(); |
michael@0 | 48 | StaticAssertTypeEq<int&, tuple_element<1, tuple<double, int&> >::type>(); |
michael@0 | 49 | StaticAssertTypeEq<bool, tuple_element<2, tuple<double, int, bool> >::type>(); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | // Tests that tuple_size<T>::value gives the number of fields in tuple |
michael@0 | 53 | // type T. |
michael@0 | 54 | TEST(tuple_size_Test, ReturnsNumberOfFields) { |
michael@0 | 55 | EXPECT_EQ(0, +tuple_size<tuple<> >::value); |
michael@0 | 56 | EXPECT_EQ(1, +tuple_size<tuple<void*> >::value); |
michael@0 | 57 | EXPECT_EQ(1, +tuple_size<tuple<char> >::value); |
michael@0 | 58 | EXPECT_EQ(1, +(tuple_size<tuple<tuple<int, double> > >::value)); |
michael@0 | 59 | EXPECT_EQ(2, +(tuple_size<tuple<int&, const char> >::value)); |
michael@0 | 60 | EXPECT_EQ(3, +(tuple_size<tuple<char*, void, const bool&> >::value)); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | // Tests comparing a tuple with itself. |
michael@0 | 64 | TEST(ComparisonTest, ComparesWithSelf) { |
michael@0 | 65 | const tuple<int, char, bool> a(5, 'a', false); |
michael@0 | 66 | |
michael@0 | 67 | EXPECT_TRUE(a == a); |
michael@0 | 68 | EXPECT_FALSE(a != a); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | // Tests comparing two tuples with the same value. |
michael@0 | 72 | TEST(ComparisonTest, ComparesEqualTuples) { |
michael@0 | 73 | const tuple<int, bool> a(5, true), b(5, true); |
michael@0 | 74 | |
michael@0 | 75 | EXPECT_TRUE(a == b); |
michael@0 | 76 | EXPECT_FALSE(a != b); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // Tests comparing two different tuples that have no reference fields. |
michael@0 | 80 | TEST(ComparisonTest, ComparesUnequalTuplesWithoutReferenceFields) { |
michael@0 | 81 | typedef tuple<const int, char> FooTuple; |
michael@0 | 82 | |
michael@0 | 83 | const FooTuple a(0, 'x'); |
michael@0 | 84 | const FooTuple b(1, 'a'); |
michael@0 | 85 | |
michael@0 | 86 | EXPECT_TRUE(a != b); |
michael@0 | 87 | EXPECT_FALSE(a == b); |
michael@0 | 88 | |
michael@0 | 89 | const FooTuple c(1, 'b'); |
michael@0 | 90 | |
michael@0 | 91 | EXPECT_TRUE(b != c); |
michael@0 | 92 | EXPECT_FALSE(b == c); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // Tests comparing two different tuples that have reference fields. |
michael@0 | 96 | TEST(ComparisonTest, ComparesUnequalTuplesWithReferenceFields) { |
michael@0 | 97 | typedef tuple<int&, const char&> FooTuple; |
michael@0 | 98 | |
michael@0 | 99 | int i = 5; |
michael@0 | 100 | const char ch = 'a'; |
michael@0 | 101 | const FooTuple a(i, ch); |
michael@0 | 102 | |
michael@0 | 103 | int j = 6; |
michael@0 | 104 | const FooTuple b(j, ch); |
michael@0 | 105 | |
michael@0 | 106 | EXPECT_TRUE(a != b); |
michael@0 | 107 | EXPECT_FALSE(a == b); |
michael@0 | 108 | |
michael@0 | 109 | j = 5; |
michael@0 | 110 | const char ch2 = 'b'; |
michael@0 | 111 | const FooTuple c(j, ch2); |
michael@0 | 112 | |
michael@0 | 113 | EXPECT_TRUE(b != c); |
michael@0 | 114 | EXPECT_FALSE(b == c); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // Tests that a tuple field with a reference type is an alias of the |
michael@0 | 118 | // variable it's supposed to reference. |
michael@0 | 119 | TEST(ReferenceFieldTest, IsAliasOfReferencedVariable) { |
michael@0 | 120 | int n = 0; |
michael@0 | 121 | tuple<bool, int&> t(true, n); |
michael@0 | 122 | |
michael@0 | 123 | n = 1; |
michael@0 | 124 | EXPECT_EQ(n, get<1>(t)) |
michael@0 | 125 | << "Changing a underlying variable should update the reference field."; |
michael@0 | 126 | |
michael@0 | 127 | // Makes sure that the implementation doesn't do anything funny with |
michael@0 | 128 | // the & operator for the return type of get<>(). |
michael@0 | 129 | EXPECT_EQ(&n, &(get<1>(t))) |
michael@0 | 130 | << "The address of a reference field should equal the address of " |
michael@0 | 131 | << "the underlying variable."; |
michael@0 | 132 | |
michael@0 | 133 | get<1>(t) = 2; |
michael@0 | 134 | EXPECT_EQ(2, n) |
michael@0 | 135 | << "Changing a reference field should update the underlying variable."; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // Tests that tuple's default constructor default initializes each field. |
michael@0 | 139 | // This test needs to compile without generating warnings. |
michael@0 | 140 | TEST(TupleConstructorTest, DefaultConstructorDefaultInitializesEachField) { |
michael@0 | 141 | // The TR1 report requires that tuple's default constructor default |
michael@0 | 142 | // initializes each field, even if it's a primitive type. If the |
michael@0 | 143 | // implementation forgets to do this, this test will catch it by |
michael@0 | 144 | // generating warnings about using uninitialized variables (assuming |
michael@0 | 145 | // a decent compiler). |
michael@0 | 146 | |
michael@0 | 147 | tuple<> empty; |
michael@0 | 148 | |
michael@0 | 149 | tuple<int> a1, b1; |
michael@0 | 150 | b1 = a1; |
michael@0 | 151 | EXPECT_EQ(0, get<0>(b1)); |
michael@0 | 152 | |
michael@0 | 153 | tuple<int, double> a2, b2; |
michael@0 | 154 | b2 = a2; |
michael@0 | 155 | EXPECT_EQ(0, get<0>(b2)); |
michael@0 | 156 | EXPECT_EQ(0.0, get<1>(b2)); |
michael@0 | 157 | |
michael@0 | 158 | tuple<double, char, bool*> a3, b3; |
michael@0 | 159 | b3 = a3; |
michael@0 | 160 | EXPECT_EQ(0.0, get<0>(b3)); |
michael@0 | 161 | EXPECT_EQ('\0', get<1>(b3)); |
michael@0 | 162 | EXPECT_TRUE(get<2>(b3) == NULL); |
michael@0 | 163 | |
michael@0 | 164 | tuple<int, int, int, int, int, int, int, int, int, int> a10, b10; |
michael@0 | 165 | b10 = a10; |
michael@0 | 166 | EXPECT_EQ(0, get<0>(b10)); |
michael@0 | 167 | EXPECT_EQ(0, get<1>(b10)); |
michael@0 | 168 | EXPECT_EQ(0, get<2>(b10)); |
michael@0 | 169 | EXPECT_EQ(0, get<3>(b10)); |
michael@0 | 170 | EXPECT_EQ(0, get<4>(b10)); |
michael@0 | 171 | EXPECT_EQ(0, get<5>(b10)); |
michael@0 | 172 | EXPECT_EQ(0, get<6>(b10)); |
michael@0 | 173 | EXPECT_EQ(0, get<7>(b10)); |
michael@0 | 174 | EXPECT_EQ(0, get<8>(b10)); |
michael@0 | 175 | EXPECT_EQ(0, get<9>(b10)); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | // Tests constructing a tuple from its fields. |
michael@0 | 179 | TEST(TupleConstructorTest, ConstructsFromFields) { |
michael@0 | 180 | int n = 1; |
michael@0 | 181 | // Reference field. |
michael@0 | 182 | tuple<int&> a(n); |
michael@0 | 183 | EXPECT_EQ(&n, &(get<0>(a))); |
michael@0 | 184 | |
michael@0 | 185 | // Non-reference fields. |
michael@0 | 186 | tuple<int, char> b(5, 'a'); |
michael@0 | 187 | EXPECT_EQ(5, get<0>(b)); |
michael@0 | 188 | EXPECT_EQ('a', get<1>(b)); |
michael@0 | 189 | |
michael@0 | 190 | // Const reference field. |
michael@0 | 191 | const int m = 2; |
michael@0 | 192 | tuple<bool, const int&> c(true, m); |
michael@0 | 193 | EXPECT_TRUE(get<0>(c)); |
michael@0 | 194 | EXPECT_EQ(&m, &(get<1>(c))); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | // Tests tuple's copy constructor. |
michael@0 | 198 | TEST(TupleConstructorTest, CopyConstructor) { |
michael@0 | 199 | tuple<double, bool> a(0.0, true); |
michael@0 | 200 | tuple<double, bool> b(a); |
michael@0 | 201 | |
michael@0 | 202 | EXPECT_DOUBLE_EQ(0.0, get<0>(b)); |
michael@0 | 203 | EXPECT_TRUE(get<1>(b)); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | // Tests constructing a tuple from another tuple that has a compatible |
michael@0 | 207 | // but different type. |
michael@0 | 208 | TEST(TupleConstructorTest, ConstructsFromDifferentTupleType) { |
michael@0 | 209 | tuple<int, int, char> a(0, 1, 'a'); |
michael@0 | 210 | tuple<double, long, int> b(a); |
michael@0 | 211 | |
michael@0 | 212 | EXPECT_DOUBLE_EQ(0.0, get<0>(b)); |
michael@0 | 213 | EXPECT_EQ(1, get<1>(b)); |
michael@0 | 214 | EXPECT_EQ('a', get<2>(b)); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | // Tests constructing a 2-tuple from an std::pair. |
michael@0 | 218 | TEST(TupleConstructorTest, ConstructsFromPair) { |
michael@0 | 219 | ::std::pair<int, char> a(1, 'a'); |
michael@0 | 220 | tuple<int, char> b(a); |
michael@0 | 221 | tuple<int, const char&> c(a); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | // Tests assigning a tuple to another tuple with the same type. |
michael@0 | 225 | TEST(TupleAssignmentTest, AssignsToSameTupleType) { |
michael@0 | 226 | const tuple<int, long> a(5, 7L); |
michael@0 | 227 | tuple<int, long> b; |
michael@0 | 228 | b = a; |
michael@0 | 229 | EXPECT_EQ(5, get<0>(b)); |
michael@0 | 230 | EXPECT_EQ(7L, get<1>(b)); |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | // Tests assigning a tuple to another tuple with a different but |
michael@0 | 234 | // compatible type. |
michael@0 | 235 | TEST(TupleAssignmentTest, AssignsToDifferentTupleType) { |
michael@0 | 236 | const tuple<int, long, bool> a(1, 7L, true); |
michael@0 | 237 | tuple<long, int, bool> b; |
michael@0 | 238 | b = a; |
michael@0 | 239 | EXPECT_EQ(1L, get<0>(b)); |
michael@0 | 240 | EXPECT_EQ(7, get<1>(b)); |
michael@0 | 241 | EXPECT_TRUE(get<2>(b)); |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | // Tests assigning an std::pair to a 2-tuple. |
michael@0 | 245 | TEST(TupleAssignmentTest, AssignsFromPair) { |
michael@0 | 246 | const ::std::pair<int, bool> a(5, true); |
michael@0 | 247 | tuple<int, bool> b; |
michael@0 | 248 | b = a; |
michael@0 | 249 | EXPECT_EQ(5, get<0>(b)); |
michael@0 | 250 | EXPECT_TRUE(get<1>(b)); |
michael@0 | 251 | |
michael@0 | 252 | tuple<long, bool> c; |
michael@0 | 253 | c = a; |
michael@0 | 254 | EXPECT_EQ(5L, get<0>(c)); |
michael@0 | 255 | EXPECT_TRUE(get<1>(c)); |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | // A fixture for testing big tuples. |
michael@0 | 259 | class BigTupleTest : public testing::Test { |
michael@0 | 260 | protected: |
michael@0 | 261 | typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple; |
michael@0 | 262 | |
michael@0 | 263 | BigTupleTest() : |
michael@0 | 264 | a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2), |
michael@0 | 265 | b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {} |
michael@0 | 266 | |
michael@0 | 267 | BigTuple a_, b_; |
michael@0 | 268 | }; |
michael@0 | 269 | |
michael@0 | 270 | // Tests constructing big tuples. |
michael@0 | 271 | TEST_F(BigTupleTest, Construction) { |
michael@0 | 272 | BigTuple a; |
michael@0 | 273 | BigTuple b(b_); |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | // Tests that get<N>(t) returns the N-th (0-based) field of tuple t. |
michael@0 | 277 | TEST_F(BigTupleTest, get) { |
michael@0 | 278 | EXPECT_EQ(1, get<0>(a_)); |
michael@0 | 279 | EXPECT_EQ(2, get<9>(a_)); |
michael@0 | 280 | |
michael@0 | 281 | // Tests that get() works on a const tuple too. |
michael@0 | 282 | const BigTuple a(a_); |
michael@0 | 283 | EXPECT_EQ(1, get<0>(a)); |
michael@0 | 284 | EXPECT_EQ(2, get<9>(a)); |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | // Tests comparing big tuples. |
michael@0 | 288 | TEST_F(BigTupleTest, Comparisons) { |
michael@0 | 289 | EXPECT_TRUE(a_ == a_); |
michael@0 | 290 | EXPECT_FALSE(a_ != a_); |
michael@0 | 291 | |
michael@0 | 292 | EXPECT_TRUE(a_ != b_); |
michael@0 | 293 | EXPECT_FALSE(a_ == b_); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | TEST(MakeTupleTest, WorksForScalarTypes) { |
michael@0 | 297 | tuple<bool, int> a; |
michael@0 | 298 | a = make_tuple(true, 5); |
michael@0 | 299 | EXPECT_TRUE(get<0>(a)); |
michael@0 | 300 | EXPECT_EQ(5, get<1>(a)); |
michael@0 | 301 | |
michael@0 | 302 | tuple<char, int, long> b; |
michael@0 | 303 | b = make_tuple('a', 'b', 5); |
michael@0 | 304 | EXPECT_EQ('a', get<0>(b)); |
michael@0 | 305 | EXPECT_EQ('b', get<1>(b)); |
michael@0 | 306 | EXPECT_EQ(5, get<2>(b)); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | TEST(MakeTupleTest, WorksForPointers) { |
michael@0 | 310 | int a[] = { 1, 2, 3, 4 }; |
michael@0 | 311 | const char* const str = "hi"; |
michael@0 | 312 | int* const p = a; |
michael@0 | 313 | |
michael@0 | 314 | tuple<const char*, int*> t; |
michael@0 | 315 | t = make_tuple(str, p); |
michael@0 | 316 | EXPECT_EQ(str, get<0>(t)); |
michael@0 | 317 | EXPECT_EQ(p, get<1>(t)); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | } // namespace |