Path: blob/master/test/hotspot/gtest/utilities/test_bitMap_search.cpp
41145 views
/*1* Copyright (c) 2017, 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*22*/2324#include "precompiled.hpp"25#include "utilities/bitMap.inline.hpp"26#include "utilities/debug.hpp"27#include "utilities/globalDefinitions.hpp"28#include "unittest.hpp"2930typedef BitMap::idx_t idx_t;31typedef BitMap::bm_word_t bm_word_t;3233static const idx_t BITMAP_SIZE = 1024;3435static const size_t search_chunk_size = 64;3637// Entries must be monotonically increasing.38// Maximum entry must be < search_chunk_size.39// Cluster values around possible word-size boundaries.40static const size_t search_offsets[] =41{ 0, 1, 2, 29, 30, 31, 32, 33, 34, 60, 62, 63 };4243static const size_t search_noffsets = ARRAY_SIZE(search_offsets);4445static const size_t search_nchunks = BITMAP_SIZE / search_chunk_size;46STATIC_ASSERT(search_nchunks * search_chunk_size == BITMAP_SIZE);4748namespace {49class TestIteratorFn : public BitMapClosure {50public:51TestIteratorFn(size_t start, size_t end, size_t left, size_t right);52virtual bool do_bit(size_t offset);5354private:55size_t _entries[2];56size_t _index;57size_t _count;58size_t _start;59size_t _end;60size_t _left;61size_t _right;6263void do_bit_aux(size_t offset);6465};66} // anonymous namespace6768TestIteratorFn::TestIteratorFn(size_t start, size_t end,69size_t left, size_t right) :70_index(0), _count(0), _start(start), _end(end), _left(left), _right(right)71{72if ((_start <= _left) && (_left < _end)) {73_entries[_count++] = _left;74}75if ((_start <= _right) && (_right < _end)) {76_entries[_count++] = _right;77}78}7980void TestIteratorFn::do_bit_aux(size_t offset) {81EXPECT_LT(_index, _count);82if (_index < _count) {83EXPECT_EQ(_entries[_index], offset);84_index += 1;85}86}8788bool TestIteratorFn::do_bit(size_t offset) {89do_bit_aux(offset);90return true;91}9293static idx_t compute_expected(idx_t search_start,94idx_t search_end,95idx_t left_bit,96idx_t right_bit) {97idx_t expected = search_end;98if (search_start <= left_bit) {99if (left_bit < search_end) {100expected = left_bit;101}102} else if (search_start <= right_bit) {103if (right_bit < search_end) {104expected = right_bit;105}106}107return expected;108}109110static void test_search_ranges(BitMap& test_ones,111BitMap& test_zeros,112idx_t left,113idx_t right) {114// Test get_next_one_offset with full range of map.115EXPECT_EQ(left, test_ones.get_next_one_offset(0));116EXPECT_EQ(right, test_ones.get_next_one_offset(left + 1));117EXPECT_EQ(BITMAP_SIZE, test_ones.get_next_one_offset(right + 1));118119// Test get_next_one_offset_aligned_right with full range of map.120EXPECT_EQ(left, test_ones.get_next_one_offset_aligned_right(0, BITMAP_SIZE));121EXPECT_EQ(right, test_ones.get_next_one_offset_aligned_right(left + 1, BITMAP_SIZE));122EXPECT_EQ(BITMAP_SIZE, test_ones.get_next_one_offset_aligned_right(right + 1, BITMAP_SIZE));123124// Test get_next_zero_offset with full range of map.125EXPECT_EQ(left, test_zeros.get_next_zero_offset(0));126EXPECT_EQ(right, test_zeros.get_next_zero_offset(left + 1));127EXPECT_EQ(BITMAP_SIZE, test_zeros.get_next_zero_offset(right + 1));128129// Check that iterate invokes the closure function on left and right values.130TestIteratorFn test_iteration(0, BITMAP_SIZE, left, right);131test_ones.iterate(&test_iteration, 0, BITMAP_SIZE);132133// Test searches with various start and end ranges.134for (size_t c_start = 0; c_start < search_nchunks; ++c_start) {135for (size_t o_start = 0; o_start < search_noffsets; ++o_start) {136idx_t start = c_start * search_chunk_size + search_offsets[o_start];137// Terminate start iteration if start is more than two full138// chunks beyond left. There isn't anything new to learn by139// continuing the iteration, and this noticably reduces the140// time to run the test.141if (left + 2 * search_chunk_size < start) {142c_start = search_nchunks; // Set to limit to terminate iteration.143break;144}145146for (size_t c_end = c_start; c_end < search_nchunks; ++c_end) {147for (size_t o_end = (c_start == c_end) ? o_start : 0;148o_end < search_noffsets;149++o_end) {150idx_t end = c_end * search_chunk_size + search_offsets[o_end];151// Similarly to start and left, terminate end iteration if152// end is more than two full chunks beyond right.153if (right + 2 * search_chunk_size < end) {154c_end = search_nchunks; // Set to limit to terminate iteration.155break;156}157// Skip this chunk if right is much larger than max(left, start)158// and this chunk is one of many similar chunks in between,159// again to reduce testing time.160if (MAX2(start, left) + 2 * search_chunk_size < end) {161if (end + 2 * search_chunk_size < right) {162break;163}164}165166bool aligned_right = search_offsets[o_end] == 0;167ASSERT_LE(start, end); // test bug if fail168ASSERT_LT(end, BITMAP_SIZE); // test bug if fail169170idx_t expected = compute_expected(start, end, left, right);171172EXPECT_EQ(expected, test_ones.get_next_one_offset(start, end));173EXPECT_EQ(expected, test_zeros.get_next_zero_offset(start, end));174if (aligned_right) {175EXPECT_EQ(176expected,177test_ones.get_next_one_offset_aligned_right(start, end));178}179180idx_t start2 = MIN2(expected + 1, end);181idx_t expected2 = compute_expected(start2, end, left, right);182183EXPECT_EQ(expected2, test_ones.get_next_one_offset(start2, end));184EXPECT_EQ(expected2, test_zeros.get_next_zero_offset(start2, end));185if (aligned_right) {186EXPECT_EQ(187expected2,188test_ones.get_next_one_offset_aligned_right(start2, end));189}190}191}192}193}194}195196TEST(BitMap, search) {197CHeapBitMap test_ones(BITMAP_SIZE);198CHeapBitMap test_zeros(BITMAP_SIZE);199200// test_ones is used to test searching for 1s in a region of 0s.201// test_zeros is used to test searching for 0s in a region of 1s.202test_ones.clear_range(0, test_ones.size());203test_zeros.set_range(0, test_zeros.size());204205// Searching "empty" sequence should return size.206EXPECT_EQ(BITMAP_SIZE, test_ones.get_next_one_offset(0));207EXPECT_EQ(BITMAP_SIZE, test_zeros.get_next_zero_offset(0));208209// With left being in the first or second chunk...210for (size_t c_left = 0; c_left < 2; ++c_left) {211// Right bit is in the same chunk as left, or next chunk, or far away...212for (size_t c_right = c_left;213c_right < search_nchunks;214(c_right == c_left + 1) ? c_right = search_nchunks - 1 : ++c_right) {215// For each offset within the left chunk...216for (size_t o_left = 0; o_left < search_noffsets; ++o_left) {217// left is start of left chunk + offset.218idx_t left = c_left * search_chunk_size + search_offsets[o_left];219220// Install the left bit.221test_ones.set_bit(left);222test_zeros.clear_bit(left);223EXPECT_TRUE(test_ones.at(left));224EXPECT_FALSE(test_zeros.at(left));225226// For each offset within the right chunk and > left...227for (size_t o_right = (c_left == c_right) ? o_left + 1 : 0;228o_right < search_noffsets;229++o_right) {230// right is start of right chunk + offset.231idx_t right = c_right * search_chunk_size + search_offsets[o_right];232233// Install the right bit.234test_ones.set_bit(right);235test_zeros.clear_bit(right);236EXPECT_TRUE(test_ones.at(right));237EXPECT_FALSE(test_zeros.at(right));238239// Apply the test.240test_search_ranges(test_ones, test_zeros, left, right);241242// Remove the right bit.243test_ones.clear_bit(right);244test_zeros.set_bit(right);245EXPECT_FALSE(test_ones.at(right));246EXPECT_TRUE(test_zeros.at(right));247}248249// Remove the left bit.250test_ones.clear_bit(left);251test_zeros.set_bit(left);252EXPECT_FALSE(test_ones.at(left));253EXPECT_TRUE(test_zeros.at(left));254}255}256}257}258259260