summaryrefslogtreecommitdiffstats
path: root/base/tuple_unittest.cc
blob: 5b43affc600f5f7d8741f1023c8b8e1b19486b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/tuple.h"

#include "base/compiler_specific.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

void DoAdd(int a, int b, int c, int* res) {
  *res = a + b + c;
}

struct Addy {
  Addy() { }
  void DoAdd(int a, int b, int c, int d, int* res) {
    *res = a + b + c + d;
  }
};

struct Addz {
  Addz() { }
  void DoAdd(int a, int b, int c, int d, int e, int* res) {
    *res = a + b + c + d + e;
  }
};

}  // namespace

TEST(TupleTest, Basic) {
  Tuple<> t0 = MakeTuple();
  ALLOW_UNUSED_LOCAL(t0);
  Tuple<int> t1(1);
  Tuple<int, const char*> t2 = MakeTuple(1, static_cast<const char*>("wee"));
  Tuple<int, int, int> t3(1, 2, 3);
  Tuple<int, int, int, int*> t4(1, 2, 3, &get<0>(t1));
  Tuple<int, int, int, int, int*> t5(1, 2, 3, 4, &get<0>(t4));
  Tuple<int, int, int, int, int, int*> t6(1, 2, 3, 4, 5, &get<0>(t4));

  EXPECT_EQ(1, get<0>(t1));
  EXPECT_EQ(1, get<0>(t2));
  EXPECT_EQ(1, get<0>(t3));
  EXPECT_EQ(2, get<1>(t3));
  EXPECT_EQ(3, get<2>(t3));
  EXPECT_EQ(1, get<0>(t4));
  EXPECT_EQ(2, get<1>(t4));
  EXPECT_EQ(3, get<2>(t4));
  EXPECT_EQ(1, get<0>(t5));
  EXPECT_EQ(2, get<1>(t5));
  EXPECT_EQ(3, get<2>(t5));
  EXPECT_EQ(4, get<3>(t5));
  EXPECT_EQ(1, get<0>(t6));
  EXPECT_EQ(2, get<1>(t6));
  EXPECT_EQ(3, get<2>(t6));
  EXPECT_EQ(4, get<3>(t6));
  EXPECT_EQ(5, get<4>(t6));

  EXPECT_EQ(1, get<0>(t1));
  DispatchToFunction(&DoAdd, t4);
  EXPECT_EQ(6, get<0>(t1));

  int res = 0;
  DispatchToFunction(&DoAdd, MakeTuple(9, 8, 7, &res));
  EXPECT_EQ(24, res);

  Addy addy;
  EXPECT_EQ(1, get<0>(t4));
  DispatchToMethod(&addy, &Addy::DoAdd, t5);
  EXPECT_EQ(10, get<0>(t4));

  Addz addz;
  EXPECT_EQ(10, get<0>(t4));
  DispatchToMethod(&addz, &Addz::DoAdd, t6);
  EXPECT_EQ(15, get<0>(t4));
}

namespace {

struct CopyLogger {
  CopyLogger() { ++TimesConstructed; }
  CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; }
  ~CopyLogger() { }

  static int TimesCopied;
  static int TimesConstructed;
};

void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) {
  *b = &logy == ptr;
}

void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) {
  *b = &logy == ptr;
}

int CopyLogger::TimesCopied = 0;
int CopyLogger::TimesConstructed = 0;

}  // namespace

TEST(TupleTest, Copying) {
  CopyLogger logger;
  EXPECT_EQ(0, CopyLogger::TimesCopied);
  EXPECT_EQ(1, CopyLogger::TimesConstructed);

  bool res = false;

  // Creating the tuple should copy the class to store internally in the tuple.
  Tuple<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res);
  get<1>(tuple) = &get<0>(tuple);
  EXPECT_EQ(2, CopyLogger::TimesConstructed);
  EXPECT_EQ(1, CopyLogger::TimesCopied);

  // Our internal Logger and the one passed to the function should be the same.
  res = false;
  DispatchToFunction(&SomeLoggerMethRef, tuple);
  EXPECT_TRUE(res);
  EXPECT_EQ(2, CopyLogger::TimesConstructed);
  EXPECT_EQ(1, CopyLogger::TimesCopied);

  // Now they should be different, since the function call will make a copy.
  res = false;
  DispatchToFunction(&SomeLoggerMethCopy, tuple);
  EXPECT_FALSE(res);
  EXPECT_EQ(3, CopyLogger::TimesConstructed);
  EXPECT_EQ(2, CopyLogger::TimesCopied);
}