< prev index next >

test/native/utilities/test_resourceHash.cpp

Print this page
rev 12087 : 8166462: Convert TestResourcehash_test to Gtest
Reviewed-by: duke

*** 1,7 **** /* ! * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 17,182 **** * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. - * */ #include "precompiled.hpp" #include "memory/allocation.hpp" #include "memory/resourceArea.hpp" #include "utilities/debug.hpp" #include "utilities/resourceHash.hpp" ! #ifndef PRODUCT ! ! /////////////// Unit tests /////////////// ! ! class TestResourceHashtable : public AllStatic { typedef void* K; typedef int V; static unsigned identity_hash(const K& k) { ! return (unsigned)(uintptr_t)k; } static unsigned bad_hash(const K& k) { return 1; } class EqualityTestIter { public: bool do_entry(K const& k, V const& v) { ! assert((uintptr_t)k == (uintptr_t)v, ""); return true; // continue iteration } }; template< unsigned (*HASH) (K const&) = primitive_hash<K>, bool (*EQUALS)(K const&, K const&) = primitive_equals<K>, unsigned SIZE = 256, ! ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA, ! MEMFLAGS MEM_TYPE = mtInternal > class Runner : public AllStatic { - static void* as_K(uintptr_t val) { return (void*)val; } - public: ! static void test_small() { EqualityTestIter et; ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh; ! assert(!rh.contains(as_K(0x1)), ""); ! assert(rh.put(as_K(0x1), 0x1), ""); ! assert(rh.contains(as_K(0x1)), ""); ! assert(!rh.put(as_K(0x1), 0x1), ""); ! assert(rh.put(as_K(0x2), 0x2), ""); ! assert(rh.put(as_K(0x3), 0x3), ""); ! assert(rh.put(as_K(0x4), 0x4), ""); ! assert(rh.put(as_K(0x5), 0x5), ""); ! assert(!rh.remove(as_K(0x0)), ""); ! rh.iterate(&et); - assert(rh.remove(as_K(0x1)), ""); rh.iterate(&et); } ! // We use keys with the low bits cleared since the default hash will do some shifting ! static void test_small_shifted() { ! EqualityTestIter et; ! ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh; ! assert(!rh.contains(as_K(0x10)), ""); ! assert(rh.put(as_K(0x10), 0x10), ""); ! assert(rh.contains(as_K(0x10)), ""); ! assert(!rh.put(as_K(0x10), 0x10), ""); ! assert(rh.put(as_K(0x20), 0x20), ""); ! assert(rh.put(as_K(0x30), 0x30), ""); ! assert(rh.put(as_K(0x40), 0x40), ""); ! assert(rh.put(as_K(0x50), 0x50), ""); ! assert(!rh.remove(as_K(0x00)), ""); ! assert(rh.remove(as_K(0x10)), ""); ! rh.iterate(&et); ! } static void test(unsigned num_elements = SIZE) { EqualityTestIter et; ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh; for (uintptr_t i = 0; i < num_elements; ++i) { ! assert(rh.put(as_K(i), i), ""); } rh.iterate(&et); for (uintptr_t i = num_elements; i > 0; --i) { uintptr_t index = i - 1; ! assert(rh.remove(as_K(index)), ""); } rh.iterate(&et); for (uintptr_t i = num_elements; i > 0; --i) { uintptr_t index = i - 1; ! assert(!rh.remove(as_K(index)), ""); } rh.iterate(&et); } }; ! public: ! static void run_tests() { ! { ResourceMark rm; - Runner<>::test_small(); - Runner<>::test_small_shifted(); Runner<>::test(); ! } ! ! { ! ResourceMark rm; ! Runner<identity_hash>::test_small(); ! Runner<identity_hash>::test_small_shifted(); ! Runner<identity_hash>::test(); ! } ! { ResourceMark rm; - Runner<bad_hash>::test_small(); - Runner<bad_hash>::test_small_shifted(); Runner<bad_hash>::test(); ! } ! assert(Thread::current()->resource_area()->nesting() == 0, "this code depends on not having an active ResourceMark"); ! // The following test calls will cause an assert if resource allocations occur since we don't have an active mark ! Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test_small(); ! Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test_small_shifted(); Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(); ! Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test_small(); ! Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test_small_shifted(); Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(); ! Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test_small(); ! Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test_small_shifted(); Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(512); - } - }; - - void TestResourcehash_test() { - TestResourceHashtable::run_tests(); } - - #endif // not PRODUCT - --- 17,226 ---- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ #include "precompiled.hpp" #include "memory/allocation.hpp" #include "memory/resourceArea.hpp" + #include "unittest.hpp" #include "utilities/debug.hpp" #include "utilities/resourceHash.hpp" ! class CommonResourceHashtableTest : public ::testing::Test { ! protected: typedef void* K; typedef int V; + const static MEMFLAGS MEM_TYPE = mtInternal; static unsigned identity_hash(const K& k) { ! return (unsigned) (uintptr_t) k; } static unsigned bad_hash(const K& k) { return 1; } + static void* as_K(uintptr_t val) { + return (void*) val; + } + class EqualityTestIter { public: + bool do_entry(K const& k, V const& v) { ! if ((uintptr_t) k != (uintptr_t) v) { ! EXPECT_EQ((uintptr_t) k, (uintptr_t) v); ! return false; ! } else { return true; // continue iteration } + } }; + }; + + class SmallResourceHashtableTest : public CommonResourceHashtableTest { + protected: template< unsigned (*HASH) (K const&) = primitive_hash<K>, bool (*EQUALS)(K const&, K const&) = primitive_equals<K>, unsigned SIZE = 256, ! ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA > class Runner : public AllStatic { public: ! ! static void test(V step) { EqualityTestIter et; ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh; ! ASSERT_FALSE(rh.contains(as_K(step))); ! ASSERT_TRUE(rh.put(as_K(step), step)); ! ASSERT_TRUE(rh.contains(as_K(step))); ! ASSERT_FALSE(rh.put(as_K(step), step)); ! ASSERT_TRUE(rh.put(as_K(2 * step), 2 * step)); ! ASSERT_TRUE(rh.put(as_K(3 * step), 3 * step)); ! ASSERT_TRUE(rh.put(as_K(4 * step), 4 * step)); ! ASSERT_TRUE(rh.put(as_K(5 * step), 5 * step)); ! ASSERT_FALSE(rh.remove(as_K(0x0))); rh.iterate(&et); + if (::testing::Test::HasFailure()) { + return; + } + ASSERT_TRUE(rh.remove(as_K(step))); + rh.iterate(&et); } + }; + }; ! TEST_F(SmallResourceHashtableTest, default) { ! ResourceMark rm; ! Runner<>::test(0x1); ! } ! TEST_F(SmallResourceHashtableTest, default_shifted) { ! ResourceMark rm; ! Runner<>::test(0x10); ! } ! ! TEST_F(SmallResourceHashtableTest, bad_hash) { ! ResourceMark rm; ! Runner<bad_hash>::test(0x1); ! } ! ! TEST_F(SmallResourceHashtableTest, bad_hash_shifted) { ! ResourceMark rm; ! Runner<bad_hash>::test(0x10); ! } ! TEST_F(SmallResourceHashtableTest, identity_hash) { ! ResourceMark rm; ! Runner<identity_hash>::test(0x1); ! } ! TEST_F(SmallResourceHashtableTest, identity_hash_shifted) { ! ResourceMark rm; ! Runner<identity_hash>::test(0x10); ! } ! TEST_F(SmallResourceHashtableTest, primitive_hash_no_rm) { ! Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1); ! } ! TEST_F(SmallResourceHashtableTest, primitive_hash_no_rm_shifted) { ! Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10); ! } ! TEST_F(SmallResourceHashtableTest, bad_hash_no_rm) { ! Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x1); ! } ! TEST_F(SmallResourceHashtableTest, bad_hash_no_rm_shifted) { ! Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(0x10); ! } ! ! TEST_F(SmallResourceHashtableTest, identity_hash_no_rm) { ! Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x1); ! } ! ! TEST_F(SmallResourceHashtableTest, identity_hash_no_rm_shifted) { ! Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(0x10); ! } ! ! class GenericResourceHashtableTest : public CommonResourceHashtableTest { ! protected: ! ! template< ! unsigned (*HASH) (K const&) = primitive_hash<K>, ! bool (*EQUALS)(K const&, K const&) = primitive_equals<K>, ! unsigned SIZE = 256, ! ResourceObj::allocation_type ALLOC_TYPE = ResourceObj::RESOURCE_AREA ! > ! class Runner : public AllStatic { ! public: static void test(unsigned num_elements = SIZE) { EqualityTestIter et; ResourceHashtable<K, V, HASH, EQUALS, SIZE, ALLOC_TYPE, MEM_TYPE> rh; for (uintptr_t i = 0; i < num_elements; ++i) { ! ASSERT_TRUE(rh.put(as_K(i), i)); } rh.iterate(&et); + if (::testing::Test::HasFailure()) { + return; + } for (uintptr_t i = num_elements; i > 0; --i) { uintptr_t index = i - 1; ! ASSERT_TRUE((rh.remove(as_K(index)))); } + rh.iterate(&et); + if (::testing::Test::HasFailure()) { + return; + } for (uintptr_t i = num_elements; i > 0; --i) { uintptr_t index = i - 1; ! ASSERT_FALSE(rh.remove(as_K(index))); } rh.iterate(&et); } }; + }; ! TEST_F(GenericResourceHashtableTest, default) { ResourceMark rm; Runner<>::test(); ! } ! TEST_F(GenericResourceHashtableTest, bad_hash) { ResourceMark rm; Runner<bad_hash>::test(); ! } + TEST_F(GenericResourceHashtableTest, identity_hash) { + ResourceMark rm; + Runner<identity_hash>::test(); + } ! TEST_F(GenericResourceHashtableTest, primitive_hash_no_rm) { Runner<primitive_hash<K>, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(); + } ! TEST_F(GenericResourceHashtableTest, bad_hash_no_rm) { Runner<bad_hash, primitive_equals<K>, 512, ResourceObj::C_HEAP>::test(); + } ! TEST_F(GenericResourceHashtableTest, identity_hash_no_rm) { Runner<identity_hash, primitive_equals<K>, 1, ResourceObj::C_HEAP>::test(512); }
< prev index next >