googletest-tuple-test.cc 9.0 KB

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