gtest-tuple_test.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 that tuple's default constructor default initializes each field.
  114. // This test needs to compile without generating warnings.
  115. TEST(TupleConstructorTest, DefaultConstructorDefaultInitializesEachField) {
  116. // The TR1 report requires that tuple's default constructor default
  117. // initializes each field, even if it's a primitive type. If the
  118. // implementation forgets to do this, this test will catch it by
  119. // generating warnings about using uninitialized variables (assuming
  120. // a decent compiler).
  121. tuple<> empty;
  122. tuple<int> a1, b1;
  123. b1 = a1;
  124. EXPECT_EQ(0, get<0>(b1));
  125. tuple<int, double> a2, b2;
  126. b2 = a2;
  127. EXPECT_EQ(0, get<0>(b2));
  128. EXPECT_EQ(0.0, get<1>(b2));
  129. tuple<double, char, bool*> a3, b3;
  130. b3 = a3;
  131. EXPECT_EQ(0.0, get<0>(b3));
  132. EXPECT_EQ('\0', get<1>(b3));
  133. EXPECT_TRUE(get<2>(b3) == NULL);
  134. tuple<int, int, int, int, int, int, int, int, int, int> a10, b10;
  135. b10 = a10;
  136. EXPECT_EQ(0, get<0>(b10));
  137. EXPECT_EQ(0, get<1>(b10));
  138. EXPECT_EQ(0, get<2>(b10));
  139. EXPECT_EQ(0, get<3>(b10));
  140. EXPECT_EQ(0, get<4>(b10));
  141. EXPECT_EQ(0, get<5>(b10));
  142. EXPECT_EQ(0, get<6>(b10));
  143. EXPECT_EQ(0, get<7>(b10));
  144. EXPECT_EQ(0, get<8>(b10));
  145. EXPECT_EQ(0, get<9>(b10));
  146. }
  147. // Tests constructing a tuple from its fields.
  148. TEST(TupleConstructorTest, ConstructsFromFields) {
  149. int n = 1;
  150. // Reference field.
  151. tuple<int&> a(n);
  152. EXPECT_EQ(&n, &(get<0>(a)));
  153. // Non-reference fields.
  154. tuple<int, char> b(5, 'a');
  155. EXPECT_EQ(5, get<0>(b));
  156. EXPECT_EQ('a', get<1>(b));
  157. // Const reference field.
  158. const int m = 2;
  159. tuple<bool, const int&> c(true, m);
  160. EXPECT_TRUE(get<0>(c));
  161. EXPECT_EQ(&m, &(get<1>(c)));
  162. }
  163. // Tests tuple's copy constructor.
  164. TEST(TupleConstructorTest, CopyConstructor) {
  165. tuple<double, bool> a(0.0, true);
  166. tuple<double, bool> b(a);
  167. EXPECT_DOUBLE_EQ(0.0, get<0>(b));
  168. EXPECT_TRUE(get<1>(b));
  169. }
  170. // Tests constructing a tuple from another tuple that has a compatible
  171. // but different type.
  172. TEST(TupleConstructorTest, ConstructsFromDifferentTupleType) {
  173. tuple<int, int, char> a(0, 1, 'a');
  174. tuple<double, long, int> b(a);
  175. EXPECT_DOUBLE_EQ(0.0, get<0>(b));
  176. EXPECT_EQ(1, get<1>(b));
  177. EXPECT_EQ('a', get<2>(b));
  178. }
  179. // Tests constructing a 2-tuple from an std::pair.
  180. TEST(TupleConstructorTest, ConstructsFromPair) {
  181. ::std::pair<int, char> a(1, 'a');
  182. tuple<int, char> b(a);
  183. tuple<int, const char&> c(a);
  184. }
  185. // Tests assigning a tuple to another tuple with the same type.
  186. TEST(TupleAssignmentTest, AssignsToSameTupleType) {
  187. const tuple<int, long> a(5, 7L);
  188. tuple<int, long> b;
  189. b = a;
  190. EXPECT_EQ(5, get<0>(b));
  191. EXPECT_EQ(7L, get<1>(b));
  192. }
  193. // Tests assigning a tuple to another tuple with a different but
  194. // compatible type.
  195. TEST(TupleAssignmentTest, AssignsToDifferentTupleType) {
  196. const tuple<int, long, bool> a(1, 7L, true);
  197. tuple<long, int, bool> b;
  198. b = a;
  199. EXPECT_EQ(1L, get<0>(b));
  200. EXPECT_EQ(7, get<1>(b));
  201. EXPECT_TRUE(get<2>(b));
  202. }
  203. // Tests assigning an std::pair to a 2-tuple.
  204. TEST(TupleAssignmentTest, AssignsFromPair) {
  205. const ::std::pair<int, bool> a(5, true);
  206. tuple<int, bool> b;
  207. b = a;
  208. EXPECT_EQ(5, get<0>(b));
  209. EXPECT_TRUE(get<1>(b));
  210. tuple<long, bool> c;
  211. c = a;
  212. EXPECT_EQ(5L, get<0>(c));
  213. EXPECT_TRUE(get<1>(c));
  214. }
  215. // A fixture for testing big tuples.
  216. class BigTupleTest : public testing::Test {
  217. protected:
  218. typedef tuple<int, int, int, int, int, int, int, int, int, int> BigTuple;
  219. BigTupleTest() :
  220. a_(1, 0, 0, 0, 0, 0, 0, 0, 0, 2),
  221. b_(1, 0, 0, 0, 0, 0, 0, 0, 0, 3) {}
  222. BigTuple a_, b_;
  223. };
  224. // Tests constructing big tuples.
  225. TEST_F(BigTupleTest, Construction) {
  226. BigTuple a;
  227. BigTuple b(b_);
  228. }
  229. // Tests that get<N>(t) returns the N-th (0-based) field of tuple t.
  230. TEST_F(BigTupleTest, get) {
  231. EXPECT_EQ(1, get<0>(a_));
  232. EXPECT_EQ(2, get<9>(a_));
  233. // Tests that get() works on a const tuple too.
  234. const BigTuple a(a_);
  235. EXPECT_EQ(1, get<0>(a));
  236. EXPECT_EQ(2, get<9>(a));
  237. }
  238. // Tests comparing big tuples.
  239. TEST_F(BigTupleTest, Comparisons) {
  240. EXPECT_TRUE(a_ == a_);
  241. EXPECT_FALSE(a_ != a_);
  242. EXPECT_TRUE(a_ != b_);
  243. EXPECT_FALSE(a_ == b_);
  244. }
  245. TEST(MakeTupleTest, WorksForScalarTypes) {
  246. tuple<bool, int> a;
  247. a = make_tuple(true, 5);
  248. EXPECT_TRUE(get<0>(a));
  249. EXPECT_EQ(5, get<1>(a));
  250. tuple<char, int, long> b;
  251. b = make_tuple('a', 'b', 5);
  252. EXPECT_EQ('a', get<0>(b));
  253. EXPECT_EQ('b', get<1>(b));
  254. EXPECT_EQ(5, get<2>(b));
  255. }
  256. TEST(MakeTupleTest, WorksForPointers) {
  257. int a[] = { 1, 2, 3, 4 };
  258. const char* const str = "hi";
  259. int* const p = a;
  260. tuple<const char*, int*> t;
  261. t = make_tuple(str, p);
  262. EXPECT_EQ(str, get<0>(t));
  263. EXPECT_EQ(p, get<1>(t));
  264. }
  265. } // namespace