Path: blob/master/test/hotspot/gtest/utilities/test_resourceHash.cpp
64438 views
/*1* Copyright (c) 2015, 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 "memory/allocation.hpp"25#include "memory/resourceArea.hpp"26#include "unittest.hpp"27#include "utilities/debug.hpp"28#include "utilities/globalDefinitions.hpp"29#include "utilities/resourceHash.hpp"3031class CommonResourceHashtableTest : public ::testing::Test {32protected:33typedef void* K;34typedef uintx V;35const static MEMFLAGS MEM_TYPE = mtInternal;3637static unsigned identity_hash(const K& k) {38return (unsigned) (uintptr_t) k;39}4041static unsigned bad_hash(const K& k) {42return 1;43}4445static void* as_K(uintptr_t val) {46return (void*) val;47}4849class EqualityTestIter {50public:5152bool do_entry(K const& k, V const& v) {53if ((uintptr_t) k != (uintptr_t) v) {54EXPECT_EQ((uintptr_t) k, (uintptr_t) v);55return false;56} else {57return true; // continue iteration58}59}60};6162class DeleterTestIter {63int _val;64public:65DeleterTestIter(int i) : _val(i) {}6667bool do_entry(K const& k, V const& v) {68if ((uintptr_t) k == (uintptr_t) _val) {69// Delete me!70return true;71} else {72return false; // continue iteration73}74}75};7677};7879class SmallResourceHashtableTest : public CommonResourceHashtableTest {80protected:8182template<83unsigned (*HASH) (K const&) = primitive_hash<K>,84bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,85unsigned SIZE = 256,86ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA87>88class Runner : public AllStatic {89public:9091static void test(V step) {92EqualityTestIter et;93ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh;9495ASSERT_FALSE(rh.contains(as_K(step)));9697ASSERT_TRUE(rh.put(as_K(step), step));98ASSERT_TRUE(rh.contains(as_K(step)));99100ASSERT_FALSE(rh.put(as_K(step), step));101102ASSERT_TRUE(rh.put(as_K(2 * step), 2 * step));103ASSERT_TRUE(rh.put(as_K(3 * step), 3 * step));104ASSERT_TRUE(rh.put(as_K(4 * step), 4 * step));105ASSERT_TRUE(rh.put(as_K(5 * step), 5 * step));106107ASSERT_FALSE(rh.remove(as_K(0x0)));108109rh.iterate(&et);110if (::testing::Test::HasFailure()) {111return;112}113114ASSERT_TRUE(rh.remove(as_K(step)));115ASSERT_FALSE(rh.contains(as_K(step)));116rh.iterate(&et);117118119// Test put_if_absent(key) (creating a default-created value)120bool created = false;121V* v = rh.put_if_absent(as_K(step), &created);122ASSERT_TRUE(rh.contains(as_K(step)));123ASSERT_TRUE(created);124*v = (V)step;125126// Calling this function a second time should yield the same value pointer127V* v2 = rh.put_if_absent(as_K(step), &created);128ASSERT_EQ(v, v2);129ASSERT_EQ(*v2, *v);130ASSERT_FALSE(created);131132ASSERT_TRUE(rh.remove(as_K(step)));133ASSERT_FALSE(rh.contains(as_K(step)));134rh.iterate(&et);135136// Test put_if_absent(key, value)137v = rh.put_if_absent(as_K(step), step, &created);138ASSERT_EQ(*v, step);139ASSERT_TRUE(rh.contains(as_K(step)));140ASSERT_TRUE(created);141142v2 = rh.put_if_absent(as_K(step), step, &created);143// Calling this function a second time should yield the same value pointer144ASSERT_EQ(v, v2);145ASSERT_EQ(*v2, (V)step);146ASSERT_FALSE(created);147148ASSERT_TRUE(rh.remove(as_K(step)));149ASSERT_FALSE(rh.contains(as_K(step)));150rh.iterate(&et);151152153}154};155};156157TEST_VM_F(SmallResourceHashtableTest, default) {158ResourceMark rm;159Runner<>::test(0x1);160}161162TEST_VM_F(SmallResourceHashtableTest, default_shifted) {163ResourceMark rm;164Runner<>::test(0x10);165}166167TEST_VM_F(SmallResourceHashtableTest, bad_hash) {168ResourceMark rm;169Runner<bad_hash>::test(0x1);170}171172TEST_VM_F(SmallResourceHashtableTest, bad_hash_shifted) {173ResourceMark rm;174Runner<bad_hash>::test(0x10);175}176177TEST_VM_F(SmallResourceHashtableTest, identity_hash) {178ResourceMark rm;179Runner<identity_hash>::test(0x1);180}181182TEST_VM_F(SmallResourceHashtableTest, identity_hash_shifted) {183ResourceMark rm;184Runner<identity_hash>::test(0x10);185}186187TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm) {188Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1);189}190191TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm_shifted) {192Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10);193}194195TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm) {196Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1);197}198199TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm_shifted) {200Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10);201}202203TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm) {204Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x1);205}206207TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm_shifted) {208Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x10);209}210211class GenericResourceHashtableTest : public CommonResourceHashtableTest {212protected:213214template<215unsigned (*HASH) (K const&) = primitive_hash<K>,216bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,217unsigned SIZE = 256,218ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA219>220class Runner : public AllStatic {221public:222223static void test(unsigned num_elements = SIZE) {224EqualityTestIter et;225ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh;226227for (uintptr_t i = 0; i < num_elements; ++i) {228ASSERT_TRUE(rh.put(as_K(i), i));229}230231rh.iterate(&et);232if (::testing::Test::HasFailure()) {233return;234}235236for (uintptr_t i = num_elements; i > 0; --i) {237uintptr_t index = i - 1;238ASSERT_TRUE((rh.remove(as_K(index))));239}240241rh.iterate(&et);242if (::testing::Test::HasFailure()) {243return;244}245for (uintptr_t i = num_elements; i > 0; --i) {246uintptr_t index = i - 1;247ASSERT_FALSE(rh.remove(as_K(index)));248}249rh.iterate(&et);250251// Add more entries in and then delete one.252for (uintptr_t i = 10; i > 0; --i) {253uintptr_t index = i - 1;254ASSERT_TRUE(rh.put(as_K(index), index));255}256DeleterTestIter dt(5);257rh.unlink(&dt);258ASSERT_FALSE(rh.get(as_K(5)));259}260};261};262263TEST_VM_F(GenericResourceHashtableTest, default) {264ResourceMark rm;265Runner<>::test();266}267268TEST_VM_F(GenericResourceHashtableTest, bad_hash) {269ResourceMark rm;270Runner<bad_hash>::test();271}272273TEST_VM_F(GenericResourceHashtableTest, identity_hash) {274ResourceMark rm;275Runner<identity_hash>::test();276}277278TEST_VM_F(GenericResourceHashtableTest, primitive_hash_no_rm) {279Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test();280}281282TEST_VM_F(GenericResourceHashtableTest, bad_hash_no_rm) {283Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test();284}285286TEST_VM_F(GenericResourceHashtableTest, identity_hash_no_rm) {287Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(512);288}289290291