blob: 5b43affc600f5f7d8741f1023c8b8e1b19486b1c [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
10namespace {
11
12void DoAdd(int a, int b, int c, int* res) {
13 *res = a + b + c;
14}
15
16struct Addy {
17 Addy() { }
18 void DoAdd(int a, int b, int c, int d, int* res) {
19 *res = a + b + c + d;
20 }
21};
22
ojan@google.com38355092008-10-10 06:58:05 +090023struct Addz {
24 Addz() { }
25 void DoAdd(int a, int b, int c, int d, int e, int* res) {
26 *res = a + b + c + d + e;
27 }
28};
29
deanm@google.com96aac0a2008-08-25 22:42:07 +090030} // namespace
31
32TEST(TupleTest, Basic) {
Avi Drissman216a3282014-12-23 03:01:32 +090033 Tuple<> t0 = MakeTuple();
pkastingd36037a2014-10-17 08:49:24 +090034 ALLOW_UNUSED_LOCAL(t0);
Avi Drissman216a3282014-12-23 03:01:32 +090035 Tuple<int> t1(1);
36 Tuple<int, const char*> t2 = MakeTuple(1, static_cast<const char*>("wee"));
37 Tuple<int, int, int> t3(1, 2, 3);
38 Tuple<int, int, int, int*> t4(1, 2, 3, &get<0>(t1));
39 Tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &get<0>(t4));
40 Tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &get<0>(t4));
deanm@google.com96aac0a2008-08-25 22:42:07 +090041
Avi Drissman216a3282014-12-23 03:01:32 +090042 EXPECT_EQ(1, get<0>(t1));
43 EXPECT_EQ(1, get<0>(t2));
44 EXPECT_EQ(1, get<0>(t3));
45 EXPECT_EQ(2, get<1>(t3));
46 EXPECT_EQ(3, get<2>(t3));
47 EXPECT_EQ(1, get<0>(t4));
48 EXPECT_EQ(2, get<1>(t4));
49 EXPECT_EQ(3, get<2>(t4));
50 EXPECT_EQ(1, get<0>(t5));
51 EXPECT_EQ(2, get<1>(t5));
52 EXPECT_EQ(3, get<2>(t5));
53 EXPECT_EQ(4, get<3>(t5));
54 EXPECT_EQ(1, get<0>(t6));
55 EXPECT_EQ(2, get<1>(t6));
56 EXPECT_EQ(3, get<2>(t6));
57 EXPECT_EQ(4, get<3>(t6));
58 EXPECT_EQ(5, get<4>(t6));
deanm@google.com96aac0a2008-08-25 22:42:07 +090059
Avi Drissman216a3282014-12-23 03:01:32 +090060 EXPECT_EQ(1, get<0>(t1));
deanm@google.com96aac0a2008-08-25 22:42:07 +090061 DispatchToFunction(&DoAdd, t4);
Avi Drissman216a3282014-12-23 03:01:32 +090062 EXPECT_EQ(6, get<0>(t1));
deanm@google.com96aac0a2008-08-25 22:42:07 +090063
64 int res = 0;
65 DispatchToFunction(&DoAdd, MakeTuple(9, 8, 7, &res));
66 EXPECT_EQ(24, res);
67
68 Addy addy;
Avi Drissman216a3282014-12-23 03:01:32 +090069 EXPECT_EQ(1, get<0>(t4));
deanm@google.com96aac0a2008-08-25 22:42:07 +090070 DispatchToMethod(&addy, &Addy::DoAdd, t5);
Avi Drissman216a3282014-12-23 03:01:32 +090071 EXPECT_EQ(10, get<0>(t4));
ojan@google.com38355092008-10-10 06:58:05 +090072
73 Addz addz;
Avi Drissman216a3282014-12-23 03:01:32 +090074 EXPECT_EQ(10, get<0>(t4));
ojan@google.com38355092008-10-10 06:58:05 +090075 DispatchToMethod(&addz, &Addz::DoAdd, t6);
Avi Drissman216a3282014-12-23 03:01:32 +090076 EXPECT_EQ(15, get<0>(t4));
deanm@google.com96aac0a2008-08-25 22:42:07 +090077}
78
79namespace {
80
81struct CopyLogger {
82 CopyLogger() { ++TimesConstructed; }
83 CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
84 ~CopyLogger() { }
85
86 static int TimesCopied;
87 static int TimesConstructed;
88};
89
90void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
91 *b = &logy == ptr;
92}
93
94void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
95 *b = &logy == ptr;
96}
97
98int CopyLogger::TimesCopied = 0;
99int CopyLogger::TimesConstructed = 0;
100
101} // namespace
102
103TEST(TupleTest, Copying) {
104 CopyLogger logger;
105 EXPECT_EQ(0, CopyLogger::TimesCopied);
106 EXPECT_EQ(1, CopyLogger::TimesConstructed);
107
108 bool res = false;
109
110 // Creating the tuple should copy the class to store internally in the tuple.
Avi Drissman216a3282014-12-23 03:01:32 +0900111 Tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
112 get<1>(tuple) = &get<0>(tuple);
deanm@google.com96aac0a2008-08-25 22:42:07 +0900113 EXPECT_EQ(2, CopyLogger::TimesConstructed);
114 EXPECT_EQ(1, CopyLogger::TimesCopied);
115
116 // Our internal Logger and the one passed to the function should be the same.
117 res = false;
118 DispatchToFunction(&SomeLoggerMethRef, tuple);
119 EXPECT_TRUE(res);
120 EXPECT_EQ(2, CopyLogger::TimesConstructed);
121 EXPECT_EQ(1, CopyLogger::TimesCopied);
122
123 // Now they should be different, since the function call will make a copy.
124 res = false;
125 DispatchToFunction(&SomeLoggerMethCopy, tuple);
126 EXPECT_FALSE(res);
127 EXPECT_EQ(3, CopyLogger::TimesConstructed);
128 EXPECT_EQ(2, CopyLogger::TimesCopied);
129}