@@ -114,6 +114,165 @@ class BitMapTest {
114
114
#endif
115
115
};
116
116
117
+ class BitMapTruncateTest {
118
+
119
+ public:
120
+ const static BitMap::idx_t BITMAP_SIZE = 128 ;
121
+
122
+ template <class ResizableBitMapClass >
123
+ static void fillBitMap (ResizableBitMapClass& map, BitMap::idx_t size) {
124
+ BitMap::idx_t set_bits[] = {0 , 31 , 63 , 64 , 95 , 127 };
125
+ for (BitMap::idx_t bit : set_bits) {
126
+ if (bit < size) {
127
+ map.set_bit (bit);
128
+ }
129
+ }
130
+ }
131
+
132
+ template <class ResizableBitMapClass >
133
+ static void testTruncateOneWord () {
134
+ ResourceMark rm;
135
+
136
+ ResizableBitMapClass map (64 );
137
+ map.set_bit (0 );
138
+ map.set_bit (1 );
139
+ map.set_bit (2 );
140
+ map.set_bit (3 );
141
+
142
+ ResizableBitMapClass result (2 );
143
+ result.set_bit (0 );
144
+ result.set_bit (1 );
145
+
146
+ map.truncate (1 , 3 );
147
+
148
+ EXPECT_TRUE (map.is_same (result));
149
+ }
150
+
151
+ template <class ResizableBitMapClass >
152
+ static void testTruncateSame () {
153
+ // Resulting map should be the same as the original
154
+ ResourceMark rm;
155
+ ResizableBitMapClass expected (BITMAP_SIZE);
156
+ fillBitMap (expected, BITMAP_SIZE);
157
+
158
+ ResizableBitMapClass map (BITMAP_SIZE);
159
+ fillBitMap (map, BITMAP_SIZE);
160
+ map.truncate (0 , BITMAP_SIZE);
161
+
162
+ EXPECT_TRUE (map.is_same (expected));
163
+ }
164
+
165
+ template <class ResizableBitMapClass >
166
+ static void testTruncateStart () {
167
+ // Resulting map should start at the beginning of the original
168
+ ResourceMark rm;
169
+ ResizableBitMapClass expected (64 );
170
+ fillBitMap (expected, 64 );
171
+
172
+ ResizableBitMapClass map (BITMAP_SIZE);
173
+ fillBitMap (map, BITMAP_SIZE);
174
+ map.truncate (0 , 64 );
175
+
176
+ EXPECT_TRUE (map.is_same (expected));
177
+ }
178
+
179
+ template <class ResizableBitMapClass >
180
+ static void testTruncateEnd () {
181
+ // Resulting map should end at the end of the original
182
+ ResourceMark rm;
183
+ ResizableBitMapClass expected (64 );
184
+ expected.set_bit (0 );
185
+ expected.set_bit (31 );
186
+ expected.set_bit (63 );
187
+
188
+ ResizableBitMapClass map (BITMAP_SIZE);
189
+ fillBitMap (map, BITMAP_SIZE);
190
+ map.truncate (64 , 128 );
191
+
192
+ EXPECT_TRUE (map.is_same (expected));
193
+ }
194
+
195
+ template <class ResizableBitMapClass >
196
+ static void testTruncateMiddle () {
197
+ // Resulting map should end at the end of the original
198
+ ResourceMark rm;
199
+ ResizableBitMapClass expected (64 );
200
+ expected.set_bit (31 );
201
+ expected.set_bit (32 );
202
+ expected.set_bit (63 );
203
+
204
+ ResizableBitMapClass map (BITMAP_SIZE);
205
+ fillBitMap (map, BITMAP_SIZE);
206
+ map.truncate (32 , 96 );
207
+
208
+ EXPECT_TRUE (map.is_same (expected));
209
+ }
210
+
211
+ template <class ResizableBitMapClass >
212
+ static void testTruncateStartUnaligned () {
213
+ // Resulting map should start at the beginning of the original
214
+ ResourceMark rm;
215
+ ResizableBitMapClass expected (96 );
216
+ fillBitMap (expected, 96 );
217
+
218
+ ResizableBitMapClass map (BITMAP_SIZE);
219
+ fillBitMap (map, BITMAP_SIZE);
220
+ map.truncate (0 , 96 );
221
+
222
+ EXPECT_TRUE (map.is_same (expected));
223
+ }
224
+
225
+ template <class ResizableBitMapClass >
226
+ static void testTruncateEndUnaligned () {
227
+ // Resulting map should end at the end of the original
228
+ ResourceMark rm;
229
+ ResizableBitMapClass expected (97 );
230
+ expected.set_bit (0 );
231
+ expected.set_bit (32 );
232
+ expected.set_bit (33 );
233
+ expected.set_bit (64 );
234
+ expected.set_bit (96 );
235
+
236
+ ResizableBitMapClass map (BITMAP_SIZE);
237
+ fillBitMap (map, BITMAP_SIZE);
238
+ map.truncate (31 , 128 );
239
+
240
+ EXPECT_TRUE (map.is_same (expected));
241
+ }
242
+
243
+ template <class ResizableBitMapClass >
244
+ static void testRandom () {
245
+ for (int i = 0 ; i < 100 ; i++) {
246
+ ResourceMark rm;
247
+
248
+ const size_t max_size = 1024 ;
249
+ const size_t size = os::random () % max_size + 1 ;
250
+ const size_t truncate_size = os::random () % size + 1 ;
251
+ const size_t truncate_start = size == truncate_size ? 0 : os::random () % (size - truncate_size);
252
+
253
+ ResizableBitMapClass map (size);
254
+ ResizableBitMapClass result (truncate_size);
255
+
256
+ for (BitMap::idx_t idx = 0 ; idx < truncate_start; idx++) {
257
+ if (os::random () % 2 == 0 ) {
258
+ map.set_bit (idx);
259
+ }
260
+ }
261
+
262
+ for (BitMap::idx_t idx = 0 ; idx < truncate_size; idx++) {
263
+ if (os::random () % 2 == 0 ) {
264
+ map.set_bit (truncate_start + idx);
265
+ result.set_bit (idx);
266
+ }
267
+ }
268
+
269
+ map.truncate (truncate_start, truncate_start + truncate_size);
270
+
271
+ EXPECT_TRUE (map.is_same (result));
272
+ }
273
+ }
274
+ };
275
+
117
276
// TestArenaBitMap is the shorthand combination of Arena and ArenaBitMap.
118
277
// Multiple inheritance guarantees to construct Arena first.
119
278
class TestArenaBitMap : private Arena , public ArenaBitMap {
@@ -204,3 +363,75 @@ TEST_VM(BitMap, print_on) {
204
363
}
205
364
206
365
#endif
366
+
367
+ TEST_VM (BitMap, truncate_same) {
368
+ BitMapTruncateTest::testTruncateSame<ResourceBitMap>();
369
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ResourceBitMap" ;
370
+ BitMapTruncateTest::testTruncateSame<TestCHeapBitMap>();
371
+ EXPECT_FALSE (HasFailure ()) << " Failed on type CHeapBitMap" ;
372
+ BitMapTruncateTest::testTruncateSame<TestArenaBitMap>();
373
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ArenaBitMap" ;
374
+ }
375
+
376
+ TEST_VM (BitMap, truncate_start) {
377
+ BitMapTruncateTest::testTruncateStart<ResourceBitMap>();
378
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ResourceBitMap" ;
379
+ BitMapTruncateTest::testTruncateStart<TestCHeapBitMap>();
380
+ EXPECT_FALSE (HasFailure ()) << " Failed on type CHeapBitMap" ;
381
+ BitMapTruncateTest::testTruncateStart<TestArenaBitMap>();
382
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ArenaBitMap" ;
383
+ }
384
+
385
+ TEST_VM (BitMap, truncate_end) {
386
+ BitMapTruncateTest::testTruncateEnd<ResourceBitMap>();
387
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ResourceBitMap" ;
388
+ BitMapTruncateTest::testTruncateEnd<TestCHeapBitMap>();
389
+ EXPECT_FALSE (HasFailure ()) << " Failed on type CHeapBitMap" ;
390
+ BitMapTruncateTest::testTruncateEnd<TestArenaBitMap>();
391
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ArenaBitMap" ;
392
+ }
393
+
394
+ TEST_VM (BitMap, truncate_middle) {
395
+ BitMapTruncateTest::testTruncateMiddle<ResourceBitMap>();
396
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ResourceBitMap" ;
397
+ BitMapTruncateTest::testTruncateMiddle<TestCHeapBitMap>();
398
+ EXPECT_FALSE (HasFailure ()) << " Failed on type CHeapBitMap" ;
399
+ BitMapTruncateTest::testTruncateMiddle<TestArenaBitMap>();
400
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ArenaBitMap" ;
401
+ }
402
+
403
+ TEST_VM (BitMap, truncate_start_unaligned) {
404
+ BitMapTruncateTest::testTruncateStartUnaligned<ResourceBitMap>();
405
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ResourceBitMap" ;
406
+ BitMapTruncateTest::testTruncateStartUnaligned<TestCHeapBitMap>();
407
+ EXPECT_FALSE (HasFailure ()) << " Failed on type CHeapBitMap" ;
408
+ BitMapTruncateTest::testTruncateStartUnaligned<TestArenaBitMap>();
409
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ArenaBitMap" ;
410
+ }
411
+
412
+ TEST_VM (BitMap, truncate_end_unaligned) {
413
+ BitMapTruncateTest::testTruncateEndUnaligned<ResourceBitMap>();
414
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ResourceBitMap" ;
415
+ BitMapTruncateTest::testTruncateEndUnaligned<TestCHeapBitMap>();
416
+ EXPECT_FALSE (HasFailure ()) << " Failed on type CHeapBitMap" ;
417
+ BitMapTruncateTest::testTruncateEndUnaligned<TestArenaBitMap>();
418
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ArenaBitMap" ;
419
+ }
420
+
421
+ TEST_VM (BitMap, truncate_one_word) {
422
+ BitMapTruncateTest::testTruncateOneWord<ResourceBitMap>();
423
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ResourceBitMap" ;
424
+ BitMapTruncateTest::testTruncateOneWord<TestCHeapBitMap>();
425
+ EXPECT_FALSE (HasFailure ()) << " Failed on type CHeapBitMap" ;
426
+ BitMapTruncateTest::testTruncateOneWord<TestArenaBitMap>();
427
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ArenaBitMap" ;
428
+ }
429
+
430
+ TEST_VM (BitMap, truncate_random) {
431
+ BitMapTruncateTest::testRandom<ResourceBitMap>();
432
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ResourceBitMap" ;
433
+ BitMapTruncateTest::testRandom<TestCHeapBitMap>();
434
+ EXPECT_FALSE (HasFailure ()) << " Failed on type CHeapBitMap" ;
435
+ BitMapTruncateTest::testRandom<TestArenaBitMap>();
436
+ EXPECT_FALSE (HasFailure ()) << " Failed on type ArenaBitMap" ;
437
+ }
0 commit comments