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.
5 #include "base/tuple.h"
7 #include "testing/gtest/include/gtest/gtest.h"
13 void DoAdd(int a, int b, int c, int* res) {
19 void DoAdd(int a, int b, int c, int d, int* res) {
26 void DoAdd(int a, int b, int c, int d, int e, int* res) {
27 *res = a + b + c + d + e;
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));
39 EXPECT_EQ(1, std::get<0>(t1));
40 DispatchToFunction(&DoAdd, t4);
41 EXPECT_EQ(6, std::get<0>(t1));
44 DispatchToFunction(&DoAdd, std::make_tuple(9, 8, 7, &res));
48 EXPECT_EQ(1, std::get<0>(t4));
49 DispatchToMethod(&addy, &Addy::DoAdd, t5);
50 EXPECT_EQ(10, std::get<0>(t4));
53 EXPECT_EQ(10, std::get<0>(t4));
54 DispatchToMethod(&addz, &Addz::DoAdd, t6);
55 EXPECT_EQ(15, std::get<0>(t4));
61 CopyLogger() { ++TimesConstructed; }
62 CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
63 ~CopyLogger() = default;
65 static int TimesCopied;
66 static int TimesConstructed;
69 void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
73 void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
77 int CopyLogger::TimesCopied = 0;
78 int CopyLogger::TimesConstructed = 0;
82 TEST(TupleTest, Copying) {
84 EXPECT_EQ(0, CopyLogger::TimesCopied);
85 EXPECT_EQ(1, CopyLogger::TimesConstructed);
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);
95 // Our internal Logger and the one passed to the function should be the same.
97 DispatchToFunction(&SomeLoggerMethRef, tuple);
99 EXPECT_EQ(2, CopyLogger::TimesConstructed);
100 EXPECT_EQ(1, CopyLogger::TimesCopied);
102 // Now they should be different, since the function call will make a copy.
104 DispatchToFunction(&SomeLoggerMethCopy, tuple);
106 EXPECT_EQ(3, CopyLogger::TimesConstructed);
107 EXPECT_EQ(2, CopyLogger::TimesCopied);