Lua exception handling test
[folly.git] / folly / test / RangeTest.cpp
index dc7e4ff0842b5470d9eaf77aa85099685e4c5612..8070b80110b6eb192075d639527359e897c14780 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 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 <folly/portability/GTest.h>
-#include <folly/portability/Memory.h>
-#include <folly/portability/SysMman.h>
-
 #include <array>
 #include <iterator>
 #include <limits>
 #include <string>
 #include <type_traits>
 #include <vector>
-#include <boost/range/concepts.hpp>
+
 #include <boost/algorithm/string/trim.hpp>
+#include <boost/range/concepts.hpp>
+
+#include <folly/portability/GTest.h>
+#include <folly/portability/Memory.h>
+#include <folly/portability/SysMman.h>
 
 using namespace folly;
 using namespace folly::detail;
@@ -315,6 +316,15 @@ TEST(StringPiece, Prefix) {
   EXPECT_FALSE(a.startsWith('x'));
   EXPECT_FALSE(a.startsWith("x"));
 
+  EXPECT_TRUE(a.startsWith("", folly::AsciiCaseInsensitive()));
+  EXPECT_TRUE(a.startsWith("hello", folly::AsciiCaseInsensitive()));
+  EXPECT_TRUE(a.startsWith("hellO", folly::AsciiCaseInsensitive()));
+  EXPECT_TRUE(a.startsWith("HELL", folly::AsciiCaseInsensitive()));
+  EXPECT_TRUE(a.startsWith("H", folly::AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.startsWith("HELLOX", folly::AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.startsWith("x", folly::AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.startsWith("X", folly::AsciiCaseInsensitive()));
+
   {
     auto b = a;
     EXPECT_TRUE(b.removePrefix(""));
@@ -362,6 +372,16 @@ TEST(StringPiece, Suffix) {
   EXPECT_FALSE(a.endsWith("x"));
   EXPECT_FALSE(a.endsWith('x'));
 
+  EXPECT_TRUE(a.endsWith("", folly::AsciiCaseInsensitive()));
+  EXPECT_TRUE(a.endsWith("o", folly::AsciiCaseInsensitive()));
+  EXPECT_TRUE(a.endsWith("O", folly::AsciiCaseInsensitive()));
+  EXPECT_TRUE(a.endsWith("hello", folly::AsciiCaseInsensitive()));
+  EXPECT_TRUE(a.endsWith("hellO", folly::AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.endsWith("xhello", folly::AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.endsWith("Xhello", folly::AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.endsWith("x", folly::AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.endsWith("X", folly::AsciiCaseInsensitive()));
+
   {
     auto b = a;
     EXPECT_TRUE(b.removeSuffix(""));
@@ -399,6 +419,13 @@ TEST(StringPiece, Suffix) {
   }
 }
 
+TEST(StringPiece, Equals) {
+  StringPiece a("hello");
+
+  EXPECT_TRUE(a.equals("HELLO", AsciiCaseInsensitive()));
+  EXPECT_FALSE(a.equals("HELLOX", AsciiCaseInsensitive()));
+}
+
 TEST(StringPiece, PrefixEmpty) {
   StringPiece a;
   EXPECT_TRUE(a.startsWith(""));
@@ -946,17 +973,17 @@ TYPED_TEST(NeedleFinderTest, Needles256) {
   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 (size_t i = minValue; i <= maxValue; i++) {  // <=
+  for (int i = minValue; i <= maxValue; i++) { // <=
     needles.push_back(i);
   }
   EXPECT_EQ(StringPiece::npos, this->find_first_byte_of("", needles));
-  for (size_t i = minValue; i <= maxValue; i++) {
+  for (int 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 (size_t i = minValue; i <= maxValue; i++) {
+  for (int i = minValue; i <= maxValue; i++) {
     EXPECT_EQ(0, this->find_first_byte_of(string(haystackSize, i), needles));
   }
 }
@@ -982,7 +1009,7 @@ void createProtectedBuf(StringPiece& contents, char** buf) {
   const size_t kSuccess = 0;
   char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
   if (pageAlignedBuf == nullptr) {
-    ASSERT_FALSE(true);
+    FAIL();
   }
   // Protect the page after the first full page-aligned region of the
   // malloc'ed buffer
@@ -1106,6 +1133,21 @@ TEST(RangeFunc, ConstexprCArray) {
   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);
@@ -1271,3 +1313,49 @@ TEST(Range, Constructors) {
   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', "");
+}