1 | /* |
2 | * Copyright (c) 2016, 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/zBitMap.inline.hpp" |
26 | #include "unittest.hpp" |
27 | |
28 | class ZBitMapTest : public ::testing::Test { |
29 | protected: |
30 | static void test_set_pair_unset(size_t size, bool finalizable) { |
31 | ZBitMap bitmap(size); |
32 | |
33 | for (BitMap::idx_t i = 0; i < size - 1; i++) { |
34 | if ((i + 1) % BitsPerWord == 0) { |
35 | // Can't set pairs of bits in different words. |
36 | continue; |
37 | } |
38 | |
39 | // ZBitMaps are not cleared when constructed. |
40 | bitmap.clear(); |
41 | |
42 | bool inc_live = false; |
43 | |
44 | bool ret = bitmap.par_set_bit_pair(i, finalizable, inc_live); |
45 | EXPECT_TRUE(ret) << "Failed to set bit" ; |
46 | EXPECT_TRUE(inc_live) << "Should have set inc_live" ; |
47 | |
48 | // First bit should always be set |
49 | EXPECT_TRUE(bitmap.at(i)) << "Should be set" ; |
50 | |
51 | // Second bit should only be set when marking strong |
52 | EXPECT_NE(bitmap.at(i + 1), finalizable); |
53 | } |
54 | } |
55 | |
56 | static void test_set_pair_set(size_t size, bool finalizable) { |
57 | ZBitMap bitmap(size); |
58 | |
59 | for (BitMap::idx_t i = 0; i < size - 1; i++) { |
60 | if ((i + 1) % BitsPerWord == 0) { |
61 | // Can't set pairs of bits in different words. |
62 | continue; |
63 | } |
64 | |
65 | // Fill the bitmap with ones. |
66 | bitmap.set_range(0, size); |
67 | |
68 | bool inc_live = false; |
69 | |
70 | bool ret = bitmap.par_set_bit_pair(i, finalizable, inc_live); |
71 | EXPECT_FALSE(ret) << "Should not succeed setting bit" ; |
72 | EXPECT_FALSE(inc_live) << "Should not have set inc_live" ; |
73 | |
74 | // Both bits were pre-set. |
75 | EXPECT_TRUE(bitmap.at(i)) << "Should be set" ; |
76 | EXPECT_TRUE(bitmap.at(i + 1)) << "Should be set" ; |
77 | } |
78 | } |
79 | |
80 | static void test_set_pair_set(bool finalizable) { |
81 | test_set_pair_set(2, finalizable); |
82 | test_set_pair_set(62, finalizable); |
83 | test_set_pair_set(64, finalizable); |
84 | test_set_pair_set(66, finalizable); |
85 | test_set_pair_set(126, finalizable); |
86 | test_set_pair_set(128, finalizable); |
87 | } |
88 | |
89 | static void test_set_pair_unset(bool finalizable) { |
90 | test_set_pair_unset(2, finalizable); |
91 | test_set_pair_unset(62, finalizable); |
92 | test_set_pair_unset(64, finalizable); |
93 | test_set_pair_unset(66, finalizable); |
94 | test_set_pair_unset(126, finalizable); |
95 | test_set_pair_unset(128, finalizable); |
96 | } |
97 | |
98 | }; |
99 | |
100 | TEST_F(ZBitMapTest, test_set_pair_set) { |
101 | test_set_pair_set(false); |
102 | test_set_pair_set(true); |
103 | } |
104 | |
105 | TEST_F(ZBitMapTest, test_set_pair_unset) { |
106 | test_set_pair_unset(false); |
107 | test_set_pair_unset(true); |
108 | } |
109 | |