utf8_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
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 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:16