test/hotspot/gtest/runtime/test_os.cpp
changeset 49177 eebf559c9e0d
parent 47216 71c04702a3d5
child 51277 dfe1cff5c2f6
equal deleted inserted replaced
49176:f413e471a6ab 49177:eebf559c9e0d
     1 /*
     1 /*
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 #include "precompiled.hpp"
    24 #include "precompiled.hpp"
       
    25 #include "memory/resourceArea.hpp"
    25 #include "runtime/os.hpp"
    26 #include "runtime/os.hpp"
       
    27 #include "utilities/ostream.hpp"
    26 #include "unittest.hpp"
    28 #include "unittest.hpp"
    27 
    29 
    28 static size_t small_page_size() {
    30 static size_t small_page_size() {
    29   return os::vm_page_size();
    31   return os::vm_page_size();
    30 }
    32 }
   148 TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") {
   150 TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") {
   149   size_t region_size = 16 * os::vm_page_size();
   151   size_t region_size = 16 * os::vm_page_size();
   150   os::page_size_for_region_aligned(region_size, 0); // should assert
   152   os::page_size_for_region_aligned(region_size, 0); // should assert
   151 }
   153 }
   152 #endif
   154 #endif
       
   155 
       
   156 //////////////////////////////////////////////////////////////////////////////
       
   157 // Test os::vsnprintf and friends.
       
   158 
       
   159 static void check_snprintf_result(int expected, size_t limit, int actual, bool expect_count) {
       
   160   if (expect_count || ((size_t)expected < limit)) {
       
   161     ASSERT_EQ(expected, actual);
       
   162   } else {
       
   163     ASSERT_GT(0, actual);
       
   164   }
       
   165 }
       
   166 
       
   167 // PrintFn is expected to be int (*)(char*, size_t, const char*, ...).
       
   168 // But jio_snprintf is a C-linkage function with that signature, which
       
   169 // has a different type on some platforms (like Solaris).
       
   170 template<typename PrintFn>
       
   171 static void test_snprintf(PrintFn pf, bool expect_count) {
       
   172   const char expected[] = "abcdefghijklmnopqrstuvwxyz";
       
   173   const int expected_len = sizeof(expected) - 1;
       
   174   const size_t padding_size = 10;
       
   175   char buffer[2 * (sizeof(expected) + padding_size)];
       
   176   char check_buffer[sizeof(buffer)];
       
   177   const char check_char = '1';  // Something not in expected.
       
   178   memset(check_buffer, check_char, sizeof(check_buffer));
       
   179   const size_t sizes_to_test[] = {
       
   180     sizeof(buffer) - padding_size,       // Fits, with plenty of space to spare.
       
   181     sizeof(buffer)/2,                    // Fits, with space to spare.
       
   182     sizeof(buffer)/4,                    // Doesn't fit.
       
   183     sizeof(expected) + padding_size + 1, // Fits, with a little room to spare
       
   184     sizeof(expected) + padding_size,     // Fits exactly.
       
   185     sizeof(expected) + padding_size - 1, // Doesn't quite fit.
       
   186     2,                                   // One char + terminating NUL.
       
   187     1,                                   // Only space for terminating NUL.
       
   188     0 };                                 // No space at all.
       
   189   for (unsigned i = 0; i < ARRAY_SIZE(sizes_to_test); ++i) {
       
   190     memset(buffer, check_char, sizeof(buffer)); // To catch stray writes.
       
   191     size_t test_size = sizes_to_test[i];
       
   192     ResourceMark rm;
       
   193     stringStream s;
       
   194     s.print("test_size: " SIZE_FORMAT, test_size);
       
   195     SCOPED_TRACE(s.as_string());
       
   196     size_t prefix_size = padding_size;
       
   197     guarantee(test_size <= (sizeof(buffer) - prefix_size), "invariant");
       
   198     size_t write_size = MIN2(sizeof(expected), test_size);
       
   199     size_t suffix_size = sizeof(buffer) - prefix_size - write_size;
       
   200     char* write_start = buffer + prefix_size;
       
   201     char* write_end = write_start + write_size;
       
   202 
       
   203     int result = pf(write_start, test_size, "%s", expected);
       
   204 
       
   205     check_snprintf_result(expected_len, test_size, result, expect_count);
       
   206 
       
   207     // Verify expected output.
       
   208     if (test_size > 0) {
       
   209       ASSERT_EQ(0, strncmp(write_start, expected, write_size - 1));
       
   210       // Verify terminating NUL of output.
       
   211       ASSERT_EQ('\0', write_start[write_size - 1]);
       
   212     } else {
       
   213       guarantee(test_size == 0, "invariant");
       
   214       guarantee(write_size == 0, "invariant");
       
   215       guarantee(prefix_size + suffix_size == sizeof(buffer), "invariant");
       
   216       guarantee(write_start == write_end, "invariant");
       
   217     }
       
   218 
       
   219     // Verify no scribbling on prefix or suffix.
       
   220     ASSERT_EQ(0, strncmp(buffer, check_buffer, prefix_size));
       
   221     ASSERT_EQ(0, strncmp(write_end, check_buffer, suffix_size));
       
   222   }
       
   223 
       
   224   // Special case of 0-length buffer with empty (except for terminator) output.
       
   225   check_snprintf_result(0, 0, pf(NULL, 0, "%s", ""), expect_count);
       
   226   check_snprintf_result(0, 0, pf(NULL, 0, ""), expect_count);
       
   227 }
       
   228 
       
   229 // This is probably equivalent to os::snprintf, but we're being
       
   230 // explicit about what we're testing here.
       
   231 static int vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {
       
   232   va_list args;
       
   233   va_start(args, fmt);
       
   234   int result = os::vsnprintf(buf, len, fmt, args);
       
   235   va_end(args);
       
   236   return result;
       
   237 }
       
   238 
       
   239 TEST(os, vsnprintf) {
       
   240   test_snprintf(vsnprintf_wrapper, true);
       
   241 }
       
   242 
       
   243 TEST(os, snprintf) {
       
   244   test_snprintf(os::snprintf, true);
       
   245 }
       
   246 
       
   247 // These are declared in jvm.h; test here, with related functions.
       
   248 extern "C" {
       
   249 int jio_vsnprintf(char*, size_t, const char*, va_list);
       
   250 int jio_snprintf(char*, size_t, const char*, ...);
       
   251 }
       
   252 
       
   253 // This is probably equivalent to jio_snprintf, but we're being
       
   254 // explicit about what we're testing here.
       
   255 static int jio_vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {
       
   256   va_list args;
       
   257   va_start(args, fmt);
       
   258   int result = jio_vsnprintf(buf, len, fmt, args);
       
   259   va_end(args);
       
   260   return result;
       
   261 }
       
   262 
       
   263 TEST(os, jio_vsnprintf) {
       
   264   test_snprintf(jio_vsnprintf_wrapper, false);
       
   265 }
       
   266 
       
   267 TEST(os, jio_snprintf) {
       
   268   test_snprintf(jio_snprintf, false);
       
   269 }