#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>
// 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();
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));
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,
bool operator==(StringPiece sp, MutableStringPiece mp) {
return mp.compare(sp) == 0;
}
-}
+} // namespace folly
TEST(ReplaceAt, exhaustiveTest) {
char input[] = "this is nice and long input";
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);
+}