Path: blob/master/test/hotspot/gtest/utilities/test_bitMap.cpp
41145 views
/*1* Copyright (c) 2016, 2020, 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 "memory/resourceArea.hpp"25#include "utilities/bitMap.inline.hpp"26#include "unittest.hpp"2728class BitMapTest {2930template <class ResizableBitMapClass>31static void fillBitMap(ResizableBitMapClass& map) {32map.set_bit(1);33map.set_bit(3);34map.set_bit(17);35map.set_bit(512);36}3738template <class ResizableBitMapClass>39static void testResize(BitMap::idx_t start_size) {40ResourceMark rm;4142ResizableBitMapClass map(start_size);43map.resize(BITMAP_SIZE);44fillBitMap(map);4546ResizableBitMapClass map2(BITMAP_SIZE);47fillBitMap(map2);48EXPECT_TRUE(map.is_same(map2)) << "With start_size " << start_size;49}5051public:52const static BitMap::idx_t BITMAP_SIZE = 1024;535455template <class ResizableBitMapClass>56static void testResizeGrow() {57testResize<ResizableBitMapClass>(0);58testResize<ResizableBitMapClass>(BITMAP_SIZE >> 3);59}6061template <class ResizableBitMapClass>62static void testResizeSame() {63testResize<ResizableBitMapClass>(BITMAP_SIZE);64}6566template <class ResizableBitMapClass>67static void testResizeShrink() {68testResize<ResizableBitMapClass>(BITMAP_SIZE * 2);69}7071template <class InitializableBitMapClass>72static void testInitialize() {73ResourceMark rm;7475InitializableBitMapClass map;76map.initialize(BITMAP_SIZE);77fillBitMap(map);7879InitializableBitMapClass map2(BITMAP_SIZE);80fillBitMap(map2);81EXPECT_TRUE(map.is_same(map2));82}838485static void testReinitialize(BitMap::idx_t init_size) {86ResourceMark rm;8788ResourceBitMap map(init_size);89map.reinitialize(BITMAP_SIZE);90fillBitMap(map);9192ResourceBitMap map2(BITMAP_SIZE);93fillBitMap(map2);94EXPECT_TRUE(map.is_same(map2)) << "With init_size " << init_size;95}9697};9899TEST_VM(BitMap, resize_grow) {100BitMapTest::testResizeGrow<ResourceBitMap>();101EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";102BitMapTest::testResizeGrow<CHeapBitMap>();103EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";104}105106TEST_VM(BitMap, resize_shrink) {107BitMapTest::testResizeShrink<ResourceBitMap>();108EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";109BitMapTest::testResizeShrink<CHeapBitMap>();110EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";111}112113TEST_VM(BitMap, resize_same) {114BitMapTest::testResizeSame<ResourceBitMap>();115EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";116BitMapTest::testResizeSame<CHeapBitMap>();117EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";118}119120// Verify that when growing with clear, all added bits get cleared,121// even those corresponding to a partial word after the old size.122TEST_VM(BitMap, resize_grow_clear) {123ResourceMark rm;124const size_t word_size = sizeof(BitMap::bm_word_t) * BitsPerByte;125const size_t size = 4 * word_size;126ResourceBitMap bm(size, true /* clear */);127bm.set_bit(size - 1);128EXPECT_EQ(bm.count_one_bits(), size_t(1));129// Discard the only set bit. But it might still be "set" in the130// partial word beyond the new size.131bm.resize(size - word_size/2);132EXPECT_EQ(bm.count_one_bits(), size_t(0));133// Grow to include the previously set bit. Verify that it ended up cleared.134bm.resize(2 * size);135EXPECT_EQ(bm.count_one_bits(), size_t(0));136}137138TEST_VM(BitMap, initialize) {139BitMapTest::testInitialize<ResourceBitMap>();140EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";141BitMapTest::testInitialize<CHeapBitMap>();142EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";143}144145TEST_VM(BitMap, reinitialize) {146BitMapTest::testReinitialize(0);147BitMapTest::testReinitialize(BitMapTest::BITMAP_SIZE >> 3);148BitMapTest::testReinitialize(BitMapTest::BITMAP_SIZE);149}150151152