?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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.  
  31. #include "gtest/internal/gtest-tuple.h"
  32. #include <utility>
  33. #include "gtest/gtest.h"
  34.  
  35. namespace {
  36.  
  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;
  43.  
  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>();
  49. }
  50.  
  51. // Tests that tuple_size<T>::value gives the number of fields in tuple
  52. // type T.
  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));
  60. }
  61.  
  62. // Tests comparing a tuple with itself.
  63. TEST(ComparisonTest, ComparesWithSelf) {
  64.   const tuple<int, char, bool> a(5, 'a', false);
  65.  
  66.   EXPECT_TRUE(a == a);
  67.   EXPECT_FALSE(a != a);
  68. }
  69.  
  70. // Tests comparing two tuples with the same value.
  71. TEST(ComparisonTest, ComparesEqualTuples) {
  72.   const tuple<int, bool> a(5, true), b(5, true);
  73.  
  74.   EXPECT_TRUE(a == b);
  75.   EXPECT_FALSE(a != b);
  76. }
  77.  
  78. // Tests comparing two different tuples that have no reference fields.
  79. TEST(ComparisonTest, ComparesUnequalTuplesWithoutReferenceFields) {
  80.   typedef tuple<const int, char> FooTuple;
  81.  
  82.   const FooTuple a(0, 'x');
  83.   const FooTuple b(1, 'a');
  84.  
  85.   EXPECT_TRUE(a != b);
  86.   EXPECT_FALSE(a == b);
  87.  
  88.   const FooTuple c(1, 'b');
  89.  
  90.   EXPECT_TRUE(b != c);
  91.   EXPECT_FALSE(b == c);
  92. }
  93.  
  94. // Tests comparing two different tuples that have reference fields.
  95. TEST(ComparisonTest, ComparesUnequalTuplesWithReferenceFields) {
  96.   typedef tuple<int&, const char&> FooTuple;
  97.  
  98.   int i = 5;
  99.   const char ch = 'a';
  100.   const FooTuple a(i, ch);
  101.  
  102.   int j = 6;
  103.   const FooTuple b(j, ch);
  104.  
  105.   EXPECT_TRUE(a != b);
  106.   EXPECT_FALSE(a == b);
  107.  
  108.   j = 5;
  109.   const char ch2 = 'b';
  110.   const FooTuple c(j, ch2);
  111.  
  112.   EXPECT_TRUE(b != c);
  113.   EXPECT_FALSE(b == c);
  114. }
  115.  
  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) {
  119.   int n = 0;
  120.   tuple<bool, int&> t(true, n);
  121.  
  122.   n = 1;
  123.   EXPECT_EQ(n, get<1>(t))
  124.       << "Changing a underlying variable should update the reference field.";
  125.  
  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.";
  131.  
  132.   get<1>(t) = 2;
  133.   EXPECT_EQ(2, n)
  134.       << "Changing a reference field should update the underlying variable.";
  135. }
  136.  
  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).
  145.  
  146.   tuple<> empty;
  147.  
  148.   tuple<int> a1, b1;
  149.   b1 = a1;
  150.   EXPECT_EQ(0, get<0>(b1));
  151.  
  152.   tuple<int, double> a2, b2;
  153.   b2 = a2;
  154.   EXPECT_EQ(0, get<0>(b2));
  155.   EXPECT_EQ(0.0, get<1>(b2));
  156.  
  157.   tuple<double, char, bool*> a3, b3;
  158.   b3 = a3;
  159.   EXPECT_EQ(0.0, get<0>(b3));
  160.   EXPECT_EQ('\0', get<1>(b3));
  161.   EXPECT_TRUE(get<2>(b3) == NULL);
  162.  
  163.   tuple<int, int, int, int, int, int, int, int, int, int> a10, b10;
  164.   b10 = a10;
  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));
  175. }
  176.  
  177. // Tests constructing a tuple from its fields.
  178. TEST(TupleConstructorTest, ConstructsFromFields) {
  179.   int n = 1;
  180.   // Reference field.
  181.   tuple<int&> a(n);
  182.   EXPECT_EQ(&n, &(get<0>(a)));
  183.  
  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));
  188.  
  189.   // Const reference field.
  190.   const int m = 2;
  191.   tuple<bool, const int&> c(true, m);
  192.   EXPECT_TRUE(get<0>(c));
  193.   EXPECT_EQ(&m, &(get<1>(c)));
  194. }
  195.  
  196. // Tests tuple's copy constructor.
  197. TEST(TupleConstructorTest, CopyConstructor) {
  198.   tuple<double, bool> a(0.0, true);
  199.   tuple<double, bool> b(a);
  200.  
  201.   EXPECT_DOUBLE_EQ(0.0, get<0>(b));
  202.   EXPECT_TRUE(get<1>(b));
  203. }
  204.  
  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);
  210.  
  211.   EXPECT_DOUBLE_EQ(0.0, get<0>(b));
  212.   EXPECT_EQ(1, get<1>(b));
  213.   EXPECT_EQ('a', get<2>(b));
  214. }
  215.  
  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);
  221. }
  222.  
  223. // Tests assigning a tuple to another tuple with the same type.
  224. TEST(TupleAssignmentTest, AssignsToSameTupleType) {
  225.   const tuple<int, long> a(5, 7L);
  226.   tuple<int, long> b;
  227.   b = a;
  228.   EXPECT_EQ(5, get<0>(b));
  229.   EXPECT_EQ(7L, get<1>(b));
  230. }
  231.  
  232. // Tests assigning a tuple to another tuple with a different but
  233. // compatible type.
  234. TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
  235.   const tuple<int, long, bool> a(1, 7L, true);
  236.   tuple<long, int, bool> b;
  237.   b = a;
  238.   EXPECT_EQ(1L, get<0>(b));
  239.   EXPECT_EQ(7, get<1>(b));
  240.   EXPECT_TRUE(get<2>(b));
  241. }
  242.  
  243. // Tests assigning an std::pair to a 2-tuple.
  244. TEST(TupleAssignmentTest, AssignsFromPair) {
  245.   const ::std::pair<int, bool> a(5, true);
  246.   tuple<int, bool> b;
  247.   b = a;
  248.   EXPECT_EQ(5, get<0>(b));
  249.   EXPECT_TRUE(get<1>(b));
  250.  
  251.   tuple<long, bool> c;
  252.   c = a;
  253.   EXPECT_EQ(5L, get<0>(c));
  254.   EXPECT_TRUE(get<1>(c));
  255. }
  256.  
  257. // A fixture for testing big tuples.
  258. class BigTupleTest : public testing::Test {
  259.  protected:
  260.   typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
  261.  
  262.   BigTupleTest() :
  263.       a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
  264.       b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
  265.  
  266.   BigTuple a_, b_;
  267. };
  268.  
  269. // Tests constructing big tuples.
  270. TEST_F(BigTupleTest, Construction) {
  271.   BigTuple a;
  272.   BigTuple b(b_);
  273. }
  274.  
  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_));
  279.  
  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));
  284. }
  285.  
  286. // Tests comparing big tuples.
  287. TEST_F(BigTupleTest, Comparisons) {
  288.   EXPECT_TRUE(a_ == a_);
  289.   EXPECT_FALSE(a_ != a_);
  290.  
  291.   EXPECT_TRUE(a_ != b_);
  292.   EXPECT_FALSE(a_ == b_);
  293. }
  294.  
  295. TEST(MakeTupleTest, WorksForScalarTypes) {
  296.   tuple<bool, int> a;
  297.   a = make_tuple(true, 5);
  298.   EXPECT_TRUE(get<0>(a));
  299.   EXPECT_EQ(5, get<1>(a));
  300.  
  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));
  306. }
  307.  
  308. TEST(MakeTupleTest, WorksForPointers) {
  309.   int a[] = { 1, 2, 3, 4 };
  310.   const char* const str = "hi";
  311.   int* const p = a;
  312.  
  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));
  317. }
  318.  
  319. }  // namespace
  320.