X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FRangeTest.cpp;h=8a2b2762def14124b57ea0c7b927d48400d3f11d;hb=009082dca9eeaa2f75f839ece82ae44ef8659387;hp=827e085cd9645d06a8b98f34f6a851d1dafc3bff;hpb=005736d3bc1d21fb0415fd68b6233890edc0b31b;p=folly.git diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index 827e085c..8a2b2762 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -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. @@ -315,6 +315,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 +371,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 +418,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("")); @@ -1093,14 +1119,10 @@ TEST(RangeFunc, Array) { testRangeFunc(x, 3); } -// MSVC doesn't like it when you try to std::move C arrays: -// https://developercommunity.visualstudio.com/content/problem/2441/ -#ifndef _MSC_VER TEST(RangeFunc, CArray) { int x[] {1, 2, 3, 4}; testRangeFunc(x, 4); } -#endif TEST(RangeFunc, ConstexprCArray) { static constexpr const int numArray[4] = {3, 17, 1, 9}; @@ -1290,3 +1312,49 @@ TEST(Range, Constructors) { EXPECT_EQ(subpiece1.begin(), subpiece2.begin()); EXPECT_EQ(subpiece1.end(), subpiece2.end()); } + +TEST(Range, ArrayConstructors) { + auto charArray = std::array{{'t', 'e', 's', 't'}}; + auto constCharArray = std::array{{'f', 'o', 'o', 'b', 'a', 'r'}}; + auto emptyArray = std::array{}; + + 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 numArray = {{3, 17, 1, 9}}; + constexpr auto numRange = Range{numArray}; + EXPECT_EQ(17, numRange[1]); + + static constexpr std::array emptyNumArray{}; + constexpr auto emptyNumRange = Range{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', ""); +}