diff options
author | Girts <girtsf@users.noreply.github.com> | 2019-03-08T20·05-0800 |
---|---|---|
committer | Derek Mauro <761129+derekmauro@users.noreply.github.com> | 2019-03-08T20·05-0500 |
commit | c1cecb25a94c075725e9d2640f6b978a8f61957b (patch) | |
tree | 505c35368522ebe4e6717b73f6c24e1279c5c173 /absl/types/span_test.cc | |
parent | 38b704384cd2f17590b3922b97744be0b43622c9 (diff) |
Implement Span::first and Span::last from C++20 (#274)
This implements `first` and `last` methods on `Span` that mimics ones in `std::span`.
Diffstat (limited to 'absl/types/span_test.cc')
-rw-r--r-- | absl/types/span_test.cc | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/absl/types/span_test.cc b/absl/types/span_test.cc index 294229ea07fe..9269f911f042 100644 --- a/absl/types/span_test.cc +++ b/absl/types/span_test.cc @@ -295,6 +295,38 @@ TEST(IntSpan, Subspan) { #endif } +TEST(IntSpan, First) { + std::vector<int> empty; + EXPECT_THAT(absl::MakeSpan(empty).first(0), SpanIs(empty)); + + auto ramp = MakeRamp(10); + EXPECT_THAT(absl::MakeSpan(ramp).first(0), SpanIs(ramp.data(), 0)); + EXPECT_THAT(absl::MakeSpan(ramp).first(10), SpanIs(ramp)); + EXPECT_THAT(absl::MakeSpan(ramp).first(3), SpanIs(ramp.data(), 3)); + +#ifdef ABSL_HAVE_EXCEPTIONS + EXPECT_THROW(absl::MakeSpan(ramp).first(11), std::out_of_range); +#else + EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(ramp).first(11), ""); +#endif +} + +TEST(IntSpan, Last) { + std::vector<int> empty; + EXPECT_THAT(absl::MakeSpan(empty).last(0), SpanIs(empty)); + + auto ramp = MakeRamp(10); + EXPECT_THAT(absl::MakeSpan(ramp).last(0), SpanIs(ramp.data() + 10, 0)); + EXPECT_THAT(absl::MakeSpan(ramp).last(10), SpanIs(ramp)); + EXPECT_THAT(absl::MakeSpan(ramp).last(3), SpanIs(ramp.data() + 7, 3)); + +#ifdef ABSL_HAVE_EXCEPTIONS + EXPECT_THROW(absl::MakeSpan(ramp).last(11), std::out_of_range); +#else + EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(ramp).last(11), ""); +#endif +} + TEST(IntSpan, MakeSpanPtrLength) { std::vector<int> empty; auto s_empty = absl::MakeSpan(empty.data(), empty.size()); @@ -769,6 +801,8 @@ TEST(ConstIntSpan, ConstexprTest) { ABSL_TEST_CONSTEXPR(span.begin()); ABSL_TEST_CONSTEXPR(span.cbegin()); ABSL_TEST_CONSTEXPR(span.subspan(0, 0)); + ABSL_TEST_CONSTEXPR(span.first(1)); + ABSL_TEST_CONSTEXPR(span.last(1)); ABSL_TEST_CONSTEXPR(span[0]); } |