18 #include <initializer_list> 22 #include <type_traits> 25 #include "gmock/gmock.h" 26 #include "gtest/gtest.h" 37 MATCHER_P(DataIs,
data,
39 testing::PrintToString(
data))) {
49 template <
typename Container>
50 auto SpanIs(
const Container& c) -> decltype(SpanIs(c.data(), c.size())) {
51 return SpanIs(c.data(), c.size());
54 std::vector<int> MakeRamp(
int len,
int offset = 0) {
55 std::vector<int>
v(len);
56 std::iota(
v.begin(),
v.end(), offset);
60 TEST(IntSpan, EmptyCtors) {
62 EXPECT_THAT(s, SpanIs(
nullptr, 0));
65 TEST(IntSpan, PtrLenCtor) {
68 EXPECT_THAT(s, SpanIs(a, 2));
71 TEST(IntSpan, ArrayCtor) {
74 EXPECT_THAT(s, SpanIs(a, 3));
88 TEST(IntSpan, ContainerCtor) {
89 std::vector<int> empty;
91 EXPECT_THAT(s_empty, SpanIs(empty));
93 std::vector<int> filled{1, 2, 3};
95 EXPECT_THAT(s_filled, SpanIs(filled));
98 EXPECT_THAT(s_from_span, SpanIs(s_filled));
101 EXPECT_THAT(const_filled, SpanIs(filled));
104 EXPECT_THAT(const_from_span, SpanIs(s_filled));
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(); }
121 TEST(IntSpan, ShallowConstness) {
122 const ContainerWithShallowConstData c{MakeRamp(20)};
126 EXPECT_EQ(c.storage[0], -1);
129 TEST(CharSpan, StringCtor) {
130 std::string empty =
"";
132 EXPECT_THAT(s_empty, SpanIs(empty));
134 std::string abc =
"abc";
136 EXPECT_THAT(s_abc, SpanIs(abc));
139 EXPECT_THAT(s_const_abc, SpanIs(abc));
148 TEST(IntSpan, FromConstPointer) {
150 std::vector<int*>>::
value));
152 std::vector<const int*>>::
value));
159 struct TypeWithMisleadingData {
160 int&
data() {
return i; }
161 int size() {
return 1; }
165 struct TypeWithMisleadingSize {
166 int*
data() {
return &
i; }
167 const char*
size() {
return "1"; }
171 TEST(IntSpan, EvilTypes) {
179 int*
data() {
return &
i; }
180 int size() {
return 1; }
183 struct Derived : Base {};
185 TEST(IntSpan, SpanOfDerived) {
196 TEST(ConstIntSpan, InitializerListConversion) {
197 TestInitializerList({}, {});
198 TestInitializerList({1}, {1});
199 TestInitializerList({1, 2, 3}, {1, 2, 3});
202 std::initializer_list<int>>::
value));
207 TEST(IntSpan, Data) {
210 EXPECT_EQ(&i, s.
data());
213 TEST(IntSpan, SizeLengthEmpty) {
215 EXPECT_EQ(empty.
size(), 0);
216 EXPECT_TRUE(empty.
empty());
219 auto v = MakeRamp(10);
221 EXPECT_EQ(s.
size(), 10);
222 EXPECT_FALSE(s.
empty());
226 TEST(IntSpan, ElementAccess) {
227 auto v = MakeRamp(10);
229 for (
int i = 0; i < s.
size(); ++
i) {
230 EXPECT_EQ(s[i], s.
at(i));
233 EXPECT_EQ(s.
front(), s[0]);
234 EXPECT_EQ(s.
back(), s[9]);
237 TEST(IntSpan, AtThrows) {
238 auto v = MakeRamp(10);
241 EXPECT_EQ(s.
at(9), 9);
243 "failed bounds check");
246 TEST(IntSpan, RemovePrefixAndSuffix) {
247 auto v = MakeRamp(20, 1);
249 EXPECT_EQ(s.
size(), 20);
253 EXPECT_EQ(s.
size(), 20);
256 EXPECT_EQ(s.
size(), 19);
260 EXPECT_EQ(s.
size(), 18);
261 EXPECT_EQ(s.
back(), 19);
264 EXPECT_EQ(s.
size(), 11);
268 EXPECT_EQ(s.
size(), 0);
270 EXPECT_EQ(v, MakeRamp(20, 1));
273 TEST(IntSpan, Subspan) {
274 std::vector<int> empty;
280 auto ramp = MakeRamp(10);
285 EXPECT_THAT(
absl::MakeSpan(ramp).subspan(0, 3), SpanIs(ramp.data(), 3));
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));
291 #ifdef ABSL_HAVE_EXCEPTIONS 292 EXPECT_THROW(
absl::MakeSpan(ramp).subspan(11, 5), std::out_of_range);
294 EXPECT_DEATH_IF_SUPPORTED(
absl::MakeSpan(ramp).subspan(11, 5),
"");
298 TEST(IntSpan, First) {
299 std::vector<int> empty;
302 auto ramp = MakeRamp(10);
303 EXPECT_THAT(
absl::MakeSpan(ramp).first(0), SpanIs(ramp.data(), 0));
305 EXPECT_THAT(
absl::MakeSpan(ramp).first(3), SpanIs(ramp.data(), 3));
307 #ifdef ABSL_HAVE_EXCEPTIONS 314 TEST(IntSpan, Last) {
315 std::vector<int> empty;
318 auto ramp = MakeRamp(10);
319 EXPECT_THAT(
absl::MakeSpan(ramp).last(0), SpanIs(ramp.data() + 10, 0));
321 EXPECT_THAT(
absl::MakeSpan(ramp).last(3), SpanIs(ramp.data() + 7, 3));
323 #ifdef ABSL_HAVE_EXCEPTIONS 330 TEST(IntSpan, MakeSpanPtrLength) {
331 std::vector<int> empty;
333 EXPECT_THAT(s_empty, SpanIs(empty));
335 std::array<int, 3> a{{1, 2, 3}};
337 EXPECT_THAT(s, SpanIs(a));
343 TEST(IntSpan, MakeSpanTwoPtrs) {
344 std::vector<int> empty;
346 EXPECT_THAT(s_empty, SpanIs(empty));
348 std::vector<int> v{1, 2, 3};
350 EXPECT_THAT(s, SpanIs(v.data(), 1));
356 TEST(IntSpan, MakeSpanContainer) {
357 std::vector<int> empty;
359 EXPECT_THAT(s_empty, SpanIs(empty));
361 std::vector<int> v{1, 2, 3};
363 EXPECT_THAT(s, SpanIs(v));
372 TEST(CharSpan, MakeSpanString) {
373 std::string empty =
"";
375 EXPECT_THAT(s_empty, SpanIs(empty));
377 std::string str =
"abc";
379 EXPECT_THAT(s_str, SpanIs(str));
385 TEST(IntSpan, MakeSpanArray) {
388 EXPECT_THAT(s, SpanIs(a, 3));
390 const int ca[] = {1, 2, 3};
392 EXPECT_THAT(s_ca, SpanIs(ca, 3));
399 template <
typename Expected,
typename T>
400 void CheckType(
const T& ) {
401 testing::StaticAssertTypeEq<Expected, T>();
404 TEST(IntSpan, MakeSpanTypes) {
405 std::vector<int> vec;
406 const std::vector<int> cvec;
408 const int ca[] = {1};
412 const std::string cs =
"";
429 TEST(ConstIntSpan, MakeConstSpanTypes) {
430 std::vector<int> vec;
431 const std::vector<int> cvec;
433 const int carray[] = {0};
435 const int* cptr = carray;
450 TEST(IntSpan, Equality) {
451 const int arr1[] = {1, 2, 3, 4, 5};
452 int arr2[] = {1, 2, 3, 4, 5};
454 std::vector<int> vec2 = vec1;
455 std::vector<int> other_vec = {2, 4, 6, 8, 10};
461 EXPECT_EQ(from1, from1);
462 EXPECT_FALSE(from1 != from1);
463 EXPECT_EQ(from1, from2);
464 EXPECT_FALSE(from1 != from2);
469 EXPECT_NE(from1, from_other);
470 EXPECT_FALSE(from1 == from_other);
474 EXPECT_EQ(vec1, from1);
475 EXPECT_FALSE(vec1 != from1);
476 EXPECT_EQ(from1, vec1);
477 EXPECT_FALSE(from1 != vec1);
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);
490 EXPECT_EQ(vec1, mutable_from1);
491 EXPECT_FALSE(vec1 != mutable_from1);
492 EXPECT_EQ(mutable_from1, vec1);
493 EXPECT_FALSE(mutable_from1 != vec1);
499 EXPECT_TRUE(arr1 == mutable_from1);
500 EXPECT_FALSE(arr1 != mutable_from1);
501 EXPECT_TRUE(mutable_from1 == arr1);
502 EXPECT_FALSE(mutable_from1 != arr1);
505 EXPECT_TRUE(arr2 == from1);
506 EXPECT_FALSE(arr2 != from1);
507 EXPECT_TRUE(from1 == arr2);
508 EXPECT_FALSE(from1 != arr2);
515 EXPECT_NE(from1, from2);
518 class IntSpanOrderComparisonTest :
public testing::Test {
520 IntSpanOrderComparisonTest()
521 : arr_before_{1, 2, 3},
523 carr_after_{1, 2, 4},
526 before_(vec_before_),
528 cbefore_(vec_before_),
529 cafter_(vec_after_) {}
532 int arr_before_[3], arr_after_[3];
533 const int carr_after_[3];
534 std::vector<int> vec_before_, vec_after_;
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_);
545 EXPECT_FALSE(cbefore_ > cafter_);
546 EXPECT_FALSE(cafter_ < cbefore_);
548 EXPECT_TRUE(before_ < after_);
549 EXPECT_TRUE(before_ <= after_);
550 EXPECT_TRUE(after_ > before_);
551 EXPECT_TRUE(after_ >= before_);
553 EXPECT_FALSE(before_ > after_);
554 EXPECT_FALSE(after_ < before_);
556 EXPECT_TRUE(cbefore_ < after_);
557 EXPECT_TRUE(cbefore_ <= after_);
558 EXPECT_TRUE(after_ > cbefore_);
559 EXPECT_TRUE(after_ >= cbefore_);
561 EXPECT_FALSE(cbefore_ > after_);
562 EXPECT_FALSE(after_ < cbefore_);
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_);
571 EXPECT_FALSE(cbefore_ > vec_after_);
572 EXPECT_FALSE(vec_after_ < cbefore_);
574 EXPECT_TRUE(arr_before_ < cafter_);
575 EXPECT_TRUE(arr_before_ <= cafter_);
576 EXPECT_TRUE(cafter_ > arr_before_);
577 EXPECT_TRUE(cafter_ >= arr_before_);
579 EXPECT_FALSE(arr_before_ > cafter_);
580 EXPECT_FALSE(cafter_ < arr_before_);
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_);
589 EXPECT_FALSE(vec_before_ > after_);
590 EXPECT_FALSE(after_ < vec_before_);
592 EXPECT_TRUE(before_ < carr_after_);
593 EXPECT_TRUE(before_ <= carr_after_);
594 EXPECT_TRUE(carr_after_ > before_);
595 EXPECT_TRUE(carr_after_ >= before_);
597 EXPECT_FALSE(before_ > carr_after_);
598 EXPECT_FALSE(carr_after_ < before_);
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_);
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);
615 EXPECT_FALSE(subspan > before_);
616 EXPECT_FALSE(before_ < subspan);
619 TEST_F(IntSpanOrderComparisonTest, EmptySpans) {
621 EXPECT_FALSE(empty < empty);
622 EXPECT_TRUE(empty <= empty);
623 EXPECT_FALSE(empty > empty);
624 EXPECT_TRUE(empty >= empty);
626 EXPECT_TRUE(empty < before_);
627 EXPECT_TRUE(empty <= before_);
628 EXPECT_TRUE(before_ > empty);
629 EXPECT_TRUE(before_ >= empty);
631 EXPECT_FALSE(empty > before_);
632 EXPECT_FALSE(before_ < empty);
635 TEST(IntSpan, ExposesContainerTypesAndConsts) {
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());
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&,
661 TEST(IntSpan, IteratorsAndReferences) {
662 auto accept_pointer = [](
int*) {};
663 auto accept_reference = [](
int&) {};
667 auto accept_const_reverse_iterator =
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());
687 accept_reference(s[0]);
688 accept_reference(s.
at(0));
689 accept_reference(s.
front());
690 accept_reference(s.
back());
693 TEST(IntSpan, IteratorsAndReferences_Const) {
694 auto accept_pointer = [](
int*) {};
695 auto accept_reference = [](
int&) {};
699 auto accept_const_reverse_iterator =
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());
719 accept_reference(s[0]);
720 accept_reference(s.
at(0));
721 accept_reference(s.
front());
722 accept_reference(s.
back());
725 TEST(IntSpan, NoexceptTest) {
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()));
768 struct ConstexprTester {};
770 #define ABSL_TEST_CONSTEXPR(expr) \ 772 ABSL_ATTRIBUTE_UNUSED ConstexprTester<(expr, 1)> t; \ 775 struct ContainerWithConstexprMethods {
776 constexpr
int size()
const {
return 1; }
777 constexpr
const int*
data()
const {
return &
i; }
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};
813 TEST(Span, SpanSize) {
819 int array[] = {1, 2, 3, 4};
820 int array2[] = {1, 2, 3};
824 T(), T(
nullptr, 0), T(array, 0), T(array2, 0),
826 T(array, 3), T(array2), T({1, 2, 3}),
828 T(array, 1), T(array, 2),
830 T(array + 1, 2), T(array + 2, 2)}));
std::reverse_iterator< iterator > reverse_iterator
constexpr const_iterator cbegin() const noexcept
const_pointer const_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
constexpr reverse_iterator rbegin() const noexcept
#define ABSL_TEST_CONSTEXPR(expr)
constexpr reference at(size_type i) const
void remove_prefix(size_type n) noexcept
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
constexpr reverse_iterator rend() const noexcept
constexpr pointer data() const noexcept
constexpr iterator end() const noexcept
constexpr bool empty() const noexcept
constexpr size_type length() const noexcept
constexpr reference back() const noexcept
constexpr reference front() const noexcept
constexpr size_type size() const noexcept
void remove_suffix(size_type n) noexcept
constexpr iterator begin() const noexcept
#define ABSL_BASE_INTERNAL_EXPECT_FAIL(expr, exception_t, text)
static char data[kDataSize]
const T & const_reference
TEST_F(GraphCyclesTest, NoCycle)
constexpr const_reverse_iterator crend() const noexcept
bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Pred &&pred)
ABSL_MUST_USE_RESULT testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(const Container &values)
constexpr Span< const T > MakeConstSpan(T *ptr, size_t size) noexcept
TEST(Symbolize, Unimplemented)
constexpr Span< T > MakeSpan(T *ptr, size_t size) noexcept
constexpr const_reverse_iterator crbegin() const noexcept
constexpr const_iterator cend() const noexcept