Path: blob/master/test/hotspot/gtest/utilities/test_ostream.cpp
41144 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2019 SAP SE. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "memory/resourceArea.hpp"27#include "runtime/os.hpp"28#include "utilities/globalDefinitions.hpp"29#include "utilities/ostream.hpp"3031#include "unittest.hpp"3233static size_t print_lorem(outputStream* st) {34// Create a ResourceMark just to make sure the stream does not use ResourceArea35ResourceMark rm;36static const char* const lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "37"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lacinia at quis "38"risus sed vulputate odio ut enim blandit. Amet risus nullam eget felis eget. Viverra "39"orci sagittis eu volutpat odio facilisis mauris sit. Erat velit scelerisque in dictum non.";40static const size_t len_lorem = strlen(lorem);41// Randomly alternate between short and long writes at a ratio of 9:1.42const bool short_write = (os::random() % 10) > 0;43const size_t len = os::random() % (short_write ? 10 : len_lorem);44st->write(lorem, len);45return len;46}4748static void test_stringStream_is_zero_terminated(const stringStream* ss) {49ASSERT_EQ(ss->base()[ss->size()], '\0');50}5152static void do_test_stringStream(stringStream* ss, size_t expected_cap) {53test_stringStream_is_zero_terminated(ss);54size_t written = 0;55for (int i = 0; i < 1000; i ++) {56written += print_lorem(ss);57if (expected_cap > 0 && written >= expected_cap) {58ASSERT_EQ(ss->size(), expected_cap - 1);59} else {60ASSERT_EQ(ss->size(), written);61}62// Internal buffer should always be zero-terminated.63test_stringStream_is_zero_terminated(ss);64}65// Reset should zero terminate too66ss->reset();67ASSERT_EQ(ss->size(), (size_t)0);68test_stringStream_is_zero_terminated(ss);69}7071TEST_VM(ostream, stringStream_dynamic_start_with_internal_buffer) {72stringStream ss;73do_test_stringStream(&ss, 0);74ss.reset();75do_test_stringStream(&ss, 0);76}7778TEST_VM(ostream, stringStream_dynamic_start_with_malloced_buffer) {79stringStream ss(128);80do_test_stringStream(&ss, 0);81ss.reset();82do_test_stringStream(&ss, 0);83}8485TEST_VM(ostream, stringStream_static) {86char buffer[128 + 1];87char* canary_at = buffer + sizeof(buffer) - 1;88*canary_at = 'X';89size_t stream_buf_size = sizeof(buffer) - 1;90stringStream ss(buffer, stream_buf_size);91do_test_stringStream(&ss, stream_buf_size);92ASSERT_EQ(*canary_at, 'X'); // canary93}9495TEST_VM(ostream, bufferedStream_static) {96char buf[100 + 1];97char* canary_at = buf + sizeof(buf) - 1;98*canary_at = 'X';99size_t stream_buf_size = sizeof(buf) - 1;100bufferedStream bs(buf, stream_buf_size);101size_t written = 0;102for (int i = 0; i < 100; i ++) {103written += print_lorem(&bs);104if (written < stream_buf_size) {105ASSERT_EQ(bs.size(), written);106} else {107ASSERT_EQ(bs.size(), stream_buf_size - 1);108}109}110ASSERT_EQ(*canary_at, 'X'); // canary111}112113TEST_VM(ostream, bufferedStream_dynamic_small) {114bufferedStream bs(1); // small to excercise realloc.115size_t written = 0;116// The max cap imposed is 100M, we should be safely below this in this test.117for (int i = 0; i < 10; i ++) {118written += print_lorem(&bs);119ASSERT_EQ(bs.size(), written);120}121}122123/* Activate to manually test bufferedStream dynamic cap.124125TEST_VM(ostream, bufferedStream_dynamic_large) {126bufferedStream bs(1); // small to excercise realloc.127size_t written = 0;128// The max cap imposed is 100M. Writing this much should safely hit it.129// Note that this will assert in debug builds which is the expected behavior.130size_t expected_cap_at = 100 * M;131for (int i = 0; i < 10000000; i ++) {132written += print_lorem(&bs);133if (written < expected_cap_at) {134ASSERT_EQ(bs.size(), written);135} else {136ASSERT_EQ(bs.size(), expected_cap_at - 1);137}138}139}140141*/142143144145146147148