8166132: Convert TestGlobalDefinitions_test to GTest

Reviewed-by: iignatyev
This commit is contained in:
Boris Molodenkov 2016-12-06 16:42:42 +03:00
parent 9d99a698ef
commit d82ff831ba
3 changed files with 134 additions and 91 deletions

View File

@ -368,93 +368,3 @@ STATIC_ASSERT(right_n_bits(1|2) == 0x7);
STATIC_ASSERT(left_n_bits(3) == (intptr_t) LP64_ONLY(0xE000000000000000) NOT_LP64(0xE0000000));
STATIC_ASSERT(left_n_bits(1|2) == (intptr_t) LP64_ONLY(0xE000000000000000) NOT_LP64(0xE0000000));
#ifndef PRODUCT
// For unit testing only
class TestGlobalDefinitions {
private:
static void test_clamp_address_in_page() {
intptr_t page_sizes[] = { os::vm_page_size(), 4096, 8192, 65536, 2*1024*1024 };
const int num_page_sizes = sizeof(page_sizes) / sizeof(page_sizes[0]);
for (int i = 0; i < num_page_sizes; i++) {
intptr_t page_size = page_sizes[i];
address a_page = (address)(10*page_size);
// Check that address within page is returned as is
assert(clamp_address_in_page(a_page, a_page, page_size) == a_page, "incorrect");
assert(clamp_address_in_page(a_page + 128, a_page, page_size) == a_page + 128, "incorrect");
assert(clamp_address_in_page(a_page + page_size - 1, a_page, page_size) == a_page + page_size - 1, "incorrect");
// Check that address above page returns start of next page
assert(clamp_address_in_page(a_page + page_size, a_page, page_size) == a_page + page_size, "incorrect");
assert(clamp_address_in_page(a_page + page_size + 1, a_page, page_size) == a_page + page_size, "incorrect");
assert(clamp_address_in_page(a_page + page_size*5 + 1, a_page, page_size) == a_page + page_size, "incorrect");
// Check that address below page returns start of page
assert(clamp_address_in_page(a_page - 1, a_page, page_size) == a_page, "incorrect");
assert(clamp_address_in_page(a_page - 2*page_size - 1, a_page, page_size) == a_page, "incorrect");
assert(clamp_address_in_page(a_page - 5*page_size - 1, a_page, page_size) == a_page, "incorrect");
}
}
static void test_exact_unit_for_byte_size() {
assert(strcmp(exact_unit_for_byte_size(0), "B") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(1), "B") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(K - 1), "B") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(K), "K") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(K + 1), "B") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(M - 1), "B") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(M), "M") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(M + 1), "B") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(M + K), "K") == 0, "incorrect");
#ifdef LP64
assert(strcmp(exact_unit_for_byte_size(G - 1), "B") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(G), "G") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(G + 1), "B") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(G + K), "K") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(G + M), "M") == 0, "incorrect");
assert(strcmp(exact_unit_for_byte_size(G + M + K), "K") == 0, "incorrect");
#endif
}
static void test_byte_size_in_exact_unit() {
assert(byte_size_in_exact_unit(0) == 0, "incorrect");
assert(byte_size_in_exact_unit(1) == 1, "incorrect");
assert(byte_size_in_exact_unit(K - 1) == K - 1, "incorrect");
assert(byte_size_in_exact_unit(K) == 1, "incorrect");
assert(byte_size_in_exact_unit(K + 1) == K + 1, "incorrect");
assert(byte_size_in_exact_unit(M - 1) == M - 1, "incorrect");
assert(byte_size_in_exact_unit(M) == 1, "incorrect");
assert(byte_size_in_exact_unit(M + 1) == M + 1, "incorrect");
assert(byte_size_in_exact_unit(M + K) == K + 1, "incorrect");
#ifdef LP64
assert(byte_size_in_exact_unit(G - 1) == G - 1, "incorrect");
assert(byte_size_in_exact_unit(G) == 1, "incorrect");
assert(byte_size_in_exact_unit(G + 1) == G + 1, "incorrect");
assert(byte_size_in_exact_unit(G + K) == M + 1, "incorrect");
assert(byte_size_in_exact_unit(G + M) == K + 1, "incorrect");
assert(byte_size_in_exact_unit(G + M + K) == M + K + 1, "incorrect");
#endif
}
static void test_exact_units() {
test_exact_unit_for_byte_size();
test_byte_size_in_exact_unit();
}
public:
static void test() {
test_clamp_address_in_page();
test_exact_units();
}
};
void TestGlobalDefinitions_test() {
TestGlobalDefinitions::test();
}
#endif // PRODUCT

View File

@ -46,7 +46,6 @@ void InternalVMTests::run() {
run_unit_test(TestVirtualSpace_test);
run_unit_test(TestMetaspaceAux_test);
run_unit_test(TestVirtualSpaceNode_test);
run_unit_test(TestGlobalDefinitions_test);
run_unit_test(GCTimer_test);
run_unit_test(ObjectMonitor_test);
run_unit_test(DirectivesParser_test);

View File

@ -0,0 +1,134 @@
/*
* Copyright (c) 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.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* 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 "runtime/os.hpp"
#include "unittest.hpp"
#include "utilities/globalDefinitions.hpp"
static ::testing::AssertionResult testPageAddress(
const char* expected_addr_expr,
const char* addr_expr,
const char* page_addr_expr,
const char* page_size_expr,
const char* actual_addr_expr,
address expected_addr,
address addr,
address page_addr,
intptr_t page_size,
address actual_addr) {
if (expected_addr == actual_addr) {
return ::testing::AssertionSuccess();
}
return ::testing::AssertionFailure()
<< actual_addr_expr << " returned unexpected address " << (void*) actual_addr << std::endl
<< "Expected " << expected_addr_expr << ": " << (void*) expected_addr << std::endl
<< "where" << std::endl
<< addr_expr << ": " << (void*) addr << std::endl
<< page_addr_expr << ": " << (void*) page_addr << std::endl
<< page_size_expr << ": " << page_size;
}
TEST_VM(globalDefinitions, clamp_address_in_page) {
const intptr_t page_sizes[] = {os::vm_page_size(), 4096, 8192, 65536, 2 * 1024 * 1024};
const int num_page_sizes = sizeof(page_sizes) / sizeof(page_sizes[0]);
for (int i = 0; i < num_page_sizes; i++) {
intptr_t page_size = page_sizes[i];
address page_address = (address) (10 * page_size);
const intptr_t within_page_offsets[] = {0, 128, page_size - 1};
const int num_within_page_offsets = sizeof(within_page_offsets) / sizeof(within_page_offsets[0]);
for (int k = 0; k < num_within_page_offsets; ++k) {
address addr = page_address + within_page_offsets[k];
address expected_address = addr;
EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
clamp_address_in_page(addr, page_address, page_size))
<< "Expect that address within page is returned as is";
}
const intptr_t above_page_offsets[] = {page_size, page_size + 1, 5 * page_size + 1};
const int num_above_page_offsets = sizeof(above_page_offsets) / sizeof(above_page_offsets[0]);
for (int k = 0; k < num_above_page_offsets; ++k) {
address addr = page_address + above_page_offsets[k];
address expected_address = page_address + page_size;
EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
clamp_address_in_page(addr, page_address, page_size))
<< "Expect that address above page returns start of next page";
}
const intptr_t below_page_offsets[] = {1, 2 * page_size + 1, 5 * page_size + 1};
const int num_below_page_offsets = sizeof(below_page_offsets) / sizeof(below_page_offsets[0]);
for (int k = 0; k < num_below_page_offsets; ++k) {
address addr = page_address - below_page_offsets[k];
address expected_address = page_address;
EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
clamp_address_in_page(addr, page_address, page_size))
<< "Expect that address below page returns start of page";
}
}
}
TEST(globalDefinitions, exact_unit_for_byte_size) {
EXPECT_STREQ("B", exact_unit_for_byte_size(0));
EXPECT_STREQ("B", exact_unit_for_byte_size(1));
EXPECT_STREQ("B", exact_unit_for_byte_size(K - 1));
EXPECT_STREQ("K", exact_unit_for_byte_size(K));
EXPECT_STREQ("B", exact_unit_for_byte_size(K + 1));
EXPECT_STREQ("B", exact_unit_for_byte_size(M - 1));
EXPECT_STREQ("M", exact_unit_for_byte_size(M));
EXPECT_STREQ("B", exact_unit_for_byte_size(M + 1));
EXPECT_STREQ("K", exact_unit_for_byte_size(M + K));
#ifdef LP64
EXPECT_STREQ("B", exact_unit_for_byte_size(G - 1));
EXPECT_STREQ("G", exact_unit_for_byte_size(G));
EXPECT_STREQ("B", exact_unit_for_byte_size(G + 1));
EXPECT_STREQ("K", exact_unit_for_byte_size(G + K));
EXPECT_STREQ("M", exact_unit_for_byte_size(G + M));
EXPECT_STREQ("K", exact_unit_for_byte_size(G + M + K));
#endif
}
TEST(globalDefinitions, byte_size_in_exact_unit) {
EXPECT_EQ(0u, byte_size_in_exact_unit(0));
EXPECT_EQ(1u, byte_size_in_exact_unit(1));
EXPECT_EQ(K - 1, byte_size_in_exact_unit(K - 1));
EXPECT_EQ(1u, byte_size_in_exact_unit(K));
EXPECT_EQ(K + 1, byte_size_in_exact_unit(K + 1));
EXPECT_EQ(M - 1, byte_size_in_exact_unit(M - 1));
EXPECT_EQ(1u, byte_size_in_exact_unit(M));
EXPECT_EQ(M + 1, byte_size_in_exact_unit(M + 1));
EXPECT_EQ(K + 1, byte_size_in_exact_unit(M + K));
#ifdef LP64
EXPECT_EQ(G - 1, byte_size_in_exact_unit(G - 1));
EXPECT_EQ(1u, byte_size_in_exact_unit(G));
EXPECT_EQ(G + 1, byte_size_in_exact_unit(G + 1));
EXPECT_EQ(M + 1, byte_size_in_exact_unit(G + K));
EXPECT_EQ(K + 1, byte_size_in_exact_unit(G + M));
EXPECT_EQ(M + K + 1, byte_size_in_exact_unit(G + M + K));
#endif
}