#include <folly/Range.h>
+#include <folly/portability/GTest.h>
#include <folly/portability/Memory.h>
+#include <folly/portability/SysMman.h>
-#include <sys/mman.h>
#include <array>
#include <iterator>
#include <limits>
#include <vector>
#include <boost/range/concepts.hpp>
#include <boost/algorithm/string/trim.hpp>
-#include <gtest/gtest.h>
using namespace folly;
using namespace folly::detail;
EXPECT_THROW(a.subpiece(6), std::out_of_range);
}
-constexpr char helloArray[] = "hello";
+constexpr const 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");
}
TEST(StringPiece, Prefix) {
}
}
+// 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());