OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/tuple.h" | 5 #include "base/tuple.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
| 10 namespace base { |
| 11 |
10 namespace { | 12 namespace { |
11 | 13 |
12 void DoAdd(int a, int b, int c, int* res) { | 14 void DoAdd(int a, int b, int c, int* res) { |
13 *res = a + b + c; | 15 *res = a + b + c; |
14 } | 16 } |
15 | 17 |
16 struct Addy { | 18 struct Addy { |
17 Addy() { } | 19 Addy() { } |
18 void DoAdd(int a, int b, int c, int d, int* res) { | 20 void DoAdd(int a, int b, int c, int d, int* res) { |
19 *res = a + b + c + d; | 21 *res = a + b + c + d; |
20 } | 22 } |
21 }; | 23 }; |
22 | 24 |
23 struct Addz { | 25 struct Addz { |
24 Addz() { } | 26 Addz() { } |
25 void DoAdd(int a, int b, int c, int d, int e, int* res) { | 27 void DoAdd(int a, int b, int c, int d, int e, int* res) { |
26 *res = a + b + c + d + e; | 28 *res = a + b + c + d + e; |
27 } | 29 } |
28 }; | 30 }; |
29 | 31 |
30 } // namespace | 32 } // namespace |
31 | 33 |
32 TEST(TupleTest, Basic) { | 34 TEST(TupleTest, Basic) { |
33 Tuple<> t0 = MakeTuple(); | 35 base::Tuple<> t0 = base::MakeTuple(); |
34 ALLOW_UNUSED_LOCAL(t0); | 36 ALLOW_UNUSED_LOCAL(t0); |
35 Tuple<int> t1(1); | 37 base::Tuple<int> t1(1); |
36 Tuple<int, const char*> t2 = MakeTuple(1, static_cast<const char*>("wee")); | 38 base::Tuple<int, const char*> t2 = |
37 Tuple<int, int, int> t3(1, 2, 3); | 39 base::MakeTuple(1, static_cast<const char*>("wee")); |
38 Tuple<int, int, int, int*> t4(1, 2, 3, &get<0>(t1)); | 40 base::Tuple<int, int, int> t3(1, 2, 3); |
39 Tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &get<0>(t4)); | 41 base::Tuple<int, int, int, int*> t4(1, 2, 3, &get<0>(t1)); |
40 Tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &get<0>(t4)); | 42 base::Tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &get<0>(t4)); |
| 43 base::Tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &get<0>(t4)); |
41 | 44 |
42 EXPECT_EQ(1, get<0>(t1)); | 45 EXPECT_EQ(1, get<0>(t1)); |
43 EXPECT_EQ(1, get<0>(t2)); | 46 EXPECT_EQ(1, get<0>(t2)); |
44 EXPECT_EQ(1, get<0>(t3)); | 47 EXPECT_EQ(1, get<0>(t3)); |
45 EXPECT_EQ(2, get<1>(t3)); | 48 EXPECT_EQ(2, get<1>(t3)); |
46 EXPECT_EQ(3, get<2>(t3)); | 49 EXPECT_EQ(3, get<2>(t3)); |
47 EXPECT_EQ(1, get<0>(t4)); | 50 EXPECT_EQ(1, get<0>(t4)); |
48 EXPECT_EQ(2, get<1>(t4)); | 51 EXPECT_EQ(2, get<1>(t4)); |
49 EXPECT_EQ(3, get<2>(t4)); | 52 EXPECT_EQ(3, get<2>(t4)); |
50 EXPECT_EQ(1, get<0>(t5)); | 53 EXPECT_EQ(1, get<0>(t5)); |
51 EXPECT_EQ(2, get<1>(t5)); | 54 EXPECT_EQ(2, get<1>(t5)); |
52 EXPECT_EQ(3, get<2>(t5)); | 55 EXPECT_EQ(3, get<2>(t5)); |
53 EXPECT_EQ(4, get<3>(t5)); | 56 EXPECT_EQ(4, get<3>(t5)); |
54 EXPECT_EQ(1, get<0>(t6)); | 57 EXPECT_EQ(1, get<0>(t6)); |
55 EXPECT_EQ(2, get<1>(t6)); | 58 EXPECT_EQ(2, get<1>(t6)); |
56 EXPECT_EQ(3, get<2>(t6)); | 59 EXPECT_EQ(3, get<2>(t6)); |
57 EXPECT_EQ(4, get<3>(t6)); | 60 EXPECT_EQ(4, get<3>(t6)); |
58 EXPECT_EQ(5, get<4>(t6)); | 61 EXPECT_EQ(5, get<4>(t6)); |
59 | 62 |
60 EXPECT_EQ(1, get<0>(t1)); | 63 EXPECT_EQ(1, get<0>(t1)); |
61 DispatchToFunction(&DoAdd, t4); | 64 DispatchToFunction(&DoAdd, t4); |
62 EXPECT_EQ(6, get<0>(t1)); | 65 EXPECT_EQ(6, get<0>(t1)); |
63 | 66 |
64 int res = 0; | 67 int res = 0; |
65 DispatchToFunction(&DoAdd, MakeTuple(9, 8, 7, &res)); | 68 DispatchToFunction(&DoAdd, base::MakeTuple(9, 8, 7, &res)); |
66 EXPECT_EQ(24, res); | 69 EXPECT_EQ(24, res); |
67 | 70 |
68 Addy addy; | 71 Addy addy; |
69 EXPECT_EQ(1, get<0>(t4)); | 72 EXPECT_EQ(1, get<0>(t4)); |
70 DispatchToMethod(&addy, &Addy::DoAdd, t5); | 73 DispatchToMethod(&addy, &Addy::DoAdd, t5); |
71 EXPECT_EQ(10, get<0>(t4)); | 74 EXPECT_EQ(10, get<0>(t4)); |
72 | 75 |
73 Addz addz; | 76 Addz addz; |
74 EXPECT_EQ(10, get<0>(t4)); | 77 EXPECT_EQ(10, get<0>(t4)); |
75 DispatchToMethod(&addz, &Addz::DoAdd, t6); | 78 DispatchToMethod(&addz, &Addz::DoAdd, t6); |
(...skipping 25 matching lines...) Expand all Loading... |
101 } // namespace | 104 } // namespace |
102 | 105 |
103 TEST(TupleTest, Copying) { | 106 TEST(TupleTest, Copying) { |
104 CopyLogger logger; | 107 CopyLogger logger; |
105 EXPECT_EQ(0, CopyLogger::TimesCopied); | 108 EXPECT_EQ(0, CopyLogger::TimesCopied); |
106 EXPECT_EQ(1, CopyLogger::TimesConstructed); | 109 EXPECT_EQ(1, CopyLogger::TimesConstructed); |
107 | 110 |
108 bool res = false; | 111 bool res = false; |
109 | 112 |
110 // Creating the tuple should copy the class to store internally in the tuple. | 113 // Creating the tuple should copy the class to store internally in the tuple. |
111 Tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res); | 114 base::Tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res); |
112 get<1>(tuple) = &get<0>(tuple); | 115 get<1>(tuple) = &get<0>(tuple); |
113 EXPECT_EQ(2, CopyLogger::TimesConstructed); | 116 EXPECT_EQ(2, CopyLogger::TimesConstructed); |
114 EXPECT_EQ(1, CopyLogger::TimesCopied); | 117 EXPECT_EQ(1, CopyLogger::TimesCopied); |
115 | 118 |
116 // Our internal Logger and the one passed to the function should be the same. | 119 // Our internal Logger and the one passed to the function should be the same. |
117 res = false; | 120 res = false; |
118 DispatchToFunction(&SomeLoggerMethRef, tuple); | 121 DispatchToFunction(&SomeLoggerMethRef, tuple); |
119 EXPECT_TRUE(res); | 122 EXPECT_TRUE(res); |
120 EXPECT_EQ(2, CopyLogger::TimesConstructed); | 123 EXPECT_EQ(2, CopyLogger::TimesConstructed); |
121 EXPECT_EQ(1, CopyLogger::TimesCopied); | 124 EXPECT_EQ(1, CopyLogger::TimesCopied); |
122 | 125 |
123 // Now they should be different, since the function call will make a copy. | 126 // Now they should be different, since the function call will make a copy. |
124 res = false; | 127 res = false; |
125 DispatchToFunction(&SomeLoggerMethCopy, tuple); | 128 DispatchToFunction(&SomeLoggerMethCopy, tuple); |
126 EXPECT_FALSE(res); | 129 EXPECT_FALSE(res); |
127 EXPECT_EQ(3, CopyLogger::TimesConstructed); | 130 EXPECT_EQ(3, CopyLogger::TimesConstructed); |
128 EXPECT_EQ(2, CopyLogger::TimesCopied); | 131 EXPECT_EQ(2, CopyLogger::TimesCopied); |
129 } | 132 } |
| 133 |
| 134 } // namespace base |
OLD | NEW |