UTF8StringTest.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/UTF8String.h>
  17. #include <folly/Range.h>
  18. #include <folly/portability/GTest.h>
  19. #include <folly/test/TestUtils.h>
  20. using namespace folly;
  21. using namespace std;
  22. const folly::StringPiece kTestUTF8 = u8"This is \U0001F602 stuff!";
  23. TEST(UTF8StringPiece, valid_utf8) {
  24. folly::StringPiece sp = kTestUTF8;
  25. UTF8StringPiece utf8 = sp;
  26. // utf8.size() not available since it's not a random-access range
  27. EXPECT_EQ(16, utf8.walk_size());
  28. }
  29. TEST(UTF8StringPiece, valid_suffix) {
  30. UTF8StringPiece utf8 = kTestUTF8.subpiece(8);
  31. EXPECT_EQ(8, utf8.walk_size());
  32. }
  33. TEST(UTF8StringPiece, empty_mid_codepoint) {
  34. UTF8StringPiece utf8 = kTestUTF8.subpiece(9, 0); // okay since it's empty
  35. EXPECT_EQ(0, utf8.walk_size());
  36. }
  37. TEST(UTF8StringPiece, invalid_mid_codepoint) {
  38. EXPECT_THROW(UTF8StringPiece(kTestUTF8.subpiece(9, 1)), std::out_of_range);
  39. }
  40. TEST(UTF8StringPiece, valid_implicit_conversion) {
  41. std::string input = u8"\U0001F602\U0001F602\U0001F602";
  42. auto checkImplicitCtor = [](UTF8StringPiece implicitCtor) {
  43. return implicitCtor.walk_size();
  44. };
  45. EXPECT_EQ(3, checkImplicitCtor(input));
  46. }