+TEST(NonConstTest, StringPiece) {
+ std::string hello("hello");
+ MutableStringPiece sp(&hello.front(), hello.size());
+ sp[0] = 'x';
+ EXPECT_EQ("xello", hello);
+ {
+ StringPiece s(sp);
+ EXPECT_EQ("xello", s);
+ }
+ {
+ ByteRange r1(sp);
+ MutableByteRange r2(sp);
+ }
+}
+
+// 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;
+ // type, conversion checks
+ Range<int*> 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(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());
+ EXPECT_EQ(n, r5.size());
+}
+
+TEST(RangeFunc, Vector) {
+ std::vector<int> x;
+ testRangeFunc(x, 0);
+ x.push_back(2);
+ testRangeFunc(x, 1);
+ testRangeFunc(std::vector<int>{1, 2}, 2);
+}
+
+TEST(RangeFunc, Array) {
+ std::array<int, 3> x;
+ testRangeFunc(x, 3);
+}
+
+TEST(RangeFunc, CArray) {
+ int x[] {1, 2, 3, 4};
+ testRangeFunc(x, 4);
+}
+
+TEST(RangeFunc, ConstexprCArray) {
+ static constexpr const int numArray[4] = {3, 17, 1, 9};
+ constexpr const auto numArrayRange = range(numArray);
+ EXPECT_EQ(17, numArrayRange[1]);
+ constexpr const auto numArrayRangeSize = numArrayRange.size();
+ EXPECT_EQ(4, numArrayRangeSize);
+}
+
+TEST(RangeFunc, ConstexprStdArray) {
+ static constexpr const std::array<int, 4> numArray = {{3, 17, 1, 9}};
+ constexpr const auto numArrayRange = range(numArray);
+ EXPECT_EQ(17, numArrayRange[1]);
+ constexpr const auto numArrayRangeSize = numArrayRange.size();
+ EXPECT_EQ(4, numArrayRangeSize);
+}
+
+TEST(RangeFunc, ConstexprStdArrayZero) {
+ static constexpr const std::array<int, 0> numArray = {};
+ constexpr const auto numArrayRange = range(numArray);
+ constexpr const auto numArrayRangeSize = numArrayRange.size();
+ EXPECT_EQ(0, numArrayRangeSize);
+}
+
+TEST(RangeFunc, ConstexprIteratorPair) {
+ static constexpr const int numArray[4] = {3, 17, 1, 9};
+ constexpr const auto numPtr = static_cast<const int*>(numArray);
+ constexpr const auto numIterRange = range(numPtr + 1, numPtr + 3);
+ EXPECT_EQ(1, numIterRange[1]);
+ constexpr const auto numIterRangeSize = numIterRange.size();
+ EXPECT_EQ(2, numIterRangeSize);
+}
+
+TEST(RangeFunc, ConstexprCollection) {
+ class IntCollection {
+ public:
+ constexpr IntCollection(const int* d, size_t s) : data_(d), size_(s) {}
+ constexpr const int* data() const {
+ return data_;
+ }
+ constexpr size_t size() const {
+ return size_;
+ }
+
+ private:
+ const int* data_;
+ size_t size_;
+ };
+ static constexpr const int numArray[4] = {3, 17, 1, 9};
+ constexpr const auto numPtr = static_cast<const int*>(numArray);
+ constexpr const auto numColl = IntCollection(numPtr + 1, 2);
+ constexpr const auto numCollRange = range(numColl);
+ EXPECT_EQ(1, numCollRange[1]);
+ constexpr const auto numCollRangeSize = numCollRange.size();
+ EXPECT_EQ(2, numCollRangeSize);
+}
+
+std::string get_rand_str(size_t size,
+ std::uniform_int_distribution<>& dist,
+ std::mt19937& gen) {
+ std::string ret(size, '\0');
+ for (size_t i = 0; i < size; ++i) {
+ ret[i] = static_cast<char>(dist(gen));
+ }
+
+ return ret;
+}
+
+namespace folly {
+bool operator==(MutableStringPiece mp, StringPiece sp) {
+ return mp.compare(sp) == 0;
+}
+
+bool operator==(StringPiece sp, MutableStringPiece mp) {
+ return mp.compare(sp) == 0;
+}
+}
+
+TEST(ReplaceAt, exhaustiveTest) {
+ char input[] = "this is nice and long input";
+ auto msp = MutableStringPiece(input);
+ auto str = std::string(input);
+ std::random_device rd;
+ std::mt19937 gen(rd());
+ std::uniform_int_distribution<> dist('a', 'z');
+
+ for (int i=0; i < 100; ++i) {
+ for (size_t j = 1; j <= msp.size(); ++j) {
+ auto replacement = get_rand_str(j, dist, gen);
+ 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);
+ }
+ }
+ }
+
+ // too far
+ EXPECT_EQ(msp.replaceAt(msp.size() - 2, StringPiece("meh")), false);
+}
+
+TEST(ReplaceAll, basicTest) {
+ char input[] = "this is nice and long input";
+ auto orig = std::string(input);
+ auto msp = MutableStringPiece(input);
+
+ EXPECT_EQ(msp.replaceAll("is", "si"), 2);
+ EXPECT_EQ("thsi si nice and long input", msp);
+ EXPECT_EQ(msp.replaceAll("si", "is"), 2);
+ EXPECT_EQ(msp, orig);
+
+ EXPECT_EQ(msp.replaceAll("abcd", "efgh"), 0); // nothing to replace
+ EXPECT_EQ(msp, orig);
+
+ // at the very beginning
+ EXPECT_EQ(msp.replaceAll("this", "siht"), 1);
+ EXPECT_EQ("siht is nice and long input", msp);
+ EXPECT_EQ(msp.replaceAll("siht", "this"), 1);
+ EXPECT_EQ(msp, orig);
+
+ // at the very end
+ EXPECT_EQ(msp.replaceAll("input", "soput"), 1);
+ EXPECT_EQ("this is nice and long soput", msp);
+ EXPECT_EQ(msp.replaceAll("soput", "input"), 1);
+ EXPECT_EQ(msp, orig);
+
+ // all spaces
+ EXPECT_EQ(msp.replaceAll(" ", "@"), 5);
+ EXPECT_EQ("this@is@nice@and@long@input", msp);
+ EXPECT_EQ(msp.replaceAll("@", " "), 5);
+ EXPECT_EQ(msp, orig);
+}
+
+TEST(ReplaceAll, randomTest) {
+ char input[] = "abcdefghijklmnoprstuwqz"; // no pattern repeata inside
+ auto orig = std::string(input);
+ auto msp = MutableStringPiece(input);
+
+ std::random_device rd;
+ std::mt19937 gen(rd());
+ std::uniform_int_distribution<> dist('A', 'Z');
+
+ for (int i=0; i < 100; ++i) {
+ for (size_t j = 1; j <= orig.size(); ++j) {
+ auto replacement = get_rand_str(j, dist, gen);
+ 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(msp.replaceAll(replacement, piece), 1);
+ EXPECT_EQ(msp, orig);
+ }
+ }
+ }
+}
+
+TEST(ReplaceAll, BadArg) {
+ int count = 0;
+ auto fst = "longer";
+ auto snd = "small";
+ char input[] = "meh meh meh";
+ auto all = MutableStringPiece(input);
+
+ try {
+ all.replaceAll(fst, snd);
+ } catch (std::invalid_argument&) {
+ ++count;
+ }
+
+ try {
+ all.replaceAll(snd, fst);
+ } catch (std::invalid_argument&) {
+ ++count;
+ }
+
+ 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());
+}
+
+TEST(Range, ArrayConstructors) {
+ auto charArray = std::array<char, 4>{{'t', 'e', 's', 't'}};
+ auto constCharArray = std::array<char, 6>{{'f', 'o', 'o', 'b', 'a', 'r'}};
+ auto emptyArray = std::array<char, 0>{};
+
+ auto sp1 = StringPiece{charArray};
+ EXPECT_EQ(4, sp1.size());
+ EXPECT_EQ(charArray.data(), sp1.data());
+
+ auto sp2 = StringPiece(constCharArray);
+ EXPECT_EQ(6, sp2.size());
+ EXPECT_EQ(constCharArray.data(), sp2.data());
+
+ auto msp = MutableStringPiece(charArray);
+ EXPECT_EQ(4, msp.size());
+ EXPECT_EQ(charArray.data(), msp.data());
+
+ auto esp = StringPiece(emptyArray);
+ EXPECT_EQ(0, esp.size());
+ EXPECT_EQ(nullptr, esp.data());
+
+ auto emsp = MutableStringPiece(emptyArray);
+ EXPECT_EQ(0, emsp.size());
+ EXPECT_EQ(nullptr, emsp.data());
+
+ static constexpr std::array<int, 4> numArray = {{3, 17, 1, 9}};
+ constexpr auto numRange = Range<const int*>{numArray};
+ EXPECT_EQ(17, numRange[1]);
+
+ static constexpr std::array<int, 0> emptyNumArray{};
+ constexpr auto emptyNumRange = Range<const int*>{emptyNumArray};
+ EXPECT_EQ(0, emptyNumRange.size());
+}
+
+TEST(Range, ConstexprAccessors) {
+ constexpr StringPiece piece = range("hello");
+ static_assert(piece.size() == 6u, "");
+ static_assert(piece.end() - piece.begin() == 6u, "");
+ static_assert(piece.data() == piece.begin(), "");
+ static_assert(piece.start() == piece.begin(), "");
+ static_assert(piece.cbegin() == piece.begin(), "");
+ static_assert(piece.cend() == piece.end(), "");
+ static_assert(*piece.begin() == 'h', "");
+ static_assert(*(piece.end() - 1) == '\0', "");
+}