media/webrtc/trunk/testing/gtest/test/gtest-tuple_test.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest-tuple_test.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,320 @@
     1.4 +// Copyright 2007, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +//
    1.33 +// Author: wan@google.com (Zhanyong Wan)
    1.34 +
    1.35 +#include "gtest/internal/gtest-tuple.h"
    1.36 +#include <utility>
    1.37 +#include "gtest/gtest.h"
    1.38 +
    1.39 +namespace {
    1.40 +
    1.41 +using ::std::tr1::get;
    1.42 +using ::std::tr1::make_tuple;
    1.43 +using ::std::tr1::tuple;
    1.44 +using ::std::tr1::tuple_element;
    1.45 +using ::std::tr1::tuple_size;
    1.46 +using ::testing::StaticAssertTypeEq;
    1.47 +
    1.48 +// Tests that tuple_element<K, tuple<T0, T1, ..., TN> >::type returns TK.
    1.49 +TEST(tuple_element_Test, ReturnsElementType) {
    1.50 +  StaticAssertTypeEq<int, tuple_element<0, tuple<int, char> >::type>();
    1.51 +  StaticAssertTypeEq<int&, tuple_element<1, tuple<double, int&> >::type>();
    1.52 +  StaticAssertTypeEq<bool, tuple_element<2, tuple<double, int, bool> >::type>();
    1.53 +}
    1.54 +
    1.55 +// Tests that tuple_size<T>::value gives the number of fields in tuple
    1.56 +// type T.
    1.57 +TEST(tuple_size_Test, ReturnsNumberOfFields) {
    1.58 +  EXPECT_EQ(0, +tuple_size<tuple<> >::value);
    1.59 +  EXPECT_EQ(1, +tuple_size<tuple<void*> >::value);
    1.60 +  EXPECT_EQ(1, +tuple_size<tuple<char> >::value);
    1.61 +  EXPECT_EQ(1, +(tuple_size<tuple<tuple<int, double> > >::value));
    1.62 +  EXPECT_EQ(2, +(tuple_size<tuple<int&, const char> >::value));
    1.63 +  EXPECT_EQ(3, +(tuple_size<tuple<char*, void, const bool&> >::value));
    1.64 +}
    1.65 +
    1.66 +// Tests comparing a tuple with itself.
    1.67 +TEST(ComparisonTest, ComparesWithSelf) {
    1.68 +  const tuple<int, char, bool> a(5, 'a', false);
    1.69 +
    1.70 +  EXPECT_TRUE(a == a);
    1.71 +  EXPECT_FALSE(a != a);
    1.72 +}
    1.73 +
    1.74 +// Tests comparing two tuples with the same value.
    1.75 +TEST(ComparisonTest, ComparesEqualTuples) {
    1.76 +  const tuple<int, bool> a(5, true), b(5, true);
    1.77 +
    1.78 +  EXPECT_TRUE(a == b);
    1.79 +  EXPECT_FALSE(a != b);
    1.80 +}
    1.81 +
    1.82 +// Tests comparing two different tuples that have no reference fields.
    1.83 +TEST(ComparisonTest, ComparesUnequalTuplesWithoutReferenceFields) {
    1.84 +  typedef tuple<const int, char> FooTuple;
    1.85 +
    1.86 +  const FooTuple a(0, 'x');
    1.87 +  const FooTuple b(1, 'a');
    1.88 +
    1.89 +  EXPECT_TRUE(a != b);
    1.90 +  EXPECT_FALSE(a == b);
    1.91 +
    1.92 +  const FooTuple c(1, 'b');
    1.93 +
    1.94 +  EXPECT_TRUE(b != c);
    1.95 +  EXPECT_FALSE(b == c);
    1.96 +}
    1.97 +
    1.98 +// Tests comparing two different tuples that have reference fields.
    1.99 +TEST(ComparisonTest, ComparesUnequalTuplesWithReferenceFields) {
   1.100 +  typedef tuple<int&, const char&> FooTuple;
   1.101 +
   1.102 +  int i = 5;
   1.103 +  const char ch = 'a';
   1.104 +  const FooTuple a(i, ch);
   1.105 +
   1.106 +  int j = 6;
   1.107 +  const FooTuple b(j, ch);
   1.108 +
   1.109 +  EXPECT_TRUE(a != b);
   1.110 +  EXPECT_FALSE(a == b);
   1.111 +
   1.112 +  j = 5;
   1.113 +  const char ch2 = 'b';
   1.114 +  const FooTuple c(j, ch2);
   1.115 +
   1.116 +  EXPECT_TRUE(b != c);
   1.117 +  EXPECT_FALSE(b == c);
   1.118 +}
   1.119 +
   1.120 +// Tests that a tuple field with a reference type is an alias of the
   1.121 +// variable it's supposed to reference.
   1.122 +TEST(ReferenceFieldTest, IsAliasOfReferencedVariable) {
   1.123 +  int n = 0;
   1.124 +  tuple<bool, int&> t(true, n);
   1.125 +
   1.126 +  n = 1;
   1.127 +  EXPECT_EQ(n, get<1>(t))
   1.128 +      << "Changing a underlying variable should update the reference field.";
   1.129 +
   1.130 +  // Makes sure that the implementation doesn't do anything funny with
   1.131 +  // the & operator for the return type of get<>().
   1.132 +  EXPECT_EQ(&n, &(get<1>(t)))
   1.133 +      << "The address of a reference field should equal the address of "
   1.134 +      << "the underlying variable.";
   1.135 +
   1.136 +  get<1>(t) = 2;
   1.137 +  EXPECT_EQ(2, n)
   1.138 +      << "Changing a reference field should update the underlying variable.";
   1.139 +}
   1.140 +
   1.141 +// Tests that tuple's default constructor default initializes each field.
   1.142 +// This test needs to compile without generating warnings.
   1.143 +TEST(TupleConstructorTest, DefaultConstructorDefaultInitializesEachField) {
   1.144 +  // The TR1 report requires that tuple's default constructor default
   1.145 +  // initializes each field, even if it's a primitive type.  If the
   1.146 +  // implementation forgets to do this, this test will catch it by
   1.147 +  // generating warnings about using uninitialized variables (assuming
   1.148 +  // a decent compiler).
   1.149 +
   1.150 +  tuple<> empty;
   1.151 +
   1.152 +  tuple<int> a1, b1;
   1.153 +  b1 = a1;
   1.154 +  EXPECT_EQ(0, get<0>(b1));
   1.155 +
   1.156 +  tuple<int, double> a2, b2;
   1.157 +  b2 = a2;
   1.158 +  EXPECT_EQ(0, get<0>(b2));
   1.159 +  EXPECT_EQ(0.0, get<1>(b2));
   1.160 +
   1.161 +  tuple<double, char, bool*> a3, b3;
   1.162 +  b3 = a3;
   1.163 +  EXPECT_EQ(0.0, get<0>(b3));
   1.164 +  EXPECT_EQ('\0', get<1>(b3));
   1.165 +  EXPECT_TRUE(get<2>(b3) == NULL);
   1.166 +
   1.167 +  tuple<int, int, int, int, int, int, int, int, int, int> a10, b10;
   1.168 +  b10 = a10;
   1.169 +  EXPECT_EQ(0, get<0>(b10));
   1.170 +  EXPECT_EQ(0, get<1>(b10));
   1.171 +  EXPECT_EQ(0, get<2>(b10));
   1.172 +  EXPECT_EQ(0, get<3>(b10));
   1.173 +  EXPECT_EQ(0, get<4>(b10));
   1.174 +  EXPECT_EQ(0, get<5>(b10));
   1.175 +  EXPECT_EQ(0, get<6>(b10));
   1.176 +  EXPECT_EQ(0, get<7>(b10));
   1.177 +  EXPECT_EQ(0, get<8>(b10));
   1.178 +  EXPECT_EQ(0, get<9>(b10));
   1.179 +}
   1.180 +
   1.181 +// Tests constructing a tuple from its fields.
   1.182 +TEST(TupleConstructorTest, ConstructsFromFields) {
   1.183 +  int n = 1;
   1.184 +  // Reference field.
   1.185 +  tuple<int&> a(n);
   1.186 +  EXPECT_EQ(&n, &(get<0>(a)));
   1.187 +
   1.188 +  // Non-reference fields.
   1.189 +  tuple<int, char> b(5, 'a');
   1.190 +  EXPECT_EQ(5, get<0>(b));
   1.191 +  EXPECT_EQ('a', get<1>(b));
   1.192 +
   1.193 +  // Const reference field.
   1.194 +  const int m = 2;
   1.195 +  tuple<bool, const int&> c(true, m);
   1.196 +  EXPECT_TRUE(get<0>(c));
   1.197 +  EXPECT_EQ(&m, &(get<1>(c)));
   1.198 +}
   1.199 +
   1.200 +// Tests tuple's copy constructor.
   1.201 +TEST(TupleConstructorTest, CopyConstructor) {
   1.202 +  tuple<double, bool> a(0.0, true);
   1.203 +  tuple<double, bool> b(a);
   1.204 +
   1.205 +  EXPECT_DOUBLE_EQ(0.0, get<0>(b));
   1.206 +  EXPECT_TRUE(get<1>(b));
   1.207 +}
   1.208 +
   1.209 +// Tests constructing a tuple from another tuple that has a compatible
   1.210 +// but different type.
   1.211 +TEST(TupleConstructorTest, ConstructsFromDifferentTupleType) {
   1.212 +  tuple<int, int, char> a(0, 1, 'a');
   1.213 +  tuple<double, long, int> b(a);
   1.214 +
   1.215 +  EXPECT_DOUBLE_EQ(0.0, get<0>(b));
   1.216 +  EXPECT_EQ(1, get<1>(b));
   1.217 +  EXPECT_EQ('a', get<2>(b));
   1.218 +}
   1.219 +
   1.220 +// Tests constructing a 2-tuple from an std::pair.
   1.221 +TEST(TupleConstructorTest, ConstructsFromPair) {
   1.222 +  ::std::pair<int, char> a(1, 'a');
   1.223 +  tuple<int, char> b(a);
   1.224 +  tuple<int, const char&> c(a);
   1.225 +}
   1.226 +
   1.227 +// Tests assigning a tuple to another tuple with the same type.
   1.228 +TEST(TupleAssignmentTest, AssignsToSameTupleType) {
   1.229 +  const tuple<int, long> a(5, 7L);
   1.230 +  tuple<int, long> b;
   1.231 +  b = a;
   1.232 +  EXPECT_EQ(5, get<0>(b));
   1.233 +  EXPECT_EQ(7L, get<1>(b));
   1.234 +}
   1.235 +
   1.236 +// Tests assigning a tuple to another tuple with a different but
   1.237 +// compatible type.
   1.238 +TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
   1.239 +  const tuple<int, long, bool> a(1, 7L, true);
   1.240 +  tuple<long, int, bool> b;
   1.241 +  b = a;
   1.242 +  EXPECT_EQ(1L, get<0>(b));
   1.243 +  EXPECT_EQ(7, get<1>(b));
   1.244 +  EXPECT_TRUE(get<2>(b));
   1.245 +}
   1.246 +
   1.247 +// Tests assigning an std::pair to a 2-tuple.
   1.248 +TEST(TupleAssignmentTest, AssignsFromPair) {
   1.249 +  const ::std::pair<int, bool> a(5, true);
   1.250 +  tuple<int, bool> b;
   1.251 +  b = a;
   1.252 +  EXPECT_EQ(5, get<0>(b));
   1.253 +  EXPECT_TRUE(get<1>(b));
   1.254 +
   1.255 +  tuple<long, bool> c;
   1.256 +  c = a;
   1.257 +  EXPECT_EQ(5L, get<0>(c));
   1.258 +  EXPECT_TRUE(get<1>(c));
   1.259 +}
   1.260 +
   1.261 +// A fixture for testing big tuples.
   1.262 +class BigTupleTest : public testing::Test {
   1.263 + protected:
   1.264 +  typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
   1.265 +
   1.266 +  BigTupleTest() :
   1.267 +      a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
   1.268 +      b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
   1.269 +
   1.270 +  BigTuple a_, b_;
   1.271 +};
   1.272 +
   1.273 +// Tests constructing big tuples.
   1.274 +TEST_F(BigTupleTest, Construction) {
   1.275 +  BigTuple a;
   1.276 +  BigTuple b(b_);
   1.277 +}
   1.278 +
   1.279 +// Tests that get<N>(t) returns the N-th (0-based) field of tuple t.
   1.280 +TEST_F(BigTupleTest, get) {
   1.281 +  EXPECT_EQ(1, get<0>(a_));
   1.282 +  EXPECT_EQ(2, get<9>(a_));
   1.283 +
   1.284 +  // Tests that get() works on a const tuple too.
   1.285 +  const BigTuple a(a_);
   1.286 +  EXPECT_EQ(1, get<0>(a));
   1.287 +  EXPECT_EQ(2, get<9>(a));
   1.288 +}
   1.289 +
   1.290 +// Tests comparing big tuples.
   1.291 +TEST_F(BigTupleTest, Comparisons) {
   1.292 +  EXPECT_TRUE(a_ == a_);
   1.293 +  EXPECT_FALSE(a_ != a_);
   1.294 +
   1.295 +  EXPECT_TRUE(a_ != b_);
   1.296 +  EXPECT_FALSE(a_ == b_);
   1.297 +}
   1.298 +
   1.299 +TEST(MakeTupleTest, WorksForScalarTypes) {
   1.300 +  tuple<bool, int> a;
   1.301 +  a = make_tuple(true, 5);
   1.302 +  EXPECT_TRUE(get<0>(a));
   1.303 +  EXPECT_EQ(5, get<1>(a));
   1.304 +
   1.305 +  tuple<char, int, long> b;
   1.306 +  b = make_tuple('a', 'b', 5);
   1.307 +  EXPECT_EQ('a', get<0>(b));
   1.308 +  EXPECT_EQ('b', get<1>(b));
   1.309 +  EXPECT_EQ(5, get<2>(b));
   1.310 +}
   1.311 +
   1.312 +TEST(MakeTupleTest, WorksForPointers) {
   1.313 +  int a[] = { 1, 2, 3, 4 };
   1.314 +  const char* const str = "hi";
   1.315 +  int* const p = a;
   1.316 +
   1.317 +  tuple<const char*, int*> t;
   1.318 +  t = make_tuple(str, p);
   1.319 +  EXPECT_EQ(str, get<0>(t));
   1.320 +  EXPECT_EQ(p, get<1>(t));
   1.321 +}
   1.322 +
   1.323 +}  // namespace

mercurial