blob: 4b38797e0e77cadf56d54c60862beace66b7d89f [file] [log] [blame]
[email protected]302bdc132008-08-25 13:42:071// 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"
[email protected]40350b12010-03-30 17:29:276
7#include "base/compiler_specific.h"
[email protected]302bdc132008-08-25 13:42:078#include "testing/gtest/include/gtest/gtest.h"
9
brettwd5ca2bc2015-05-29 22:15:4710namespace base {
11
[email protected]302bdc132008-08-25 13:42:0712namespace {
13
14void DoAdd(int a, int b, int c, int* res) {
15 *res = a + b + c;
16}
17
18struct Addy {
Chris Watkinsbb7211c2017-11-29 07:16:3819 Addy() = default;
[email protected]302bdc132008-08-25 13:42:0720 void DoAdd(int a, int b, int c, int d, int* res) {
21 *res = a + b + c + d;
22 }
23};
24
[email protected]8a2820a2008-10-09 21:58:0525struct Addz {
Chris Watkinsbb7211c2017-11-29 07:16:3826 Addz() = default;
[email protected]8a2820a2008-10-09 21:58:0527 void DoAdd(int a, int b, int c, int d, int e, int* res) {
28 *res = a + b + c + d + e;
29 }
30};
31
[email protected]302bdc132008-08-25 13:42:0732} // namespace
33
34TEST(TupleTest, Basic) {
tzik1068f1be2016-06-03 07:25:2035 std::tuple<> t0 = std::make_tuple();
pkasting99867ef2014-10-16 23:49:2436 ALLOW_UNUSED_LOCAL(t0);
tzik1068f1be2016-06-03 07:25:2037 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);
Reid Klecknerd7b58ec2017-10-16 23:23:4242 ALLOW_UNUSED_LOCAL(t3);
tzik1068f1be2016-06-03 07:25:2043 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));
[email protected]302bdc132008-08-25 13:42:0746
tzik1068f1be2016-06-03 07:25:2047 EXPECT_EQ(1, std::get<0>(t1));
[email protected]302bdc132008-08-25 13:42:0748 DispatchToFunction(&DoAdd, t4);
tzik1068f1be2016-06-03 07:25:2049 EXPECT_EQ(6, std::get<0>(t1));
[email protected]302bdc132008-08-25 13:42:0750
51 int res = 0;
tzik1068f1be2016-06-03 07:25:2052 DispatchToFunction(&DoAdd, std::make_tuple(9, 8, 7, &res));
[email protected]302bdc132008-08-25 13:42:0753 EXPECT_EQ(24, res);
54
55 Addy addy;
tzik1068f1be2016-06-03 07:25:2056 EXPECT_EQ(1, std::get<0>(t4));
[email protected]302bdc132008-08-25 13:42:0757 DispatchToMethod(&addy, &Addy::DoAdd, t5);
tzik1068f1be2016-06-03 07:25:2058 EXPECT_EQ(10, std::get<0>(t4));
[email protected]8a2820a2008-10-09 21:58:0559
60 Addz addz;
tzik1068f1be2016-06-03 07:25:2061 EXPECT_EQ(10, std::get<0>(t4));
[email protected]8a2820a2008-10-09 21:58:0562 DispatchToMethod(&addz, &Addz::DoAdd, t6);
tzik1068f1be2016-06-03 07:25:2063 EXPECT_EQ(15, std::get<0>(t4));
[email protected]302bdc132008-08-25 13:42:0764}
65
66namespace {
67
68struct CopyLogger {
69 CopyLogger() { ++TimesConstructed; }
70 CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
Chris Watkinsbb7211c2017-11-29 07:16:3871 ~CopyLogger() = default;
[email protected]302bdc132008-08-25 13:42:0772
73 static int TimesCopied;
74 static int TimesConstructed;
75};
76
77void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
78 *b = &logy == ptr;
79}
80
81void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
82 *b = &logy == ptr;
83}
84
85int CopyLogger::TimesCopied = 0;
86int CopyLogger::TimesConstructed = 0;
87
88} // namespace
89
90TEST(TupleTest, Copying) {
91 CopyLogger logger;
92 EXPECT_EQ(0, CopyLogger::TimesCopied);
93 EXPECT_EQ(1, CopyLogger::TimesConstructed);
94
95 bool res = false;
96
97 // Creating the tuple should copy the class to store internally in the tuple.
tzik1068f1be2016-06-03 07:25:2098 std::tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
Peter Kasting1b368e72018-01-13 01:49:5299 std::get<CopyLogger*>(tuple) = &std::get<CopyLogger>(tuple);
[email protected]302bdc132008-08-25 13:42:07100 EXPECT_EQ(2, CopyLogger::TimesConstructed);
101 EXPECT_EQ(1, CopyLogger::TimesCopied);
102
103 // Our internal Logger and the one passed to the function should be the same.
104 res = false;
105 DispatchToFunction(&SomeLoggerMethRef, tuple);
106 EXPECT_TRUE(res);
107 EXPECT_EQ(2, CopyLogger::TimesConstructed);
108 EXPECT_EQ(1, CopyLogger::TimesCopied);
109
110 // Now they should be different, since the function call will make a copy.
111 res = false;
112 DispatchToFunction(&SomeLoggerMethCopy, tuple);
113 EXPECT_FALSE(res);
114 EXPECT_EQ(3, CopyLogger::TimesConstructed);
115 EXPECT_EQ(2, CopyLogger::TimesCopied);
116}
brettwd5ca2bc2015-05-29 22:15:47117
118} // namespace base