xref: /aosp_15_r20/external/pdfium/core/fxcrt/code_point_view_unittest.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2023 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "core/fxcrt/code_point_view.h"
6 
7 #include <string>
8 
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace {
13 
14 using ::pdfium::CodePointView;
15 
Materialize(CodePointView view)16 std::u32string Materialize(CodePointView view) {
17   std::u32string materialized;
18   for (char32_t code_point : view) {
19     materialized += code_point;
20   }
21   return materialized;
22 }
23 
24 }  // namespace
25 
TEST(CodePointViewTest,Empty)26 TEST(CodePointViewTest, Empty) {
27   EXPECT_EQ(U"", Materialize(CodePointView(L"")));
28 }
29 
TEST(CodePointViewTest,Basic)30 TEST(CodePointViewTest, Basic) {
31   EXPECT_EQ(U"(\u0080\uffff)", Materialize(CodePointView(L"(\u0080\uffff)")));
32 }
33 
TEST(CodePointViewTest,Supplementary)34 TEST(CodePointViewTest, Supplementary) {
35   EXPECT_EQ(U"(��)", Materialize(CodePointView(L"(��)")));
36 }
37 
TEST(CodePointViewTest,UnpairedHighSurrogate)38 TEST(CodePointViewTest, UnpairedHighSurrogate) {
39   EXPECT_EQ(U"\xd800", Materialize(CodePointView(L"\xd800")));
40 }
41 
TEST(CodePointViewTest,UnpairedLowSurrogate)42 TEST(CodePointViewTest, UnpairedLowSurrogate) {
43   EXPECT_EQ(U"\xdc00", Materialize(CodePointView(L"\xdc00")));
44 }
45 
46 #if defined(WCHAR_T_IS_UTF16)
TEST(CodePointViewTest,SurrogateErrorRecovery)47 TEST(CodePointViewTest, SurrogateErrorRecovery) {
48   EXPECT_EQ(U"(\xd800)", Materialize(CodePointView(L"(\xd800)"))) << "High";
49   EXPECT_EQ(U"(\xdc00)", Materialize(CodePointView(L"(\xdc00)"))) << "Low";
50   EXPECT_EQ(U"(\xd800��)", Materialize(CodePointView(L"(\xd800\xd83c\xdfa8)")))
51       << "High-high";
52   EXPECT_EQ(U"(��\xdc00)", Materialize(CodePointView(L"(\xd83c\xdfa8\xdc00)")))
53       << "Low-low";
54 }
55 #endif  // defined(WCHAR_T_IS_UTF16)
56