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"
14 void DoAdd(int a, int b, int c, int* res) {
20 void DoAdd(int a, int b, int c, int d, int* res) {
27 void DoAdd(int a, int b, int c, int d, int e, int* res) {
28 *res = a + b + c + d + e;
34 TEST(TupleTest, Basic) {
35 std::tuple<> t0 = std::make_tuple();
36 ALLOW_UNUSED_LOCAL(t0);
37 std::tuple<int> t1(1);
38 std::tuple<int, const char*> t2 =
39 std::make_tuple(1, static_cast<const char*>("wee"));
40 ALLOW_UNUSED_LOCAL(t2);
41 std::tuple<int, int, int> t3(1, 2, 3);
42 ALLOW_UNUSED_LOCAL(t3);
43 std::tuple<int, int, int, int*> t4(1, 2, 3, &std::get<0>(t1));
44 std::tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &std::get<0>(t4));
45 std::tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &std::get<0>(t4));
47 EXPECT_EQ(1, std::get<0>(t1));
48 DispatchToFunction(&DoAdd, t4);
49 EXPECT_EQ(6, std::get<0>(t1));
52 DispatchToFunction(&DoAdd, std::make_tuple(9, 8, 7, &res));
56 EXPECT_EQ(1, std::get<0>(t4));
57 DispatchToMethod(&addy, &Addy::DoAdd, t5);
58 EXPECT_EQ(10, std::get<0>(t4));
61 EXPECT_EQ(10, std::get<0>(t4));
62 DispatchToMethod(&addz, &Addz::DoAdd, t6);
63 EXPECT_EQ(15, std::get<0>(t4));
69 CopyLogger() { ++TimesConstructed; }
70 CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
71 ~CopyLogger() = default;
73 static int TimesCopied;
74 static int TimesConstructed;
77 void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
81 void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
85 int CopyLogger::TimesCopied = 0;
86 int CopyLogger::TimesConstructed = 0;
90 TEST(TupleTest, Copying) {
92 EXPECT_EQ(0, CopyLogger::TimesCopied);
93 EXPECT_EQ(1, CopyLogger::TimesConstructed);
97 // Creating the tuple should copy the class to store internally in the tuple.
98 std::tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
99 std::get<CopyLogger*>(tuple) = &std::get<CopyLogger>(tuple);
100 EXPECT_EQ(2, CopyLogger::TimesConstructed);
101 EXPECT_EQ(1, CopyLogger::TimesCopied);
103 // Our internal Logger and the one passed to the function should be the same.
105 DispatchToFunction(&SomeLoggerMethRef, tuple);
107 EXPECT_EQ(2, CopyLogger::TimesConstructed);
108 EXPECT_EQ(1, CopyLogger::TimesCopied);
110 // Now they should be different, since the function call will make a copy.
112 DispatchToFunction(&SomeLoggerMethCopy, tuple);
114 EXPECT_EQ(3, CopyLogger::TimesConstructed);
115 EXPECT_EQ(2, CopyLogger::TimesCopied);