Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/internal/utf8.h"
00016
00017 #include <cstdint>
00018 #include <utility>
00019
00020 #include "gtest/gtest.h"
00021 #include "absl/base/port.h"
00022
00023 namespace {
00024
00025 #if !defined(__cpp_char8_t)
00026 #if defined(__clang__)
00027 #pragma clang diagnostic push
00028 #pragma clang diagnostic ignored "-Wc++2a-compat"
00029 #endif
00030 TEST(EncodeUTF8Char, BasicFunction) {
00031 std::pair<char32_t, std::string> tests[] = {{0x0030, u8"\u0030"},
00032 {0x00A3, u8"\u00A3"},
00033 {0x00010000, u8"\U00010000"},
00034 {0x0000FFFF, u8"\U0000FFFF"},
00035 {0x0010FFFD, u8"\U0010FFFD"}};
00036 for (auto &test : tests) {
00037 char buf0[7] = {'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'};
00038 char buf1[7] = {'\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF'};
00039 char *buf0_written =
00040 &buf0[absl::strings_internal::EncodeUTF8Char(buf0, test.first)];
00041 char *buf1_written =
00042 &buf1[absl::strings_internal::EncodeUTF8Char(buf1, test.first)];
00043 int apparent_length = 7;
00044 while (buf0[apparent_length - 1] == '\x00' &&
00045 buf1[apparent_length - 1] == '\xFF') {
00046 if (--apparent_length == 0) break;
00047 }
00048 EXPECT_EQ(apparent_length, buf0_written - buf0);
00049 EXPECT_EQ(apparent_length, buf1_written - buf1);
00050 EXPECT_EQ(apparent_length, test.second.length());
00051 EXPECT_EQ(std::string(buf0, apparent_length), test.second);
00052 EXPECT_EQ(std::string(buf1, apparent_length), test.second);
00053 }
00054 char buf[32] = "Don't Tread On Me";
00055 EXPECT_LE(absl::strings_internal::EncodeUTF8Char(buf, 0x00110000),
00056 absl::strings_internal::kMaxEncodedUTF8Size);
00057 char buf2[32] = "Negative is invalid but sane";
00058 EXPECT_LE(absl::strings_internal::EncodeUTF8Char(buf2, -1),
00059 absl::strings_internal::kMaxEncodedUTF8Size);
00060 }
00061 #if defined(__clang__)
00062 #pragma clang diagnostic pop
00063 #endif
00064 #endif // !defined(__cpp_char8_t)
00065
00066 }