author | manc |
Mon, 14 Oct 2019 18:48:10 -0700 | |
changeset 58652 | 9b67dd88a931 |
parent 55236 | c87e52dbdca0 |
permissions | -rw-r--r-- |
54974 | 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, bool short_len) { |
|
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 |
size_t len; |
|
43 |
if (short_len) { |
|
44 |
len = os::random() % 10; |
|
45 |
} else { |
|
46 |
len = MAX2(1, (int)(os::random() % len_lorem)); |
|
47 |
} |
|
48 |
st->write(lorem, len); |
|
49 |
return len; |
|
50 |
} |
|
51 |
||
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
52 |
static void test_stringStream_is_zero_terminated(const stringStream* ss) { |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
53 |
ASSERT_EQ(ss->base()[ss->size()], '\0'); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
54 |
} |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
55 |
|
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
56 |
|
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
57 |
static void do_test_stringStream(stringStream* ss, bool short_len, size_t expected_cap) { |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
58 |
test_stringStream_is_zero_terminated(ss); |
54974 | 59 |
size_t written = 0; |
60 |
for (int i = 0; i < 1000; i ++) { |
|
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
61 |
written += print_lorem(ss, short_len); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
62 |
if (expected_cap > 0 && written >= expected_cap) { |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
63 |
ASSERT_EQ(ss->size(), expected_cap - 1); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
64 |
} else { |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
65 |
ASSERT_EQ(ss->size(), written); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
66 |
} |
54974 | 67 |
// Internal buffer should always be zero-terminated. |
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
68 |
test_stringStream_is_zero_terminated(ss); |
54974 | 69 |
} |
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
70 |
// Reset should zero terminate too |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
71 |
ss->reset(); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
72 |
ASSERT_EQ(ss->size(), (size_t)0); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
73 |
test_stringStream_is_zero_terminated(ss); |
54974 | 74 |
} |
75 |
||
76 |
TEST_VM(ostream, stringStream_dynamic_realloc_1) { |
|
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
77 |
stringStream ss(2); // dynamic buffer with very small starting size |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
78 |
do_test_stringStream(&ss, false, 0); |
54974 | 79 |
} |
80 |
||
81 |
TEST_VM(ostream, stringStream_dynamic_realloc_2) { |
|
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
82 |
stringStream ss(2); // dynamic buffer with very small starting size |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
83 |
do_test_stringStream(&ss, true, 0); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
84 |
} |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
85 |
|
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
86 |
TEST_VM(ostream, stringStream_static) { |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
87 |
char buffer[128 + 1]; |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
88 |
char* canary_at = buffer + sizeof(buffer) - 1; |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
89 |
*canary_at = 'X'; |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
90 |
size_t stream_buf_size = sizeof(buffer) - 1; |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
91 |
stringStream ss(buffer, stream_buf_size); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
92 |
do_test_stringStream(&ss, false, stream_buf_size); |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
93 |
ASSERT_EQ(*canary_at, 'X'); // canary |
54974 | 94 |
} |
55023 | 95 |
|
96 |
TEST_VM(ostream, bufferedStream_static) { |
|
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
97 |
char buf[100 + 1]; |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
98 |
char* canary_at = buf + sizeof(buf) - 1; |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
99 |
*canary_at = 'X'; |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
100 |
size_t stream_buf_size = sizeof(buf) - 1; |
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
101 |
bufferedStream bs(buf, stream_buf_size); |
55023 | 102 |
size_t written = 0; |
103 |
for (int i = 0; i < 100; i ++) { |
|
104 |
written += print_lorem(&bs, true); |
|
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
105 |
if (written < stream_buf_size) { |
55023 | 106 |
ASSERT_EQ(bs.size(), written); |
107 |
} else { |
|
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
108 |
ASSERT_EQ(bs.size(), stream_buf_size - 1); |
55023 | 109 |
} |
110 |
} |
|
55236
c87e52dbdca0
8225225: stringStream internal buffer should always be zero terminated
stuefe
parents:
55023
diff
changeset
|
111 |
ASSERT_EQ(*canary_at, 'X'); // canary |
55023 | 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, true); |
|
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, false); |
|
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 |