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