blob: 3073fd879f5ab17062b44aaec1b85d998e1ec0bd [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2006-2008 The Chromium Authors
[email protected]302bdc132008-08-25 13:42:072// 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]40350b12010-03-30 17:29:276
[email protected]302bdc132008-08-25 13:42:077#include "testing/gtest/include/gtest/gtest.h"
8
brettwd5ca2bc2015-05-29 22:15:479namespace base {
10
[email protected]302bdc132008-08-25 13:42:0711namespace {
12
13void DoAdd(int a, int b, int c, int* res) {
14 *res = a + b + c;
15}
16
17struct Addy {
Chris Watkinsbb7211c2017-11-29 07:16:3818 Addy() = default;
Peter Kasting134ef9af2024-12-28 02:30:0919 void DoAdd(int a, int b, int c, int d, int* res) { *res = a + b + c + d; }
[email protected]302bdc132008-08-25 13:42:0720};
21
[email protected]8a2820a2008-10-09 21:58:0522struct Addz {
Chris Watkinsbb7211c2017-11-29 07:16:3823 Addz() = default;
[email protected]8a2820a2008-10-09 21:58:0524 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]302bdc132008-08-25 13:42:0729} // namespace
30
31TEST(TupleTest, Basic) {
tzik1068f1be2016-06-03 07:25:2032 std::tuple<int> t1(1);
tzik1068f1be2016-06-03 07:25:2033 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]302bdc132008-08-25 13:42:0736
tzik1068f1be2016-06-03 07:25:2037 EXPECT_EQ(1, std::get<0>(t1));
[email protected]302bdc132008-08-25 13:42:0738 DispatchToFunction(&DoAdd, t4);
tzik1068f1be2016-06-03 07:25:2039 EXPECT_EQ(6, std::get<0>(t1));
[email protected]302bdc132008-08-25 13:42:0740
41 int res = 0;
tzik1068f1be2016-06-03 07:25:2042 DispatchToFunction(&DoAdd, std::make_tuple(9, 8, 7, &res));
[email protected]302bdc132008-08-25 13:42:0743 EXPECT_EQ(24, res);
44
45 Addy addy;
tzik1068f1be2016-06-03 07:25:2046 EXPECT_EQ(1, std::get<0>(t4));
[email protected]302bdc132008-08-25 13:42:0747 DispatchToMethod(&addy, &Addy::DoAdd, t5);
tzik1068f1be2016-06-03 07:25:2048 EXPECT_EQ(10, std::get<0>(t4));
[email protected]8a2820a2008-10-09 21:58:0549
50 Addz addz;
tzik1068f1be2016-06-03 07:25:2051 EXPECT_EQ(10, std::get<0>(t4));
[email protected]8a2820a2008-10-09 21:58:0552 DispatchToMethod(&addz, &Addz::DoAdd, t6);
tzik1068f1be2016-06-03 07:25:2053 EXPECT_EQ(15, std::get<0>(t4));
[email protected]302bdc132008-08-25 13:42:0754}
55
56namespace {
57
58struct CopyLogger {
59 CopyLogger() { ++TimesConstructed; }
Peter Kasting134ef9af2024-12-28 02:30:0960 CopyLogger(const CopyLogger& tocopy) {
61 ++TimesConstructed;
62 ++TimesCopied;
63 }
Chris Watkinsbb7211c2017-11-29 07:16:3864 ~CopyLogger() = default;
[email protected]302bdc132008-08-25 13:42:0765
66 static int TimesCopied;
67 static int TimesConstructed;
68};
69
70void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
71 *b = &logy == ptr;
72}
73
74void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
75 *b = &logy == ptr;
76}
77
78int CopyLogger::TimesCopied = 0;
79int CopyLogger::TimesConstructed = 0;
80
81} // namespace
82
83TEST(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.
tzik1068f1be2016-06-03 07:25:2091 std::tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
Peter Kasting1b368e72018-01-13 01:49:5292 std::get<CopyLogger*>(tuple) = &std::get<CopyLogger>(tuple);
[email protected]302bdc132008-08-25 13:42:0793 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}
brettwd5ca2bc2015-05-29 22:15:47110
111} // namespace base