Path: blob/master/test/hotspot/gtest/utilities/test_globalDefinitions.cpp
41144 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "runtime/os.hpp"25#include "utilities/align.hpp"26#include "utilities/globalDefinitions.hpp"27#include <type_traits>28#include "unittest.hpp"2930static ::testing::AssertionResult testPageAddress(31const char* expected_addr_expr,32const char* addr_expr,33const char* page_addr_expr,34const char* page_size_expr,35const char* actual_addr_expr,36address expected_addr,37address addr,38address page_addr,39intptr_t page_size,40address actual_addr) {41if (expected_addr == actual_addr) {42return ::testing::AssertionSuccess();43}4445return ::testing::AssertionFailure()46<< actual_addr_expr << " returned unexpected address " << (void*) actual_addr << std::endl47<< "Expected " << expected_addr_expr << ": " << (void*) expected_addr << std::endl48<< "where" << std::endl49<< addr_expr << ": " << (void*) addr << std::endl50<< page_addr_expr << ": " << (void*) page_addr << std::endl51<< page_size_expr << ": " << page_size;52}5354TEST_VM(globalDefinitions, clamp_address_in_page) {55const intptr_t page_sizes[] = {os::vm_page_size(), 4096, 8192, 65536, 2 * 1024 * 1024};56const int num_page_sizes = sizeof(page_sizes) / sizeof(page_sizes[0]);5758for (int i = 0; i < num_page_sizes; i++) {59intptr_t page_size = page_sizes[i];60address page_address = (address) (10 * page_size);6162const intptr_t within_page_offsets[] = {0, 128, page_size - 1};63const int num_within_page_offsets = sizeof(within_page_offsets) / sizeof(within_page_offsets[0]);6465for (int k = 0; k < num_within_page_offsets; ++k) {66address addr = page_address + within_page_offsets[k];67address expected_address = addr;68EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,69clamp_address_in_page(addr, page_address, page_size))70<< "Expect that address within page is returned as is";71}7273const intptr_t above_page_offsets[] = {page_size, page_size + 1, 5 * page_size + 1};74const int num_above_page_offsets = sizeof(above_page_offsets) / sizeof(above_page_offsets[0]);7576for (int k = 0; k < num_above_page_offsets; ++k) {77address addr = page_address + above_page_offsets[k];78address expected_address = page_address + page_size;79EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,80clamp_address_in_page(addr, page_address, page_size))81<< "Expect that address above page returns start of next page";82}8384const intptr_t below_page_offsets[] = {1, 2 * page_size + 1, 5 * page_size + 1};85const int num_below_page_offsets = sizeof(below_page_offsets) / sizeof(below_page_offsets[0]);8687for (int k = 0; k < num_below_page_offsets; ++k) {88address addr = page_address - below_page_offsets[k];89address expected_address = page_address;90EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,91clamp_address_in_page(addr, page_address, page_size))92<< "Expect that address below page returns start of page";93}94}95}9697TEST(globalDefinitions, proper_unit) {98EXPECT_EQ(0u, byte_size_in_proper_unit(0u));99EXPECT_STREQ("B", proper_unit_for_byte_size(0u));100101EXPECT_EQ(1u, byte_size_in_proper_unit(1u));102EXPECT_STREQ("B", proper_unit_for_byte_size(1u));103104EXPECT_EQ(1023u, byte_size_in_proper_unit(K - 1));105EXPECT_STREQ("B", proper_unit_for_byte_size(K - 1));106107EXPECT_EQ(1024u, byte_size_in_proper_unit(K));108EXPECT_STREQ("B", proper_unit_for_byte_size(K));109110EXPECT_EQ(1025u, byte_size_in_proper_unit(K + 1));111EXPECT_STREQ("B", proper_unit_for_byte_size(K + 1));112113EXPECT_EQ(51200u, byte_size_in_proper_unit(50*K));114EXPECT_STREQ("B", proper_unit_for_byte_size(50*K));115116EXPECT_EQ(1023u, byte_size_in_proper_unit(M - 1));117EXPECT_STREQ("K", proper_unit_for_byte_size(M - 1));118119EXPECT_EQ(1024u, byte_size_in_proper_unit(M));120EXPECT_STREQ("K", proper_unit_for_byte_size(M));121122EXPECT_EQ(1024u, byte_size_in_proper_unit(M + 1));123EXPECT_STREQ("K", proper_unit_for_byte_size(M + 1));124125EXPECT_EQ(1025u, byte_size_in_proper_unit(M + K));126EXPECT_STREQ("K", proper_unit_for_byte_size(M + K));127128EXPECT_EQ(51200u, byte_size_in_proper_unit(50*M));129EXPECT_STREQ("K", proper_unit_for_byte_size(50*M));130131#ifdef _LP64132EXPECT_EQ(1023u, byte_size_in_proper_unit(G - 1));133EXPECT_STREQ("M", proper_unit_for_byte_size(G - 1));134135EXPECT_EQ(1024u, byte_size_in_proper_unit(G));136EXPECT_STREQ("M", proper_unit_for_byte_size(G));137138EXPECT_EQ(1024u, byte_size_in_proper_unit(G + 1));139EXPECT_STREQ("M", proper_unit_for_byte_size(G + 1));140141EXPECT_EQ(1024u, byte_size_in_proper_unit(G + K));142EXPECT_STREQ("M", proper_unit_for_byte_size(G + K));143144EXPECT_EQ(1025u, byte_size_in_proper_unit(G + M));145EXPECT_STREQ("M", proper_unit_for_byte_size(G + M));146147EXPECT_EQ(51200u, byte_size_in_proper_unit(50*G));148EXPECT_STREQ("M", proper_unit_for_byte_size(50*G));149#endif150}151152TEST(globalDefinitions, exact_unit_for_byte_size) {153EXPECT_STREQ("B", exact_unit_for_byte_size(0));154EXPECT_STREQ("B", exact_unit_for_byte_size(1));155EXPECT_STREQ("B", exact_unit_for_byte_size(K - 1));156EXPECT_STREQ("K", exact_unit_for_byte_size(K));157EXPECT_STREQ("B", exact_unit_for_byte_size(K + 1));158EXPECT_STREQ("B", exact_unit_for_byte_size(M - 1));159EXPECT_STREQ("M", exact_unit_for_byte_size(M));160EXPECT_STREQ("B", exact_unit_for_byte_size(M + 1));161EXPECT_STREQ("K", exact_unit_for_byte_size(M + K));162#ifdef _LP64163EXPECT_STREQ("B", exact_unit_for_byte_size(G - 1));164EXPECT_STREQ("G", exact_unit_for_byte_size(G));165EXPECT_STREQ("B", exact_unit_for_byte_size(G + 1));166EXPECT_STREQ("K", exact_unit_for_byte_size(G + K));167EXPECT_STREQ("M", exact_unit_for_byte_size(G + M));168EXPECT_STREQ("K", exact_unit_for_byte_size(G + M + K));169#endif170}171172TEST(globalDefinitions, byte_size_in_exact_unit) {173EXPECT_EQ(0u, byte_size_in_exact_unit(0));174EXPECT_EQ(1u, byte_size_in_exact_unit(1));175EXPECT_EQ(K - 1, byte_size_in_exact_unit(K - 1));176EXPECT_EQ(1u, byte_size_in_exact_unit(K));177EXPECT_EQ(K + 1, byte_size_in_exact_unit(K + 1));178EXPECT_EQ(M - 1, byte_size_in_exact_unit(M - 1));179EXPECT_EQ(1u, byte_size_in_exact_unit(M));180EXPECT_EQ(M + 1, byte_size_in_exact_unit(M + 1));181EXPECT_EQ(K + 1, byte_size_in_exact_unit(M + K));182#ifdef _LP64183EXPECT_EQ(G - 1, byte_size_in_exact_unit(G - 1));184EXPECT_EQ(1u, byte_size_in_exact_unit(G));185EXPECT_EQ(G + 1, byte_size_in_exact_unit(G + 1));186EXPECT_EQ(M + 1, byte_size_in_exact_unit(G + K));187EXPECT_EQ(K + 1, byte_size_in_exact_unit(G + M));188EXPECT_EQ(M + K + 1, byte_size_in_exact_unit(G + M + K));189#endif190}191192TEST(globalDefinitions, array_size) {193const size_t test_size = 10;194195{196int test_array[test_size] = {};197static_assert(test_size == ARRAY_SIZE(test_array), "must be");198}199200{201double test_array[test_size] = {};202static_assert(test_size == ARRAY_SIZE(test_array), "must be");203}204205struct ArrayElt { int x; };206207{208ArrayElt test_array[test_size] = {};209static_assert(test_size == ARRAY_SIZE(test_array), "must be");210}211212{213const ArrayElt test_array[] = { {0}, {1}, {2}, {3}, {4}, {5} };214static_assert(6 == ARRAY_SIZE(test_array), "must be");215}216217}218219220