Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_ostream.cpp
41144 views
1
/*
2
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2019 SAP SE. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*
24
*/
25
26
#include "precompiled.hpp"
27
#include "memory/resourceArea.hpp"
28
#include "runtime/os.hpp"
29
#include "utilities/globalDefinitions.hpp"
30
#include "utilities/ostream.hpp"
31
32
#include "unittest.hpp"
33
34
static size_t print_lorem(outputStream* st) {
35
// Create a ResourceMark just to make sure the stream does not use ResourceArea
36
ResourceMark rm;
37
static const char* const lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
38
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lacinia at quis "
39
"risus sed vulputate odio ut enim blandit. Amet risus nullam eget felis eget. Viverra "
40
"orci sagittis eu volutpat odio facilisis mauris sit. Erat velit scelerisque in dictum non.";
41
static const size_t len_lorem = strlen(lorem);
42
// Randomly alternate between short and long writes at a ratio of 9:1.
43
const bool short_write = (os::random() % 10) > 0;
44
const size_t len = os::random() % (short_write ? 10 : len_lorem);
45
st->write(lorem, len);
46
return len;
47
}
48
49
static void test_stringStream_is_zero_terminated(const stringStream* ss) {
50
ASSERT_EQ(ss->base()[ss->size()], '\0');
51
}
52
53
static void do_test_stringStream(stringStream* ss, size_t expected_cap) {
54
test_stringStream_is_zero_terminated(ss);
55
size_t written = 0;
56
for (int i = 0; i < 1000; i ++) {
57
written += print_lorem(ss);
58
if (expected_cap > 0 && written >= expected_cap) {
59
ASSERT_EQ(ss->size(), expected_cap - 1);
60
} else {
61
ASSERT_EQ(ss->size(), written);
62
}
63
// Internal buffer should always be zero-terminated.
64
test_stringStream_is_zero_terminated(ss);
65
}
66
// Reset should zero terminate too
67
ss->reset();
68
ASSERT_EQ(ss->size(), (size_t)0);
69
test_stringStream_is_zero_terminated(ss);
70
}
71
72
TEST_VM(ostream, stringStream_dynamic_start_with_internal_buffer) {
73
stringStream ss;
74
do_test_stringStream(&ss, 0);
75
ss.reset();
76
do_test_stringStream(&ss, 0);
77
}
78
79
TEST_VM(ostream, stringStream_dynamic_start_with_malloced_buffer) {
80
stringStream ss(128);
81
do_test_stringStream(&ss, 0);
82
ss.reset();
83
do_test_stringStream(&ss, 0);
84
}
85
86
TEST_VM(ostream, stringStream_static) {
87
char buffer[128 + 1];
88
char* canary_at = buffer + sizeof(buffer) - 1;
89
*canary_at = 'X';
90
size_t stream_buf_size = sizeof(buffer) - 1;
91
stringStream ss(buffer, stream_buf_size);
92
do_test_stringStream(&ss, stream_buf_size);
93
ASSERT_EQ(*canary_at, 'X'); // canary
94
}
95
96
TEST_VM(ostream, bufferedStream_static) {
97
char buf[100 + 1];
98
char* canary_at = buf + sizeof(buf) - 1;
99
*canary_at = 'X';
100
size_t stream_buf_size = sizeof(buf) - 1;
101
bufferedStream bs(buf, stream_buf_size);
102
size_t written = 0;
103
for (int i = 0; i < 100; i ++) {
104
written += print_lorem(&bs);
105
if (written < stream_buf_size) {
106
ASSERT_EQ(bs.size(), written);
107
} else {
108
ASSERT_EQ(bs.size(), stream_buf_size - 1);
109
}
110
}
111
ASSERT_EQ(*canary_at, 'X'); // canary
112
}
113
114
TEST_VM(ostream, bufferedStream_dynamic_small) {
115
bufferedStream bs(1); // small to excercise realloc.
116
size_t written = 0;
117
// The max cap imposed is 100M, we should be safely below this in this test.
118
for (int i = 0; i < 10; i ++) {
119
written += print_lorem(&bs);
120
ASSERT_EQ(bs.size(), written);
121
}
122
}
123
124
/* Activate to manually test bufferedStream dynamic cap.
125
126
TEST_VM(ostream, bufferedStream_dynamic_large) {
127
bufferedStream bs(1); // small to excercise realloc.
128
size_t written = 0;
129
// The max cap imposed is 100M. Writing this much should safely hit it.
130
// Note that this will assert in debug builds which is the expected behavior.
131
size_t expected_cap_at = 100 * M;
132
for (int i = 0; i < 10000000; i ++) {
133
written += print_lorem(&bs);
134
if (written < expected_cap_at) {
135
ASSERT_EQ(bs.size(), written);
136
} else {
137
ASSERT_EQ(bs.size(), expected_cap_at - 1);
138
}
139
}
140
}
141
142
*/
143
144
145
146
147
148