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/utf16.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace pdfium {
10
11 static_assert(kSurrogateMask == 0x3ff);
12 static_assert(kMaximumSupplementaryCodePoint == 0x10ffff);
13 static_assert(kMaximumHighSurrogateCodeUnit == 0xdbff);
14 static_assert(kMinimumLowSurrogateCodeUnit == 0xdc00);
15 static_assert(kMaximumLowSurrogateCodeUnit == 0xdfff);
16
17 static_assert(!IsSupplementary(0xffff));
18 static_assert(IsSupplementary(0x10000));
19 static_assert(IsSupplementary(0x10ffff));
20 static_assert(!IsSupplementary(0x110000));
21
22 static_assert(!IsHighSurrogate(0xd7ff));
23 static_assert(IsHighSurrogate(0xd800));
24 static_assert(IsHighSurrogate(0xdbff));
25 static_assert(!IsHighSurrogate(0xdc00));
26
27 static_assert(!IsLowSurrogate(0xdbff));
28 static_assert(IsLowSurrogate(0xdc00));
29 static_assert(IsLowSurrogate(0xdfff));
30 static_assert(!IsLowSurrogate(0xe000));
31
32 static_assert(SurrogatePair(0xd800, 0xdc00).high() == 0xd800);
33 static_assert(SurrogatePair(0xd800, 0xdc00).low() == 0xdc00);
34 static_assert(SurrogatePair(0xd800, 0xdc00).ToCodePoint() == 0x10000);
35
36 static_assert(SurrogatePair(0xdbff, 0xdfff).high() == 0xdbff);
37 static_assert(SurrogatePair(0xdbff, 0xdfff).low() == 0xdfff);
38 static_assert(SurrogatePair(0xdbff, 0xdfff).ToCodePoint() == 0x10ffff);
39
40 static_assert(SurrogatePair(0x10000).high() == 0xd800);
41 static_assert(SurrogatePair(0x10000).low() == 0xdc00);
42 static_assert(SurrogatePair(0x10000).ToCodePoint() == 0x10000);
43
44 static_assert(SurrogatePair(0x10ffff).high() == 0xdbff);
45 static_assert(SurrogatePair(0x10ffff).low() == 0xdfff);
46 static_assert(SurrogatePair(0x10ffff).ToCodePoint() == 0x10ffff);
47
TEST(SurrogatePairTest,RoundTrip)48 TEST(SurrogatePairTest, RoundTrip) {
49 for (char32_t code_point = kMinimumSupplementaryCodePoint;
50 code_point <= kMaximumSupplementaryCodePoint; ++code_point) {
51 SurrogatePair from_code_point(code_point);
52 EXPECT_EQ(code_point, from_code_point.ToCodePoint());
53
54 SurrogatePair from_pair(from_code_point.high(), from_code_point.low());
55 EXPECT_EQ(from_code_point.high(), from_pair.high());
56 EXPECT_EQ(from_code_point.low(), from_pair.low());
57 EXPECT_EQ(code_point, from_pair.ToCodePoint());
58 }
59 }
60
61 } // namespace pdfium
62