Path: blob/master/test/hotspot/gtest/gc/g1/test_g1BiasedArray.cpp
41149 views
/*1* Copyright (c) 2013, 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 "gc/g1/g1BiasedArray.hpp"25#include "unittest.hpp"2627class TestMappedArray : public G1BiasedMappedArray<int> {28public:29virtual int default_value() const {30return 0xBAADBABE;31}32int* my_address_mapped_to(HeapWord* address) {33return address_mapped_to(address);34}35};3637TEST_VM(G1BiasedArray, simple) {38const size_t REGION_SIZE_IN_WORDS = 512;39const size_t NUM_REGIONS = 20;40// Any value that is non-zero41HeapWord* fake_heap =42(HeapWord*) LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000);4344TestMappedArray array;45MemRegion range(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS);46array.initialize(range, REGION_SIZE_IN_WORDS * HeapWordSize);47const int DEFAULT_VALUE = array.default_value();4849// Check address calculation (bounds)50ASSERT_EQ(fake_heap, array.bottom_address_mapped())51<< "bottom mapped address should be "52<< p2i(array.bottom_address_mapped())53<< ", but is "54<< p2i(fake_heap);55ASSERT_EQ(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS,56array.end_address_mapped());5758int* bottom = array.my_address_mapped_to(fake_heap);59ASSERT_EQ((void*) bottom, (void*) array.base());60int* end = array.my_address_mapped_to(fake_heap +61REGION_SIZE_IN_WORDS * NUM_REGIONS);62ASSERT_EQ((void*) end, (void*) (array.base() + array.length()));63// The entire array should contain default value elements64for (int* current = bottom; current < end; current++) {65ASSERT_EQ(DEFAULT_VALUE, *current);66}6768// Test setting values in the table69HeapWord* region_start_address =70fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2);71HeapWord* region_end_address =72fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) +73REGION_SIZE_IN_WORDS - 1);7475// Set/get by address tests: invert some value; first retrieve one76int actual_value = array.get_by_index(NUM_REGIONS / 2);77array.set_by_index(NUM_REGIONS / 2, ~actual_value);78// Get the same value by address, should correspond to the start of the "region"79int value = array.get_by_address(region_start_address);80ASSERT_EQ(value, ~actual_value);81// Get the same value by address, at one HeapWord before the start82value = array.get_by_address(region_start_address - 1);83ASSERT_EQ(DEFAULT_VALUE, value);84// Get the same value by address, at the end of the "region"85value = array.get_by_address(region_end_address);86ASSERT_EQ(value, ~actual_value);87// Make sure the next value maps to another index88value = array.get_by_address(region_end_address + 1);89ASSERT_EQ(DEFAULT_VALUE, value);9091// Reset the value in the array92array.set_by_address(region_start_address +93(region_end_address - region_start_address) / 2,94actual_value);9596// The entire array should have the default value again97for (int* current = bottom; current < end; current++) {98ASSERT_EQ(DEFAULT_VALUE, *current);99}100101// Set/get by index tests: invert some value102size_t index = NUM_REGIONS / 2;103actual_value = array.get_by_index(index);104array.set_by_index(index, ~actual_value);105106value = array.get_by_index(index);107ASSERT_EQ(~actual_value, value);108109value = array.get_by_index(index - 1);110ASSERT_EQ(DEFAULT_VALUE, value);111112value = array.get_by_index(index + 1);113ASSERT_EQ(DEFAULT_VALUE, value);114115array.set_by_index(0, 0);116value = array.get_by_index(0);117ASSERT_EQ(0, value);118119array.set_by_index(array.length() - 1, 0);120value = array.get_by_index(array.length() - 1);121ASSERT_EQ(0, value);122123array.set_by_index(index, 0);124125// The array should have three zeros, and default values otherwise126size_t num_zeros = 0;127for (int* current = bottom; current < end; current++) {128ASSERT_TRUE(*current == DEFAULT_VALUE || *current == 0);129if (*current == 0) {130num_zeros++;131}132}133ASSERT_EQ((size_t) 3, num_zeros);134}135136137138