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