/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/Range.h>
-#include <sys/mman.h>
+#include <folly/portability/Memory.h>
+#include <folly/portability/SysMman.h>
+
#include <array>
-#include <cstdlib>
#include <iterator>
#include <limits>
#include <random>
#include <type_traits>
#include <vector>
#include <boost/range/concepts.hpp>
+#include <boost/algorithm/string/trim.hpp>
#include <gtest/gtest.h>
-namespace folly { namespace detail {
-
-// declaration of functions in Range.cpp
-size_t qfind_first_byte_of_memchr(const StringPiece& haystack,
- const StringPiece& needles);
-
-size_t qfind_first_byte_of_byteset(const StringPiece& haystack,
- const StringPiece& needles);
-
-}} // namespaces
-
using namespace folly;
+using namespace folly::detail;
using namespace std;
static_assert(std::is_literal_type<StringPiece>::value, "");
EXPECT_THROW(a.subpiece(6), std::out_of_range);
}
-#if FOLLY_HAVE_CONSTEXPR_STRLEN
constexpr char helloArray[] = "hello";
TEST(StringPiece, Constexpr) {
constexpr StringPiece hello1("hello");
EXPECT_EQ("hello", hello1);
+ static_assert(hello1.size() == 5, "hello size should be 5 at compile time");
constexpr StringPiece hello2(helloArray);
EXPECT_EQ("hello", hello2);
+ static_assert(hello2.size() == 5, "hello size should be 5 at compile time");
}
-#endif
TEST(StringPiece, Prefix) {
StringPiece a("hello");
EXPECT_EQ("", a);
}
+TEST(StringPiece, erase) {
+ StringPiece a("hello");
+ auto b = a.begin();
+ auto e = b + 1;
+ a.erase(b, e);
+ EXPECT_EQ("ello", a);
+
+ e = a.end();
+ b = e - 1;
+ a.erase(b, e);
+ EXPECT_EQ("ell", a);
+
+ b = a.end() - 1;
+ e = a.end() - 1;
+ EXPECT_THROW(a.erase(b, e), std::out_of_range);
+
+ b = a.begin();
+ e = a.end();
+ a.erase(b, e);
+ EXPECT_EQ("", a);
+
+ a = "hello";
+ b = a.begin();
+ e = b + 2;
+ a.erase(b, e);
+ EXPECT_EQ("llo", a);
+
+ b = a.end() - 2;
+ e = a.end();
+ a.erase(b, e);
+ EXPECT_EQ("l", a);
+
+ a = " hello ";
+ boost::algorithm::trim(a);
+ EXPECT_EQ(a, "hello");
+}
+
TEST(StringPiece, split_step_char_delimiter) {
// 0 1 2
// 012345678901234567890123456
EXPECT_TRUE(p.empty());
}
+TEST(StringPiece, NoInvalidImplicitConversions) {
+ struct IsString {
+ bool operator()(folly::Range<int*>) { return false; }
+ bool operator()(folly::StringPiece) { return true; }
+ };
+
+ std::string s = "hello";
+ EXPECT_TRUE(IsString()(s));
+}
+
TEST(qfind, UInt32_Ranges) {
vector<uint32_t> a({1, 2, 3, 260, 5});
vector<uint32_t> b({2, 3, 4});
}
};
-struct MemchrNeedleFinder {
- static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
- return detail::qfind_first_byte_of_memchr(haystack, needles);
- }
-};
-
struct ByteSetNeedleFinder {
static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
return detail::qfind_first_byte_of_byteset(haystack, needles);
}
};
-typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
+typedef ::testing::Types<SseNeedleFinder,
+ NoSseNeedleFinder,
ByteSetNeedleFinder> NeedleFinders;
TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
TYPED_TEST(NeedleFinderTest, Unaligned) {
// works correctly even if input buffers are not 16-byte aligned
string s = "0123456789ABCDEFGH";
- for (int i = 0; i < s.size(); ++i) {
+ for (size_t i = 0; i < s.size(); ++i) {
StringPiece a(s.c_str() + i);
- for (int j = 0; j < s.size(); ++j) {
+ for (size_t j = 0; j < s.size(); ++j) {
StringPiece b(s.c_str() + j);
EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
}
const auto maxValue = std::numeric_limits<StringPiece::value_type>::max();
// make the size ~big to avoid any edge-case branches for tiny haystacks
const int haystackSize = 50;
- for (int i = minValue; i <= maxValue; i++) { // <=
+ for (size_t i = minValue; i <= maxValue; i++) { // <=
needles.push_back(i);
}
EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
- for (int i = minValue; i <= maxValue; i++) {
+ for (size_t i = minValue; i <= maxValue; i++) {
EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
}
needles.append("these are redundant characters");
EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
- for (int i = minValue; i <= maxValue; i++) {
+ for (size_t i = minValue; i <= maxValue; i++) {
EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
}
}
TYPED_TEST(NeedleFinderTest, Base) {
- for (int i = 0; i < 32; ++i) {
+ for (size_t i = 0; i < 32; ++i) {
for (int j = 0; j < 32; ++j) {
string s = string(i, 'X') + "abca" + string(i, 'X');
string delims = string(j, 'Y') + "a" + string(j, 'Y');
void createProtectedBuf(StringPiece& contents, char** buf) {
ASSERT_LE(contents.size(), kPageSize);
const size_t kSuccess = 0;
- if (kSuccess != posix_memalign((void**)buf, kPageSize, 4 * kPageSize)) {
+ char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
+ if (pageAlignedBuf == nullptr) {
ASSERT_FALSE(true);
}
- mprotect(*buf + kPageSize, kPageSize, PROT_NONE);
+ // Protect the page after the first full page-aligned region of the
+ // malloc'ed buffer
+ mprotect(pageAlignedBuf + kPageSize, kPageSize, PROT_NONE);
size_t newBegin = kPageSize - contents.size();
- memcpy(*buf + newBegin, contents.data(), contents.size());
- contents.reset(*buf + newBegin, contents.size());
+ memcpy(pageAlignedBuf + newBegin, contents.data(), contents.size());
+ contents.reset(pageAlignedBuf + newBegin, contents.size());
+ *buf = pageAlignedBuf;
}
void freeProtectedBuf(char* buf) {
mprotect(buf + kPageSize, kPageSize, PROT_READ | PROT_WRITE);
- free(buf);
+ aligned_free(buf);
}
TYPED_TEST(NeedleFinderTest, NoSegFault) {
}
}
+// Similar to the begin() template functions, but instread of returing
+// an iterator, return a pointer to data.
+template <class Container>
+typename Container::value_type* dataPtr(Container& cont) {
+ // NOTE: &cont[0] is undefined if cont is empty (it creates a
+ // reference to nullptr - which is not dereferenced, but still UBSAN).
+ return cont.data();
+}
+template <class T, size_t N>
+constexpr T* dataPtr(T (&arr)[N]) noexcept {
+ return &arr[0];
+}
+
template<class C>
void testRangeFunc(C&& x, size_t n) {
const auto& cx = 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(r1.begin(), &x[0]);
- EXPECT_EQ(r1.end(), &x[n]);
+ EXPECT_EQ(r1.begin(), dataPtr(x));
+ EXPECT_EQ(r1.end(), dataPtr(x) + n);
EXPECT_EQ(n, r1.size());
EXPECT_EQ(n, r2.size());
EXPECT_EQ(n, r3.size());
testRangeFunc(x, 4);
}
-std::string get_rand_str(
- int size, std::uniform_int_distribution<>& dist, std::mt19937& gen) {
+std::string get_rand_str(size_t size,
+ std::uniform_int_distribution<>& dist,
+ std::mt19937& gen) {
std::string ret(size, '\0');
- for (int i=0; i<size; ++i) {
+ for (size_t i = 0; i < size; ++i) {
ret[i] = static_cast<char>(dist(gen));
}
std::uniform_int_distribution<> dist('a', 'z');
for (int i=0; i < 100; ++i) {
- for (int j = 1; j <= msp.size(); ++j) {
+ for (size_t j = 1; j <= msp.size(); ++j) {
auto replacement = get_rand_str(j, dist, gen);
- for (int pos=0; pos < msp.size() - j; ++pos) {
+ for (size_t pos = 0; pos < msp.size() - j; ++pos) {
msp.replaceAt(pos, replacement);
str.replace(pos, replacement.size(), replacement);
EXPECT_EQ(msp.compare(str), 0);
std::uniform_int_distribution<> dist('A', 'Z');
for (int i=0; i < 100; ++i) {
- for (int j = 1; j <= orig.size(); ++j) {
+ for (size_t j = 1; j <= orig.size(); ++j) {
auto replacement = get_rand_str(j, dist, gen);
- for (int pos=0; pos < msp.size() - j; ++pos) {
+ for (size_t pos = 0; pos < msp.size() - j; ++pos) {
auto piece = orig.substr(pos, j);
EXPECT_EQ(msp.replaceAll(piece, replacement), 1);
EXPECT_EQ(msp.find(replacement), pos);
EXPECT_EQ(count, 2);
}
+
+TEST(Range, Constructors) {
+ vector<int> c = {1, 2, 3};
+ typedef Range<vector<int>::iterator> RangeType;
+ typedef Range<vector<int>::const_iterator> ConstRangeType;
+ RangeType cr(c.begin(), c.end());
+ auto subpiece1 = ConstRangeType(cr, 1, 5);
+ auto subpiece2 = ConstRangeType(cr, 1);
+ EXPECT_EQ(subpiece1.size(), 2);
+ EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
+ EXPECT_EQ(subpiece1.end(), subpiece2.end());
+}