test/hotspot/gtest/gc/z/test_zUtils.cpp
author pliden
Wed, 13 Mar 2019 11:31:00 +0100
changeset 54094 ed3c6f07faab
parent 50975 b833992fa8fa
permissions -rw-r--r--
8219633: ZGC: Rename ZPageSizeMin to ZGranuleSize Reviewed-by: eosterlund, stefank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     1
/*
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
50975
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
     4
 *
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
     7
 * published by the Free Software Foundation.
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
     8
 *
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    13
 * accompanied this code).
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    14
 *
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    18
 *
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b833992fa8fa 8206322: ZGC: Incorrect license header in gtests
pliden
parents: 50525
diff changeset
    21
 * questions.
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    22
 */
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    23
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    24
#include "precompiled.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    25
#include "gc/z/zUtils.inline.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    26
#include "unittest.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    27
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    28
#include <limits>
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    29
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    30
template <typename T>
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    31
static T max_alignment() {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    32
  T max = std::numeric_limits<T>::max();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    33
  return max ^ (max >> 1);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    34
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    35
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    36
TEST(ZUtilsTest, round_up_power_of_2) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    37
  EXPECT_EQ(ZUtils::round_up_power_of_2(1u), 1u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    38
  EXPECT_EQ(ZUtils::round_up_power_of_2(2u), 2u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    39
  EXPECT_EQ(ZUtils::round_up_power_of_2(3u), 4u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    40
  EXPECT_EQ(ZUtils::round_up_power_of_2(4u), 4u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    41
  EXPECT_EQ(ZUtils::round_up_power_of_2(5u), 8u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    42
  EXPECT_EQ(ZUtils::round_up_power_of_2(6u), 8u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    43
  EXPECT_EQ(ZUtils::round_up_power_of_2(7u), 8u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    44
  EXPECT_EQ(ZUtils::round_up_power_of_2(8u), 8u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    45
  EXPECT_EQ(ZUtils::round_up_power_of_2(9u), 16u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    46
  EXPECT_EQ(ZUtils::round_up_power_of_2(10u), 16u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    47
  EXPECT_EQ(ZUtils::round_up_power_of_2(1023u), 1024u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    48
  EXPECT_EQ(ZUtils::round_up_power_of_2(1024u), 1024u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    49
  EXPECT_EQ(ZUtils::round_up_power_of_2(1025u), 2048u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    50
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    51
  const size_t max = max_alignment<size_t>();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    52
  EXPECT_EQ(ZUtils::round_up_power_of_2(max - 1), max);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    53
  EXPECT_EQ(ZUtils::round_up_power_of_2(max), max);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    54
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    55
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    56
TEST(ZUtilsTest, round_down_power_of_2) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    57
  EXPECT_EQ(ZUtils::round_down_power_of_2(1u), 1u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    58
  EXPECT_EQ(ZUtils::round_down_power_of_2(2u), 2u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    59
  EXPECT_EQ(ZUtils::round_down_power_of_2(3u), 2u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    60
  EXPECT_EQ(ZUtils::round_down_power_of_2(4u), 4u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    61
  EXPECT_EQ(ZUtils::round_down_power_of_2(5u), 4u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    62
  EXPECT_EQ(ZUtils::round_down_power_of_2(6u), 4u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    63
  EXPECT_EQ(ZUtils::round_down_power_of_2(7u), 4u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    64
  EXPECT_EQ(ZUtils::round_down_power_of_2(8u), 8u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    65
  EXPECT_EQ(ZUtils::round_down_power_of_2(9u), 8u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    66
  EXPECT_EQ(ZUtils::round_down_power_of_2(10u), 8u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    67
  EXPECT_EQ(ZUtils::round_down_power_of_2(1023u), 512u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    68
  EXPECT_EQ(ZUtils::round_down_power_of_2(1024u), 1024u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    69
  EXPECT_EQ(ZUtils::round_down_power_of_2(1025u), 1024u);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    70
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    71
  const size_t max = max_alignment<size_t>();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    72
  EXPECT_EQ(ZUtils::round_down_power_of_2(max), max);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    73
  EXPECT_EQ(ZUtils::round_down_power_of_2(max - 1), max / 2);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    74
}