Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/tuple.h" |
| 6 | |
Vitaly Buka | 8750b27 | 2015-08-18 18:39:08 -0700 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | void DoAdd(int a, int b, int c, int* res) { |
| 16 | *res = a + b + c; |
| 17 | } |
| 18 | |
| 19 | struct Addy { |
| 20 | Addy() { } |
| 21 | void DoAdd(int a, int b, int c, int d, int* res) { |
| 22 | *res = a + b + c + d; |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | struct Addz { |
| 27 | Addz() { } |
| 28 | void DoAdd(int a, int b, int c, int d, int e, int* res) { |
| 29 | *res = a + b + c + d + e; |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | } // namespace |
| 34 | |
| 35 | TEST(TupleTest, Basic) { |
| 36 | base::Tuple<> t0 = base::MakeTuple(); |
| 37 | ALLOW_UNUSED_LOCAL(t0); |
| 38 | base::Tuple<int> t1(1); |
| 39 | base::Tuple<int, const char*> t2 = |
| 40 | base::MakeTuple(1, static_cast<const char*>("wee")); |
| 41 | base::Tuple<int, int, int> t3(1, 2, 3); |
| 42 | base::Tuple<int, int, int, int*> t4(1, 2, 3, &get<0>(t1)); |
| 43 | base::Tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &get<0>(t4)); |
| 44 | base::Tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &get<0>(t4)); |
| 45 | |
| 46 | EXPECT_EQ(1, get<0>(t1)); |
| 47 | EXPECT_EQ(1, get<0>(t2)); |
| 48 | EXPECT_EQ(1, get<0>(t3)); |
| 49 | EXPECT_EQ(2, get<1>(t3)); |
| 50 | EXPECT_EQ(3, get<2>(t3)); |
| 51 | EXPECT_EQ(1, get<0>(t4)); |
| 52 | EXPECT_EQ(2, get<1>(t4)); |
| 53 | EXPECT_EQ(3, get<2>(t4)); |
| 54 | EXPECT_EQ(1, get<0>(t5)); |
| 55 | EXPECT_EQ(2, get<1>(t5)); |
| 56 | EXPECT_EQ(3, get<2>(t5)); |
| 57 | EXPECT_EQ(4, get<3>(t5)); |
| 58 | EXPECT_EQ(1, get<0>(t6)); |
| 59 | EXPECT_EQ(2, get<1>(t6)); |
| 60 | EXPECT_EQ(3, get<2>(t6)); |
| 61 | EXPECT_EQ(4, get<3>(t6)); |
| 62 | EXPECT_EQ(5, get<4>(t6)); |
| 63 | |
| 64 | EXPECT_EQ(1, get<0>(t1)); |
| 65 | DispatchToFunction(&DoAdd, t4); |
| 66 | EXPECT_EQ(6, get<0>(t1)); |
| 67 | |
| 68 | int res = 0; |
| 69 | DispatchToFunction(&DoAdd, base::MakeTuple(9, 8, 7, &res)); |
| 70 | EXPECT_EQ(24, res); |
| 71 | |
| 72 | Addy addy; |
| 73 | EXPECT_EQ(1, get<0>(t4)); |
| 74 | DispatchToMethod(&addy, &Addy::DoAdd, t5); |
| 75 | EXPECT_EQ(10, get<0>(t4)); |
| 76 | |
| 77 | Addz addz; |
| 78 | EXPECT_EQ(10, get<0>(t4)); |
| 79 | DispatchToMethod(&addz, &Addz::DoAdd, t6); |
| 80 | EXPECT_EQ(15, get<0>(t4)); |
| 81 | } |
| 82 | |
| 83 | namespace { |
| 84 | |
| 85 | struct CopyLogger { |
| 86 | CopyLogger() { ++TimesConstructed; } |
| 87 | CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; } |
| 88 | ~CopyLogger() { } |
| 89 | |
| 90 | static int TimesCopied; |
| 91 | static int TimesConstructed; |
| 92 | }; |
| 93 | |
| 94 | void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) { |
| 95 | *b = &logy == ptr; |
| 96 | } |
| 97 | |
| 98 | void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) { |
| 99 | *b = &logy == ptr; |
| 100 | } |
| 101 | |
| 102 | int CopyLogger::TimesCopied = 0; |
| 103 | int CopyLogger::TimesConstructed = 0; |
| 104 | |
| 105 | } // namespace |
| 106 | |
| 107 | TEST(TupleTest, Copying) { |
| 108 | CopyLogger logger; |
| 109 | EXPECT_EQ(0, CopyLogger::TimesCopied); |
| 110 | EXPECT_EQ(1, CopyLogger::TimesConstructed); |
| 111 | |
| 112 | bool res = false; |
| 113 | |
| 114 | // Creating the tuple should copy the class to store internally in the tuple. |
| 115 | base::Tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res); |
| 116 | get<1>(tuple) = &get<0>(tuple); |
| 117 | EXPECT_EQ(2, CopyLogger::TimesConstructed); |
| 118 | EXPECT_EQ(1, CopyLogger::TimesCopied); |
| 119 | |
| 120 | // Our internal Logger and the one passed to the function should be the same. |
| 121 | res = false; |
| 122 | DispatchToFunction(&SomeLoggerMethRef, tuple); |
| 123 | EXPECT_TRUE(res); |
| 124 | EXPECT_EQ(2, CopyLogger::TimesConstructed); |
| 125 | EXPECT_EQ(1, CopyLogger::TimesCopied); |
| 126 | |
| 127 | // Now they should be different, since the function call will make a copy. |
| 128 | res = false; |
| 129 | DispatchToFunction(&SomeLoggerMethCopy, tuple); |
| 130 | EXPECT_FALSE(res); |
| 131 | EXPECT_EQ(3, CopyLogger::TimesConstructed); |
| 132 | EXPECT_EQ(2, CopyLogger::TimesCopied); |
| 133 | } |
| 134 | |
| 135 | } // namespace base |