1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "memory/resourceArea.hpp"
  26 #include "utilities/bitMap.inline.hpp"
  27 #include "unittest.hpp"
  28 
  29 class BitMapCopyTest {
  30 
  31   template <class ResizableBitMapClass>
  32   static void fillBitMap(ResizableBitMapClass& map) {
  33     map.set_bit(0);
  34     map.set_bit(1);
  35     map.set_bit(3);
  36     map.set_bit(17);
  37     map.set_bit(512);
  38   }
  39 
  40 public:
  41   static void testcopy0() {
  42     BitMap::idx_t size = 1024;
  43     ResourceMark rm;
  44     ResourceBitMap map1(size);
  45     fillBitMap(map1);
  46 
  47     ResourceBitMap map2(size);
  48     map2.copy_from(map1, 0, 0);
  49     EXPECT_TRUE(map2.is_empty());
  50     EXPECT_TRUE(map2.count_one_bits() == 0);
  51   }
  52 
  53   static void testcopy1() {
  54     BitMap::idx_t size = 1024;
  55     ResourceMark rm;
  56     ResourceBitMap map1(size);
  57     fillBitMap(map1);
  58 
  59     ResourceBitMap map2(size);
  60     map2.copy_from(map1, 0, 1);
  61     EXPECT_TRUE(map2.at(0));
  62     EXPECT_TRUE(map2.count_one_bits() == 1);
  63     
  64   }
  65 
  66   static void testcopy4() {
  67     BitMap::idx_t size = 1024;
  68     ResourceMark rm;
  69     ResourceBitMap map1(size);
  70     map1.set_range(0, 1024);
  71 
  72     ResourceBitMap map2(size);
  73     map2.copy_from(map1, 6, 10);
  74     EXPECT_FALSE(map2.at(5));
  75     EXPECT_TRUE(map2.at(6));
  76     EXPECT_TRUE(map2.at(7));
  77     EXPECT_TRUE(map2.at(8));
  78     EXPECT_TRUE(map2.at(9));
  79     EXPECT_FALSE(map2.at(10));
  80     EXPECT_TRUE(map2.count_one_bits() == 4);
  81     
  82   }
  83 
  84   static void testcopy8() {
  85     BitMap::idx_t size = 1024;
  86     ResourceMark rm;
  87     ResourceBitMap map1(size);
  88     map1.set_range(0, 1024);
  89 
  90     ResourceBitMap map2(size);
  91     map2.copy_from(map1, 0, 8);
  92     EXPECT_TRUE(map2.at(0));
  93     EXPECT_TRUE(map2.at(1));
  94     EXPECT_TRUE(map2.at(2));
  95     EXPECT_TRUE(map2.at(3));
  96     EXPECT_TRUE(map2.at(4));
  97     EXPECT_TRUE(map2.at(5));
  98     EXPECT_TRUE(map2.at(6));
  99     EXPECT_TRUE(map2.at(7));
 100     EXPECT_FALSE(map2.at(8));
 101     EXPECT_TRUE(map2.count_one_bits() == 8);
 102     
 103   }
 104 
 105   static void testcopy100() {
 106     BitMap::idx_t size = 1024;
 107     ResourceMark rm;
 108     ResourceBitMap map1(size);
 109     map1.set_range(0, 1024);
 110 
 111     ResourceBitMap map2(size);
 112     map2.copy_from(map1, 48, 148);
 113     EXPECT_FALSE(map2.at(47));
 114     EXPECT_TRUE(map2.at(48));
 115     EXPECT_TRUE(map2.at(147));
 116     EXPECT_FALSE(map2.at(148));
 117     EXPECT_TRUE(map2.count_one_bits() == 100);
 118     
 119   }
 120 
 121   static void testcopyall() {
 122     BitMap::idx_t size = 1024;
 123     ResourceMark rm;
 124     ResourceBitMap map1(size);
 125     fillBitMap(map1);
 126 
 127     ResourceBitMap map2(size);
 128     map2.set_range(0, 512);
 129     map2.copy_from(map1, 0, 1024);
 130     EXPECT_TRUE(map2.at(0));
 131     EXPECT_TRUE(map2.at(1));
 132     EXPECT_TRUE(map2.at(3));
 133     EXPECT_TRUE(map2.at(17));
 134     EXPECT_TRUE(map2.at(512));
 135     EXPECT_TRUE(map2.count_one_bits() == 5);
 136     
 137   }
 138 
 139 };
 140 
 141 TEST_VM(BitMap, copy0) {
 142   BitMapCopyTest::testcopy0();
 143 }
 144 
 145 TEST_VM(BitMap, copy1) {
 146   BitMapCopyTest::testcopy1();
 147 }
 148 
 149 TEST_VM(BitMap, copy4) {
 150   BitMapCopyTest::testcopy4();
 151 }
 152 
 153 TEST_VM(BitMap, copy8) {
 154   BitMapCopyTest::testcopy8();
 155 }
 156 
 157 TEST_VM(BitMap, copy100) {
 158   BitMapCopyTest::testcopy100();
 159 }
 160 
 161 TEST_VM(BitMap, copyall) {
 162   BitMapCopyTest::testcopyall();
 163 }