File: voting_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (225 lines) | stat: -rw-r--r-- 8,377 bytes parent folder | download | duplicates (5)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/public/voting/voting.h"

#include "base/test/gtest_util.h"
#include "components/performance_manager/test_support/voting.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace performance_manager {

namespace {

using ::testing::AssertionFailure;
using ::testing::AssertionResult;
using ::testing::AssertionSuccess;

using TestVote = voting::Vote<void, int, 0>;
using TestVotingChannel = voting::VotingChannel<TestVote>;
using TestVotingChannelFactory = voting::VotingChannelFactory<TestVote>;

using DummyVoteObserver = voting::test::DummyVoteObserver<TestVote>;

// Some dummy contexts.
const void* kDummyContext1 = reinterpret_cast<const void*>(0xDEADBEEF);
const void* kDummyContext2 = reinterpret_cast<const void*>(0xBAADF00D);

static const char kReason[] = "reason";

}  // namespace

TEST(VotingTest, SimpleVoter) {
  DummyVoteObserver observer;

  TestVotingChannel voting_channel = observer.BuildVotingChannel();
  voting::VoterId<TestVote> voter_id = voting_channel.voter_id();

  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));

  voting_channel.SubmitVote(kDummyContext1, TestVote(5, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));

  voting_channel.ChangeVote(kDummyContext1, TestVote(10, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 10, kReason));

  voting_channel.InvalidateVote(kDummyContext1);
  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
}

// Tests that an observer can receive votes for different contexts from the same
// voting channel.
TEST(VotingTest, OneVoterMultipleContexts) {
  DummyVoteObserver observer;

  TestVotingChannel voting_channel = observer.BuildVotingChannel();
  voting::VoterId<TestVote> voter_id = voting_channel.voter_id();

  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));

  voting_channel.SubmitVote(kDummyContext1, TestVote(5, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));
  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext2));

  voting_channel.SubmitVote(kDummyContext2, TestVote(100, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext2, 100, kReason));

  voting_channel.ChangeVote(kDummyContext1, TestVote(10, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 10, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext2, 100, kReason));

  voting_channel.InvalidateVote(kDummyContext1);
  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext2, 100, kReason));

  voting_channel.InvalidateVote(kDummyContext2);
  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext2));
}

// Tests that an observer can receive votes from more than one voting channel.
TEST(VotingTest, TwoVoter) {
  DummyVoteObserver observer;

  TestVotingChannel voting_channel_1 = observer.BuildVotingChannel();
  voting::VoterId<TestVote> voter_id_1 = voting_channel_1.voter_id();

  TestVotingChannel voting_channel_2 = observer.BuildVotingChannel();
  voting::VoterId<TestVote> voter_id_2 = voting_channel_2.voter_id();

  EXPECT_FALSE(observer.HasVote(voter_id_1, kDummyContext1));
  EXPECT_FALSE(observer.HasVote(voter_id_2, kDummyContext1));

  voting_channel_1.SubmitVote(kDummyContext1, TestVote(5, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id_1, kDummyContext1, 5, kReason));
  EXPECT_FALSE(observer.HasVote(voter_id_2, kDummyContext1));

  voting_channel_2.SubmitVote(kDummyContext1, TestVote(5, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id_1, kDummyContext1, 5, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id_2, kDummyContext1, 5, kReason));

  voting_channel_1.ChangeVote(kDummyContext1, TestVote(10, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id_1, kDummyContext1, 10, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id_2, kDummyContext1, 5, kReason));

  voting_channel_2.ChangeVote(kDummyContext1, TestVote(10, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id_1, kDummyContext1, 10, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id_2, kDummyContext1, 10, kReason));

  voting_channel_1.InvalidateVote(kDummyContext1);
  EXPECT_FALSE(observer.HasVote(voter_id_1, kDummyContext1));
  EXPECT_TRUE(observer.HasVote(voter_id_2, kDummyContext1, 10, kReason));

  voting_channel_2.InvalidateVote(kDummyContext1);
  EXPECT_FALSE(observer.HasVote(voter_id_1, kDummyContext1));
  EXPECT_FALSE(observer.HasVote(voter_id_2, kDummyContext1));
}

TEST(VotingTest, ResetVotingChannel) {
  DummyVoteObserver observer;

  TestVotingChannel voting_channel = observer.BuildVotingChannel();
  EXPECT_TRUE(voting_channel.IsValid());

  voting_channel.Reset();

  EXPECT_FALSE(voting_channel.IsValid());
}

// Tests that VotingChannel supports move sementics.
TEST(VotingTest, MoveVotingChannel) {
  DummyVoteObserver observer;

  // Build the voting channel.
  TestVotingChannel voting_channel_1 = observer.BuildVotingChannel();
  voting::VoterId<TestVote> voter_id = voting_channel_1.voter_id();
  EXPECT_TRUE(voting_channel_1.IsValid());

  // Cast a vote with that voting channel.
  voting_channel_1.SubmitVote(kDummyContext1, TestVote(5, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));

  // Move the voting channel.
  TestVotingChannel voting_channel_2 = std::move(voting_channel_1);
  EXPECT_TRUE(voting_channel_2.IsValid());

  // Use the second variable to change the vote.
  voting_channel_2.ChangeVote(kDummyContext1, TestVote(10, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 10, kReason));

  // Move the voting channel back using the move assignment operator.
  voting_channel_1 = std::move(voting_channel_2);
  EXPECT_TRUE(voting_channel_1.IsValid());

  // Invalidate the vote.
  voting_channel_1.InvalidateVote(kDummyContext1);
  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
}

// Tests that submitting 2 votes for the same context using a VotingChannel
// results in a DCHECK.
TEST(VotingTest, SubmitDuplicateVote) {
  DummyVoteObserver observer;

  TestVotingChannel voting_channel = observer.BuildVotingChannel();
  voting::VoterId<TestVote> voter_id = voting_channel.voter_id();

  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));

  voting_channel.SubmitVote(kDummyContext1, TestVote(5, kReason));
  EXPECT_TRUE(observer.HasVote(voter_id, kDummyContext1, 5, kReason));

  EXPECT_DCHECK_DEATH(
      voting_channel.SubmitVote(kDummyContext1, TestVote(10, kReason)));

  // Clean up.
  voting_channel.InvalidateVote(kDummyContext1);
}

// Tests that calling ChangeVote() for a context before a vote was submitted for
// that context results in a DCHECK.
TEST(VotingTest, ChangeNonExisting) {
  DummyVoteObserver observer;

  TestVotingChannel voting_channel = observer.BuildVotingChannel();
  voting::VoterId<TestVote> voter_id = voting_channel.voter_id();

  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  // TODO(pbos): This is a DCHECK-build-only CHECK, see voting.h. This should
  // probably be either a DCHECK or a CHECK outside DCHECK builds.
  if (DCHECK_IS_ON()) {
    EXPECT_CHECK_DEATH(
        voting_channel.ChangeVote(kDummyContext1, TestVote(5, kReason)));
  }
}

// Tests that calling InvalidateVote() for a context before a vote was submitted
// for that context results in a DCHECK.
TEST(VotingTest, InvalidateNonExisting) {
  DummyVoteObserver observer;

  TestVotingChannel voting_channel = observer.BuildVotingChannel();
  voting::VoterId<TestVote> voter_id = voting_channel.voter_id();

  EXPECT_FALSE(observer.HasVote(voter_id, kDummyContext1));
  EXPECT_DCHECK_DEATH(voting_channel.InvalidateVote(kDummyContext1));
}

// Tests that destroying a VotingChannelFactory before all of its VotingChannels
// results in a DCHECK.
TEST(VotingTest, DestroyFactoryBeforeChannel) {
  TestVotingChannel voting_channel;

  auto observer = std::make_unique<DummyVoteObserver>();

  voting_channel = observer->BuildVotingChannel();

  EXPECT_DCHECK_DEATH(observer.reset());

  // Clean up.
  voting_channel.Reset();
}

}  // namespace performance_manager