utf8_test.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/internal/utf8.h"
  15. #include <cstdint>
  16. #include <utility>
  17. #include "gtest/gtest.h"
  18. #include "absl/base/port.h"
  19. namespace {
  20. #if !defined(__cpp_char8_t)
  21. #if defined(__clang__)
  22. #pragma clang diagnostic push
  23. #pragma clang diagnostic ignored "-Wc++2a-compat"
  24. #endif
  25. TEST(EncodeUTF8Char, BasicFunction) {
  26. std::pair<char32_t, std::string> tests[] = {{0x0030, u8"\u0030"},
  27. {0x00A3, u8"\u00A3"},
  28. {0x00010000, u8"\U00010000"},
  29. {0x0000FFFF, u8"\U0000FFFF"},
  30. {0x0010FFFD, u8"\U0010FFFD"}};
  31. for (auto &test : tests) {
  32. char buf0[7] = {'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'};
  33. char buf1[7] = {'\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF'};
  34. char *buf0_written =
  35. &buf0[absl::strings_internal::EncodeUTF8Char(buf0, test.first)];
  36. char *buf1_written =
  37. &buf1[absl::strings_internal::EncodeUTF8Char(buf1, test.first)];
  38. int apparent_length = 7;
  39. while (buf0[apparent_length - 1] == '\x00' &&
  40. buf1[apparent_length - 1] == '\xFF') {
  41. if (--apparent_length == 0) break;
  42. }
  43. EXPECT_EQ(apparent_length, buf0_written - buf0);
  44. EXPECT_EQ(apparent_length, buf1_written - buf1);
  45. EXPECT_EQ(apparent_length, test.second.length());
  46. EXPECT_EQ(std::string(buf0, apparent_length), test.second);
  47. EXPECT_EQ(std::string(buf1, apparent_length), test.second);
  48. }
  49. char buf[32] = "Don't Tread On Me";
  50. EXPECT_LE(absl::strings_internal::EncodeUTF8Char(buf, 0x00110000),
  51. absl::strings_internal::kMaxEncodedUTF8Size);
  52. char buf2[32] = "Negative is invalid but sane";
  53. EXPECT_LE(absl::strings_internal::EncodeUTF8Char(buf2, -1),
  54. absl::strings_internal::kMaxEncodedUTF8Size);
  55. }
  56. #if defined(__clang__)
  57. #pragma clang diagnostic pop
  58. #endif
  59. #endif // !defined(__cpp_char8_t)
  60. } // namespace