test/hotspot/gtest/utilities/test_bitMap_setops.cpp
author manc
Mon, 14 Oct 2019 18:48:10 -0700
changeset 58652 9b67dd88a931
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232232: G1RemSetSummary::_rs_threads_vtimes is not initialized to zero Summary: Fix error in "Concurrent refinement threads times" in GC log and cleanup. Reviewed-by: tschatzl, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     1
/*
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     4
 *
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     7
 * published by the Free Software Foundation.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     8
 *
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    13
 * accompanied this code).
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    14
 *
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    18
 *
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    21
 * questions.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    22
 */
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    23
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    24
#include "precompiled.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    25
#include "utilities/bitMap.inline.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    26
#include "utilities/copy.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    27
#include "utilities/debug.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    28
#include "utilities/globalDefinitions.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    29
#include <stdlib.h>
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    30
#include "unittest.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    31
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    32
typedef BitMap::idx_t idx_t;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    33
typedef BitMap::bm_word_t bm_word_t;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    34
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    35
class BitMapMemory {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    36
private:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    37
  idx_t _words;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    38
  bm_word_t* _memory;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    39
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    40
public:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    41
  BitMapMemory(idx_t bits) :
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    42
    _words(BitMap::calc_size_in_words(bits)),
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    43
    _memory(static_cast<bm_word_t*>(malloc(_words * sizeof(bm_word_t))))
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    44
  { }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    45
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    46
  ~BitMapMemory() {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    47
    free(_memory);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    48
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    49
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    50
  BitMapView make_view(idx_t bits, bm_word_t value) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    51
    vmassert(BitMap::calc_size_in_words(bits) <= _words, "invalid request");
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    52
    STATIC_ASSERT(sizeof(bm_word_t) == sizeof(HeapWord));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    53
    Copy::fill_to_aligned_words((HeapWord*)_memory, _words, value);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    54
    return BitMapView(_memory, bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    55
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    56
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    57
  bm_word_t* memory() { return _memory; }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    58
};
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    59
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    60
const idx_t aligned_size = 4 * BitsPerWord;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    61
const idx_t unaligned_size = aligned_size - (BitsPerWord / 2);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    62
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    63
static bm_word_t make_even_bits() {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    64
  bm_word_t result = 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    65
  while (true) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    66
    bm_word_t next = (result << 2) | 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    67
    if (next == result) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    68
      return result;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    69
    }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    70
    result = next;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    71
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    72
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    73
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    74
const bm_word_t even_bits = make_even_bits();
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    75
const bm_word_t odd_bits = ~even_bits;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    76
const bm_word_t one_bits = ~bm_word_t(0);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    77
const bm_word_t zero_bits = 0;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    78
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    79
// Scoped set a clear bit and restore to clear.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    80
class WithBitSet {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    81
private:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    82
  BitMap& _bm;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    83
  idx_t _index;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    84
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    85
public:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    86
  WithBitSet(BitMap& bm, idx_t index) : _bm(bm), _index(index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    87
    // Failure may indicate test bug; can't use ASSERT_xxx in constructor.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    88
    EXPECT_FALSE(_bm.at(_index));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    89
    bm.set_bit(_index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    90
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    91
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    92
  ~WithBitSet() {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    93
    _bm.clear_bit(_index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    94
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    95
};
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    96
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    97
// Scoped clear a set bit and restore to set.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    98
class WithBitClear {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    99
private:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   100
  BitMap& _bm;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   101
  idx_t _index;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   102
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   103
public:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   104
  WithBitClear(BitMap& bm, idx_t index) : _bm(bm), _index(index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   105
    // Failure may indicate test bug; can't use ASSERT_xxx in constructor.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   106
    EXPECT_TRUE(_bm.at(_index));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   107
    bm.clear_bit(_index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   108
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   109
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   110
  ~WithBitClear() {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   111
    _bm.set_bit(_index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   112
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   113
};
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   114
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   115
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   116
// bool is_same(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   117
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   118
TEST(BitMap, is_same__aligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   119
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   120
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   121
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   122
  BitMapView x = mx.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   123
  BitMapView y = my.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   124
  EXPECT_TRUE(x.is_same(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   125
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   126
  WithBitClear wbc(x, aligned_size / 2);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   127
  EXPECT_FALSE(x.is_same(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   128
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   129
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   130
TEST(BitMap, is_same__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   131
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   132
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   133
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   134
  BitMapView x = mx.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   135
  BitMapView y = my.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   136
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   137
  // Check that a difference beyond the end of x/y doesn't count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   138
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   139
    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   140
    const idx_t index = aligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   141
    STATIC_ASSERT(unaligned_size <= index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   142
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   143
    WithBitClear wbc(aligned, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   144
    EXPECT_TRUE(x.is_same(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   145
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   146
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   147
  // Check that a difference in the final partial word does count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   148
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   149
    idx_t index = unaligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   150
    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   151
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   152
    WithBitClear wbc(y, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   153
    EXPECT_FALSE(x.is_same(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   154
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   155
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   156
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   157
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   158
// bool is_full();
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   159
// bool is_empty();
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   160
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   161
TEST(BitMap, is_full_or_empty__aligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   162
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   163
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   164
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   165
    BitMapView x = mx.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   166
    EXPECT_FALSE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   167
    EXPECT_FALSE(x.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   168
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   169
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   170
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   171
    BitMapView x = mx.make_view(aligned_size, zero_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   172
    EXPECT_FALSE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   173
    EXPECT_TRUE(x.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   174
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   175
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   176
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   177
    BitMapView x = mx.make_view(aligned_size, one_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   178
    EXPECT_TRUE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   179
    EXPECT_FALSE(x.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   180
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   181
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   182
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   183
TEST(BitMap, is_full__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   184
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   185
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   186
  BitMapView x = mx.make_view(unaligned_size, one_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   187
  EXPECT_TRUE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   188
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   189
  // Check that a missing bit beyond the end doesn't count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   190
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   191
    idx_t index = aligned_size - 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   192
    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   193
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   194
    WithBitClear wcb(aligned, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   195
    EXPECT_FALSE(aligned.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   196
    EXPECT_TRUE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   197
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   198
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   199
  // Check that a missing bit in the final partial word does count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   200
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   201
    WithBitClear wcb(x, unaligned_size - 1);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   202
    EXPECT_FALSE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   203
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   204
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   205
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   206
TEST(BitMap, is_empty__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   207
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   208
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   209
  BitMapView x = mx.make_view(unaligned_size, zero_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   210
  EXPECT_TRUE(x.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   211
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   212
  // Check that a set bit beyond the end doesn't count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   213
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   214
    idx_t index = aligned_size - 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   215
    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   216
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   217
    WithBitSet wbs(aligned, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   218
    EXPECT_FALSE(aligned.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   219
    EXPECT_TRUE(x.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   220
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   221
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   222
  // Check that a set bit in the final partial word does count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   223
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   224
    WithBitSet wbs(x, unaligned_size - 1);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   225
    EXPECT_FALSE(x.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   226
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   227
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   228
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   229
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   230
// bool contains(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   231
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   232
TEST(BitMap, contains__aligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   233
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   234
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   235
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   236
  BitMapView x = mx.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   237
  BitMapView y = my.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   238
  EXPECT_TRUE(x.contains(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   239
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   240
  WithBitClear wbc(x, aligned_size / 2);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   241
  EXPECT_FALSE(x.contains(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   242
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   243
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   244
TEST(BitMap, contains__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   245
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   246
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   247
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   248
  BitMapView x = mx.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   249
  BitMapView y = my.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   250
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   251
  // Check that a missing bit beyond the end of x doesn't count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   252
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   253
    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   254
    const idx_t index = aligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   255
    STATIC_ASSERT(unaligned_size <= index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   256
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   257
    WithBitClear wbc(aligned, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   258
    EXPECT_TRUE(x.contains(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   259
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   260
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   261
  // Check that a missing bit in the final partial word does count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   262
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   263
    idx_t index = unaligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   264
    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   265
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   266
    WithBitClear wbc(x, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   267
    EXPECT_FALSE(x.contains(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   268
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   269
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   270
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   271
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   272
// bool intersects(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   273
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   274
TEST(BitMap, intersects__aligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   275
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   276
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   277
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   278
  BitMapView x = mx.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   279
  BitMapView y = my.make_view(aligned_size, zero_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   280
  EXPECT_FALSE(x.intersects(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   281
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   282
  ASSERT_TRUE(x.at(aligned_size / 2));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   283
  WithBitSet wbs(y, aligned_size / 2);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   284
  EXPECT_TRUE(x.intersects(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   285
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   286
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   287
TEST(BitMap, intersects__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   288
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   289
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   290
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   291
  BitMapView x = mx.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   292
  BitMapView y = my.make_view(unaligned_size, zero_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   293
  EXPECT_FALSE(x.intersects(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   294
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   295
  // Check that adding a bit beyond the end of y doesn't count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   296
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   297
    BitMapView aligned_x = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   298
    BitMapView aligned_y = BitMapView(my.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   299
    const idx_t index = aligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   300
    STATIC_ASSERT(unaligned_size <= index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   301
    ASSERT_TRUE(aligned_x.at(index));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   302
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   303
    WithBitSet wbs(aligned_y, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   304
    EXPECT_FALSE(x.intersects(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   305
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   306
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   307
  // Check that adding a bit in the final partial word does count.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   308
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   309
    idx_t index = unaligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   310
    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   311
    ASSERT_TRUE(x.at(index));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   312
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   313
    WithBitSet wbs(y, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   314
    EXPECT_TRUE(x.intersects(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   315
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   316
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   317
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   318
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   319
// void set_from(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   320
// void set_union(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   321
// void set_difference(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   322
// void set_intersection(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   323
//
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   324
// bool set_union_with_result(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   325
// bool set_difference_with_result(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   326
// bool set_intersection_with_result(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   327
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   328
static void check_tail_unmodified(BitMapMemory& mem,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   329
                                  idx_t bits,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   330
                                  bm_word_t fill_word) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   331
  if (!BitMap::is_word_aligned(bits)) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   332
    idx_t last_word_bit_index = BitMap::word_align_down(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   333
    idx_t last_word_index = BitMap::calc_size_in_words(last_word_bit_index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   334
    bm_word_t last_word = mem.memory()[last_word_index];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   335
    idx_t shift = bits - last_word_bit_index;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   336
    EXPECT_EQ(fill_word >> shift, last_word >> shift);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   337
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   338
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   339
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   340
static void check_mod_setop(void (BitMap::*f)(const BitMap&),
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   341
                            idx_t bits,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   342
                            bm_word_t wx,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   343
                            bm_word_t wy,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   344
                            bm_word_t wexp) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   345
  BitMapMemory mx(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   346
  BitMapMemory my(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   347
  BitMapMemory mexp(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   348
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   349
  BitMapView x = mx.make_view(bits, wx);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   350
  BitMapView y = my.make_view(bits, wy);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   351
  BitMapView exp = mexp.make_view(bits, wexp);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   352
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   353
  (x.*f)(y);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   354
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   355
  EXPECT_TRUE(exp.is_same(x));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   356
  check_tail_unmodified(mx, bits, wx);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   357
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   358
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   359
static void check_mod_setop_with_result(bool (BitMap::*f)(const BitMap&),
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   360
                                        idx_t bits,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   361
                                        bm_word_t wx,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   362
                                        bm_word_t wy,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   363
                                        bm_word_t wexp) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   364
  BitMapMemory mx(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   365
  BitMapMemory my(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   366
  BitMapMemory mexp(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   367
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   368
  BitMapView x = mx.make_view(bits, wx);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   369
  BitMapView y = my.make_view(bits, wy);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   370
  BitMapView exp = mexp.make_view(bits, wexp);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   371
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   372
  bool value = (x.*f)(y);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   373
  EXPECT_EQ(value, wx != wexp);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   374
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   375
  EXPECT_TRUE(exp.is_same(x));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   376
  check_tail_unmodified(mx, bits, wx);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   377
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   378
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   379
#define CHECK_MOD_SETOP_AUX(checker, name, x, y, exp)   \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   380
  TEST(BitMap, name ## __ ## x ## _ ## y) {             \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   381
    checker(&BitMap::name, aligned_size,                \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   382
            x ## _bits, y ## _bits, exp ## _bits);      \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   383
    checker(&BitMap::name, unaligned_size,              \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   384
            x ## _bits, y ## _bits, exp ## _bits);      \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   385
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   386
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   387
#define CHECK_MOD_SETOP(name, x, y, exp) \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   388
  CHECK_MOD_SETOP_AUX(check_mod_setop, name, x, y, exp)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   389
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   390
#define CHECK_MOD_SETOP_WITH_RESULT(name, x, y, exp) \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   391
  CHECK_MOD_SETOP_AUX(check_mod_setop_with_result, name, x, y, exp)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   392
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   393
#define CHECK_MOD_SETOPS(name, x, y, exp)                       \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   394
  CHECK_MOD_SETOP(name, x, y, exp)                              \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   395
  CHECK_MOD_SETOP_WITH_RESULT(name ## _with_result, x, y, exp)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   396
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   397
CHECK_MOD_SETOP(set_from, even, even, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   398
CHECK_MOD_SETOP(set_from, even, odd, odd)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   399
CHECK_MOD_SETOP(set_from, even, one, one)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   400
CHECK_MOD_SETOP(set_from, even, zero, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   401
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   402
CHECK_MOD_SETOPS(set_union, even, even, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   403
CHECK_MOD_SETOPS(set_union, even, odd, one)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   404
CHECK_MOD_SETOPS(set_union, even, one, one)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   405
CHECK_MOD_SETOPS(set_union, even, zero, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   406
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   407
CHECK_MOD_SETOPS(set_difference, even, even, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   408
CHECK_MOD_SETOPS(set_difference, even, odd, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   409
CHECK_MOD_SETOPS(set_difference, even, one, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   410
CHECK_MOD_SETOPS(set_difference, even, zero, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   411
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   412
CHECK_MOD_SETOPS(set_intersection, even, even, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   413
CHECK_MOD_SETOPS(set_intersection, even, odd, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   414
CHECK_MOD_SETOPS(set_intersection, even, one, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   415
CHECK_MOD_SETOPS(set_intersection, even, zero, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   416