span_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/types/span.h"
00016 
00017 #include <array>
00018 #include <initializer_list>
00019 #include <numeric>
00020 #include <stdexcept>
00021 #include <string>
00022 #include <type_traits>
00023 #include <vector>
00024 
00025 #include "gmock/gmock.h"
00026 #include "gtest/gtest.h"
00027 #include "absl/base/attributes.h"
00028 #include "absl/base/config.h"
00029 #include "absl/base/internal/exception_testing.h"
00030 #include "absl/container/fixed_array.h"
00031 #include "absl/container/inlined_vector.h"
00032 #include "absl/hash/hash_testing.h"
00033 #include "absl/strings/str_cat.h"
00034 
00035 namespace {
00036 
00037 MATCHER_P(DataIs, data,
00038           absl::StrCat("data() is ", negation ? "is " : "isn't ",
00039                        testing::PrintToString(data))) {
00040   return arg.data() == data;
00041 }
00042 
00043 template <typename T>
00044 auto SpanIs(T data, size_t size)
00045     -> decltype(testing::AllOf(DataIs(data), testing::SizeIs(size))) {
00046   return testing::AllOf(DataIs(data), testing::SizeIs(size));
00047 }
00048 
00049 template <typename Container>
00050 auto SpanIs(const Container& c) -> decltype(SpanIs(c.data(), c.size())) {
00051   return SpanIs(c.data(), c.size());
00052 }
00053 
00054 std::vector<int> MakeRamp(int len, int offset = 0) {
00055   std::vector<int> v(len);
00056   std::iota(v.begin(), v.end(), offset);
00057   return v;
00058 }
00059 
00060 TEST(IntSpan, EmptyCtors) {
00061   absl::Span<int> s;
00062   EXPECT_THAT(s, SpanIs(nullptr, 0));
00063 }
00064 
00065 TEST(IntSpan, PtrLenCtor) {
00066   int a[] = {1, 2, 3};
00067   absl::Span<int> s(&a[0], 2);
00068   EXPECT_THAT(s, SpanIs(a, 2));
00069 }
00070 
00071 TEST(IntSpan, ArrayCtor) {
00072   int a[] = {1, 2, 3};
00073   absl::Span<int> s(a);
00074   EXPECT_THAT(s, SpanIs(a, 3));
00075 
00076   EXPECT_TRUE((std::is_constructible<absl::Span<const int>, int[3]>::value));
00077   EXPECT_TRUE(
00078       (std::is_constructible<absl::Span<const int>, const int[3]>::value));
00079   EXPECT_FALSE((std::is_constructible<absl::Span<int>, const int[3]>::value));
00080   EXPECT_TRUE((std::is_convertible<int[3], absl::Span<const int>>::value));
00081   EXPECT_TRUE(
00082       (std::is_convertible<const int[3], absl::Span<const int>>::value));
00083 }
00084 
00085 template <typename T>
00086 void TakesGenericSpan(absl::Span<T>) {}
00087 
00088 TEST(IntSpan, ContainerCtor) {
00089   std::vector<int> empty;
00090   absl::Span<int> s_empty(empty);
00091   EXPECT_THAT(s_empty, SpanIs(empty));
00092 
00093   std::vector<int> filled{1, 2, 3};
00094   absl::Span<int> s_filled(filled);
00095   EXPECT_THAT(s_filled, SpanIs(filled));
00096 
00097   absl::Span<int> s_from_span(filled);
00098   EXPECT_THAT(s_from_span, SpanIs(s_filled));
00099 
00100   absl::Span<const int> const_filled = filled;
00101   EXPECT_THAT(const_filled, SpanIs(filled));
00102 
00103   absl::Span<const int> const_from_span = s_filled;
00104   EXPECT_THAT(const_from_span, SpanIs(s_filled));
00105 
00106   EXPECT_TRUE(
00107       (std::is_convertible<std::vector<int>&, absl::Span<const int>>::value));
00108   EXPECT_TRUE(
00109       (std::is_convertible<absl::Span<int>&, absl::Span<const int>>::value));
00110 
00111   TakesGenericSpan(absl::Span<int>(filled));
00112 }
00113 
00114 // A struct supplying shallow data() const.
00115 struct ContainerWithShallowConstData {
00116   std::vector<int> storage;
00117   int* data() const { return const_cast<int*>(storage.data()); }
00118   int size() const { return storage.size(); }
00119 };
00120 
00121 TEST(IntSpan, ShallowConstness) {
00122   const ContainerWithShallowConstData c{MakeRamp(20)};
00123   absl::Span<int> s(
00124       c);  // We should be able to do this even though data() is const.
00125   s[0] = -1;
00126   EXPECT_EQ(c.storage[0], -1);
00127 }
00128 
00129 TEST(CharSpan, StringCtor) {
00130   std::string empty = "";
00131   absl::Span<char> s_empty(empty);
00132   EXPECT_THAT(s_empty, SpanIs(empty));
00133 
00134   std::string abc = "abc";
00135   absl::Span<char> s_abc(abc);
00136   EXPECT_THAT(s_abc, SpanIs(abc));
00137 
00138   absl::Span<const char> s_const_abc = abc;
00139   EXPECT_THAT(s_const_abc, SpanIs(abc));
00140 
00141   EXPECT_FALSE((std::is_constructible<absl::Span<int>, std::string>::value));
00142   EXPECT_FALSE(
00143       (std::is_constructible<absl::Span<const int>, std::string>::value));
00144   EXPECT_TRUE(
00145       (std::is_convertible<std::string, absl::Span<const char>>::value));
00146 }
00147 
00148 TEST(IntSpan, FromConstPointer) {
00149   EXPECT_TRUE((std::is_constructible<absl::Span<const int* const>,
00150                                      std::vector<int*>>::value));
00151   EXPECT_TRUE((std::is_constructible<absl::Span<const int* const>,
00152                                      std::vector<const int*>>::value));
00153   EXPECT_FALSE((
00154       std::is_constructible<absl::Span<const int*>, std::vector<int*>>::value));
00155   EXPECT_FALSE((
00156       std::is_constructible<absl::Span<int*>, std::vector<const int*>>::value));
00157 }
00158 
00159 struct TypeWithMisleadingData {
00160   int& data() { return i; }
00161   int size() { return 1; }
00162   int i;
00163 };
00164 
00165 struct TypeWithMisleadingSize {
00166   int* data() { return &i; }
00167   const char* size() { return "1"; }
00168   int i;
00169 };
00170 
00171 TEST(IntSpan, EvilTypes) {
00172   EXPECT_FALSE(
00173       (std::is_constructible<absl::Span<int>, TypeWithMisleadingData&>::value));
00174   EXPECT_FALSE(
00175       (std::is_constructible<absl::Span<int>, TypeWithMisleadingSize&>::value));
00176 }
00177 
00178 struct Base {
00179   int* data() { return &i; }
00180   int size() { return 1; }
00181   int i;
00182 };
00183 struct Derived : Base {};
00184 
00185 TEST(IntSpan, SpanOfDerived) {
00186   EXPECT_TRUE((std::is_constructible<absl::Span<int>, Base&>::value));
00187   EXPECT_TRUE((std::is_constructible<absl::Span<int>, Derived&>::value));
00188   EXPECT_FALSE(
00189       (std::is_constructible<absl::Span<Base>, std::vector<Derived>>::value));
00190 }
00191 
00192 void TestInitializerList(absl::Span<const int> s, const std::vector<int>& v) {
00193   EXPECT_TRUE(absl::equal(s.begin(), s.end(), v.begin(), v.end()));
00194 }
00195 
00196 TEST(ConstIntSpan, InitializerListConversion) {
00197   TestInitializerList({}, {});
00198   TestInitializerList({1}, {1});
00199   TestInitializerList({1, 2, 3}, {1, 2, 3});
00200 
00201   EXPECT_FALSE((std::is_constructible<absl::Span<int>,
00202                                       std::initializer_list<int>>::value));
00203   EXPECT_FALSE((
00204       std::is_convertible<absl::Span<int>, std::initializer_list<int>>::value));
00205 }
00206 
00207 TEST(IntSpan, Data) {
00208   int i;
00209   absl::Span<int> s(&i, 1);
00210   EXPECT_EQ(&i, s.data());
00211 }
00212 
00213 TEST(IntSpan, SizeLengthEmpty) {
00214   absl::Span<int> empty;
00215   EXPECT_EQ(empty.size(), 0);
00216   EXPECT_TRUE(empty.empty());
00217   EXPECT_EQ(empty.size(), empty.length());
00218 
00219   auto v = MakeRamp(10);
00220   absl::Span<int> s(v);
00221   EXPECT_EQ(s.size(), 10);
00222   EXPECT_FALSE(s.empty());
00223   EXPECT_EQ(s.size(), s.length());
00224 }
00225 
00226 TEST(IntSpan, ElementAccess) {
00227   auto v = MakeRamp(10);
00228   absl::Span<int> s(v);
00229   for (int i = 0; i < s.size(); ++i) {
00230     EXPECT_EQ(s[i], s.at(i));
00231   }
00232 
00233   EXPECT_EQ(s.front(), s[0]);
00234   EXPECT_EQ(s.back(), s[9]);
00235 }
00236 
00237 TEST(IntSpan, AtThrows) {
00238   auto v = MakeRamp(10);
00239   absl::Span<int> s(v);
00240 
00241   EXPECT_EQ(s.at(9), 9);
00242   ABSL_BASE_INTERNAL_EXPECT_FAIL(s.at(10), std::out_of_range,
00243                                  "failed bounds check");
00244 }
00245 
00246 TEST(IntSpan, RemovePrefixAndSuffix) {
00247   auto v = MakeRamp(20, 1);
00248   absl::Span<int> s(v);
00249   EXPECT_EQ(s.size(), 20);
00250 
00251   s.remove_suffix(0);
00252   s.remove_prefix(0);
00253   EXPECT_EQ(s.size(), 20);
00254 
00255   s.remove_prefix(1);
00256   EXPECT_EQ(s.size(), 19);
00257   EXPECT_EQ(s[0], 2);
00258 
00259   s.remove_suffix(1);
00260   EXPECT_EQ(s.size(), 18);
00261   EXPECT_EQ(s.back(), 19);
00262 
00263   s.remove_prefix(7);
00264   EXPECT_EQ(s.size(), 11);
00265   EXPECT_EQ(s[0], 9);
00266 
00267   s.remove_suffix(11);
00268   EXPECT_EQ(s.size(), 0);
00269 
00270   EXPECT_EQ(v, MakeRamp(20, 1));
00271 }
00272 
00273 TEST(IntSpan, Subspan) {
00274   std::vector<int> empty;
00275   EXPECT_EQ(absl::MakeSpan(empty).subspan(), empty);
00276   EXPECT_THAT(absl::MakeSpan(empty).subspan(0, 0), SpanIs(empty));
00277   EXPECT_THAT(absl::MakeSpan(empty).subspan(0, absl::Span<const int>::npos),
00278               SpanIs(empty));
00279 
00280   auto ramp = MakeRamp(10);
00281   EXPECT_THAT(absl::MakeSpan(ramp).subspan(), SpanIs(ramp));
00282   EXPECT_THAT(absl::MakeSpan(ramp).subspan(0, 10), SpanIs(ramp));
00283   EXPECT_THAT(absl::MakeSpan(ramp).subspan(0, absl::Span<const int>::npos),
00284               SpanIs(ramp));
00285   EXPECT_THAT(absl::MakeSpan(ramp).subspan(0, 3), SpanIs(ramp.data(), 3));
00286   EXPECT_THAT(absl::MakeSpan(ramp).subspan(5, absl::Span<const int>::npos),
00287               SpanIs(ramp.data() + 5, 5));
00288   EXPECT_THAT(absl::MakeSpan(ramp).subspan(3, 3), SpanIs(ramp.data() + 3, 3));
00289   EXPECT_THAT(absl::MakeSpan(ramp).subspan(10, 5), SpanIs(ramp.data() + 10, 0));
00290 
00291 #ifdef ABSL_HAVE_EXCEPTIONS
00292   EXPECT_THROW(absl::MakeSpan(ramp).subspan(11, 5), std::out_of_range);
00293 #else
00294   EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(ramp).subspan(11, 5), "");
00295 #endif
00296 }
00297 
00298 TEST(IntSpan, First) {
00299   std::vector<int> empty;
00300   EXPECT_THAT(absl::MakeSpan(empty).first(0), SpanIs(empty));
00301 
00302   auto ramp = MakeRamp(10);
00303   EXPECT_THAT(absl::MakeSpan(ramp).first(0), SpanIs(ramp.data(), 0));
00304   EXPECT_THAT(absl::MakeSpan(ramp).first(10), SpanIs(ramp));
00305   EXPECT_THAT(absl::MakeSpan(ramp).first(3), SpanIs(ramp.data(), 3));
00306 
00307 #ifdef ABSL_HAVE_EXCEPTIONS
00308   EXPECT_THROW(absl::MakeSpan(ramp).first(11), std::out_of_range);
00309 #else
00310   EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(ramp).first(11), "");
00311 #endif
00312 }
00313 
00314 TEST(IntSpan, Last) {
00315   std::vector<int> empty;
00316   EXPECT_THAT(absl::MakeSpan(empty).last(0), SpanIs(empty));
00317 
00318   auto ramp = MakeRamp(10);
00319   EXPECT_THAT(absl::MakeSpan(ramp).last(0), SpanIs(ramp.data() + 10, 0));
00320   EXPECT_THAT(absl::MakeSpan(ramp).last(10), SpanIs(ramp));
00321   EXPECT_THAT(absl::MakeSpan(ramp).last(3), SpanIs(ramp.data() + 7, 3));
00322 
00323 #ifdef ABSL_HAVE_EXCEPTIONS
00324   EXPECT_THROW(absl::MakeSpan(ramp).last(11), std::out_of_range);
00325 #else
00326   EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(ramp).last(11), "");
00327 #endif
00328 }
00329 
00330 TEST(IntSpan, MakeSpanPtrLength) {
00331   std::vector<int> empty;
00332   auto s_empty = absl::MakeSpan(empty.data(), empty.size());
00333   EXPECT_THAT(s_empty, SpanIs(empty));
00334 
00335   std::array<int, 3> a{{1, 2, 3}};
00336   auto s = absl::MakeSpan(a.data(), a.size());
00337   EXPECT_THAT(s, SpanIs(a));
00338 
00339   EXPECT_THAT(absl::MakeConstSpan(empty.data(), empty.size()), SpanIs(s_empty));
00340   EXPECT_THAT(absl::MakeConstSpan(a.data(), a.size()), SpanIs(s));
00341 }
00342 
00343 TEST(IntSpan, MakeSpanTwoPtrs) {
00344   std::vector<int> empty;
00345   auto s_empty = absl::MakeSpan(empty.data(), empty.data());
00346   EXPECT_THAT(s_empty, SpanIs(empty));
00347 
00348   std::vector<int> v{1, 2, 3};
00349   auto s = absl::MakeSpan(v.data(), v.data() + 1);
00350   EXPECT_THAT(s, SpanIs(v.data(), 1));
00351 
00352   EXPECT_THAT(absl::MakeConstSpan(empty.data(), empty.data()), SpanIs(s_empty));
00353   EXPECT_THAT(absl::MakeConstSpan(v.data(), v.data() + 1), SpanIs(s));
00354 }
00355 
00356 TEST(IntSpan, MakeSpanContainer) {
00357   std::vector<int> empty;
00358   auto s_empty = absl::MakeSpan(empty);
00359   EXPECT_THAT(s_empty, SpanIs(empty));
00360 
00361   std::vector<int> v{1, 2, 3};
00362   auto s = absl::MakeSpan(v);
00363   EXPECT_THAT(s, SpanIs(v));
00364 
00365   EXPECT_THAT(absl::MakeConstSpan(empty), SpanIs(s_empty));
00366   EXPECT_THAT(absl::MakeConstSpan(v), SpanIs(s));
00367 
00368   EXPECT_THAT(absl::MakeSpan(s), SpanIs(s));
00369   EXPECT_THAT(absl::MakeConstSpan(s), SpanIs(s));
00370 }
00371 
00372 TEST(CharSpan, MakeSpanString) {
00373   std::string empty = "";
00374   auto s_empty = absl::MakeSpan(empty);
00375   EXPECT_THAT(s_empty, SpanIs(empty));
00376 
00377   std::string str = "abc";
00378   auto s_str = absl::MakeSpan(str);
00379   EXPECT_THAT(s_str, SpanIs(str));
00380 
00381   EXPECT_THAT(absl::MakeConstSpan(empty), SpanIs(s_empty));
00382   EXPECT_THAT(absl::MakeConstSpan(str), SpanIs(s_str));
00383 }
00384 
00385 TEST(IntSpan, MakeSpanArray) {
00386   int a[] = {1, 2, 3};
00387   auto s = absl::MakeSpan(a);
00388   EXPECT_THAT(s, SpanIs(a, 3));
00389 
00390   const int ca[] = {1, 2, 3};
00391   auto s_ca = absl::MakeSpan(ca);
00392   EXPECT_THAT(s_ca, SpanIs(ca, 3));
00393 
00394   EXPECT_THAT(absl::MakeConstSpan(a), SpanIs(s));
00395   EXPECT_THAT(absl::MakeConstSpan(ca), SpanIs(s_ca));
00396 }
00397 
00398 // Compile-asserts that the argument has the expected decayed type.
00399 template <typename Expected, typename T>
00400 void CheckType(const T& /* value */) {
00401   testing::StaticAssertTypeEq<Expected, T>();
00402 }
00403 
00404 TEST(IntSpan, MakeSpanTypes) {
00405   std::vector<int> vec;
00406   const std::vector<int> cvec;
00407   int a[1];
00408   const int ca[] = {1};
00409   int* ip = a;
00410   const int* cip = ca;
00411   std::string s = "";
00412   const std::string cs = "";
00413   CheckType<absl::Span<int>>(absl::MakeSpan(vec));
00414   CheckType<absl::Span<const int>>(absl::MakeSpan(cvec));
00415   CheckType<absl::Span<int>>(absl::MakeSpan(ip, ip + 1));
00416   CheckType<absl::Span<int>>(absl::MakeSpan(ip, 1));
00417   CheckType<absl::Span<const int>>(absl::MakeSpan(cip, cip + 1));
00418   CheckType<absl::Span<const int>>(absl::MakeSpan(cip, 1));
00419   CheckType<absl::Span<int>>(absl::MakeSpan(a));
00420   CheckType<absl::Span<int>>(absl::MakeSpan(a, a + 1));
00421   CheckType<absl::Span<int>>(absl::MakeSpan(a, 1));
00422   CheckType<absl::Span<const int>>(absl::MakeSpan(ca));
00423   CheckType<absl::Span<const int>>(absl::MakeSpan(ca, ca + 1));
00424   CheckType<absl::Span<const int>>(absl::MakeSpan(ca, 1));
00425   CheckType<absl::Span<char>>(absl::MakeSpan(s));
00426   CheckType<absl::Span<const char>>(absl::MakeSpan(cs));
00427 }
00428 
00429 TEST(ConstIntSpan, MakeConstSpanTypes) {
00430   std::vector<int> vec;
00431   const std::vector<int> cvec;
00432   int array[1];
00433   const int carray[] = {0};
00434   int* ptr = array;
00435   const int* cptr = carray;
00436   std::string s = "";
00437   std::string cs = "";
00438   CheckType<absl::Span<const int>>(absl::MakeConstSpan(vec));
00439   CheckType<absl::Span<const int>>(absl::MakeConstSpan(cvec));
00440   CheckType<absl::Span<const int>>(absl::MakeConstSpan(ptr, ptr + 1));
00441   CheckType<absl::Span<const int>>(absl::MakeConstSpan(ptr, 1));
00442   CheckType<absl::Span<const int>>(absl::MakeConstSpan(cptr, cptr + 1));
00443   CheckType<absl::Span<const int>>(absl::MakeConstSpan(cptr, 1));
00444   CheckType<absl::Span<const int>>(absl::MakeConstSpan(array));
00445   CheckType<absl::Span<const int>>(absl::MakeConstSpan(carray));
00446   CheckType<absl::Span<const char>>(absl::MakeConstSpan(s));
00447   CheckType<absl::Span<const char>>(absl::MakeConstSpan(cs));
00448 }
00449 
00450 TEST(IntSpan, Equality) {
00451   const int arr1[] = {1, 2, 3, 4, 5};
00452   int arr2[] = {1, 2, 3, 4, 5};
00453   std::vector<int> vec1(std::begin(arr1), std::end(arr1));
00454   std::vector<int> vec2 = vec1;
00455   std::vector<int> other_vec = {2, 4, 6, 8, 10};
00456   // These two slices are from different vectors, but have the same size and
00457   // have the same elements (right now).  They should compare equal. Test both
00458   // == and !=.
00459   const absl::Span<const int> from1 = vec1;
00460   const absl::Span<const int> from2 = vec2;
00461   EXPECT_EQ(from1, from1);
00462   EXPECT_FALSE(from1 != from1);
00463   EXPECT_EQ(from1, from2);
00464   EXPECT_FALSE(from1 != from2);
00465 
00466   // These two slices have different underlying vector values. They should be
00467   // considered not equal. Test both == and !=.
00468   const absl::Span<const int> from_other = other_vec;
00469   EXPECT_NE(from1, from_other);
00470   EXPECT_FALSE(from1 == from_other);
00471 
00472   // Comparison between a vector and its slice should be equal. And vice-versa.
00473   // This ensures implicit conversion to Span works on both sides of ==.
00474   EXPECT_EQ(vec1, from1);
00475   EXPECT_FALSE(vec1 != from1);
00476   EXPECT_EQ(from1, vec1);
00477   EXPECT_FALSE(from1 != vec1);
00478 
00479   // This verifies that absl::Span<T> can be compared freely with
00480   // absl::Span<const T>.
00481   const absl::Span<int> mutable_from1(vec1);
00482   const absl::Span<int> mutable_from2(vec2);
00483   EXPECT_EQ(from1, mutable_from1);
00484   EXPECT_EQ(mutable_from1, from1);
00485   EXPECT_EQ(mutable_from1, mutable_from2);
00486   EXPECT_EQ(mutable_from2, mutable_from1);
00487 
00488   // Comparison between a vector and its slice should be equal for mutable
00489   // Spans as well.
00490   EXPECT_EQ(vec1, mutable_from1);
00491   EXPECT_FALSE(vec1 != mutable_from1);
00492   EXPECT_EQ(mutable_from1, vec1);
00493   EXPECT_FALSE(mutable_from1 != vec1);
00494 
00495   // Comparison between convertible-to-Span-of-const and Span-of-mutable. Arrays
00496   // are used because they're the only value type which converts to a
00497   // Span-of-mutable. EXPECT_TRUE is used instead of EXPECT_EQ to avoid
00498   // array-to-pointer decay.
00499   EXPECT_TRUE(arr1 == mutable_from1);
00500   EXPECT_FALSE(arr1 != mutable_from1);
00501   EXPECT_TRUE(mutable_from1 == arr1);
00502   EXPECT_FALSE(mutable_from1 != arr1);
00503 
00504   // Comparison between convertible-to-Span-of-mutable and Span-of-const
00505   EXPECT_TRUE(arr2 == from1);
00506   EXPECT_FALSE(arr2 != from1);
00507   EXPECT_TRUE(from1 == arr2);
00508   EXPECT_FALSE(from1 != arr2);
00509 
00510   // With a different size, the array slices should not be equal.
00511   EXPECT_NE(from1, absl::Span<const int>(from1).subspan(0, from1.size() - 1));
00512 
00513   // With different contents, the array slices should not be equal.
00514   ++vec2.back();
00515   EXPECT_NE(from1, from2);
00516 }
00517 
00518 class IntSpanOrderComparisonTest : public testing::Test {
00519  public:
00520   IntSpanOrderComparisonTest()
00521       : arr_before_{1, 2, 3},
00522         arr_after_{1, 2, 4},
00523         carr_after_{1, 2, 4},
00524         vec_before_(std::begin(arr_before_), std::end(arr_before_)),
00525         vec_after_(std::begin(arr_after_), std::end(arr_after_)),
00526         before_(vec_before_),
00527         after_(vec_after_),
00528         cbefore_(vec_before_),
00529         cafter_(vec_after_) {}
00530 
00531  protected:
00532   int arr_before_[3], arr_after_[3];
00533   const int carr_after_[3];
00534   std::vector<int> vec_before_, vec_after_;
00535   absl::Span<int> before_, after_;
00536   absl::Span<const int> cbefore_, cafter_;
00537 };
00538 
00539 TEST_F(IntSpanOrderComparisonTest, CompareSpans) {
00540   EXPECT_TRUE(cbefore_ < cafter_);
00541   EXPECT_TRUE(cbefore_ <= cafter_);
00542   EXPECT_TRUE(cafter_ > cbefore_);
00543   EXPECT_TRUE(cafter_ >= cbefore_);
00544 
00545   EXPECT_FALSE(cbefore_ > cafter_);
00546   EXPECT_FALSE(cafter_ < cbefore_);
00547 
00548   EXPECT_TRUE(before_ < after_);
00549   EXPECT_TRUE(before_ <= after_);
00550   EXPECT_TRUE(after_ > before_);
00551   EXPECT_TRUE(after_ >= before_);
00552 
00553   EXPECT_FALSE(before_ > after_);
00554   EXPECT_FALSE(after_ < before_);
00555 
00556   EXPECT_TRUE(cbefore_ < after_);
00557   EXPECT_TRUE(cbefore_ <= after_);
00558   EXPECT_TRUE(after_ > cbefore_);
00559   EXPECT_TRUE(after_ >= cbefore_);
00560 
00561   EXPECT_FALSE(cbefore_ > after_);
00562   EXPECT_FALSE(after_ < cbefore_);
00563 }
00564 
00565 TEST_F(IntSpanOrderComparisonTest, SpanOfConstAndContainer) {
00566   EXPECT_TRUE(cbefore_ < vec_after_);
00567   EXPECT_TRUE(cbefore_ <= vec_after_);
00568   EXPECT_TRUE(vec_after_ > cbefore_);
00569   EXPECT_TRUE(vec_after_ >= cbefore_);
00570 
00571   EXPECT_FALSE(cbefore_ > vec_after_);
00572   EXPECT_FALSE(vec_after_ < cbefore_);
00573 
00574   EXPECT_TRUE(arr_before_ < cafter_);
00575   EXPECT_TRUE(arr_before_ <= cafter_);
00576   EXPECT_TRUE(cafter_ > arr_before_);
00577   EXPECT_TRUE(cafter_ >= arr_before_);
00578 
00579   EXPECT_FALSE(arr_before_ > cafter_);
00580   EXPECT_FALSE(cafter_ < arr_before_);
00581 }
00582 
00583 TEST_F(IntSpanOrderComparisonTest, SpanOfMutableAndContainer) {
00584   EXPECT_TRUE(vec_before_ < after_);
00585   EXPECT_TRUE(vec_before_ <= after_);
00586   EXPECT_TRUE(after_ > vec_before_);
00587   EXPECT_TRUE(after_ >= vec_before_);
00588 
00589   EXPECT_FALSE(vec_before_ > after_);
00590   EXPECT_FALSE(after_ < vec_before_);
00591 
00592   EXPECT_TRUE(before_ < carr_after_);
00593   EXPECT_TRUE(before_ <= carr_after_);
00594   EXPECT_TRUE(carr_after_ > before_);
00595   EXPECT_TRUE(carr_after_ >= before_);
00596 
00597   EXPECT_FALSE(before_ > carr_after_);
00598   EXPECT_FALSE(carr_after_ < before_);
00599 }
00600 
00601 TEST_F(IntSpanOrderComparisonTest, EqualSpans) {
00602   EXPECT_FALSE(before_ < before_);
00603   EXPECT_TRUE(before_ <= before_);
00604   EXPECT_FALSE(before_ > before_);
00605   EXPECT_TRUE(before_ >= before_);
00606 }
00607 
00608 TEST_F(IntSpanOrderComparisonTest, Subspans) {
00609   auto subspan = before_.subspan(0, 1);
00610   EXPECT_TRUE(subspan < before_);
00611   EXPECT_TRUE(subspan <= before_);
00612   EXPECT_TRUE(before_ > subspan);
00613   EXPECT_TRUE(before_ >= subspan);
00614 
00615   EXPECT_FALSE(subspan > before_);
00616   EXPECT_FALSE(before_ < subspan);
00617 }
00618 
00619 TEST_F(IntSpanOrderComparisonTest, EmptySpans) {
00620   absl::Span<int> empty;
00621   EXPECT_FALSE(empty < empty);
00622   EXPECT_TRUE(empty <= empty);
00623   EXPECT_FALSE(empty > empty);
00624   EXPECT_TRUE(empty >= empty);
00625 
00626   EXPECT_TRUE(empty < before_);
00627   EXPECT_TRUE(empty <= before_);
00628   EXPECT_TRUE(before_ > empty);
00629   EXPECT_TRUE(before_ >= empty);
00630 
00631   EXPECT_FALSE(empty > before_);
00632   EXPECT_FALSE(before_ < empty);
00633 }
00634 
00635 TEST(IntSpan, ExposesContainerTypesAndConsts) {
00636   absl::Span<int> slice;
00637   CheckType<absl::Span<int>::iterator>(slice.begin());
00638   EXPECT_TRUE((std::is_convertible<decltype(slice.begin()),
00639                                    absl::Span<int>::const_iterator>::value));
00640   CheckType<absl::Span<int>::const_iterator>(slice.cbegin());
00641   EXPECT_TRUE((std::is_convertible<decltype(slice.end()),
00642                                    absl::Span<int>::const_iterator>::value));
00643   CheckType<absl::Span<int>::const_iterator>(slice.cend());
00644   CheckType<absl::Span<int>::reverse_iterator>(slice.rend());
00645   EXPECT_TRUE(
00646       (std::is_convertible<decltype(slice.rend()),
00647                            absl::Span<int>::const_reverse_iterator>::value));
00648   CheckType<absl::Span<int>::const_reverse_iterator>(slice.crend());
00649   testing::StaticAssertTypeEq<int, absl::Span<int>::value_type>();
00650   testing::StaticAssertTypeEq<int, absl::Span<const int>::value_type>();
00651   testing::StaticAssertTypeEq<int*, absl::Span<int>::pointer>();
00652   testing::StaticAssertTypeEq<const int*, absl::Span<const int>::pointer>();
00653   testing::StaticAssertTypeEq<int&, absl::Span<int>::reference>();
00654   testing::StaticAssertTypeEq<const int&, absl::Span<const int>::reference>();
00655   testing::StaticAssertTypeEq<const int&, absl::Span<int>::const_reference>();
00656   testing::StaticAssertTypeEq<const int&,
00657                               absl::Span<const int>::const_reference>();
00658   EXPECT_EQ(static_cast<absl::Span<int>::size_type>(-1), absl::Span<int>::npos);
00659 }
00660 
00661 TEST(IntSpan, IteratorsAndReferences) {
00662   auto accept_pointer = [](int*) {};
00663   auto accept_reference = [](int&) {};
00664   auto accept_iterator = [](absl::Span<int>::iterator) {};
00665   auto accept_const_iterator = [](absl::Span<int>::const_iterator) {};
00666   auto accept_reverse_iterator = [](absl::Span<int>::reverse_iterator) {};
00667   auto accept_const_reverse_iterator =
00668       [](absl::Span<int>::const_reverse_iterator) {};
00669 
00670   int a[1];
00671   absl::Span<int> s = a;
00672 
00673   accept_pointer(s.data());
00674   accept_iterator(s.begin());
00675   accept_const_iterator(s.begin());
00676   accept_const_iterator(s.cbegin());
00677   accept_iterator(s.end());
00678   accept_const_iterator(s.end());
00679   accept_const_iterator(s.cend());
00680   accept_reverse_iterator(s.rbegin());
00681   accept_const_reverse_iterator(s.rbegin());
00682   accept_const_reverse_iterator(s.crbegin());
00683   accept_reverse_iterator(s.rend());
00684   accept_const_reverse_iterator(s.rend());
00685   accept_const_reverse_iterator(s.crend());
00686 
00687   accept_reference(s[0]);
00688   accept_reference(s.at(0));
00689   accept_reference(s.front());
00690   accept_reference(s.back());
00691 }
00692 
00693 TEST(IntSpan, IteratorsAndReferences_Const) {
00694   auto accept_pointer = [](int*) {};
00695   auto accept_reference = [](int&) {};
00696   auto accept_iterator = [](absl::Span<int>::iterator) {};
00697   auto accept_const_iterator = [](absl::Span<int>::const_iterator) {};
00698   auto accept_reverse_iterator = [](absl::Span<int>::reverse_iterator) {};
00699   auto accept_const_reverse_iterator =
00700       [](absl::Span<int>::const_reverse_iterator) {};
00701 
00702   int a[1];
00703   const absl::Span<int> s = a;
00704 
00705   accept_pointer(s.data());
00706   accept_iterator(s.begin());
00707   accept_const_iterator(s.begin());
00708   accept_const_iterator(s.cbegin());
00709   accept_iterator(s.end());
00710   accept_const_iterator(s.end());
00711   accept_const_iterator(s.cend());
00712   accept_reverse_iterator(s.rbegin());
00713   accept_const_reverse_iterator(s.rbegin());
00714   accept_const_reverse_iterator(s.crbegin());
00715   accept_reverse_iterator(s.rend());
00716   accept_const_reverse_iterator(s.rend());
00717   accept_const_reverse_iterator(s.crend());
00718 
00719   accept_reference(s[0]);
00720   accept_reference(s.at(0));
00721   accept_reference(s.front());
00722   accept_reference(s.back());
00723 }
00724 
00725 TEST(IntSpan, NoexceptTest) {
00726   int a[] = {1, 2, 3};
00727   std::vector<int> v;
00728   EXPECT_TRUE(noexcept(absl::Span<const int>()));
00729   EXPECT_TRUE(noexcept(absl::Span<const int>(a, 2)));
00730   EXPECT_TRUE(noexcept(absl::Span<const int>(a)));
00731   EXPECT_TRUE(noexcept(absl::Span<const int>(v)));
00732   EXPECT_TRUE(noexcept(absl::Span<int>(v)));
00733   EXPECT_TRUE(noexcept(absl::Span<const int>({1, 2, 3})));
00734   EXPECT_TRUE(noexcept(absl::MakeSpan(v)));
00735   EXPECT_TRUE(noexcept(absl::MakeSpan(a)));
00736   EXPECT_TRUE(noexcept(absl::MakeSpan(a, 2)));
00737   EXPECT_TRUE(noexcept(absl::MakeSpan(a, a + 1)));
00738   EXPECT_TRUE(noexcept(absl::MakeConstSpan(v)));
00739   EXPECT_TRUE(noexcept(absl::MakeConstSpan(a)));
00740   EXPECT_TRUE(noexcept(absl::MakeConstSpan(a, 2)));
00741   EXPECT_TRUE(noexcept(absl::MakeConstSpan(a, a + 1)));
00742 
00743   absl::Span<int> s(v);
00744   EXPECT_TRUE(noexcept(s.data()));
00745   EXPECT_TRUE(noexcept(s.size()));
00746   EXPECT_TRUE(noexcept(s.length()));
00747   EXPECT_TRUE(noexcept(s.empty()));
00748   EXPECT_TRUE(noexcept(s[0]));
00749   EXPECT_TRUE(noexcept(s.front()));
00750   EXPECT_TRUE(noexcept(s.back()));
00751   EXPECT_TRUE(noexcept(s.begin()));
00752   EXPECT_TRUE(noexcept(s.cbegin()));
00753   EXPECT_TRUE(noexcept(s.end()));
00754   EXPECT_TRUE(noexcept(s.cend()));
00755   EXPECT_TRUE(noexcept(s.rbegin()));
00756   EXPECT_TRUE(noexcept(s.crbegin()));
00757   EXPECT_TRUE(noexcept(s.rend()));
00758   EXPECT_TRUE(noexcept(s.crend()));
00759   EXPECT_TRUE(noexcept(s.remove_prefix(0)));
00760   EXPECT_TRUE(noexcept(s.remove_suffix(0)));
00761 }
00762 
00763 // ConstexprTester exercises expressions in a constexpr context. Simply placing
00764 // the expression in a constexpr function is not enough, as some compilers will
00765 // simply compile the constexpr function as runtime code. Using template
00766 // parameters forces compile-time execution.
00767 template <int i>
00768 struct ConstexprTester {};
00769 
00770 #define ABSL_TEST_CONSTEXPR(expr)                       \
00771   do {                                                  \
00772     ABSL_ATTRIBUTE_UNUSED ConstexprTester<(expr, 1)> t; \
00773   } while (0)
00774 
00775 struct ContainerWithConstexprMethods {
00776   constexpr int size() const { return 1; }
00777   constexpr const int* data() const { return &i; }
00778   const int i;
00779 };
00780 
00781 TEST(ConstIntSpan, ConstexprTest) {
00782   static constexpr int a[] = {1, 2, 3};
00783   static constexpr int sized_arr[2] = {1, 2};
00784   static constexpr ContainerWithConstexprMethods c{1};
00785   ABSL_TEST_CONSTEXPR(absl::Span<const int>());
00786   ABSL_TEST_CONSTEXPR(absl::Span<const int>(a, 2));
00787   ABSL_TEST_CONSTEXPR(absl::Span<const int>(sized_arr));
00788   ABSL_TEST_CONSTEXPR(absl::Span<const int>(c));
00789   ABSL_TEST_CONSTEXPR(absl::MakeSpan(&a[0], 1));
00790   ABSL_TEST_CONSTEXPR(absl::MakeSpan(c));
00791   ABSL_TEST_CONSTEXPR(absl::MakeSpan(a));
00792   ABSL_TEST_CONSTEXPR(absl::MakeConstSpan(&a[0], 1));
00793   ABSL_TEST_CONSTEXPR(absl::MakeConstSpan(c));
00794   ABSL_TEST_CONSTEXPR(absl::MakeConstSpan(a));
00795 
00796   constexpr absl::Span<const int> span = c;
00797   ABSL_TEST_CONSTEXPR(span.data());
00798   ABSL_TEST_CONSTEXPR(span.size());
00799   ABSL_TEST_CONSTEXPR(span.length());
00800   ABSL_TEST_CONSTEXPR(span.empty());
00801   ABSL_TEST_CONSTEXPR(span.begin());
00802   ABSL_TEST_CONSTEXPR(span.cbegin());
00803   ABSL_TEST_CONSTEXPR(span.subspan(0, 0));
00804   ABSL_TEST_CONSTEXPR(span.first(1));
00805   ABSL_TEST_CONSTEXPR(span.last(1));
00806   ABSL_TEST_CONSTEXPR(span[0]);
00807 }
00808 
00809 struct BigStruct {
00810   char bytes[10000];
00811 };
00812 
00813 TEST(Span, SpanSize) {
00814   EXPECT_LE(sizeof(absl::Span<int>), 2 * sizeof(void*));
00815   EXPECT_LE(sizeof(absl::Span<BigStruct>), 2 * sizeof(void*));
00816 }
00817 
00818 TEST(Span, Hash) {
00819   int array[] = {1, 2, 3, 4};
00820   int array2[] = {1, 2, 3};
00821   using T = absl::Span<const int>;
00822   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
00823       {// Empties
00824        T(), T(nullptr, 0), T(array, 0), T(array2, 0),
00825        // Different array with same value
00826        T(array, 3), T(array2), T({1, 2, 3}),
00827        // Same array, but different length
00828        T(array, 1), T(array, 2),
00829        // Same length, but different array
00830        T(array + 1, 2), T(array + 2, 2)}));
00831 }
00832 
00833 }  // namespace


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