Path: blob/master/test/hotspot/gtest/utilities/test_resourceHash.cpp
41145 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};6162};6364class SmallResourceHashtableTest : public CommonResourceHashtableTest {65protected:6667template<68unsigned (*HASH) (K const&) = primitive_hash<K>,69bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,70unsigned SIZE = 256,71ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA72>73class Runner : public AllStatic {74public:7576static void test(V step) {77EqualityTestIter et;78ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh;7980ASSERT_FALSE(rh.contains(as_K(step)));8182ASSERT_TRUE(rh.put(as_K(step), step));83ASSERT_TRUE(rh.contains(as_K(step)));8485ASSERT_FALSE(rh.put(as_K(step), step));8687ASSERT_TRUE(rh.put(as_K(2 * step), 2 * step));88ASSERT_TRUE(rh.put(as_K(3 * step), 3 * step));89ASSERT_TRUE(rh.put(as_K(4 * step), 4 * step));90ASSERT_TRUE(rh.put(as_K(5 * step), 5 * step));9192ASSERT_FALSE(rh.remove(as_K(0x0)));9394rh.iterate(&et);95if (::testing::Test::HasFailure()) {96return;97}9899ASSERT_TRUE(rh.remove(as_K(step)));100ASSERT_FALSE(rh.contains(as_K(step)));101rh.iterate(&et);102103104// Test put_if_absent(key) (creating a default-created value)105bool created = false;106V* v = rh.put_if_absent(as_K(step), &created);107ASSERT_TRUE(rh.contains(as_K(step)));108ASSERT_TRUE(created);109*v = (V)step;110111// Calling this function a second time should yield the same value pointer112V* v2 = rh.put_if_absent(as_K(step), &created);113ASSERT_EQ(v, v2);114ASSERT_EQ(*v2, *v);115ASSERT_FALSE(created);116117ASSERT_TRUE(rh.remove(as_K(step)));118ASSERT_FALSE(rh.contains(as_K(step)));119rh.iterate(&et);120121// Test put_if_absent(key, value)122v = rh.put_if_absent(as_K(step), step, &created);123ASSERT_EQ(*v, step);124ASSERT_TRUE(rh.contains(as_K(step)));125ASSERT_TRUE(created);126127v2 = rh.put_if_absent(as_K(step), step, &created);128// Calling this function a second time should yield the same value pointer129ASSERT_EQ(v, v2);130ASSERT_EQ(*v2, (V)step);131ASSERT_FALSE(created);132133ASSERT_TRUE(rh.remove(as_K(step)));134ASSERT_FALSE(rh.contains(as_K(step)));135rh.iterate(&et);136137138}139};140};141142TEST_VM_F(SmallResourceHashtableTest, default) {143ResourceMark rm;144Runner<>::test(0x1);145}146147TEST_VM_F(SmallResourceHashtableTest, default_shifted) {148ResourceMark rm;149Runner<>::test(0x10);150}151152TEST_VM_F(SmallResourceHashtableTest, bad_hash) {153ResourceMark rm;154Runner<bad_hash>::test(0x1);155}156157TEST_VM_F(SmallResourceHashtableTest, bad_hash_shifted) {158ResourceMark rm;159Runner<bad_hash>::test(0x10);160}161162TEST_VM_F(SmallResourceHashtableTest, identity_hash) {163ResourceMark rm;164Runner<identity_hash>::test(0x1);165}166167TEST_VM_F(SmallResourceHashtableTest, identity_hash_shifted) {168ResourceMark rm;169Runner<identity_hash>::test(0x10);170}171172TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm) {173Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1);174}175176TEST_VM_F(SmallResourceHashtableTest, primitive_hash_no_rm_shifted) {177Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10);178}179180TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm) {181Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1);182}183184TEST_VM_F(SmallResourceHashtableTest, bad_hash_no_rm_shifted) {185Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10);186}187188TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm) {189Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x1);190}191192TEST_VM_F(SmallResourceHashtableTest, identity_hash_no_rm_shifted) {193Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x10);194}195196class GenericResourceHashtableTest : public CommonResourceHashtableTest {197protected:198199template<200unsigned (*HASH) (K const&) = primitive_hash<K>,201bool (*EQUALS)(K const&, K const&) = primitive_equals<K>,202unsigned SIZE = 256,203ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA204>205class Runner : public AllStatic {206public:207208static void test(unsigned num_elements = SIZE) {209EqualityTestIter et;210ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh;211212for (uintptr_t i = 0; i < num_elements; ++i) {213ASSERT_TRUE(rh.put(as_K(i), i));214}215216rh.iterate(&et);217if (::testing::Test::HasFailure()) {218return;219}220221for (uintptr_t i = num_elements; i > 0; --i) {222uintptr_t index = i - 1;223ASSERT_TRUE((rh.remove(as_K(index))));224}225226rh.iterate(&et);227if (::testing::Test::HasFailure()) {228return;229}230for (uintptr_t i = num_elements; i > 0; --i) {231uintptr_t index = i - 1;232ASSERT_FALSE(rh.remove(as_K(index)));233}234rh.iterate(&et);235}236};237};238239TEST_VM_F(GenericResourceHashtableTest, default) {240ResourceMark rm;241Runner<>::test();242}243244TEST_VM_F(GenericResourceHashtableTest, bad_hash) {245ResourceMark rm;246Runner<bad_hash>::test();247}248249TEST_VM_F(GenericResourceHashtableTest, identity_hash) {250ResourceMark rm;251Runner<identity_hash>::test();252}253254TEST_VM_F(GenericResourceHashtableTest, primitive_hash_no_rm) {255Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test();256}257258TEST_VM_F(GenericResourceHashtableTest, bad_hash_no_rm) {259Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test();260}261262TEST_VM_F(GenericResourceHashtableTest, identity_hash_no_rm) {263Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(512);264}265266267