File: auto_spanification_helper_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 (267 lines) | stat: -rw-r--r-- 8,536 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/containers/auto_spanification_helper.h"

#include <array>
#include <cstdint>

#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
namespace {

TEST(AutoSpanificationIncrementTest, PreIncrementSpan) {
  std::vector<int> data = {1, 2, 3, 4, 5};
  span<int> s(data);

  span<int> result = PreIncrementSpan(s);
  EXPECT_THAT(s, testing::ElementsAre(2, 3, 4, 5));

  EXPECT_EQ(result.data(), s.data());
  EXPECT_EQ(result.size(), s.size());
}

TEST(AutoSpanificationIncrementTest, PreIncrementSingleElementSpan) {
  std::vector<int> single_element_data = {42};
  span<int> s(single_element_data);

  span<int> result = PreIncrementSpan(s);

  EXPECT_TRUE(s.empty());
  EXPECT_EQ(s.size(), 0u);

  EXPECT_TRUE(result.empty());
  EXPECT_EQ(result.size(), 0u);
}

TEST(AutoSpanificationIncrementTest, PreIncrementEmptySpan) {
  std::vector<int> empty_data;
  span<int> s(empty_data);

  // An iterator that is at the end is expressed as an empty span and it shall
  // not be incremented. Expect a CHECK failure when trying to pre-increment an
  // empty span.
  ASSERT_DEATH_IF_SUPPORTED({ PreIncrementSpan(s); }, "");
}

TEST(AutoSpanificationIncrementTest, PreIncrementConstSpan) {
  const std::vector<int> data = {1, 2, 3, 4, 5};
  span<const int> s(data);

  span<const int> result = PreIncrementSpan(s);

  EXPECT_THAT(s, testing::ElementsAre(2, 3, 4, 5));

  EXPECT_EQ(result.data(), s.data());
  EXPECT_EQ(result.size(), s.size());
}

TEST(AutoSpanificationIncrementTest, PostIncrementSpan) {
  std::vector<int> data = {1, 2, 3, 4, 5};
  span<int> s(data);

  span<int> result = PostIncrementSpan(s);

  EXPECT_THAT(result, testing::ElementsAre(1, 2, 3, 4, 5));
  EXPECT_THAT(s, testing::ElementsAre(2, 3, 4, 5));
}

TEST(AutoSpanificationIncrementTest, PostIncrementSingleElementSpan) {
  std::vector<int> single_element_data = {42};
  span<int> s(single_element_data);

  span<int> result = PostIncrementSpan(s);

  EXPECT_EQ(result.size(), 1u);
  EXPECT_EQ(result[0], 42);

  EXPECT_TRUE(s.empty());
  EXPECT_EQ(s.size(), 0u);
}

TEST(AutoSpanificationIncrementTest, PostIncrementEmptySpan) {
  std::vector<int> empty_data;
  span<int> s(empty_data);

  // An iterator that is at the end is expressed as an empty span and it shall
  // not be incremented. Expect a CHECK failure when trying to post-increment an
  // empty span.
  ASSERT_DEATH_IF_SUPPORTED({ PostIncrementSpan(s); }, "");
}

TEST(AutoSpanificationIncrementTest, PostIncrementConstSpan) {
  const std::vector<int> data = {1, 2, 3, 4, 5};
  span<const int> s(data);

  span<const int> result = PostIncrementSpan(s);

  EXPECT_THAT(result, testing::ElementsAre(1, 2, 3, 4, 5));

  EXPECT_EQ(s.size(), 4u);
  EXPECT_THAT(s, testing::ElementsAre(2, 3, 4, 5));
}

}  // namespace
}  // namespace base

namespace base::internal::spanification {

namespace {

TEST(AutoSpanificationHelperTest, SpanificationSizeofForStdArray) {
  std::array<char, 7> char_array;
  EXPECT_EQ(SpanificationSizeofForStdArray(char_array), 7);

  std::array<uint16_t, 3> uint16_array;
  EXPECT_EQ(SpanificationSizeofForStdArray(uint16_array), sizeof(uint16_t) * 3);
}

// Minimized mock of SkBitmap class defined in
// //third_party/skia/include/core/SkBitmap.h
class SkBitmap {
 public:
  uint32_t* getAddr32(int x, int y) const { return &row_[x]; }
  int width() const { return static_cast<int>(row_.size()); }

  mutable std::array<uint32_t, 128> row_{};
};

// The main purpose of the following test cases is to exercise C++ compilation
// on the macro usage rather than testing their behaviors.

TEST(AutoSpanificationHelperTest, SkBitmapGetAddr32Pointer) {
  SkBitmap sk_bitmap;
  const int x = 123;
  base::span<uint32_t> span = UNSAFE_SKBITMAP_GETADDR32(&sk_bitmap, x, 0);
  EXPECT_EQ(span.data(), &sk_bitmap.row_[x]);
  EXPECT_EQ(span.size(), sk_bitmap.row_.size() - x);
}

TEST(AutoSpanificationHelperTest, SkBitmapGetAddr32Reference) {
  SkBitmap sk_bitmap;
  const int x = 123;
  base::span<uint32_t> span = UNSAFE_SKBITMAP_GETADDR32(sk_bitmap, x, 0);
  EXPECT_EQ(span.data(), &sk_bitmap.row_[x]);
  EXPECT_EQ(span.size(), sk_bitmap.row_.size() - x);
}

TEST(AutoSpanificationHelperTest, SkBitmapGetAddr32SmartPtr) {
  std::unique_ptr<SkBitmap> sk_bitmap = std::make_unique<SkBitmap>();
  const int x = 123;
  base::span<uint32_t> span = UNSAFE_SKBITMAP_GETADDR32(sk_bitmap, x, 0);
  EXPECT_EQ(span.data(), &sk_bitmap->row_[x]);
  EXPECT_EQ(span.size(), sk_bitmap->row_.size() - x);
}

// Minimized mock of CRYPTO_BUFFER_data and CRYPTO_BUFFER_len defined in
// //third_party/boringssl/src/include/openssl/pool.h
struct CRYPTO_BUFFER {
  base::raw_ptr<uint8_t> data = nullptr;
  size_t len = 0;
};
const uint8_t* CRYPTO_BUFFER_data(const CRYPTO_BUFFER* buf) {
  return buf->data.get();
}
size_t CRYPTO_BUFFER_len(const CRYPTO_BUFFER* buf) {
  return buf->len;
}

TEST(AutoSpanificationHelperTest, CryptoBufferData) {
  std::array<uint8_t, 128> array;
  CRYPTO_BUFFER buffer = {array.data(), array.size()};

  base::span<const uint8_t> span = UNSAFE_CRYPTO_BUFFER_DATA(&buffer);
  EXPECT_EQ(span.data(), array.data());
  EXPECT_EQ(span.size(), array.size());
}

// Minimized mock of hb_buffer_get_glyph_infos and
// hb_buffer_get_glyph_positions defined in
// //third_party/harfbuzz-ng/src/src/hb-buffer.h
struct hb_glyph_info_t {};
struct hb_glyph_position_t {};
struct hb_buffer_t {
  base::raw_ptr<hb_glyph_info_t> info = nullptr;
  base::raw_ptr<hb_glyph_position_t> pos = nullptr;
  unsigned int len = 0;
};
hb_glyph_info_t* hb_buffer_get_glyph_infos(hb_buffer_t* buffer,
                                           unsigned int* length) {
  if (length) {
    *length = buffer->len;
  }
  return buffer->info.get();
}
hb_glyph_position_t* hb_buffer_get_glyph_positions(hb_buffer_t* buffer,
                                                   unsigned int* length) {
  if (length) {
    *length = buffer->len;
  }
  return buffer->pos.get();
}

TEST(AutoSpanificationHelperTest, HbBufferGetGlyphInfos) {
  std::array<hb_glyph_info_t, 128> info_array;
  hb_buffer_t buffer;
  unsigned int length = 0;
  base::span<hb_glyph_info_t> infos;

  buffer = {.info = info_array.data(), .len = info_array.size()};
  infos = UNSAFE_HB_BUFFER_GET_GLYPH_INFOS(&buffer, &length);
  EXPECT_EQ(infos.data(), info_array.data());
  EXPECT_EQ(infos.size(), info_array.size());
  EXPECT_EQ(length, info_array.size());

  infos = UNSAFE_HB_BUFFER_GET_GLYPH_INFOS(&buffer, nullptr);
  EXPECT_EQ(infos.data(), info_array.data());
  EXPECT_EQ(infos.size(), info_array.size());
}

TEST(AutoSpanificationHelperTest, HbBufferGetGlyphPositions) {
  std::array<hb_glyph_position_t, 128> pos_array;
  hb_buffer_t buffer;
  unsigned int length = 0;
  base::span<hb_glyph_position_t> positions;

  buffer = {.pos = pos_array.data(), .len = pos_array.size()};
  positions = UNSAFE_HB_BUFFER_GET_GLYPH_POSITIONS(&buffer, &length);
  EXPECT_EQ(positions.data(), pos_array.data());
  EXPECT_EQ(positions.size(), pos_array.size());
  EXPECT_EQ(length, pos_array.size());

  buffer = {.pos = pos_array.data(), .len = pos_array.size()};
  positions = UNSAFE_HB_BUFFER_GET_GLYPH_POSITIONS(&buffer, /*length=*/nullptr);
  EXPECT_EQ(positions.data(), pos_array.data());
  EXPECT_EQ(positions.size(), pos_array.size());

  buffer = {.pos = nullptr,
            .len = pos_array.size()};  // pos == nullptr, len != 0
  positions = UNSAFE_HB_BUFFER_GET_GLYPH_POSITIONS(&buffer, &length);
  EXPECT_EQ(positions.data(), nullptr);
  EXPECT_EQ(positions.size(), 0);  // The span's size is 0
  EXPECT_NE(length, 0);            // even when `length` is non-zero.
}

// Minimized mock of g_get_system_data_dirs
// https://web.mit.edu/barnowl/share/gtk-doc/html/glib/glib-Miscellaneous-Utility-Functions.html#g-get-system-data-dirs
using gchar = char;
constexpr auto kGlibSystemDataDirs =
    std::to_array<const gchar* const>({"foo", "bar", "baz", nullptr});
const gchar* const* g_get_system_data_dirs() {
  return kGlibSystemDataDirs.data();
}

TEST(AutoSpanificationHelperTest, GGetSystemDataDirs) {
  base::span<const gchar* const> dirs = UNSAFE_G_GET_SYSTEM_DATA_DIRS();
  EXPECT_EQ(dirs.data(), kGlibSystemDataDirs.data());
  EXPECT_EQ(dirs.size(), kGlibSystemDataDirs.size());
}

}  // namespace

}  // namespace base::internal::spanification