span_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/types/span.h"
16 
17 #include <array>
18 #include <initializer_list>
19 #include <numeric>
20 #include <stdexcept>
21 #include <string>
22 #include <type_traits>
23 #include <vector>
24 
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27 #include "absl/base/attributes.h"
28 #include "absl/base/config.h"
32 #include "absl/hash/hash_testing.h"
33 #include "absl/strings/str_cat.h"
34 
35 namespace {
36 
37 MATCHER_P(DataIs, data,
38  absl::StrCat("data() is ", negation ? "is " : "isn't ",
39  testing::PrintToString(data))) {
40  return arg.data() == data;
41 }
42 
43 template <typename T>
44 auto SpanIs(T data, size_t size)
45  -> decltype(testing::AllOf(DataIs(data), testing::SizeIs(size))) {
46  return testing::AllOf(DataIs(data), testing::SizeIs(size));
47 }
48 
49 template <typename Container>
50 auto SpanIs(const Container& c) -> decltype(SpanIs(c.data(), c.size())) {
51  return SpanIs(c.data(), c.size());
52 }
53 
54 std::vector<int> MakeRamp(int len, int offset = 0) {
55  std::vector<int> v(len);
56  std::iota(v.begin(), v.end(), offset);
57  return v;
58 }
59 
60 TEST(IntSpan, EmptyCtors) {
62  EXPECT_THAT(s, SpanIs(nullptr, 0));
63 }
64 
65 TEST(IntSpan, PtrLenCtor) {
66  int a[] = {1, 2, 3};
67  absl::Span<int> s(&a[0], 2);
68  EXPECT_THAT(s, SpanIs(a, 2));
69 }
70 
71 TEST(IntSpan, ArrayCtor) {
72  int a[] = {1, 2, 3};
73  absl::Span<int> s(a);
74  EXPECT_THAT(s, SpanIs(a, 3));
75 
76  EXPECT_TRUE((std::is_constructible<absl::Span<const int>, int[3]>::value));
77  EXPECT_TRUE(
78  (std::is_constructible<absl::Span<const int>, const int[3]>::value));
79  EXPECT_FALSE((std::is_constructible<absl::Span<int>, const int[3]>::value));
80  EXPECT_TRUE((std::is_convertible<int[3], absl::Span<const int>>::value));
81  EXPECT_TRUE(
82  (std::is_convertible<const int[3], absl::Span<const int>>::value));
83 }
84 
85 template <typename T>
86 void TakesGenericSpan(absl::Span<T>) {}
87 
88 TEST(IntSpan, ContainerCtor) {
89  std::vector<int> empty;
90  absl::Span<int> s_empty(empty);
91  EXPECT_THAT(s_empty, SpanIs(empty));
92 
93  std::vector<int> filled{1, 2, 3};
94  absl::Span<int> s_filled(filled);
95  EXPECT_THAT(s_filled, SpanIs(filled));
96 
97  absl::Span<int> s_from_span(filled);
98  EXPECT_THAT(s_from_span, SpanIs(s_filled));
99 
100  absl::Span<const int> const_filled = filled;
101  EXPECT_THAT(const_filled, SpanIs(filled));
102 
103  absl::Span<const int> const_from_span = s_filled;
104  EXPECT_THAT(const_from_span, SpanIs(s_filled));
105 
106  EXPECT_TRUE(
107  (std::is_convertible<std::vector<int>&, absl::Span<const int>>::value));
108  EXPECT_TRUE(
109  (std::is_convertible<absl::Span<int>&, absl::Span<const int>>::value));
110 
111  TakesGenericSpan(absl::Span<int>(filled));
112 }
113 
114 // A struct supplying shallow data() const.
115 struct ContainerWithShallowConstData {
116  std::vector<int> storage;
117  int* data() const { return const_cast<int*>(storage.data()); }
118  int size() const { return storage.size(); }
119 };
120 
121 TEST(IntSpan, ShallowConstness) {
122  const ContainerWithShallowConstData c{MakeRamp(20)};
123  absl::Span<int> s(
124  c); // We should be able to do this even though data() is const.
125  s[0] = -1;
126  EXPECT_EQ(c.storage[0], -1);
127 }
128 
129 TEST(CharSpan, StringCtor) {
130  std::string empty = "";
131  absl::Span<char> s_empty(empty);
132  EXPECT_THAT(s_empty, SpanIs(empty));
133 
134  std::string abc = "abc";
135  absl::Span<char> s_abc(abc);
136  EXPECT_THAT(s_abc, SpanIs(abc));
137 
138  absl::Span<const char> s_const_abc = abc;
139  EXPECT_THAT(s_const_abc, SpanIs(abc));
140 
141  EXPECT_FALSE((std::is_constructible<absl::Span<int>, std::string>::value));
142  EXPECT_FALSE(
143  (std::is_constructible<absl::Span<const int>, std::string>::value));
144  EXPECT_TRUE(
145  (std::is_convertible<std::string, absl::Span<const char>>::value));
146 }
147 
148 TEST(IntSpan, FromConstPointer) {
149  EXPECT_TRUE((std::is_constructible<absl::Span<const int* const>,
150  std::vector<int*>>::value));
151  EXPECT_TRUE((std::is_constructible<absl::Span<const int* const>,
152  std::vector<const int*>>::value));
153  EXPECT_FALSE((
154  std::is_constructible<absl::Span<const int*>, std::vector<int*>>::value));
155  EXPECT_FALSE((
156  std::is_constructible<absl::Span<int*>, std::vector<const int*>>::value));
157 }
158 
159 struct TypeWithMisleadingData {
160  int& data() { return i; }
161  int size() { return 1; }
162  int i;
163 };
164 
165 struct TypeWithMisleadingSize {
166  int* data() { return &i; }
167  const char* size() { return "1"; }
168  int i;
169 };
170 
171 TEST(IntSpan, EvilTypes) {
172  EXPECT_FALSE(
173  (std::is_constructible<absl::Span<int>, TypeWithMisleadingData&>::value));
174  EXPECT_FALSE(
175  (std::is_constructible<absl::Span<int>, TypeWithMisleadingSize&>::value));
176 }
177 
178 struct Base {
179  int* data() { return &i; }
180  int size() { return 1; }
181  int i;
182 };
183 struct Derived : Base {};
184 
185 TEST(IntSpan, SpanOfDerived) {
186  EXPECT_TRUE((std::is_constructible<absl::Span<int>, Base&>::value));
187  EXPECT_TRUE((std::is_constructible<absl::Span<int>, Derived&>::value));
188  EXPECT_FALSE(
189  (std::is_constructible<absl::Span<Base>, std::vector<Derived>>::value));
190 }
191 
192 void TestInitializerList(absl::Span<const int> s, const std::vector<int>& v) {
193  EXPECT_TRUE(absl::equal(s.begin(), s.end(), v.begin(), v.end()));
194 }
195 
196 TEST(ConstIntSpan, InitializerListConversion) {
197  TestInitializerList({}, {});
198  TestInitializerList({1}, {1});
199  TestInitializerList({1, 2, 3}, {1, 2, 3});
200 
201  EXPECT_FALSE((std::is_constructible<absl::Span<int>,
202  std::initializer_list<int>>::value));
203  EXPECT_FALSE((
204  std::is_convertible<absl::Span<int>, std::initializer_list<int>>::value));
205 }
206 
207 TEST(IntSpan, Data) {
208  int i;
209  absl::Span<int> s(&i, 1);
210  EXPECT_EQ(&i, s.data());
211 }
212 
213 TEST(IntSpan, SizeLengthEmpty) {
214  absl::Span<int> empty;
215  EXPECT_EQ(empty.size(), 0);
216  EXPECT_TRUE(empty.empty());
217  EXPECT_EQ(empty.size(), empty.length());
218 
219  auto v = MakeRamp(10);
220  absl::Span<int> s(v);
221  EXPECT_EQ(s.size(), 10);
222  EXPECT_FALSE(s.empty());
223  EXPECT_EQ(s.size(), s.length());
224 }
225 
226 TEST(IntSpan, ElementAccess) {
227  auto v = MakeRamp(10);
228  absl::Span<int> s(v);
229  for (int i = 0; i < s.size(); ++i) {
230  EXPECT_EQ(s[i], s.at(i));
231  }
232 
233  EXPECT_EQ(s.front(), s[0]);
234  EXPECT_EQ(s.back(), s[9]);
235 }
236 
237 TEST(IntSpan, AtThrows) {
238  auto v = MakeRamp(10);
239  absl::Span<int> s(v);
240 
241  EXPECT_EQ(s.at(9), 9);
242  ABSL_BASE_INTERNAL_EXPECT_FAIL(s.at(10), std::out_of_range,
243  "failed bounds check");
244 }
245 
246 TEST(IntSpan, RemovePrefixAndSuffix) {
247  auto v = MakeRamp(20, 1);
248  absl::Span<int> s(v);
249  EXPECT_EQ(s.size(), 20);
250 
251  s.remove_suffix(0);
252  s.remove_prefix(0);
253  EXPECT_EQ(s.size(), 20);
254 
255  s.remove_prefix(1);
256  EXPECT_EQ(s.size(), 19);
257  EXPECT_EQ(s[0], 2);
258 
259  s.remove_suffix(1);
260  EXPECT_EQ(s.size(), 18);
261  EXPECT_EQ(s.back(), 19);
262 
263  s.remove_prefix(7);
264  EXPECT_EQ(s.size(), 11);
265  EXPECT_EQ(s[0], 9);
266 
267  s.remove_suffix(11);
268  EXPECT_EQ(s.size(), 0);
269 
270  EXPECT_EQ(v, MakeRamp(20, 1));
271 }
272 
273 TEST(IntSpan, Subspan) {
274  std::vector<int> empty;
275  EXPECT_EQ(absl::MakeSpan(empty).subspan(), empty);
276  EXPECT_THAT(absl::MakeSpan(empty).subspan(0, 0), SpanIs(empty));
277  EXPECT_THAT(absl::MakeSpan(empty).subspan(0, absl::Span<const int>::npos),
278  SpanIs(empty));
279 
280  auto ramp = MakeRamp(10);
281  EXPECT_THAT(absl::MakeSpan(ramp).subspan(), SpanIs(ramp));
282  EXPECT_THAT(absl::MakeSpan(ramp).subspan(0, 10), SpanIs(ramp));
283  EXPECT_THAT(absl::MakeSpan(ramp).subspan(0, absl::Span<const int>::npos),
284  SpanIs(ramp));
285  EXPECT_THAT(absl::MakeSpan(ramp).subspan(0, 3), SpanIs(ramp.data(), 3));
286  EXPECT_THAT(absl::MakeSpan(ramp).subspan(5, absl::Span<const int>::npos),
287  SpanIs(ramp.data() + 5, 5));
288  EXPECT_THAT(absl::MakeSpan(ramp).subspan(3, 3), SpanIs(ramp.data() + 3, 3));
289  EXPECT_THAT(absl::MakeSpan(ramp).subspan(10, 5), SpanIs(ramp.data() + 10, 0));
290 
291 #ifdef ABSL_HAVE_EXCEPTIONS
292  EXPECT_THROW(absl::MakeSpan(ramp).subspan(11, 5), std::out_of_range);
293 #else
294  EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(ramp).subspan(11, 5), "");
295 #endif
296 }
297 
298 TEST(IntSpan, First) {
299  std::vector<int> empty;
300  EXPECT_THAT(absl::MakeSpan(empty).first(0), SpanIs(empty));
301 
302  auto ramp = MakeRamp(10);
303  EXPECT_THAT(absl::MakeSpan(ramp).first(0), SpanIs(ramp.data(), 0));
304  EXPECT_THAT(absl::MakeSpan(ramp).first(10), SpanIs(ramp));
305  EXPECT_THAT(absl::MakeSpan(ramp).first(3), SpanIs(ramp.data(), 3));
306 
307 #ifdef ABSL_HAVE_EXCEPTIONS
308  EXPECT_THROW(absl::MakeSpan(ramp).first(11), std::out_of_range);
309 #else
310  EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(ramp).first(11), "");
311 #endif
312 }
313 
314 TEST(IntSpan, Last) {
315  std::vector<int> empty;
316  EXPECT_THAT(absl::MakeSpan(empty).last(0), SpanIs(empty));
317 
318  auto ramp = MakeRamp(10);
319  EXPECT_THAT(absl::MakeSpan(ramp).last(0), SpanIs(ramp.data() + 10, 0));
320  EXPECT_THAT(absl::MakeSpan(ramp).last(10), SpanIs(ramp));
321  EXPECT_THAT(absl::MakeSpan(ramp).last(3), SpanIs(ramp.data() + 7, 3));
322 
323 #ifdef ABSL_HAVE_EXCEPTIONS
324  EXPECT_THROW(absl::MakeSpan(ramp).last(11), std::out_of_range);
325 #else
326  EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(ramp).last(11), "");
327 #endif
328 }
329 
330 TEST(IntSpan, MakeSpanPtrLength) {
331  std::vector<int> empty;
332  auto s_empty = absl::MakeSpan(empty.data(), empty.size());
333  EXPECT_THAT(s_empty, SpanIs(empty));
334 
335  std::array<int, 3> a{{1, 2, 3}};
336  auto s = absl::MakeSpan(a.data(), a.size());
337  EXPECT_THAT(s, SpanIs(a));
338 
339  EXPECT_THAT(absl::MakeConstSpan(empty.data(), empty.size()), SpanIs(s_empty));
340  EXPECT_THAT(absl::MakeConstSpan(a.data(), a.size()), SpanIs(s));
341 }
342 
343 TEST(IntSpan, MakeSpanTwoPtrs) {
344  std::vector<int> empty;
345  auto s_empty = absl::MakeSpan(empty.data(), empty.data());
346  EXPECT_THAT(s_empty, SpanIs(empty));
347 
348  std::vector<int> v{1, 2, 3};
349  auto s = absl::MakeSpan(v.data(), v.data() + 1);
350  EXPECT_THAT(s, SpanIs(v.data(), 1));
351 
352  EXPECT_THAT(absl::MakeConstSpan(empty.data(), empty.data()), SpanIs(s_empty));
353  EXPECT_THAT(absl::MakeConstSpan(v.data(), v.data() + 1), SpanIs(s));
354 }
355 
356 TEST(IntSpan, MakeSpanContainer) {
357  std::vector<int> empty;
358  auto s_empty = absl::MakeSpan(empty);
359  EXPECT_THAT(s_empty, SpanIs(empty));
360 
361  std::vector<int> v{1, 2, 3};
362  auto s = absl::MakeSpan(v);
363  EXPECT_THAT(s, SpanIs(v));
364 
365  EXPECT_THAT(absl::MakeConstSpan(empty), SpanIs(s_empty));
366  EXPECT_THAT(absl::MakeConstSpan(v), SpanIs(s));
367 
368  EXPECT_THAT(absl::MakeSpan(s), SpanIs(s));
369  EXPECT_THAT(absl::MakeConstSpan(s), SpanIs(s));
370 }
371 
372 TEST(CharSpan, MakeSpanString) {
373  std::string empty = "";
374  auto s_empty = absl::MakeSpan(empty);
375  EXPECT_THAT(s_empty, SpanIs(empty));
376 
377  std::string str = "abc";
378  auto s_str = absl::MakeSpan(str);
379  EXPECT_THAT(s_str, SpanIs(str));
380 
381  EXPECT_THAT(absl::MakeConstSpan(empty), SpanIs(s_empty));
382  EXPECT_THAT(absl::MakeConstSpan(str), SpanIs(s_str));
383 }
384 
385 TEST(IntSpan, MakeSpanArray) {
386  int a[] = {1, 2, 3};
387  auto s = absl::MakeSpan(a);
388  EXPECT_THAT(s, SpanIs(a, 3));
389 
390  const int ca[] = {1, 2, 3};
391  auto s_ca = absl::MakeSpan(ca);
392  EXPECT_THAT(s_ca, SpanIs(ca, 3));
393 
394  EXPECT_THAT(absl::MakeConstSpan(a), SpanIs(s));
395  EXPECT_THAT(absl::MakeConstSpan(ca), SpanIs(s_ca));
396 }
397 
398 // Compile-asserts that the argument has the expected decayed type.
399 template <typename Expected, typename T>
400 void CheckType(const T& /* value */) {
401  testing::StaticAssertTypeEq<Expected, T>();
402 }
403 
404 TEST(IntSpan, MakeSpanTypes) {
405  std::vector<int> vec;
406  const std::vector<int> cvec;
407  int a[1];
408  const int ca[] = {1};
409  int* ip = a;
410  const int* cip = ca;
411  std::string s = "";
412  const std::string cs = "";
413  CheckType<absl::Span<int>>(absl::MakeSpan(vec));
414  CheckType<absl::Span<const int>>(absl::MakeSpan(cvec));
415  CheckType<absl::Span<int>>(absl::MakeSpan(ip, ip + 1));
416  CheckType<absl::Span<int>>(absl::MakeSpan(ip, 1));
417  CheckType<absl::Span<const int>>(absl::MakeSpan(cip, cip + 1));
418  CheckType<absl::Span<const int>>(absl::MakeSpan(cip, 1));
419  CheckType<absl::Span<int>>(absl::MakeSpan(a));
420  CheckType<absl::Span<int>>(absl::MakeSpan(a, a + 1));
421  CheckType<absl::Span<int>>(absl::MakeSpan(a, 1));
422  CheckType<absl::Span<const int>>(absl::MakeSpan(ca));
423  CheckType<absl::Span<const int>>(absl::MakeSpan(ca, ca + 1));
424  CheckType<absl::Span<const int>>(absl::MakeSpan(ca, 1));
425  CheckType<absl::Span<char>>(absl::MakeSpan(s));
426  CheckType<absl::Span<const char>>(absl::MakeSpan(cs));
427 }
428 
429 TEST(ConstIntSpan, MakeConstSpanTypes) {
430  std::vector<int> vec;
431  const std::vector<int> cvec;
432  int array[1];
433  const int carray[] = {0};
434  int* ptr = array;
435  const int* cptr = carray;
436  std::string s = "";
437  std::string cs = "";
438  CheckType<absl::Span<const int>>(absl::MakeConstSpan(vec));
439  CheckType<absl::Span<const int>>(absl::MakeConstSpan(cvec));
440  CheckType<absl::Span<const int>>(absl::MakeConstSpan(ptr, ptr + 1));
441  CheckType<absl::Span<const int>>(absl::MakeConstSpan(ptr, 1));
442  CheckType<absl::Span<const int>>(absl::MakeConstSpan(cptr, cptr + 1));
443  CheckType<absl::Span<const int>>(absl::MakeConstSpan(cptr, 1));
444  CheckType<absl::Span<const int>>(absl::MakeConstSpan(array));
445  CheckType<absl::Span<const int>>(absl::MakeConstSpan(carray));
446  CheckType<absl::Span<const char>>(absl::MakeConstSpan(s));
447  CheckType<absl::Span<const char>>(absl::MakeConstSpan(cs));
448 }
449 
450 TEST(IntSpan, Equality) {
451  const int arr1[] = {1, 2, 3, 4, 5};
452  int arr2[] = {1, 2, 3, 4, 5};
453  std::vector<int> vec1(std::begin(arr1), std::end(arr1));
454  std::vector<int> vec2 = vec1;
455  std::vector<int> other_vec = {2, 4, 6, 8, 10};
456  // These two slices are from different vectors, but have the same size and
457  // have the same elements (right now). They should compare equal. Test both
458  // == and !=.
459  const absl::Span<const int> from1 = vec1;
460  const absl::Span<const int> from2 = vec2;
461  EXPECT_EQ(from1, from1);
462  EXPECT_FALSE(from1 != from1);
463  EXPECT_EQ(from1, from2);
464  EXPECT_FALSE(from1 != from2);
465 
466  // These two slices have different underlying vector values. They should be
467  // considered not equal. Test both == and !=.
468  const absl::Span<const int> from_other = other_vec;
469  EXPECT_NE(from1, from_other);
470  EXPECT_FALSE(from1 == from_other);
471 
472  // Comparison between a vector and its slice should be equal. And vice-versa.
473  // This ensures implicit conversion to Span works on both sides of ==.
474  EXPECT_EQ(vec1, from1);
475  EXPECT_FALSE(vec1 != from1);
476  EXPECT_EQ(from1, vec1);
477  EXPECT_FALSE(from1 != vec1);
478 
479  // This verifies that absl::Span<T> can be compared freely with
480  // absl::Span<const T>.
481  const absl::Span<int> mutable_from1(vec1);
482  const absl::Span<int> mutable_from2(vec2);
483  EXPECT_EQ(from1, mutable_from1);
484  EXPECT_EQ(mutable_from1, from1);
485  EXPECT_EQ(mutable_from1, mutable_from2);
486  EXPECT_EQ(mutable_from2, mutable_from1);
487 
488  // Comparison between a vector and its slice should be equal for mutable
489  // Spans as well.
490  EXPECT_EQ(vec1, mutable_from1);
491  EXPECT_FALSE(vec1 != mutable_from1);
492  EXPECT_EQ(mutable_from1, vec1);
493  EXPECT_FALSE(mutable_from1 != vec1);
494 
495  // Comparison between convertible-to-Span-of-const and Span-of-mutable. Arrays
496  // are used because they're the only value type which converts to a
497  // Span-of-mutable. EXPECT_TRUE is used instead of EXPECT_EQ to avoid
498  // array-to-pointer decay.
499  EXPECT_TRUE(arr1 == mutable_from1);
500  EXPECT_FALSE(arr1 != mutable_from1);
501  EXPECT_TRUE(mutable_from1 == arr1);
502  EXPECT_FALSE(mutable_from1 != arr1);
503 
504  // Comparison between convertible-to-Span-of-mutable and Span-of-const
505  EXPECT_TRUE(arr2 == from1);
506  EXPECT_FALSE(arr2 != from1);
507  EXPECT_TRUE(from1 == arr2);
508  EXPECT_FALSE(from1 != arr2);
509 
510  // With a different size, the array slices should not be equal.
511  EXPECT_NE(from1, absl::Span<const int>(from1).subspan(0, from1.size() - 1));
512 
513  // With different contents, the array slices should not be equal.
514  ++vec2.back();
515  EXPECT_NE(from1, from2);
516 }
517 
518 class IntSpanOrderComparisonTest : public testing::Test {
519  public:
520  IntSpanOrderComparisonTest()
521  : arr_before_{1, 2, 3},
522  arr_after_{1, 2, 4},
523  carr_after_{1, 2, 4},
524  vec_before_(std::begin(arr_before_), std::end(arr_before_)),
525  vec_after_(std::begin(arr_after_), std::end(arr_after_)),
526  before_(vec_before_),
527  after_(vec_after_),
528  cbefore_(vec_before_),
529  cafter_(vec_after_) {}
530 
531  protected:
532  int arr_before_[3], arr_after_[3];
533  const int carr_after_[3];
534  std::vector<int> vec_before_, vec_after_;
535  absl::Span<int> before_, after_;
536  absl::Span<const int> cbefore_, cafter_;
537 };
538 
539 TEST_F(IntSpanOrderComparisonTest, CompareSpans) {
540  EXPECT_TRUE(cbefore_ < cafter_);
541  EXPECT_TRUE(cbefore_ <= cafter_);
542  EXPECT_TRUE(cafter_ > cbefore_);
543  EXPECT_TRUE(cafter_ >= cbefore_);
544 
545  EXPECT_FALSE(cbefore_ > cafter_);
546  EXPECT_FALSE(cafter_ < cbefore_);
547 
548  EXPECT_TRUE(before_ < after_);
549  EXPECT_TRUE(before_ <= after_);
550  EXPECT_TRUE(after_ > before_);
551  EXPECT_TRUE(after_ >= before_);
552 
553  EXPECT_FALSE(before_ > after_);
554  EXPECT_FALSE(after_ < before_);
555 
556  EXPECT_TRUE(cbefore_ < after_);
557  EXPECT_TRUE(cbefore_ <= after_);
558  EXPECT_TRUE(after_ > cbefore_);
559  EXPECT_TRUE(after_ >= cbefore_);
560 
561  EXPECT_FALSE(cbefore_ > after_);
562  EXPECT_FALSE(after_ < cbefore_);
563 }
564 
565 TEST_F(IntSpanOrderComparisonTest, SpanOfConstAndContainer) {
566  EXPECT_TRUE(cbefore_ < vec_after_);
567  EXPECT_TRUE(cbefore_ <= vec_after_);
568  EXPECT_TRUE(vec_after_ > cbefore_);
569  EXPECT_TRUE(vec_after_ >= cbefore_);
570 
571  EXPECT_FALSE(cbefore_ > vec_after_);
572  EXPECT_FALSE(vec_after_ < cbefore_);
573 
574  EXPECT_TRUE(arr_before_ < cafter_);
575  EXPECT_TRUE(arr_before_ <= cafter_);
576  EXPECT_TRUE(cafter_ > arr_before_);
577  EXPECT_TRUE(cafter_ >= arr_before_);
578 
579  EXPECT_FALSE(arr_before_ > cafter_);
580  EXPECT_FALSE(cafter_ < arr_before_);
581 }
582 
583 TEST_F(IntSpanOrderComparisonTest, SpanOfMutableAndContainer) {
584  EXPECT_TRUE(vec_before_ < after_);
585  EXPECT_TRUE(vec_before_ <= after_);
586  EXPECT_TRUE(after_ > vec_before_);
587  EXPECT_TRUE(after_ >= vec_before_);
588 
589  EXPECT_FALSE(vec_before_ > after_);
590  EXPECT_FALSE(after_ < vec_before_);
591 
592  EXPECT_TRUE(before_ < carr_after_);
593  EXPECT_TRUE(before_ <= carr_after_);
594  EXPECT_TRUE(carr_after_ > before_);
595  EXPECT_TRUE(carr_after_ >= before_);
596 
597  EXPECT_FALSE(before_ > carr_after_);
598  EXPECT_FALSE(carr_after_ < before_);
599 }
600 
601 TEST_F(IntSpanOrderComparisonTest, EqualSpans) {
602  EXPECT_FALSE(before_ < before_);
603  EXPECT_TRUE(before_ <= before_);
604  EXPECT_FALSE(before_ > before_);
605  EXPECT_TRUE(before_ >= before_);
606 }
607 
608 TEST_F(IntSpanOrderComparisonTest, Subspans) {
609  auto subspan = before_.subspan(0, 1);
610  EXPECT_TRUE(subspan < before_);
611  EXPECT_TRUE(subspan <= before_);
612  EXPECT_TRUE(before_ > subspan);
613  EXPECT_TRUE(before_ >= subspan);
614 
615  EXPECT_FALSE(subspan > before_);
616  EXPECT_FALSE(before_ < subspan);
617 }
618 
619 TEST_F(IntSpanOrderComparisonTest, EmptySpans) {
620  absl::Span<int> empty;
621  EXPECT_FALSE(empty < empty);
622  EXPECT_TRUE(empty <= empty);
623  EXPECT_FALSE(empty > empty);
624  EXPECT_TRUE(empty >= empty);
625 
626  EXPECT_TRUE(empty < before_);
627  EXPECT_TRUE(empty <= before_);
628  EXPECT_TRUE(before_ > empty);
629  EXPECT_TRUE(before_ >= empty);
630 
631  EXPECT_FALSE(empty > before_);
632  EXPECT_FALSE(before_ < empty);
633 }
634 
635 TEST(IntSpan, ExposesContainerTypesAndConsts) {
636  absl::Span<int> slice;
637  CheckType<absl::Span<int>::iterator>(slice.begin());
638  EXPECT_TRUE((std::is_convertible<decltype(slice.begin()),
640  CheckType<absl::Span<int>::const_iterator>(slice.cbegin());
641  EXPECT_TRUE((std::is_convertible<decltype(slice.end()),
643  CheckType<absl::Span<int>::const_iterator>(slice.cend());
644  CheckType<absl::Span<int>::reverse_iterator>(slice.rend());
645  EXPECT_TRUE(
646  (std::is_convertible<decltype(slice.rend()),
648  CheckType<absl::Span<int>::const_reverse_iterator>(slice.crend());
651  testing::StaticAssertTypeEq<int*, absl::Span<int>::pointer>();
652  testing::StaticAssertTypeEq<const int*, absl::Span<const int>::pointer>();
653  testing::StaticAssertTypeEq<int&, absl::Span<int>::reference>();
654  testing::StaticAssertTypeEq<const int&, absl::Span<const int>::reference>();
655  testing::StaticAssertTypeEq<const int&, absl::Span<int>::const_reference>();
656  testing::StaticAssertTypeEq<const int&,
658  EXPECT_EQ(static_cast<absl::Span<int>::size_type>(-1), absl::Span<int>::npos);
659 }
660 
661 TEST(IntSpan, IteratorsAndReferences) {
662  auto accept_pointer = [](int*) {};
663  auto accept_reference = [](int&) {};
664  auto accept_iterator = [](absl::Span<int>::iterator) {};
665  auto accept_const_iterator = [](absl::Span<int>::const_iterator) {};
666  auto accept_reverse_iterator = [](absl::Span<int>::reverse_iterator) {};
667  auto accept_const_reverse_iterator =
669 
670  int a[1];
671  absl::Span<int> s = a;
672 
673  accept_pointer(s.data());
674  accept_iterator(s.begin());
675  accept_const_iterator(s.begin());
676  accept_const_iterator(s.cbegin());
677  accept_iterator(s.end());
678  accept_const_iterator(s.end());
679  accept_const_iterator(s.cend());
680  accept_reverse_iterator(s.rbegin());
681  accept_const_reverse_iterator(s.rbegin());
682  accept_const_reverse_iterator(s.crbegin());
683  accept_reverse_iterator(s.rend());
684  accept_const_reverse_iterator(s.rend());
685  accept_const_reverse_iterator(s.crend());
686 
687  accept_reference(s[0]);
688  accept_reference(s.at(0));
689  accept_reference(s.front());
690  accept_reference(s.back());
691 }
692 
693 TEST(IntSpan, IteratorsAndReferences_Const) {
694  auto accept_pointer = [](int*) {};
695  auto accept_reference = [](int&) {};
696  auto accept_iterator = [](absl::Span<int>::iterator) {};
697  auto accept_const_iterator = [](absl::Span<int>::const_iterator) {};
698  auto accept_reverse_iterator = [](absl::Span<int>::reverse_iterator) {};
699  auto accept_const_reverse_iterator =
701 
702  int a[1];
703  const absl::Span<int> s = a;
704 
705  accept_pointer(s.data());
706  accept_iterator(s.begin());
707  accept_const_iterator(s.begin());
708  accept_const_iterator(s.cbegin());
709  accept_iterator(s.end());
710  accept_const_iterator(s.end());
711  accept_const_iterator(s.cend());
712  accept_reverse_iterator(s.rbegin());
713  accept_const_reverse_iterator(s.rbegin());
714  accept_const_reverse_iterator(s.crbegin());
715  accept_reverse_iterator(s.rend());
716  accept_const_reverse_iterator(s.rend());
717  accept_const_reverse_iterator(s.crend());
718 
719  accept_reference(s[0]);
720  accept_reference(s.at(0));
721  accept_reference(s.front());
722  accept_reference(s.back());
723 }
724 
725 TEST(IntSpan, NoexceptTest) {
726  int a[] = {1, 2, 3};
727  std::vector<int> v;
728  EXPECT_TRUE(noexcept(absl::Span<const int>()));
729  EXPECT_TRUE(noexcept(absl::Span<const int>(a, 2)));
730  EXPECT_TRUE(noexcept(absl::Span<const int>(a)));
731  EXPECT_TRUE(noexcept(absl::Span<const int>(v)));
732  EXPECT_TRUE(noexcept(absl::Span<int>(v)));
733  EXPECT_TRUE(noexcept(absl::Span<const int>({1, 2, 3})));
734  EXPECT_TRUE(noexcept(absl::MakeSpan(v)));
735  EXPECT_TRUE(noexcept(absl::MakeSpan(a)));
736  EXPECT_TRUE(noexcept(absl::MakeSpan(a, 2)));
737  EXPECT_TRUE(noexcept(absl::MakeSpan(a, a + 1)));
738  EXPECT_TRUE(noexcept(absl::MakeConstSpan(v)));
739  EXPECT_TRUE(noexcept(absl::MakeConstSpan(a)));
740  EXPECT_TRUE(noexcept(absl::MakeConstSpan(a, 2)));
741  EXPECT_TRUE(noexcept(absl::MakeConstSpan(a, a + 1)));
742 
743  absl::Span<int> s(v);
744  EXPECT_TRUE(noexcept(s.data()));
745  EXPECT_TRUE(noexcept(s.size()));
746  EXPECT_TRUE(noexcept(s.length()));
747  EXPECT_TRUE(noexcept(s.empty()));
748  EXPECT_TRUE(noexcept(s[0]));
749  EXPECT_TRUE(noexcept(s.front()));
750  EXPECT_TRUE(noexcept(s.back()));
751  EXPECT_TRUE(noexcept(s.begin()));
752  EXPECT_TRUE(noexcept(s.cbegin()));
753  EXPECT_TRUE(noexcept(s.end()));
754  EXPECT_TRUE(noexcept(s.cend()));
755  EXPECT_TRUE(noexcept(s.rbegin()));
756  EXPECT_TRUE(noexcept(s.crbegin()));
757  EXPECT_TRUE(noexcept(s.rend()));
758  EXPECT_TRUE(noexcept(s.crend()));
759  EXPECT_TRUE(noexcept(s.remove_prefix(0)));
760  EXPECT_TRUE(noexcept(s.remove_suffix(0)));
761 }
762 
763 // ConstexprTester exercises expressions in a constexpr context. Simply placing
764 // the expression in a constexpr function is not enough, as some compilers will
765 // simply compile the constexpr function as runtime code. Using template
766 // parameters forces compile-time execution.
767 template <int i>
768 struct ConstexprTester {};
769 
770 #define ABSL_TEST_CONSTEXPR(expr) \
771  do { \
772  ABSL_ATTRIBUTE_UNUSED ConstexprTester<(expr, 1)> t; \
773  } while (0)
774 
775 struct ContainerWithConstexprMethods {
776  constexpr int size() const { return 1; }
777  constexpr const int* data() const { return &i; }
778  const int i;
779 };
780 
781 TEST(ConstIntSpan, ConstexprTest) {
782  static constexpr int a[] = {1, 2, 3};
783  static constexpr int sized_arr[2] = {1, 2};
784  static constexpr ContainerWithConstexprMethods c{1};
795 
796  constexpr absl::Span<const int> span = c;
797  ABSL_TEST_CONSTEXPR(span.data());
798  ABSL_TEST_CONSTEXPR(span.size());
799  ABSL_TEST_CONSTEXPR(span.length());
800  ABSL_TEST_CONSTEXPR(span.empty());
801  ABSL_TEST_CONSTEXPR(span.begin());
802  ABSL_TEST_CONSTEXPR(span.cbegin());
803  ABSL_TEST_CONSTEXPR(span.subspan(0, 0));
804  ABSL_TEST_CONSTEXPR(span.first(1));
805  ABSL_TEST_CONSTEXPR(span.last(1));
806  ABSL_TEST_CONSTEXPR(span[0]);
807 }
808 
809 struct BigStruct {
810  char bytes[10000];
811 };
812 
813 TEST(Span, SpanSize) {
814  EXPECT_LE(sizeof(absl::Span<int>), 2 * sizeof(void*));
815  EXPECT_LE(sizeof(absl::Span<BigStruct>), 2 * sizeof(void*));
816 }
817 
818 TEST(Span, Hash) {
819  int array[] = {1, 2, 3, 4};
820  int array2[] = {1, 2, 3};
821  using T = absl::Span<const int>;
823  {// Empties
824  T(), T(nullptr, 0), T(array, 0), T(array2, 0),
825  // Different array with same value
826  T(array, 3), T(array2), T({1, 2, 3}),
827  // Same array, but different length
828  T(array, 1), T(array, 2),
829  // Same length, but different array
830  T(array + 1, 2), T(array + 2, 2)}));
831 }
832 
833 } // namespace
int v
Definition: variant_test.cc:81
std::reverse_iterator< iterator > reverse_iterator
Definition: span.h:180
constexpr const_iterator cbegin() const noexcept
Definition: span.h:314
const_pointer const_iterator
Definition: span.h:179
char * begin
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: span.h:181
constexpr reverse_iterator rbegin() const noexcept
Definition: span.h:329
#define ABSL_TEST_CONSTEXPR(expr)
Definition: span_test.cc:770
constexpr reference at(size_type i) const
Definition: span.h:284
void remove_prefix(size_type n) noexcept
Definition: span.h:355
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: str_cat.cc:98
constexpr reverse_iterator rend() const noexcept
Definition: span.h:341
constexpr pointer data() const noexcept
Definition: span.h:256
constexpr iterator end() const noexcept
Definition: span.h:319
constexpr bool empty() const noexcept
Definition: span.h:271
constexpr size_type length() const noexcept
Definition: span.h:266
char * end
constexpr reference back() const noexcept
Definition: span.h:302
constexpr reference front() const noexcept
Definition: span.h:295
constexpr size_type size() const noexcept
Definition: span.h:261
void remove_suffix(size_type n) noexcept
Definition: span.h:364
constexpr iterator begin() const noexcept
Definition: span.h:309
#define ABSL_BASE_INTERNAL_EXPECT_FAIL(expr, exception_t, text)
char * ptr
size_t value
static char data[kDataSize]
Definition: city_test.cc:31
const T & const_reference
Definition: span.h:177
TEST_F(GraphCyclesTest, NoCycle)
constexpr const_reverse_iterator crend() const noexcept
Definition: span.h:348
pointer iterator
Definition: span.h:178
bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Pred &&pred)
Definition: algorithm.h:99
ABSL_MUST_USE_RESULT testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(const Container &values)
Definition: hash_testing.h:344
constexpr Span< const T > MakeConstSpan(T *ptr, size_t size) noexcept
Definition: span.h:692
uintptr_t size
constexpr bool AllOf()
Definition: checker.h:20
void * arg
Definition: mutex.cc:292
TEST(Symbolize, Unimplemented)
constexpr Span< T > MakeSpan(T *ptr, size_t size) noexcept
Definition: span.h:647
size_t size_type
Definition: span.h:182
constexpr const_reverse_iterator crbegin() const noexcept
Definition: span.h:336
constexpr const_iterator cend() const noexcept
Definition: span.h:324


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:57