abseil-cpp/absl/strings/internal/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 
15 #include "absl/strings/internal/utf8.h"
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";
57  char buf2[32] = "Negative is invalid but sane";
60 }
61 #if defined(__clang__)
62 #pragma clang diagnostic pop
63 #endif
64 #endif // !defined(__cpp_char8_t)
65 
66 } // namespace
test
Definition: spinlock_test.cc:36
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::strings_internal::EncodeUTF8Char
size_t EncodeUTF8Char(char *buffer, char32_t utf8_char)
Definition: abseil-cpp/absl/strings/internal/utf8.cc:23
EXPECT_LE
#define EXPECT_LE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2030
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
absl::strings_internal::kMaxEncodedUTF8Size
@ kMaxEncodedUTF8Size
Definition: abseil-cpp/absl/strings/internal/utf8.h:43
tests
Definition: src/python/grpcio_tests/tests/__init__.py:1
buf2
static char buf2[32]
Definition: test-fs.c:127


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:49