File: wtf_test_helper.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (298 lines) | stat: -rw-r--r-- 8,042 bytes parent folder | download | duplicates (8)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_WTF_TEST_HELPER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_WTF_TEST_HELPER_H_

#include <type_traits>

#include "base/memory/scoped_refptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_functions.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"

namespace WTF {

class DestructCounter {
  USING_FAST_MALLOC(DestructCounter);

 public:
  explicit DestructCounter(int i, int* destruct_number)
      : i_(i), destruct_number_(destruct_number) {}

  ~DestructCounter() { ++(*destruct_number_); }
  int Get() const { return i_; }

 private:
  int i_;
  int* destruct_number_;
};

class MoveOnly {
  DISALLOW_NEW();

 public:
  explicit MoveOnly(int i = 0) : i_(i) {}

  MoveOnly(MoveOnly&& other) : i_(other.i_) { other.i_ = 0; }

  MoveOnly& operator=(MoveOnly&& other) {
    if (this != &other) {
      i_ = other.i_;
      other.i_ = 0;
    }
    return *this;
  }

  int Value() const { return i_; }

 private:
  int i_;
};

class MoveOnlyHashValue {
 public:
  // kEmpty and kDeleted have special meanings when MoveOnlyHashValue is used as
  // the key of a hash table.
  enum { kEmpty = 0, kDeleted = -1, kMovedOut = -2 };

  explicit MoveOnlyHashValue(int value = kEmpty, int id = 0)
      : value_(value), id_(id) {}
  MoveOnlyHashValue(MoveOnlyHashValue&& other)
      : value_(other.value_), id_(other.id_) {
    other.value_ = kMovedOut;
    other.id_ = 0;
  }
  MoveOnlyHashValue& operator=(MoveOnlyHashValue&& other) {
    value_ = other.value_;
    id_ = other.id_;
    other.value_ = kMovedOut;
    other.id_ = 0;
    return *this;
  }

  int Value() const { return value_; }
  // id() is used for distinguishing MoveOnlys with the same value().
  int Id() const { return id_; }

 private:
  MoveOnlyHashValue(const MoveOnlyHashValue&) = delete;
  MoveOnlyHashValue& operator=(const MoveOnlyHashValue&) = delete;

  int value_;
  int id_;
};

struct MoveOnlyHashTraits : public GenericHashTraits<MoveOnlyHashValue> {
  // This is actually true, but we pretend that it's false to disable the
  // optimization.
  static const bool kEmptyValueIsZero = false;

  static bool IsEmptyValue(const MoveOnlyHashValue& value) {
    return value.Value() == MoveOnlyHashValue::kEmpty;
  }
  static void ConstructDeletedValue(MoveOnlyHashValue& slot) {
    slot = MoveOnlyHashValue(MoveOnlyHashValue::kDeleted);
  }
  static bool IsDeletedValue(const MoveOnlyHashValue& value) {
    return value.Value() == MoveOnlyHashValue::kDeleted;
  }
  static unsigned GetHash(const MoveOnlyHashValue& value) {
    return WTF::GetHash(value.Value());
  }
  static bool Equal(const MoveOnlyHashValue& left,
                    const MoveOnlyHashValue& right) {
    return left.Value() == right.Value();
  }
};

template <>
struct HashTraits<MoveOnlyHashValue> : MoveOnlyHashTraits {};

class CountCopy final {
 public:
  static int* const kDeletedValue;

  CountCopy() : counter_(nullptr) {}
  explicit CountCopy(int* counter) : counter_(counter) {}
  explicit CountCopy(int& counter) : counter_(&counter) {}
  CountCopy(const CountCopy& other) : counter_(other.counter_) {
    if (counter_ && counter_ != kDeletedValue)
      ++*counter_;
  }
  CountCopy& operator=(const CountCopy& other) {
    counter_ = other.counter_;
    if (counter_ && counter_ != kDeletedValue)
      ++*counter_;
    return *this;
  }

  const int* Counter() const { return counter_; }

 private:
  int* counter_;
};

struct CountCopyHashTraits : public GenericHashTraits<CountCopy> {
  static const bool kEmptyValueIsZero = false;
  static bool IsEmptyValue(const CountCopy& value) { return !value.Counter(); }
  static void ConstructDeletedValue(CountCopy& slot) {
    slot = CountCopy(CountCopy::kDeletedValue);
  }
  static bool IsDeletedValue(const CountCopy& value) {
    return value.Counter() == CountCopy::kDeletedValue;
  }
  static unsigned GetHash(const CountCopy& value) {
    return WTF::GetHash(value.Counter());
  }
  static bool Equal(const CountCopy& left, const CountCopy& right) {
    return left.Counter() == right.Counter();
  }
};

template <>
struct HashTraits<CountCopy> : CountCopyHashTraits {};

template <typename T>
class ValueInstanceCount final {
 public:
  static int* const kDeletedValue;

  ValueInstanceCount() : counter_(nullptr), value_(T()) {}
  explicit ValueInstanceCount(int* counter, T value = T())
      : counter_(counter), value_(value) {
    if (counter_ && counter_ != kDeletedValue)
      ++*counter_;
  }
  ValueInstanceCount(const ValueInstanceCount& other)
      : counter_(other.counter_), value_(other.value_) {
    if (counter_ && counter_ != kDeletedValue)
      ++*counter_;
  }
  ValueInstanceCount& operator=(const ValueInstanceCount& other) {
    if (counter_ && counter_ != kDeletedValue)
      --*counter_;
    counter_ = other.counter_;
    value_ = other.value_;
    if (counter_ && counter_ != kDeletedValue)
      ++*counter_;
    return *this;
  }
  ~ValueInstanceCount() {
    if (counter_ && counter_ != kDeletedValue)
      --*counter_;
  }

  const int* Counter() const { return counter_; }
  const T& Value() const { return value_; }

 private:
  int* counter_;
  T value_;
};

template <typename T>
struct ValueInstanceCountHashTraits
    : public GenericHashTraits<ValueInstanceCount<T>> {
  static const bool kEmptyValueIsZero = false;
  static bool IsEmptyValue(const ValueInstanceCount<T>& value) {
    return !value.Counter();
  }
  static void ConstructDeletedValue(ValueInstanceCount<T>& slot) {
    slot = ValueInstanceCount<T>(ValueInstanceCount<T>::kDeletedValue);
  }
  static bool IsDeletedValue(const ValueInstanceCount<T>& value) {
    return value.Counter() == ValueInstanceCount<T>::kDeletedValue;
  }
  static unsigned GetHash(const ValueInstanceCount<T>& value) {
    return WTF::GetHash(value.Counter());
  }
  static bool Equal(const ValueInstanceCount<T>& left,
                    const ValueInstanceCount<T>& right) {
    return left.Counter() == right.Counter();
  }
};

template <typename T>
struct HashTraits<ValueInstanceCount<T>>
    : public ValueInstanceCountHashTraits<T> {};

class DummyRefCounted : public RefCounted<DummyRefCounted> {
 public:
  DummyRefCounted(bool& is_deleted) : is_deleted_(is_deleted) {
    is_deleted_ = false;
  }
  ~DummyRefCounted() {
    DCHECK(!is_deleted_);
    is_deleted_ = true;
  }

  void AddRef() {
    DCHECK(!is_deleted_);
    WTF::RefCounted<DummyRefCounted>::AddRef();
    ++ref_invokes_count_;
  }

  void Release() {
    DCHECK(!is_deleted_);
    WTF::RefCounted<DummyRefCounted>::Release();
  }

  static int ref_invokes_count_;

 private:
  bool& is_deleted_;
};

struct Dummy {
  Dummy(bool& deleted) : deleted(deleted) {}

  ~Dummy() { deleted = true; }

  bool& deleted;
};

// WrappedInt class will fail if it was memmoved or memcpyed.
extern HashSet<void*> g_constructed_wrapped_ints;
class WrappedInt {
 public:
  WrappedInt(int i = 0) : original_this_ptr_(this), i_(i) {
    g_constructed_wrapped_ints.insert(this);
  }

  WrappedInt(const WrappedInt& other) : original_this_ptr_(this), i_(other.i_) {
    g_constructed_wrapped_ints.insert(this);
  }

  WrappedInt& operator=(const WrappedInt& other) {
    i_ = other.i_;
    return *this;
  }

  ~WrappedInt() {
    EXPECT_EQ(original_this_ptr_, this);
    EXPECT_TRUE(g_constructed_wrapped_ints.Contains(this));
    g_constructed_wrapped_ints.erase(this);
  }

  int Get() const { return i_; }

 private:
  void* original_this_ptr_;
  int i_;
};

class LivenessCounter {
 public:
  void AddRef() { live_++; }
  void Release() { live_--; }

  static unsigned live_;
};

}  // namespace WTF

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_WTF_TEST_HELPER_H_