1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
require "amq/bit_set"
# extracted from amqp gem. MK.
RSpec.describe AMQ::BitSet do
#
# Environment
#
let(:nbits) { (1 << 16) - 1 }
#
# Examples
#
describe "#new" do
it "has no bits set at the start" do
bs = AMQ::BitSet.new(128)
0.upto(127) do |i|
expect(bs[i]).to be_falsey
end
end # it
end # describe
describe "#word_index" do
subject do
described_class.new(nbits)
end
it "returns 0 when the word is between 0 and 63" do
expect(subject.word_index(0)).to eq(0)
expect(subject.word_index(63)).to eq(0)
end # it
it "returns 1 when the word is between 64 and 127" do
expect(subject.word_index(64)).to be(1)
expect(subject.word_index(127)).to be(1)
end # it
it "returns 2 when the word is between 128 and another number" do
expect(subject.word_index(128)).to be(2)
end # it
end # describe
describe "#get, #[]" do
describe "when bit at given position is set" do
subject do
o = described_class.new(nbits)
o.set(3)
o
end
it "returns true" do
expect(subject.get(3)).to be_truthy
end # it
end # describe
describe "when bit at given position is off" do
subject do
described_class.new(nbits)
end
it "returns false" do
expect(subject.get(5)).to be_falsey
end # it
end # describe
describe "when index out of range" do
subject do
described_class.new(nbits)
end
it "should raise IndexError for negative index" do
expect { subject.get(-1) }.to raise_error(IndexError)
end # it
it "should raise IndexError for index >= number of bits" do
expect { subject.get(nbits) }.to raise_error(IndexError)
end # it
end # describe
end # describe
describe "#set" do
describe "when bit at given position is set" do
subject do
described_class.new(nbits)
end
it "has no effect" do
subject.set(3)
expect(subject.get(3)).to be_truthy
subject.set(3)
expect(subject[3]).to be_truthy
end # it
end # describe
describe "when bit at given position is off" do
subject do
described_class.new(nbits)
end
it "sets that bit" do
subject.set(3)
expect(subject.get(3)).to be_truthy
subject.set(33)
expect(subject.get(33)).to be_truthy
subject.set(3387)
expect(subject.get(3387)).to be_truthy
end # it
end # describe
describe "when index out of range" do
subject do
described_class.new(nbits)
end
it "should raise IndexError for negative index" do
expect { subject.set(-1) }.to raise_error(IndexError)
end # it
it "should raise IndexError for index >= number of bits" do
expect { subject.set(nbits) }.to raise_error(IndexError)
end # it
end # describe
end # describe
describe "#unset" do
describe "when bit at a given position is set" do
subject do
described_class.new(nbits)
end
it "unsets that bit" do
subject.set(3)
expect(subject.get(3)).to be_truthy
subject.unset(3)
expect(subject.get(3)).to be_falsey
end # it
end # describe
describe "when bit at a given position is off" do
subject do
described_class.new(nbits)
end
it "has no effect" do
expect(subject.get(3)).to be_falsey
subject.unset(3)
expect(subject.get(3)).to be_falsey
end # it
end # describe
describe "when index out of range" do
subject do
described_class.new(nbits)
end
it "should raise IndexError for negative index" do
expect { subject.unset(-1) }.to raise_error(IndexError)
end # it
it "should raise IndexError for index >= number of bits" do
expect { subject.unset(nbits) }.to raise_error(IndexError)
end # it
end # describe
end # describe
describe "#clear" do
subject do
described_class.new(nbits)
end
it "clears all bits" do
subject.set(3)
expect(subject.get(3)).to be_truthy
subject.set(7668)
expect(subject.get(7668)).to be_truthy
subject.clear
expect(subject.get(3)).to be_falsey
expect(subject.get(7668)).to be_falsey
end # it
end # describe
describe "#number_of_trailing_ones" do
it "calculates them" do
expect(described_class.number_of_trailing_ones(0)).to eq(0)
expect(described_class.number_of_trailing_ones(1)).to eq(1)
expect(described_class.number_of_trailing_ones(2)).to eq(0)
expect(described_class.number_of_trailing_ones(3)).to eq(2)
expect(described_class.number_of_trailing_ones(4)).to eq(0)
end # it
end # describe
describe '#next_clear_bit' do
subject do
described_class.new(255)
end
it "returns sequential values when none have been returned" do
expect(subject.next_clear_bit).to eq(0)
subject.set(0)
expect(subject.next_clear_bit).to eq(1)
subject.set(1)
expect(subject.next_clear_bit).to eq(2)
subject.unset(1)
expect(subject.next_clear_bit).to eq(1)
end # it
it "returns the same number as long as nothing is set" do
expect(subject.next_clear_bit).to eq(0)
expect(subject.next_clear_bit).to eq(0)
end # it
it "handles more than 128 bits" do
0.upto(254) do |i|
subject.set(i)
expect(subject.next_clear_bit).to eq(i + 1)
end
subject.unset(254)
expect(subject.get(254)).to be_falsey
end # it
end # describe
end
|