Debian Codegraph @

core/unicode_test.cpp

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 Copyright 2025 Google Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #include <array>
18 #include <string>
19 #include <sstream>
20 #include <iostream>
21 #include "unicode.h"
22 #include "gtest/gtest.h"
23
24 namespace jsonnet::internal {
25 namespace {
26
27 void testEncodeDecode(char32_t codepoint, const std::string &expect_utf8) {
28 std::string buffer;
29 size_t len = encode_utf8(codepoint, buffer);
30 EXPECT_EQ(len, expect_utf8.size());
31 EXPECT_EQ(buffer, expect_utf8);
32
33 size_t at = 0;
34 char32_t decoded = decode_utf8(expect_utf8, at);
35 EXPECT_EQ(decoded, codepoint);
36 EXPECT_EQ(at, expect_utf8.size() - 1);
37 }
38
39 TEST(Unicode, TestUTF8)
40 {
41 // ASCII encodes as itself.
42 testEncodeDecode(0x00, std::string("\x00", 1));
43 testEncodeDecode(0x41, "A");
44 testEncodeDecode(0x7f, "\x7f");
45
46 testEncodeDecode(0x80, "\xc2\x80");
47 testEncodeDecode(0x100, "\xc4\x80");
48 testEncodeDecode(0x7ff, "\xdf\xbf");
49
50 testEncodeDecode(0x800, "\xe0\xa0\x80");
51 testEncodeDecode(0x1482, "\xe1\x92\x82");
52 testEncodeDecode(0xffff, "\xef\xbf\xbf");
53
54 testEncodeDecode(0x010000, "\xf0\x90\x80\x80");
55 testEncodeDecode(0x01f600, "\xf0\x9f\x98\x80"); // U+1F600 "Grinning Face"
56 testEncodeDecode(0x0f057e, "\xf3\xb0\x95\xbe"); // U+F057E Private use area character
57 testEncodeDecode(0x10ffff, "\xf4\x8f\xbf\xbf");
58 }
59
60 TEST(Unicode, TestUTF8RejectBad)
61 {
62 const auto test_cases = std::array<const char*, 20>{
63 "\x80", // Continuation byte without leading byte
64 "\xa0", // Continuation byte without leading byte
65 "\xbf", // Continuation byte without leading byte
66 "\xc0", // Leading byte for 2-byte sequence (missing tail)
67 "\xe0", // Leading byte for 3-byte sequence (missing tail)
68 "\xf0", // Leading byte for 4-byte sequence (missing tail)
69 "\xf8\x83\x83\x83", // Invalid leading byte
70 "\xe0\x80", // Leading byte for 3-byte sequence (missing tail)
71 "\xf0\x80", // Leading byte for 4-byte sequence (missing tail)
72 "\xf0\x80\x80", // Leading byte for 4-byte sequence (missing tail)
73 "\xc0\xcf", // Leading byte for 2-byte sequence (incorrect tail)
74 "\xe0\xcf", // Leading byte for 3-byte sequence (incorrect tail)
75 "\xf0\xcf", // Leading byte for 4-byte sequence (incorrect tail)
76 "\xe0\xcf\x80", // Leading byte for 3-byte sequence (incorrect tail)
77 "\xf0\xcf\x80", // Leading byte for 4-byte sequence (incorrect tail)
78 "\xe0\x80\xcf", // Leading byte for 3-byte sequence (incorrect tail)
79 "\xf0\x80\xcf", // Leading byte for 4-byte sequence (incorrect tail)
80 "\xf0\x80\x80\xcf", // Leading byte for 4-byte sequence (incorrect tail)
81 "\xf0\x80\xcf\x80", // Leading byte for 4-byte sequence (incorrect tail)
82 "\xf0\xcf\x80\x80", // Leading byte for 4-byte sequence (incorrect tail)
83 };
84 for (size_t i = 0; i < test_cases.size(); ++i) {
85 const auto str = test_cases[i];
86 size_t at = 0;
87 char32_t c = decode_utf8(str, at);
88
89 EXPECT_EQ(c, JSONNET_CODEPOINT_ERROR) << "expect decode to reject. case " << i << std::endl;
90 }
91 }
92
93 TEST(Unicode, TestUTF8RoundTripExhaustive)
94 {
95 // Encode every Unicode code-point as UTF-8 and verify that
96 // it decodes to the same value.
97 std::string buffer;
98 for (int x = 0; x < JSONNET_CODEPOINT_MAX; ++x) {
99 if (x == JSONNET_CODEPOINT_ERROR) {
100 continue;
101 }
102 buffer.clear();
103 encode_utf8(x, buffer);
104
105 size_t at = 0;
106 char32_t y = decode_utf8(buffer, at);
107 EXPECT_NE(y, JSONNET_CODEPOINT_ERROR) << "UTF-8 roundtrip failed for codepoint " << x << " decode rejects" << std::endl;
•108 EXPECT_EQ(x, y) << "UTF-8 roundtrip failed for codepoint " << static_cast<uint32_t>(x)
109 << " converts to " << static_cast<uint32_t>(y) << std::endl;
110 EXPECT_EQ(at, buffer.size() - 1) << "UTF-8 roundtrip failed for codepoint " << x << " decodes incorrect length" << std::endl;
111 }
112 }
113
114 } // namespace
115 } // namespace jsonnet::internal