utf8_test.cc
Go to the documentation of this file.
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 
16 
17 #include <cstdint>
18 #include <utility>
19 
20 #include "gtest/gtest.h"
21 #include "absl/base/port.h"
22 
23 namespace {
24 
25 #if !defined(__cpp_char8_t)
26 #if defined(__clang__)
27 #pragma clang diagnostic push
28 #pragma clang diagnostic ignored "-Wc++2a-compat"
29 #endif
30 TEST(EncodeUTF8Char, BasicFunction) {
31  std::pair<char32_t, std::string> tests[] = {{0x0030, u8"\u0030"},
32  {0x00A3, u8"\u00A3"},
33  {0x00010000, u8"\U00010000"},
34  {0x0000FFFF, u8"\U0000FFFF"},
35  {0x0010FFFD, u8"\U0010FFFD"}};
36  for (auto &test : tests) {
37  char buf0[7] = {'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'};
38  char buf1[7] = {'\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF'};
39  char *buf0_written =
40  &buf0[absl::strings_internal::EncodeUTF8Char(buf0, test.first)];
41  char *buf1_written =
42  &buf1[absl::strings_internal::EncodeUTF8Char(buf1, test.first)];
43  int apparent_length = 7;
44  while (buf0[apparent_length - 1] == '\x00' &&
45  buf1[apparent_length - 1] == '\xFF') {
46  if (--apparent_length == 0) break;
47  }
48  EXPECT_EQ(apparent_length, buf0_written - buf0);
49  EXPECT_EQ(apparent_length, buf1_written - buf1);
50  EXPECT_EQ(apparent_length, test.second.length());
51  EXPECT_EQ(std::string(buf0, apparent_length), test.second);
52  EXPECT_EQ(std::string(buf1, apparent_length), test.second);
53  }
54  char buf[32] = "Don't Tread On Me";
55  EXPECT_LE(absl::strings_internal::EncodeUTF8Char(buf, 0x00110000),
57  char buf2[32] = "Negative is invalid but sane";
58  EXPECT_LE(absl::strings_internal::EncodeUTF8Char(buf2, -1),
60 }
61 #if defined(__clang__)
62 #pragma clang diagnostic pop
63 #endif
64 #endif // !defined(__cpp_char8_t)
65 
66 } // namespace
char buf[N]
size_t EncodeUTF8Char(char *buffer, char32_t utf8_char)
Definition: utf8.cc:22
TEST(Symbolize, Unimplemented)


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:21