summaryrefslogtreecommitdiff
path: root/absl/container/internal/common_policy_traits_test.cc
blob: 5eaa4aae00f7383614b5ce1de314247a76329d15 (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
// Copyright 2022 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "absl/container/internal/common_policy_traits.h"

#include <functional>
#include <memory>
#include <new>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {

using ::testing::MockFunction;
using ::testing::AnyNumber;
using ::testing::ReturnRef;

using Slot = int;

struct PolicyWithoutOptionalOps {
  using slot_type = Slot;
  using key_type = Slot;
  using init_type = Slot;

  static std::function<void(void*, Slot*, Slot)> construct;
  static std::function<void(void*, Slot*)> destroy;

  static std::function<Slot&(Slot*)> element;
};

std::function<void(void*, Slot*, Slot)> PolicyWithoutOptionalOps::construct;
std::function<void(void*, Slot*)> PolicyWithoutOptionalOps::destroy;

std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::element;

struct PolicyWithOptionalOps : PolicyWithoutOptionalOps {
  static std::function<void(void*, Slot*, Slot*)> transfer;
};

std::function<void(void*, Slot*, Slot*)> PolicyWithOptionalOps::transfer;

struct Test : ::testing::Test {
  Test() {
    PolicyWithoutOptionalOps::construct = [&](void* a1, Slot* a2, Slot a3) {
      construct.Call(a1, a2, std::move(a3));
    };
    PolicyWithoutOptionalOps::destroy = [&](void* a1, Slot* a2) {
      destroy.Call(a1, a2);
    };

    PolicyWithoutOptionalOps::element = [&](Slot* a1) -> Slot& {
      return element.Call(a1);
    };

    PolicyWithOptionalOps::transfer = [&](void* a1, Slot* a2, Slot* a3) {
      return transfer.Call(a1, a2, a3);
    };
  }

  std::allocator<Slot> alloc;
  int a = 53;

  MockFunction<void(void*, Slot*, Slot)> construct;
  MockFunction<void(void*, Slot*)> destroy;

  MockFunction<Slot&(Slot*)> element;

  MockFunction<void(void*, Slot*, Slot*)> transfer;
};

TEST_F(Test, construct) {
  EXPECT_CALL(construct, Call(&alloc, &a, 53));
  common_policy_traits<PolicyWithoutOptionalOps>::construct(&alloc, &a, 53);
}

TEST_F(Test, destroy) {
  EXPECT_CALL(destroy, Call(&alloc, &a));
  common_policy_traits<PolicyWithoutOptionalOps>::destroy(&alloc, &a);
}

TEST_F(Test, element) {
  int b = 0;
  EXPECT_CALL(element, Call(&a)).WillOnce(ReturnRef(b));
  EXPECT_EQ(&b, &common_policy_traits<PolicyWithoutOptionalOps>::element(&a));
}

TEST_F(Test, without_transfer) {
  int b = 42;
  EXPECT_CALL(element, Call(&a)).Times(AnyNumber()).WillOnce(ReturnRef(a));
  EXPECT_CALL(element, Call(&b)).WillOnce(ReturnRef(b));
  EXPECT_CALL(construct, Call(&alloc, &a, b)).Times(AnyNumber());
  EXPECT_CALL(destroy, Call(&alloc, &b)).Times(AnyNumber());
  common_policy_traits<PolicyWithoutOptionalOps>::transfer(&alloc, &a, &b);
}

TEST_F(Test, with_transfer) {
  int b = 42;
  EXPECT_CALL(transfer, Call(&alloc, &a, &b));
  common_policy_traits<PolicyWithOptionalOps>::transfer(&alloc, &a, &b);
}

}  // namespace
}  // namespace container_internal
ABSL_NAMESPACE_END
}  // namespace absl