1 // Copyright 2006-2008 The Chromium Authors
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"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace base {
10
11 namespace {
12
DoAdd(int a,int b,int c,int * res)13 void DoAdd(int a, int b, int c, int* res) {
14 *res = a + b + c;
15 }
16
17 struct Addy {
18 Addy() = default;
DoAddbase::__anon080dd7a80111::Addy19 void DoAdd(int a, int b, int c, int d, int* res) {
20 *res = a + b + c + d;
21 }
22 };
23
24 struct Addz {
25 Addz() = default;
DoAddbase::__anon080dd7a80111::Addz26 void DoAdd(int a, int b, int c, int d, int e, int* res) {
27 *res = a + b + c + d + e;
28 }
29 };
30
31 } // namespace
32
TEST(TupleTest,Basic)33 TEST(TupleTest, Basic) {
34 std::tuple<int> t1(1);
35 std::tuple<int, int, int, int*> t4(1, 2, 3, &std::get<0>(t1));
36 std::tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &std::get<0>(t4));
37 std::tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &std::get<0>(t4));
38
39 EXPECT_EQ(1, std::get<0>(t1));
40 DispatchToFunction(&DoAdd, t4);
41 EXPECT_EQ(6, std::get<0>(t1));
42
43 int res = 0;
44 DispatchToFunction(&DoAdd, std::make_tuple(9, 8, 7, &res));
45 EXPECT_EQ(24, res);
46
47 Addy addy;
48 EXPECT_EQ(1, std::get<0>(t4));
49 DispatchToMethod(&addy, &Addy::DoAdd, t5);
50 EXPECT_EQ(10, std::get<0>(t4));
51
52 Addz addz;
53 EXPECT_EQ(10, std::get<0>(t4));
54 DispatchToMethod(&addz, &Addz::DoAdd, t6);
55 EXPECT_EQ(15, std::get<0>(t4));
56 }
57
58 namespace {
59
60 struct CopyLogger {
CopyLoggerbase::__anon080dd7a80211::CopyLogger61 CopyLogger() { ++TimesConstructed; }
CopyLoggerbase::__anon080dd7a80211::CopyLogger62 CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
63 ~CopyLogger() = default;
64
65 static int TimesCopied;
66 static int TimesConstructed;
67 };
68
SomeLoggerMethRef(const CopyLogger & logy,const CopyLogger * ptr,bool * b)69 void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
70 *b = &logy == ptr;
71 }
72
SomeLoggerMethCopy(CopyLogger logy,const CopyLogger * ptr,bool * b)73 void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
74 *b = &logy == ptr;
75 }
76
77 int CopyLogger::TimesCopied = 0;
78 int CopyLogger::TimesConstructed = 0;
79
80 } // namespace
81
TEST(TupleTest,Copying)82 TEST(TupleTest, Copying) {
83 CopyLogger logger;
84 EXPECT_EQ(0, CopyLogger::TimesCopied);
85 EXPECT_EQ(1, CopyLogger::TimesConstructed);
86
87 bool res = false;
88
89 // Creating the tuple should copy the class to store internally in the tuple.
90 std::tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
91 std::get<CopyLogger*>(tuple) = &std::get<CopyLogger>(tuple);
92 EXPECT_EQ(2, CopyLogger::TimesConstructed);
93 EXPECT_EQ(1, CopyLogger::TimesCopied);
94
95 // Our internal Logger and the one passed to the function should be the same.
96 res = false;
97 DispatchToFunction(&SomeLoggerMethRef, tuple);
98 EXPECT_TRUE(res);
99 EXPECT_EQ(2, CopyLogger::TimesConstructed);
100 EXPECT_EQ(1, CopyLogger::TimesCopied);
101
102 // Now they should be different, since the function call will make a copy.
103 res = false;
104 DispatchToFunction(&SomeLoggerMethCopy, tuple);
105 EXPECT_FALSE(res);
106 EXPECT_EQ(3, CopyLogger::TimesConstructed);
107 EXPECT_EQ(2, CopyLogger::TimesCopied);
108 }
109
110 } // namespace base
111