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