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 |
|
|
52 |
static void do_test_stringStream_dynamic_realloc(bool short_len) {
|
|
53 |
stringStream ss(2); // small buffer to force lots of reallocations.
|
|
54 |
size_t written = 0;
|
|
55 |
for (int i = 0; i < 1000; i ++) {
|
|
56 |
written += print_lorem(&ss, short_len);
|
|
57 |
ASSERT_EQ(ss.size(), written);
|
|
58 |
// Internal buffer should always be zero-terminated.
|
|
59 |
ASSERT_EQ(ss.base()[ss.size()], '\0');
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
TEST_VM(ostream, stringStream_dynamic_realloc_1) {
|
|
64 |
do_test_stringStream_dynamic_realloc(false);
|
|
65 |
}
|
|
66 |
|
|
67 |
TEST_VM(ostream, stringStream_dynamic_realloc_2) {
|
|
68 |
do_test_stringStream_dynamic_realloc(true);
|
|
69 |
}
|
55023
|
70 |
|
|
71 |
TEST_VM(ostream, bufferedStream_static) {
|
|
72 |
char buf[100];
|
|
73 |
bufferedStream bs(buf, sizeof(buf));
|
|
74 |
size_t written = 0;
|
|
75 |
for (int i = 0; i < 100; i ++) {
|
|
76 |
written += print_lorem(&bs, true);
|
|
77 |
if (written < sizeof(buf)) {
|
|
78 |
ASSERT_EQ(bs.size(), written);
|
|
79 |
} else {
|
|
80 |
ASSERT_EQ(bs.size(), sizeof(buf) - 1);
|
|
81 |
}
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
TEST_VM(ostream, bufferedStream_dynamic_small) {
|
|
86 |
bufferedStream bs(1); // small to excercise realloc.
|
|
87 |
size_t written = 0;
|
|
88 |
// The max cap imposed is 100M, we should be safely below this in this test.
|
|
89 |
for (int i = 0; i < 10; i ++) {
|
|
90 |
written += print_lorem(&bs, true);
|
|
91 |
ASSERT_EQ(bs.size(), written);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
/* Activate to manually test bufferedStream dynamic cap.
|
|
96 |
|
|
97 |
TEST_VM(ostream, bufferedStream_dynamic_large) {
|
|
98 |
bufferedStream bs(1); // small to excercise realloc.
|
|
99 |
size_t written = 0;
|
|
100 |
// The max cap imposed is 100M. Writing this much should safely hit it.
|
|
101 |
// Note that this will assert in debug builds which is the expected behavior.
|
|
102 |
size_t expected_cap_at = 100 * M;
|
|
103 |
for (int i = 0; i < 10000000; i ++) {
|
|
104 |
written += print_lorem(&bs, false);
|
|
105 |
if (written < expected_cap_at) {
|
|
106 |
ASSERT_EQ(bs.size(), written);
|
|
107 |
} else {
|
|
108 |
ASSERT_EQ(bs.size(), expected_cap_at - 1);
|
|
109 |
}
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
*/
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|