tuple_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/tuple.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace base {
  7. namespace {
  8. void DoAdd(int a, int b, int c, int* res) {
  9. *res = a + b + c;
  10. }
  11. struct Addy {
  12. Addy() = default;
  13. void DoAdd(int a, int b, int c, int d, int* res) {
  14. *res = a + b + c + d;
  15. }
  16. };
  17. struct Addz {
  18. Addz() = default;
  19. void DoAdd(int a, int b, int c, int d, int e, int* res) {
  20. *res = a + b + c + d + e;
  21. }
  22. };
  23. } // namespace
  24. TEST(TupleTest, Basic) {
  25. std::tuple<int> t1(1);
  26. std::tuple<int, int, int, int*> t4(1, 2, 3, &std::get<0>(t1));
  27. std::tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &std::get<0>(t4));
  28. std::tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &std::get<0>(t4));
  29. EXPECT_EQ(1, std::get<0>(t1));
  30. DispatchToFunction(&DoAdd, t4);
  31. EXPECT_EQ(6, std::get<0>(t1));
  32. int res = 0;
  33. DispatchToFunction(&DoAdd, std::make_tuple(9, 8, 7, &res));
  34. EXPECT_EQ(24, res);
  35. Addy addy;
  36. EXPECT_EQ(1, std::get<0>(t4));
  37. DispatchToMethod(&addy, &Addy::DoAdd, t5);
  38. EXPECT_EQ(10, std::get<0>(t4));
  39. Addz addz;
  40. EXPECT_EQ(10, std::get<0>(t4));
  41. DispatchToMethod(&addz, &Addz::DoAdd, t6);
  42. EXPECT_EQ(15, std::get<0>(t4));
  43. }
  44. namespace {
  45. struct CopyLogger {
  46. CopyLogger() { ++TimesConstructed; }
  47. CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
  48. ~CopyLogger() = default;
  49. static int TimesCopied;
  50. static int TimesConstructed;
  51. };
  52. void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
  53. *b = &logy == ptr;
  54. }
  55. void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
  56. *b = &logy == ptr;
  57. }
  58. int CopyLogger::TimesCopied = 0;
  59. int CopyLogger::TimesConstructed = 0;
  60. } // namespace
  61. TEST(TupleTest, Copying) {
  62. CopyLogger logger;
  63. EXPECT_EQ(0, CopyLogger::TimesCopied);
  64. EXPECT_EQ(1, CopyLogger::TimesConstructed);
  65. bool res = false;
  66. // Creating the tuple should copy the class to store internally in the tuple.
  67. std::tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
  68. std::get<CopyLogger*>(tuple) = &std::get<CopyLogger>(tuple);
  69. EXPECT_EQ(2, CopyLogger::TimesConstructed);
  70. EXPECT_EQ(1, CopyLogger::TimesCopied);
  71. // Our internal Logger and the one passed to the function should be the same.
  72. res = false;
  73. DispatchToFunction(&SomeLoggerMethRef, tuple);
  74. EXPECT_TRUE(res);
  75. EXPECT_EQ(2, CopyLogger::TimesConstructed);
  76. EXPECT_EQ(1, CopyLogger::TimesCopied);
  77. // Now they should be different, since the function call will make a copy.
  78. res = false;
  79. DispatchToFunction(&SomeLoggerMethCopy, tuple);
  80. EXPECT_FALSE(res);
  81. EXPECT_EQ(3, CopyLogger::TimesConstructed);
  82. EXPECT_EQ(2, CopyLogger::TimesCopied);
  83. }
  84. } // namespace base