logging: set the thread name for the AsyncFileWriter thread
[folly.git] / folly / test / RangeTest.cpp
index 3538969c4756c40c5bcd92ca7992d67ba3455a67..dbe8d88fa2b6fa216c8207696127138ddcd413c4 100644 (file)
@@ -30,6 +30,7 @@
 #include <boost/algorithm/string/trim.hpp>
 #include <boost/range/concepts.hpp>
 
+#include <folly/portability/GMock.h>
 #include <folly/portability/GTest.h>
 #include <folly/portability/Memory.h>
 #include <folly/portability/SysMman.h>
@@ -1003,7 +1004,6 @@ const size_t kPageSize = 4096;
 // This function will also initialize buf, which caller must free().
 void createProtectedBuf(StringPiece& contents, char** buf) {
   ASSERT_LE(contents.size(), kPageSize);
-  const size_t kSuccess = 0;
   char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
   if (pageAlignedBuf == nullptr) {
     FAIL();
@@ -1092,7 +1092,9 @@ template <class C>
 void testRangeFunc(C&& x, size_t n) {
   const auto& cx = x;
   // type, conversion checks
-  Range<int*> r1 = range(std::forward<C>(x));
+  using R1Iter =
+      _t<std::conditional<_t<std::is_reference<C>>::value, int*, int const*>>;
+  Range<R1Iter> r1 = range(std::forward<C>(x));
   Range<const int*> r2 = range(std::forward<C>(x));
   Range<const int*> r3 = range(cx);
   Range<const int*> r5 = range(std::move(cx));
@@ -1178,6 +1180,57 @@ TEST(RangeFunc, ConstexprCollection) {
   EXPECT_EQ(2, numCollRangeSize);
 }
 
+TEST(CRangeFunc, CArray) {
+  int numArray[4] = {3, 17, 1, 9};
+  auto const numArrayRange = crange(numArray);
+  EXPECT_TRUE(
+      (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
+  EXPECT_THAT(numArrayRange, testing::ElementsAreArray(numArray));
+}
+
+TEST(CRangeFunc, StdArray) {
+  std::array<int, 4> numArray = {{3, 17, 1, 9}};
+  auto const numArrayRange = crange(numArray);
+  EXPECT_TRUE(
+      (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
+  EXPECT_THAT(numArrayRange, testing::ElementsAreArray(numArray));
+}
+
+TEST(CRangeFunc, StdArrayZero) {
+  std::array<int, 0> numArray = {};
+  auto const numArrayRange = crange(numArray);
+  EXPECT_TRUE(
+      (std::is_same<int const*, decltype(numArrayRange)::iterator>::value));
+  EXPECT_THAT(numArrayRange, testing::ElementsAreArray(numArray));
+}
+
+TEST(CRangeFunc, Collection) {
+  class IntCollection {
+   public:
+    constexpr IntCollection(int* d, size_t s) : data_(d), size_(s) {}
+    constexpr int* data() {
+      return data_;
+    }
+    constexpr int const* data() const {
+      return data_;
+    }
+    constexpr size_t size() const {
+      return size_;
+    }
+
+   private:
+    int* data_;
+    size_t size_;
+  };
+  int numArray[4] = {3, 17, 1, 9};
+  auto numPtr = static_cast<int*>(numArray);
+  auto numColl = IntCollection(numPtr + 1, 2);
+  auto const numCollRange = crange(numColl);
+  EXPECT_TRUE(
+      (std::is_same<int const*, decltype(numCollRange)::iterator>::value));
+  EXPECT_THAT(numCollRange, testing::ElementsAreArray({17, 1}));
+}
+
 std::string get_rand_str(
     size_t size,
     std::uniform_int_distribution<>& dist,
@@ -1198,7 +1251,7 @@ bool operator==(MutableStringPiece mp, StringPiece sp) {
 bool operator==(StringPiece sp, MutableStringPiece mp) {
   return mp.compare(sp) == 0;
 }
-}
+} // namespace folly
 
 TEST(ReplaceAt, exhaustiveTest) {
   char input[] = "this is nice and long input";
@@ -1357,3 +1410,21 @@ TEST(Range, ConstexprAccessors) {
   static_assert(*piece.begin() == 'h', "");
   static_assert(*(piece.end() - 1) == '\0', "");
 }
+
+TEST(Range, LiteralSuffix) {
+  constexpr auto literalPiece = "hello"_sp;
+  constexpr StringPiece piece = "hello";
+  EXPECT_EQ(literalPiece, piece);
+  constexpr auto literalPiece8 = u8"hello"_sp;
+  constexpr Range<char const*> piece8 = u8"hello";
+  EXPECT_EQ(literalPiece8, piece8);
+  constexpr auto literalPiece16 = u"hello"_sp;
+  constexpr Range<char16_t const*> piece16{u"hello", 5};
+  EXPECT_EQ(literalPiece16, piece16);
+  constexpr auto literalPiece32 = U"hello"_sp;
+  constexpr Range<char32_t const*> piece32{U"hello", 5};
+  EXPECT_EQ(literalPiece32, piece32);
+  constexpr auto literalPieceW = L"hello"_sp;
+  constexpr Range<wchar_t const*> pieceW{L"hello", 5};
+  EXPECT_EQ(literalPieceW, pieceW);
+}