Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_utf8.cpp
41144 views
1
/*
2
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
#include "precompiled.hpp"
25
#include "utilities/utf8.hpp"
26
#include "unittest.hpp"
27
28
static void stamp(char* p, size_t len) {
29
if (len > 0) {
30
::memset(p, 'A', len);
31
}
32
}
33
34
static bool test_stamp(const char* p, size_t len) {
35
for (const char* q = p; q < p + len; q++) {
36
if (*q != 'A') {
37
return false;
38
}
39
}
40
return true;
41
}
42
43
TEST_VM(utf8, jchar_length) {
44
char res[60];
45
jchar str[20];
46
47
for (int i = 0; i < 20; i++) {
48
str[i] = 0x0800; // char that is 2B in UTF-16 but 3B in UTF-8
49
}
50
str[19] = (jchar) '\0';
51
52
// The resulting string in UTF-8 is 3*19 bytes long, but should be truncated
53
stamp(res, sizeof(res));
54
UNICODE::as_utf8(str, 19, res, 10);
55
ASSERT_EQ(strlen(res), (size_t) 9) << "string should be truncated here";
56
ASSERT_TRUE(test_stamp(res + 10, sizeof(res) - 10));
57
58
stamp(res, sizeof(res));
59
UNICODE::as_utf8(str, 19, res, 18);
60
ASSERT_EQ(strlen(res), (size_t) 15) << "string should be truncated here";
61
ASSERT_TRUE(test_stamp(res + 18, sizeof(res) - 18));
62
63
stamp(res, sizeof(res));
64
UNICODE::as_utf8(str, 19, res, 20);
65
ASSERT_EQ(strlen(res), (size_t) 18) << "string should be truncated here";
66
ASSERT_TRUE(test_stamp(res + 20, sizeof(res) - 20));
67
68
// Test with an "unbounded" buffer
69
UNICODE::as_utf8(str, 19, res, INT_MAX);
70
ASSERT_EQ(strlen(res), (size_t) 3 * 19) << "string should end here";
71
72
// Test that we do not overflow the output buffer
73
for (int i = 1; i < 5; i ++) {
74
stamp(res, sizeof(res));
75
UNICODE::as_utf8(str, 19, res, i);
76
EXPECT_TRUE(test_stamp(res + i, sizeof(res) - i));
77
}
78
79
}
80
81
TEST_VM(utf8, jbyte_length) {
82
char res[60];
83
jbyte str[20];
84
85
for (int i = 0; i < 19; i++) {
86
str[i] = 0x42;
87
}
88
str[19] = '\0';
89
90
stamp(res, sizeof(res));
91
UNICODE::as_utf8(str, 19, res, 10);
92
ASSERT_EQ(strlen(res), (size_t) 9) << "string should be truncated here";
93
ASSERT_TRUE(test_stamp(res + 10, sizeof(res) - 10));
94
95
UNICODE::as_utf8(str, 19, res, INT_MAX);
96
ASSERT_EQ(strlen(res), (size_t) 19) << "string should end here";
97
98
// Test that we do not overflow the output buffer
99
for (int i = 1; i < 5; i ++) {
100
stamp(res, sizeof(res));
101
UNICODE::as_utf8(str, 19, res, i);
102
EXPECT_TRUE(test_stamp(res + i, sizeof(res) - i));
103
}
104
105
}
106
107