hotspot/test/native/utilities/test_bitMap_setops.cpp
author vtewari
Tue, 13 Sep 2016 17:00:06 +0530
changeset 40937 f72e61ec734b
parent 40639 83c879c8db57
child 41705 332239c052cc
permissions -rw-r--r--
8075484: SocketInputStream.socketRead0 can hang even with soTimeout set Reviewed-by: chegar, dsamersoff, msheppar, clanger
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    25
#include "precompiled.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    26
#include "utilities/bitMap.inline.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    27
#include "utilities/copy.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    28
#include "utilities/debug.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    29
#include "utilities/globalDefinitions.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    30
#include <stdlib.h>
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    31
#include "unittest.hpp"
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    32
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    33
typedef BitMap::idx_t idx_t;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    34
typedef BitMap::bm_word_t bm_word_t;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    35
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    36
class BitMapMemory {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    37
private:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    38
  idx_t _words;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    39
  bm_word_t* _memory;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    40
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    41
public:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    42
  BitMapMemory(idx_t bits) :
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    43
    _words(BitMap::calc_size_in_words(bits)),
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    44
    _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
    45
  { }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    46
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    47
  ~BitMapMemory() {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    48
    free(_memory);
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    51
  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
    52
    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
    53
    STATIC_ASSERT(sizeof(bm_word_t) == sizeof(HeapWord));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    54
    Copy::fill_to_aligned_words((HeapWord*)_memory, _words, value);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    55
    return BitMapView(_memory, bits);
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    58
  bm_word_t* memory() { return _memory; }
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    61
const idx_t aligned_size = 4 * BitsPerWord;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    62
const idx_t unaligned_size = aligned_size - (BitsPerWord / 2);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    63
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    64
static bm_word_t make_even_bits() {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    65
  bm_word_t result = 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    66
  while (true) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    67
    bm_word_t next = (result << 2) | 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    68
    if (next == result) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    69
      return result;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    70
    }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    71
    result = next;
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    75
const bm_word_t even_bits = make_even_bits();
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    76
const bm_word_t odd_bits = ~even_bits;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    77
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
    78
const bm_word_t zero_bits = 0;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    79
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    80
// Scoped set a clear bit and restore to clear.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    81
class WithBitSet {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    82
private:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    83
  BitMap& _bm;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    84
  idx_t _index;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    85
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    86
public:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    87
  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
    88
    // 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
    89
    EXPECT_FALSE(_bm.at(_index));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    90
    bm.set_bit(_index);
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    93
  ~WithBitSet() {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    94
    _bm.clear_bit(_index);
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    98
// Scoped clear a set bit and restore to set.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
    99
class WithBitClear {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   100
private:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   101
  BitMap& _bm;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   102
  idx_t _index;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   103
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   104
public:
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   105
  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
   106
    // 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
   107
    EXPECT_TRUE(_bm.at(_index));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   108
    bm.clear_bit(_index);
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   111
  ~WithBitClear() {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   112
    _bm.set_bit(_index);
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
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   117
// bool is_same(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   118
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   119
TEST(BitMap, is_same__aligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   120
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   121
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   122
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   123
  BitMapView x = mx.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   124
  BitMapView y = my.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   125
  EXPECT_TRUE(x.is_same(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   126
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   127
  WithBitClear wbc(x, aligned_size / 2);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   128
  EXPECT_FALSE(x.is_same(y));
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   131
TEST(BitMap, is_same__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   132
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   133
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   134
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   135
  BitMapView x = mx.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   136
  BitMapView y = my.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   137
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   138
  // 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
   139
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   140
    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   141
    const idx_t index = aligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   142
    STATIC_ASSERT(unaligned_size <= index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   143
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   144
    WithBitClear wbc(aligned, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   145
    EXPECT_TRUE(x.is_same(y));
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   148
  // 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
   149
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   150
    idx_t index = unaligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   151
    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   152
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   153
    WithBitClear wbc(y, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   154
    EXPECT_FALSE(x.is_same(y));
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
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   159
// bool is_full();
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   160
// bool is_empty();
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   161
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   162
TEST(BitMap, is_full_or_empty__aligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   163
  BitMapMemory mx(aligned_size);
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
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   166
    BitMapView x = mx.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   167
    EXPECT_FALSE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   168
    EXPECT_FALSE(x.is_empty());
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
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   172
    BitMapView x = mx.make_view(aligned_size, zero_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   173
    EXPECT_FALSE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   174
    EXPECT_TRUE(x.is_empty());
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
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   178
    BitMapView x = mx.make_view(aligned_size, one_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   179
    EXPECT_TRUE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   180
    EXPECT_FALSE(x.is_empty());
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   184
TEST(BitMap, is_full__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   185
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   186
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   187
  BitMapView x = mx.make_view(unaligned_size, one_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   188
  EXPECT_TRUE(x.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   189
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   190
  // 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
   191
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   192
    idx_t index = aligned_size - 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   193
    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   194
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   195
    WithBitClear wcb(aligned, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   196
    EXPECT_FALSE(aligned.is_full());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   197
    EXPECT_TRUE(x.is_full());
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   200
  // 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
   201
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   202
    WithBitClear wcb(x, unaligned_size - 1);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   203
    EXPECT_FALSE(x.is_full());
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   207
TEST(BitMap, is_empty__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   208
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   209
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   210
  BitMapView x = mx.make_view(unaligned_size, zero_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   211
  EXPECT_TRUE(x.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   212
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   213
  // 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
   214
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   215
    idx_t index = aligned_size - 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   216
    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   217
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   218
    WithBitSet wbs(aligned, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   219
    EXPECT_FALSE(aligned.is_empty());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   220
    EXPECT_TRUE(x.is_empty());
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   223
  // 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
   224
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   225
    WithBitSet wbs(x, unaligned_size - 1);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   226
    EXPECT_FALSE(x.is_empty());
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
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   231
// bool contains(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   232
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   233
TEST(BitMap, contains__aligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   234
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   235
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   236
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   237
  BitMapView x = mx.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   238
  BitMapView y = my.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   239
  EXPECT_TRUE(x.contains(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   240
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   241
  WithBitClear wbc(x, aligned_size / 2);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   242
  EXPECT_FALSE(x.contains(y));
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   245
TEST(BitMap, contains__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   246
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   247
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   248
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   249
  BitMapView x = mx.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   250
  BitMapView y = my.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   251
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   252
  // 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
   253
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   254
    BitMapView aligned = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   255
    const idx_t index = aligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   256
    STATIC_ASSERT(unaligned_size <= index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   257
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   258
    WithBitClear wbc(aligned, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   259
    EXPECT_TRUE(x.contains(y));
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   262
  // 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
   263
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   264
    idx_t index = unaligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   265
    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   266
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   267
    WithBitClear wbc(x, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   268
    EXPECT_FALSE(x.contains(y));
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
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   273
// bool intersects(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   274
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   275
TEST(BitMap, intersects__aligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   276
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   277
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   278
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   279
  BitMapView x = mx.make_view(aligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   280
  BitMapView y = my.make_view(aligned_size, zero_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   281
  EXPECT_FALSE(x.intersects(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   282
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   283
  ASSERT_TRUE(x.at(aligned_size / 2));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   284
  WithBitSet wbs(y, aligned_size / 2);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   285
  EXPECT_TRUE(x.intersects(y));
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   288
TEST(BitMap, intersects__unaligned) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   289
  BitMapMemory mx(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   290
  BitMapMemory my(aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   291
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   292
  BitMapView x = mx.make_view(unaligned_size, even_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   293
  BitMapView y = my.make_view(unaligned_size, zero_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   294
  EXPECT_FALSE(x.intersects(y));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   295
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   296
  // 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
   297
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   298
    BitMapView aligned_x = BitMapView(mx.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   299
    BitMapView aligned_y = BitMapView(my.memory(), aligned_size);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   300
    const idx_t index = aligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   301
    STATIC_ASSERT(unaligned_size <= index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   302
    ASSERT_TRUE(aligned_x.at(index));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   303
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   304
    WithBitSet wbs(aligned_y, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   305
    EXPECT_FALSE(x.intersects(y));
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   308
  // 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
   309
  {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   310
    idx_t index = unaligned_size - 2;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   311
    ASSERT_LE(BitMap::word_align_down(unaligned_size), index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   312
    ASSERT_TRUE(x.at(index));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   313
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   314
    WithBitSet wbs(y, index);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   315
    EXPECT_TRUE(x.intersects(y));
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
//////////////////////////////////////////////////////////////////////////////
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   320
// void set_from(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   321
// void set_union(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   322
// void set_difference(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   323
// void set_intersection(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   324
//
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   325
// bool set_union_with_result(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   326
// bool set_difference_with_result(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   327
// bool set_intersection_with_result(const BitMap& bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   328
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   329
static void check_tail_unmodified(BitMapMemory& mem,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   330
                                  idx_t bits,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   331
                                  bm_word_t fill_word) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   332
  if (!BitMap::is_word_aligned(bits)) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   333
    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
   334
    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
   335
    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
   336
    idx_t shift = bits - last_word_bit_index;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   337
    EXPECT_EQ(fill_word >> shift, last_word >> shift);
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   341
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
   342
                            idx_t bits,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   343
                            bm_word_t wx,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   344
                            bm_word_t wy,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   345
                            bm_word_t wexp) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   346
  BitMapMemory mx(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   347
  BitMapMemory my(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   348
  BitMapMemory mexp(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   349
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   350
  BitMapView x = mx.make_view(bits, wx);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   351
  BitMapView y = my.make_view(bits, wy);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   352
  BitMapView exp = mexp.make_view(bits, wexp);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   353
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   354
  (x.*f)(y);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   355
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   356
  EXPECT_TRUE(exp.is_same(x));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   357
  check_tail_unmodified(mx, bits, wx);
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   360
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
   361
                                        idx_t bits,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   362
                                        bm_word_t wx,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   363
                                        bm_word_t wy,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   364
                                        bm_word_t wexp) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   365
  BitMapMemory mx(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   366
  BitMapMemory my(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   367
  BitMapMemory mexp(bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   368
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   369
  BitMapView x = mx.make_view(bits, wx);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   370
  BitMapView y = my.make_view(bits, wy);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   371
  BitMapView exp = mexp.make_view(bits, wexp);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   372
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   373
  bool value = (x.*f)(y);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   374
  EXPECT_EQ(value, wx != wexp);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   375
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   376
  EXPECT_TRUE(exp.is_same(x));
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   377
  check_tail_unmodified(mx, bits, wx);
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   380
#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
   381
  TEST(BitMap, name ## __ ## x ## _ ## y) {             \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   382
    checker(&BitMap::name, aligned_size,                \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   383
            x ## _bits, y ## _bits, exp ## _bits);      \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   384
    checker(&BitMap::name, unaligned_size,              \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   385
            x ## _bits, y ## _bits, exp ## _bits);      \
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
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   388
#define CHECK_MOD_SETOP(name, x, y, exp) \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   389
  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
   390
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   391
#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
   392
  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
   393
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   394
#define CHECK_MOD_SETOPS(name, x, y, exp)                       \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   395
  CHECK_MOD_SETOP(name, x, y, exp)                              \
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   396
  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
   397
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   398
CHECK_MOD_SETOP(set_from, even, even, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   399
CHECK_MOD_SETOP(set_from, even, odd, odd)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   400
CHECK_MOD_SETOP(set_from, even, one, one)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   401
CHECK_MOD_SETOP(set_from, even, zero, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   402
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   403
CHECK_MOD_SETOPS(set_union, even, even, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   404
CHECK_MOD_SETOPS(set_union, even, odd, one)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   405
CHECK_MOD_SETOPS(set_union, even, one, one)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   406
CHECK_MOD_SETOPS(set_union, even, zero, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   407
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   408
CHECK_MOD_SETOPS(set_difference, even, even, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   409
CHECK_MOD_SETOPS(set_difference, even, odd, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   410
CHECK_MOD_SETOPS(set_difference, even, one, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   411
CHECK_MOD_SETOPS(set_difference, even, zero, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   412
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   413
CHECK_MOD_SETOPS(set_intersection, even, even, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   414
CHECK_MOD_SETOPS(set_intersection, even, odd, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   415
CHECK_MOD_SETOPS(set_intersection, even, one, even)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   416
CHECK_MOD_SETOPS(set_intersection, even, zero, zero)
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents:
diff changeset
   417