1 // 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.
5 #include "base/tuple.h"
7 #include "base/compiler_specific.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 void DoAdd(int a
, int b
, int c
, int* res
) {
18 void DoAdd(int a
, int b
, int c
, int d
, int* res
) {
25 void DoAdd(int a
, int b
, int c
, int d
, int e
, int* res
) {
26 *res
= a
+ b
+ c
+ d
+ e
;
32 TEST(TupleTest
, Basic
) {
33 Tuple0 t0 ALLOW_UNUSED
= MakeTuple();
35 Tuple2
<int, const char*> t2
= MakeTuple(1, static_cast<const char*>("wee"));
36 Tuple3
<int, int, int> t3(1, 2, 3);
37 Tuple4
<int, int, int, int*> t4(1, 2, 3, &t1
.a
);
38 Tuple5
<int, int, int, int, int*> t5(1, 2, 3, 4, &t4
.a
);
39 Tuple6
<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &t4
.a
);
60 DispatchToFunction(&DoAdd
, t4
);
64 DispatchToFunction(&DoAdd
, MakeTuple(9, 8, 7, &res
));
69 DispatchToMethod(&addy
, &Addy::DoAdd
, t5
);
74 DispatchToMethod(&addz
, &Addz::DoAdd
, t6
);
81 CopyLogger() { ++TimesConstructed
; }
82 CopyLogger(const CopyLogger
& tocopy
) { ++TimesConstructed
; ++TimesCopied
; }
85 static int TimesCopied
;
86 static int TimesConstructed
;
89 void SomeLoggerMethRef(const CopyLogger
& logy
, const CopyLogger
* ptr
, bool* b
) {
93 void SomeLoggerMethCopy(CopyLogger logy
, const CopyLogger
* ptr
, bool* b
) {
97 int CopyLogger::TimesCopied
= 0;
98 int CopyLogger::TimesConstructed
= 0;
102 TEST(TupleTest
, Copying
) {
104 EXPECT_EQ(0, CopyLogger::TimesCopied
);
105 EXPECT_EQ(1, CopyLogger::TimesConstructed
);
109 // Creating the tuple should copy the class to store internally in the tuple.
110 Tuple3
<CopyLogger
, CopyLogger
*, bool*> tuple(logger
, &logger
, &res
);
112 EXPECT_EQ(2, CopyLogger::TimesConstructed
);
113 EXPECT_EQ(1, CopyLogger::TimesCopied
);
115 // Our internal Logger and the one passed to the function should be the same.
117 DispatchToFunction(&SomeLoggerMethRef
, tuple
);
119 EXPECT_EQ(2, CopyLogger::TimesConstructed
);
120 EXPECT_EQ(1, CopyLogger::TimesCopied
);
122 // Now they should be different, since the function call will make a copy.
124 DispatchToFunction(&SomeLoggerMethCopy
, tuple
);
126 EXPECT_EQ(3, CopyLogger::TimesConstructed
);
127 EXPECT_EQ(2, CopyLogger::TimesCopied
);