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