blob: 55a91392353076d49f7f896dd477c8e03e2b4ad9 [file] [log] [blame]
deanm@google.com96aac0a2008-08-25 22:42:07 +09001// 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
5#include "base/tuple.h"
evan@chromium.org2003be92010-03-31 02:29:27 +09006
7#include "base/compiler_specific.h"
deanm@google.com96aac0a2008-08-25 22:42:07 +09008#include "testing/gtest/include/gtest/gtest.h"
9
brettw79a90902015-05-30 07:15:47 +090010namespace base {
11
deanm@google.com96aac0a2008-08-25 22:42:07 +090012namespace {
13
14void DoAdd(int a, int b, int c, int* res) {
15 *res = a + b + c;
16}
17
18struct Addy {
19 Addy() { }
20 void DoAdd(int a, int b, int c, int d, int* res) {
21 *res = a + b + c + d;
22 }
23};
24
ojan@google.com38355092008-10-10 06:58:05 +090025struct Addz {
26 Addz() { }
27 void DoAdd(int a, int b, int c, int d, int e, int* res) {
28 *res = a + b + c + d + e;
29 }
30};
31
deanm@google.com96aac0a2008-08-25 22:42:07 +090032} // namespace
33
34TEST(TupleTest, Basic) {
brettw79a90902015-05-30 07:15:47 +090035 base::Tuple<> t0 = base::MakeTuple();
pkastingd36037a2014-10-17 08:49:24 +090036 ALLOW_UNUSED_LOCAL(t0);
brettw79a90902015-05-30 07:15:47 +090037 base::Tuple<int> t1(1);
38 base::Tuple<int, const char*> t2 =
39 base::MakeTuple(1, static_cast<const char*>("wee"));
40 base::Tuple<int, int, int> t3(1, 2, 3);
41 base::Tuple<int, int, int, int*> t4(1, 2, 3, &get<0>(t1));
42 base::Tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &get<0>(t4));
43 base::Tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &get<0>(t4));
deanm@google.com96aac0a2008-08-25 22:42:07 +090044
Avi Drissman216a3282014-12-23 03:01:32 +090045 EXPECT_EQ(1, get<0>(t1));
46 EXPECT_EQ(1, get<0>(t2));
47 EXPECT_EQ(1, get<0>(t3));
48 EXPECT_EQ(2, get<1>(t3));
49 EXPECT_EQ(3, get<2>(t3));
50 EXPECT_EQ(1, get<0>(t4));
51 EXPECT_EQ(2, get<1>(t4));
52 EXPECT_EQ(3, get<2>(t4));
53 EXPECT_EQ(1, get<0>(t5));
54 EXPECT_EQ(2, get<1>(t5));
55 EXPECT_EQ(3, get<2>(t5));
56 EXPECT_EQ(4, get<3>(t5));
57 EXPECT_EQ(1, get<0>(t6));
58 EXPECT_EQ(2, get<1>(t6));
59 EXPECT_EQ(3, get<2>(t6));
60 EXPECT_EQ(4, get<3>(t6));
61 EXPECT_EQ(5, get<4>(t6));
deanm@google.com96aac0a2008-08-25 22:42:07 +090062
Avi Drissman216a3282014-12-23 03:01:32 +090063 EXPECT_EQ(1, get<0>(t1));
deanm@google.com96aac0a2008-08-25 22:42:07 +090064 DispatchToFunction(&DoAdd, t4);
Avi Drissman216a3282014-12-23 03:01:32 +090065 EXPECT_EQ(6, get<0>(t1));
deanm@google.com96aac0a2008-08-25 22:42:07 +090066
67 int res = 0;
brettw79a90902015-05-30 07:15:47 +090068 DispatchToFunction(&DoAdd, base::MakeTuple(9, 8, 7, &res));
deanm@google.com96aac0a2008-08-25 22:42:07 +090069 EXPECT_EQ(24, res);
70
71 Addy addy;
Avi Drissman216a3282014-12-23 03:01:32 +090072 EXPECT_EQ(1, get<0>(t4));
deanm@google.com96aac0a2008-08-25 22:42:07 +090073 DispatchToMethod(&addy, &Addy::DoAdd, t5);
Avi Drissman216a3282014-12-23 03:01:32 +090074 EXPECT_EQ(10, get<0>(t4));
ojan@google.com38355092008-10-10 06:58:05 +090075
76 Addz addz;
Avi Drissman216a3282014-12-23 03:01:32 +090077 EXPECT_EQ(10, get<0>(t4));
ojan@google.com38355092008-10-10 06:58:05 +090078 DispatchToMethod(&addz, &Addz::DoAdd, t6);
Avi Drissman216a3282014-12-23 03:01:32 +090079 EXPECT_EQ(15, get<0>(t4));
deanm@google.com96aac0a2008-08-25 22:42:07 +090080}
81
82namespace {
83
84struct CopyLogger {
85 CopyLogger() { ++TimesConstructed; }
86 CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
87 ~CopyLogger() { }
88
89 static int TimesCopied;
90 static int TimesConstructed;
91};
92
93void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
94 *b = &logy == ptr;
95}
96
97void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
98 *b = &logy == ptr;
99}
100
101int CopyLogger::TimesCopied = 0;
102int CopyLogger::TimesConstructed = 0;
103
104} // namespace
105
106TEST(TupleTest, Copying) {
107 CopyLogger logger;
108 EXPECT_EQ(0, CopyLogger::TimesCopied);
109 EXPECT_EQ(1, CopyLogger::TimesConstructed);
110
111 bool res = false;
112
113 // Creating the tuple should copy the class to store internally in the tuple.
brettw79a90902015-05-30 07:15:47 +0900114 base::Tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
Avi Drissman216a3282014-12-23 03:01:32 +0900115 get<1>(tuple) = &get<0>(tuple);
deanm@google.com96aac0a2008-08-25 22:42:07 +0900116 EXPECT_EQ(2, CopyLogger::TimesConstructed);
117 EXPECT_EQ(1, CopyLogger::TimesCopied);
118
119 // Our internal Logger and the one passed to the function should be the same.
120 res = false;
121 DispatchToFunction(&SomeLoggerMethRef, tuple);
122 EXPECT_TRUE(res);
123 EXPECT_EQ(2, CopyLogger::TimesConstructed);
124 EXPECT_EQ(1, CopyLogger::TimesCopied);
125
126 // Now they should be different, since the function call will make a copy.
127 res = false;
128 DispatchToFunction(&SomeLoggerMethCopy, tuple);
129 EXPECT_FALSE(res);
130 EXPECT_EQ(3, CopyLogger::TimesConstructed);
131 EXPECT_EQ(2, CopyLogger::TimesCopied);
132}
brettw79a90902015-05-30 07:15:47 +0900133
134} // namespace base