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 { | 10 namespace { |
(...skipping 12 matching lines...) Expand all Loading... |
23 struct Addz { | 23 struct Addz { |
24 Addz() { } | 24 Addz() { } |
25 void DoAdd(int a, int b, int c, int d, int e, int* res) { | 25 void DoAdd(int a, int b, int c, int d, int e, int* res) { |
26 *res = a + b + c + d + e; | 26 *res = a + b + c + d + e; |
27 } | 27 } |
28 }; | 28 }; |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 TEST(TupleTest, Basic) { | 32 TEST(TupleTest, Basic) { |
33 Tuple0 t0 = MakeTuple(); | 33 Tuple<> t0 = MakeTuple(); |
34 ALLOW_UNUSED_LOCAL(t0); | 34 ALLOW_UNUSED_LOCAL(t0); |
35 Tuple1<int> t1(1); | 35 Tuple<int> t1(1); |
36 Tuple2<int, const char*> t2 = MakeTuple(1, static_cast<const char*>("wee")); | 36 Tuple<int, const char*> t2 = MakeTuple(1, static_cast<const char*>("wee")); |
37 Tuple3<int, int, int> t3(1, 2, 3); | 37 Tuple<int, int, int> t3(1, 2, 3); |
38 Tuple4<int, int, int, int*> t4(1, 2, 3, &t1.a); | 38 Tuple<int, int, int, int*> t4(1, 2, 3, &get<0>(t1)); |
39 Tuple5<int, int, int, int, int*> t5(1, 2, 3, 4, &t4.a); | 39 Tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &get<0>(t4)); |
40 Tuple6<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &t4.a); | 40 Tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &get<0>(t4)); |
41 | 41 |
42 EXPECT_EQ(1, t1.a); | 42 EXPECT_EQ(1, get<0>(t1)); |
43 EXPECT_EQ(1, t2.a); | 43 EXPECT_EQ(1, get<0>(t2)); |
44 EXPECT_EQ(1, t3.a); | 44 EXPECT_EQ(1, get<0>(t3)); |
45 EXPECT_EQ(2, t3.b); | 45 EXPECT_EQ(2, get<1>(t3)); |
46 EXPECT_EQ(3, t3.c); | 46 EXPECT_EQ(3, get<2>(t3)); |
47 EXPECT_EQ(1, t4.a); | 47 EXPECT_EQ(1, get<0>(t4)); |
48 EXPECT_EQ(2, t4.b); | 48 EXPECT_EQ(2, get<1>(t4)); |
49 EXPECT_EQ(3, t4.c); | 49 EXPECT_EQ(3, get<2>(t4)); |
50 EXPECT_EQ(1, t5.a); | 50 EXPECT_EQ(1, get<0>(t5)); |
51 EXPECT_EQ(2, t5.b); | 51 EXPECT_EQ(2, get<1>(t5)); |
52 EXPECT_EQ(3, t5.c); | 52 EXPECT_EQ(3, get<2>(t5)); |
53 EXPECT_EQ(4, t5.d); | 53 EXPECT_EQ(4, get<3>(t5)); |
54 EXPECT_EQ(1, t6.a); | 54 EXPECT_EQ(1, get<0>(t6)); |
55 EXPECT_EQ(2, t6.b); | 55 EXPECT_EQ(2, get<1>(t6)); |
56 EXPECT_EQ(3, t6.c); | 56 EXPECT_EQ(3, get<2>(t6)); |
57 EXPECT_EQ(4, t6.d); | 57 EXPECT_EQ(4, get<3>(t6)); |
58 EXPECT_EQ(5, t6.e); | 58 EXPECT_EQ(5, get<4>(t6)); |
59 | 59 |
60 EXPECT_EQ(1, t1.a); | 60 EXPECT_EQ(1, get<0>(t1)); |
61 DispatchToFunction(&DoAdd, t4); | 61 DispatchToFunction(&DoAdd, t4); |
62 EXPECT_EQ(6, t1.a); | 62 EXPECT_EQ(6, get<0>(t1)); |
63 | 63 |
64 int res = 0; | 64 int res = 0; |
65 DispatchToFunction(&DoAdd, MakeTuple(9, 8, 7, &res)); | 65 DispatchToFunction(&DoAdd, MakeTuple(9, 8, 7, &res)); |
66 EXPECT_EQ(24, res); | 66 EXPECT_EQ(24, res); |
67 | 67 |
68 Addy addy; | 68 Addy addy; |
69 EXPECT_EQ(1, t4.a); | 69 EXPECT_EQ(1, get<0>(t4)); |
70 DispatchToMethod(&addy, &Addy::DoAdd, t5); | 70 DispatchToMethod(&addy, &Addy::DoAdd, t5); |
71 EXPECT_EQ(10, t4.a); | 71 EXPECT_EQ(10, get<0>(t4)); |
72 | 72 |
73 Addz addz; | 73 Addz addz; |
74 EXPECT_EQ(10, t4.a); | 74 EXPECT_EQ(10, get<0>(t4)); |
75 DispatchToMethod(&addz, &Addz::DoAdd, t6); | 75 DispatchToMethod(&addz, &Addz::DoAdd, t6); |
76 EXPECT_EQ(15, t4.a); | 76 EXPECT_EQ(15, get<0>(t4)); |
77 } | 77 } |
78 | 78 |
79 namespace { | 79 namespace { |
80 | 80 |
81 struct CopyLogger { | 81 struct CopyLogger { |
82 CopyLogger() { ++TimesConstructed; } | 82 CopyLogger() { ++TimesConstructed; } |
83 CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; } | 83 CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; } |
84 ~CopyLogger() { } | 84 ~CopyLogger() { } |
85 | 85 |
86 static int TimesCopied; | 86 static int TimesCopied; |
(...skipping 14 matching lines...) Expand all Loading... |
101 } // namespace | 101 } // namespace |
102 | 102 |
103 TEST(TupleTest, Copying) { | 103 TEST(TupleTest, Copying) { |
104 CopyLogger logger; | 104 CopyLogger logger; |
105 EXPECT_EQ(0, CopyLogger::TimesCopied); | 105 EXPECT_EQ(0, CopyLogger::TimesCopied); |
106 EXPECT_EQ(1, CopyLogger::TimesConstructed); | 106 EXPECT_EQ(1, CopyLogger::TimesConstructed); |
107 | 107 |
108 bool res = false; | 108 bool res = false; |
109 | 109 |
110 // Creating the tuple should copy the class to store internally in the tuple. | 110 // Creating the tuple should copy the class to store internally in the tuple. |
111 Tuple3<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res); | 111 Tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res); |
112 tuple.b = &tuple.a; | 112 get<1>(tuple) = &get<0>(tuple); |
113 EXPECT_EQ(2, CopyLogger::TimesConstructed); | 113 EXPECT_EQ(2, CopyLogger::TimesConstructed); |
114 EXPECT_EQ(1, CopyLogger::TimesCopied); | 114 EXPECT_EQ(1, CopyLogger::TimesCopied); |
115 | 115 |
116 // Our internal Logger and the one passed to the function should be the same. | 116 // Our internal Logger and the one passed to the function should be the same. |
117 res = false; | 117 res = false; |
118 DispatchToFunction(&SomeLoggerMethRef, tuple); | 118 DispatchToFunction(&SomeLoggerMethRef, tuple); |
119 EXPECT_TRUE(res); | 119 EXPECT_TRUE(res); |
120 EXPECT_EQ(2, CopyLogger::TimesConstructed); | 120 EXPECT_EQ(2, CopyLogger::TimesConstructed); |
121 EXPECT_EQ(1, CopyLogger::TimesCopied); | 121 EXPECT_EQ(1, CopyLogger::TimesCopied); |
122 | 122 |
123 // Now they should be different, since the function call will make a copy. | 123 // Now they should be different, since the function call will make a copy. |
124 res = false; | 124 res = false; |
125 DispatchToFunction(&SomeLoggerMethCopy, tuple); | 125 DispatchToFunction(&SomeLoggerMethCopy, tuple); |
126 EXPECT_FALSE(res); | 126 EXPECT_FALSE(res); |
127 EXPECT_EQ(3, CopyLogger::TimesConstructed); | 127 EXPECT_EQ(3, CopyLogger::TimesConstructed); |
128 EXPECT_EQ(2, CopyLogger::TimesCopied); | 128 EXPECT_EQ(2, CopyLogger::TimesCopied); |
129 } | 129 } |
OLD | NEW |