test/hotspot/gtest/utilities/test_ostream.cpp
author manc
Mon, 14 Oct 2019 18:48:10 -0700
changeset 58652 9b67dd88a931
parent 55236 c87e52dbdca0
permissions -rw-r--r--
8232232: G1RemSetSummary::_rs_threads_vtimes is not initialized to zero Summary: Fix error in "Concurrent refinement threads times" in GC log and cleanup. Reviewed-by: tschatzl, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54974
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     1
/*
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     3
 * Copyright (c) 2019 SAP SE. All rights reserved.
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     5
 *
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     8
 * published by the Free Software Foundation.
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
     9
 *
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    14
 * accompanied this code).
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    15
 *
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    19
 *
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    22
 * questions.
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    23
 *
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    24
 */
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    25
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    26
#include "precompiled.hpp"
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    27
#include "memory/resourceArea.hpp"
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    28
#include "runtime/os.hpp"
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    29
#include "utilities/globalDefinitions.hpp"
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    30
#include "utilities/ostream.hpp"
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    31
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    32
#include "unittest.hpp"
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    33
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    34
static size_t print_lorem(outputStream* st, bool short_len) {
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    35
  // Create a ResourceMark just to make sure the stream does not use ResourceArea
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    36
  ResourceMark rm;
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    37
  static const char* const lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    38
      "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lacinia at quis "
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    39
      "risus sed vulputate odio ut enim blandit. Amet risus nullam eget felis eget. Viverra "
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    40
      "orci sagittis eu volutpat odio facilisis mauris sit. Erat velit scelerisque in dictum non.";
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    41
  static const size_t len_lorem = strlen(lorem);
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    42
  size_t len;
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    43
  if (short_len) {
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    44
    len = os::random() % 10;
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    45
  } else {
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    46
    len = MAX2(1, (int)(os::random() % len_lorem));
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    47
  }
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    48
  st->write(lorem, len);
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    49
  return len;
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    50
}
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    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
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    59
  size_t written = 0;
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    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
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    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
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    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
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    74
}
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    75
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    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
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    79
}
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    80
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    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
22961d673487 8224193: stringStream should not use Resouce Area
stuefe
parents:
diff changeset
    94
}
55023
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
    95
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
    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
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   102
  size_t written = 0;
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   103
  for (int i = 0; i < 100; i ++) {
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   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
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   106
      ASSERT_EQ(bs.size(), written);
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   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
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   109
    }
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   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
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   112
}
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   113
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   114
TEST_VM(ostream, bufferedStream_dynamic_small) {
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   115
  bufferedStream bs(1); // small to excercise realloc.
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   116
  size_t written = 0;
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   117
  // The max cap imposed is 100M, we should be safely below this in this test.
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   118
  for (int i = 0; i < 10; i ++) {
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   119
    written += print_lorem(&bs, true);
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   120
    ASSERT_EQ(bs.size(), written);
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   121
  }
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   122
}
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   123
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   124
/* Activate to manually test bufferedStream dynamic cap.
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   125
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   126
TEST_VM(ostream, bufferedStream_dynamic_large) {
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   127
  bufferedStream bs(1); // small to excercise realloc.
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   128
  size_t written = 0;
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   129
  // The max cap imposed is 100M. Writing this much should safely hit it.
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   130
  // Note that this will assert in debug builds which is the expected behavior.
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   131
  size_t expected_cap_at = 100 * M;
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   132
  for (int i = 0; i < 10000000; i ++) {
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   133
    written += print_lorem(&bs, false);
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   134
    if (written < expected_cap_at) {
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   135
      ASSERT_EQ(bs.size(), written);
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   136
    } else {
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   137
      ASSERT_EQ(bs.size(), expected_cap_at - 1);
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   138
    }
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   139
  }
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   140
}
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   141
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   142
*/
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   143
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   144
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   145
d56d8e40b6cd 8220394: bufferedStream does not honor size limit
stuefe
parents: 54974
diff changeset
   146