/Unittests/googletest/test/gtest-tuple_test.cc

http://unladen-swallow.googlecode.com/ · C++ · 288 lines · 178 code · 50 blank · 60 comment · 16 complexity · abd6dd0fd00aa732bb4e5f30812c0cec MD5 · raw file

  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. #include <gtest/internal/gtest-tuple.h>
  32. #include <utility>
  33. #include <gtest/gtest.h>
  34. namespace {
  35. using ::std::tr1::get;
  36. using ::std::tr1::make_tuple;
  37. using ::std::tr1::tuple;
  38. using ::std::tr1::tuple_element;
  39. using ::std::tr1::tuple_size;
  40. using ::testing::StaticAssertTypeEq;
  41. // Tests that tuple_element<K, tuple<T0, T1, ..., TN> >::type returns TK.
  42. TEST(tuple_element_Test, ReturnsElementType) {
  43. StaticAssertTypeEq<int, tuple_element<0, tuple<int, char> >::type>();
  44. StaticAssertTypeEq<int&, tuple_element<1, tuple<double, int&> >::type>();
  45. StaticAssertTypeEq<bool, tuple_element<2, tuple<double, int, bool> >::type>();
  46. }
  47. // Tests that tuple_size<T>::value gives the number of fields in tuple
  48. // type T.
  49. TEST(tuple_size_Test, ReturnsNumberOfFields) {
  50. EXPECT_EQ(0, +tuple_size<tuple<> >::value);
  51. EXPECT_EQ(1, +tuple_size<tuple<void*> >::value);
  52. EXPECT_EQ(1, +tuple_size<tuple<char> >::value);
  53. EXPECT_EQ(1, +(tuple_size<tuple<tuple<int, double> > >::value));
  54. EXPECT_EQ(2, +(tuple_size<tuple<int&, const char> >::value));
  55. EXPECT_EQ(3, +(tuple_size<tuple<char*, void, const bool&> >::value));
  56. }
  57. // Tests comparing a tuple with itself.
  58. TEST(ComparisonTest, ComparesWithSelf) {
  59. const tuple<int, char, bool> a(5, 'a', false);
  60. EXPECT_TRUE(a == a);
  61. EXPECT_FALSE(a != a);
  62. }
  63. // Tests comparing two tuples with the same value.
  64. TEST(ComparisonTest, ComparesEqualTuples) {
  65. const tuple<int, bool> a(5, true), b(5, true);
  66. EXPECT_TRUE(a == b);
  67. EXPECT_FALSE(a != b);
  68. }
  69. // Tests comparing two different tuples that have no reference fields.
  70. TEST(ComparisonTest, ComparesUnequalTuplesWithoutReferenceFields) {
  71. typedef tuple<const int, char> FooTuple;
  72. const FooTuple a(0, 'x');
  73. const FooTuple b(1, 'a');
  74. EXPECT_TRUE(a != b);
  75. EXPECT_FALSE(a == b);
  76. const FooTuple c(1, 'b');
  77. EXPECT_TRUE(b != c);
  78. EXPECT_FALSE(b == c);
  79. }
  80. // Tests comparing two different tuples that have reference fields.
  81. TEST(ComparisonTest, ComparesUnequalTuplesWithReferenceFields) {
  82. typedef tuple<int&, const char&> FooTuple;
  83. int i = 5;
  84. const char ch = 'a';
  85. const FooTuple a(i, ch);
  86. int j = 6;
  87. const FooTuple b(j, ch);
  88. EXPECT_TRUE(a != b);
  89. EXPECT_FALSE(a == b);
  90. j = 5;
  91. const char ch2 = 'b';
  92. const FooTuple c(j, ch2);
  93. EXPECT_TRUE(b != c);
  94. EXPECT_FALSE(b == c);
  95. }
  96. // Tests that a tuple field with a reference type is an alias of the
  97. // variable it's supposed to reference.
  98. TEST(ReferenceFieldTest, IsAliasOfReferencedVariable) {
  99. int n = 0;
  100. tuple<bool, int&> t(true, n);
  101. n = 1;
  102. EXPECT_EQ(n, get<1>(t))
  103. << "Changing a underlying variable should update the reference field.";
  104. // Makes sure that the implementation doesn't do anything funny with
  105. // the & operator for the return type of get<>().
  106. EXPECT_EQ(&n, &(get<1>(t)))
  107. << "The address of a reference field should equal the address of "
  108. << "the underlying variable.";
  109. get<1>(t) = 2;
  110. EXPECT_EQ(2, n)
  111. << "Changing a reference field should update the underlying variable.";
  112. }
  113. // Tests tuple's default constructor.
  114. TEST(TupleConstructorTest, DefaultConstructor) {
  115. // We are just testing that the following compiles.
  116. tuple<> empty;
  117. tuple<int> one_field;
  118. tuple<double, char, bool*> three_fields;
  119. }
  120. // Tests constructing a tuple from its fields.
  121. TEST(TupleConstructorTest, ConstructsFromFields) {
  122. int n = 1;
  123. // Reference field.
  124. tuple<int&> a(n);
  125. EXPECT_EQ(&n, &(get<0>(a)));
  126. // Non-reference fields.
  127. tuple<int, char> b(5, 'a');
  128. EXPECT_EQ(5, get<0>(b));
  129. EXPECT_EQ('a', get<1>(b));
  130. // Const reference field.
  131. const int m = 2;
  132. tuple<bool, const int&> c(true, m);
  133. EXPECT_TRUE(get<0>(c));
  134. EXPECT_EQ(&m, &(get<1>(c)));
  135. }
  136. // Tests tuple's copy constructor.
  137. TEST(TupleConstructorTest, CopyConstructor) {
  138. tuple<double, bool> a(0.0, true);
  139. tuple<double, bool> b(a);
  140. EXPECT_DOUBLE_EQ(0.0, get<0>(b));
  141. EXPECT_TRUE(get<1>(b));
  142. }
  143. // Tests constructing a tuple from another tuple that has a compatible
  144. // but different type.
  145. TEST(TupleConstructorTest, ConstructsFromDifferentTupleType) {
  146. tuple<int, int, char> a(0, 1, 'a');
  147. tuple<double, long, int> b(a);
  148. EXPECT_DOUBLE_EQ(0.0, get<0>(b));
  149. EXPECT_EQ(1, get<1>(b));
  150. EXPECT_EQ('a', get<2>(b));
  151. }
  152. // Tests constructing a 2-tuple from an std::pair.
  153. TEST(TupleConstructorTest, ConstructsFromPair) {
  154. ::std::pair<int, char> a(1, 'a');
  155. tuple<int, char> b(a);
  156. tuple<int, const char&> c(a);
  157. }
  158. // Tests assigning a tuple to another tuple with the same type.
  159. TEST(TupleAssignmentTest, AssignsToSameTupleType) {
  160. const tuple<int, long> a(5, 7L);
  161. tuple<int, long> b;
  162. b = a;
  163. EXPECT_EQ(5, get<0>(b));
  164. EXPECT_EQ(7L, get<1>(b));
  165. }
  166. // Tests assigning a tuple to another tuple with a different but
  167. // compatible type.
  168. TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
  169. const tuple<int, long, bool> a(1, 7L, true);
  170. tuple<long, int, bool> b;
  171. b = a;
  172. EXPECT_EQ(1L, get<0>(b));
  173. EXPECT_EQ(7, get<1>(b));
  174. EXPECT_TRUE(get<2>(b));
  175. }
  176. // Tests assigning an std::pair to a 2-tuple.
  177. TEST(TupleAssignmentTest, AssignsFromPair) {
  178. const ::std::pair<int, bool> a(5, true);
  179. tuple<int, bool> b;
  180. b = a;
  181. EXPECT_EQ(5, get<0>(b));
  182. EXPECT_TRUE(get<1>(b));
  183. tuple<long, bool> c;
  184. c = a;
  185. EXPECT_EQ(5L, get<0>(c));
  186. EXPECT_TRUE(get<1>(c));
  187. }
  188. // A fixture for testing big tuples.
  189. class BigTupleTest : public testing::Test {
  190. protected:
  191. typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
  192. BigTupleTest() :
  193. a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
  194. b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
  195. BigTuple a_, b_;
  196. };
  197. // Tests constructing big tuples.
  198. TEST_F(BigTupleTest, Construction) {
  199. BigTuple a;
  200. BigTuple b(b_);
  201. }
  202. // Tests that get<N>(t) returns the N-th (0-based) field of tuple t.
  203. TEST_F(BigTupleTest, get) {
  204. EXPECT_EQ(1, get<0>(a_));
  205. EXPECT_EQ(2, get<9>(a_));
  206. // Tests that get() works on a const tuple too.
  207. const BigTuple a(a_);
  208. EXPECT_EQ(1, get<0>(a));
  209. EXPECT_EQ(2, get<9>(a));
  210. }
  211. // Tests comparing big tuples.
  212. TEST_F(BigTupleTest, Comparisons) {
  213. EXPECT_TRUE(a_ == a_);
  214. EXPECT_FALSE(a_ != a_);
  215. EXPECT_TRUE(a_ != b_);
  216. EXPECT_FALSE(a_ == b_);
  217. }
  218. TEST(MakeTupleTest, WorksForScalarTypes) {
  219. tuple<bool, int> a;
  220. a = make_tuple(true, 5);
  221. EXPECT_TRUE(get<0>(a));
  222. EXPECT_EQ(5, get<1>(a));
  223. tuple<char, int, long> b;
  224. b = make_tuple('a', 'b', 5);
  225. EXPECT_EQ('a', get<0>(b));
  226. EXPECT_EQ('b', get<1>(b));
  227. EXPECT_EQ(5, get<2>(b));
  228. }
  229. TEST(MakeTupleTest, WorksForPointers) {
  230. int a[] = { 1, 2, 3, 4 };
  231. const char* const str = "hi";
  232. int* const p = a;
  233. tuple<const char*, int*> t;
  234. t = make_tuple(str, p);
  235. EXPECT_EQ(str, get<0>(t));
  236. EXPECT_EQ(p, get<1>(t));
  237. }
  238. } // namespace