Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2006-2008 The Chromium Authors |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/tuple.h" |
[email protected] | 40350b1 | 2010-03-30 17:29:27 | [diff] [blame] | 6 | |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 9 | namespace base { |
| 10 | |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 11 | namespace { |
| 12 | |
| 13 | void DoAdd(int a, int b, int c, int* res) { |
| 14 | *res = a + b + c; |
| 15 | } |
| 16 | |
| 17 | struct Addy { |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 18 | Addy() = default; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 19 | void DoAdd(int a, int b, int c, int d, int* res) { *res = a + b + c + d; } |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 20 | }; |
| 21 | |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 22 | struct Addz { |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 23 | Addz() = default; |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 24 | void DoAdd(int a, int b, int c, int d, int e, int* res) { |
| 25 | *res = a + b + c + d + e; |
| 26 | } |
| 27 | }; |
| 28 | |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 29 | } // namespace |
| 30 | |
| 31 | TEST(TupleTest, Basic) { |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 32 | std::tuple<int> t1(1); |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 33 | std::tuple<int, int, int, int*> t4(1, 2, 3, &std::get<0>(t1)); |
| 34 | std::tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &std::get<0>(t4)); |
| 35 | std::tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &std::get<0>(t4)); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 36 | |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 37 | EXPECT_EQ(1, std::get<0>(t1)); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 38 | DispatchToFunction(&DoAdd, t4); |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 39 | EXPECT_EQ(6, std::get<0>(t1)); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 40 | |
| 41 | int res = 0; |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 42 | DispatchToFunction(&DoAdd, std::make_tuple(9, 8, 7, &res)); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 43 | EXPECT_EQ(24, res); |
| 44 | |
| 45 | Addy addy; |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 46 | EXPECT_EQ(1, std::get<0>(t4)); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 47 | DispatchToMethod(&addy, &Addy::DoAdd, t5); |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 48 | EXPECT_EQ(10, std::get<0>(t4)); |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 49 | |
| 50 | Addz addz; |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 51 | EXPECT_EQ(10, std::get<0>(t4)); |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 52 | DispatchToMethod(&addz, &Addz::DoAdd, t6); |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 53 | EXPECT_EQ(15, std::get<0>(t4)); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | namespace { |
| 57 | |
| 58 | struct CopyLogger { |
| 59 | CopyLogger() { ++TimesConstructed; } |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 60 | CopyLogger(const CopyLogger& tocopy) { |
| 61 | ++TimesConstructed; |
| 62 | ++TimesCopied; |
| 63 | } |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 64 | ~CopyLogger() = default; |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 65 | |
| 66 | static int TimesCopied; |
| 67 | static int TimesConstructed; |
| 68 | }; |
| 69 | |
| 70 | void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) { |
| 71 | *b = &logy == ptr; |
| 72 | } |
| 73 | |
| 74 | void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) { |
| 75 | *b = &logy == ptr; |
| 76 | } |
| 77 | |
| 78 | int CopyLogger::TimesCopied = 0; |
| 79 | int CopyLogger::TimesConstructed = 0; |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | TEST(TupleTest, Copying) { |
| 84 | CopyLogger logger; |
| 85 | EXPECT_EQ(0, CopyLogger::TimesCopied); |
| 86 | EXPECT_EQ(1, CopyLogger::TimesConstructed); |
| 87 | |
| 88 | bool res = false; |
| 89 | |
| 90 | // Creating the tuple should copy the class to store internally in the tuple. |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 91 | std::tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res); |
Peter Kasting | 1b368e7 | 2018-01-13 01:49:52 | [diff] [blame] | 92 | std::get<CopyLogger*>(tuple) = &std::get<CopyLogger>(tuple); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 93 | EXPECT_EQ(2, CopyLogger::TimesConstructed); |
| 94 | EXPECT_EQ(1, CopyLogger::TimesCopied); |
| 95 | |
| 96 | // Our internal Logger and the one passed to the function should be the same. |
| 97 | res = false; |
| 98 | DispatchToFunction(&SomeLoggerMethRef, tuple); |
| 99 | EXPECT_TRUE(res); |
| 100 | EXPECT_EQ(2, CopyLogger::TimesConstructed); |
| 101 | EXPECT_EQ(1, CopyLogger::TimesCopied); |
| 102 | |
| 103 | // Now they should be different, since the function call will make a copy. |
| 104 | res = false; |
| 105 | DispatchToFunction(&SomeLoggerMethCopy, tuple); |
| 106 | EXPECT_FALSE(res); |
| 107 | EXPECT_EQ(3, CopyLogger::TimesConstructed); |
| 108 | EXPECT_EQ(2, CopyLogger::TimesCopied); |
| 109 | } |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 110 | |
| 111 | } // namespace base |