Skip to content

Commit

Permalink
implement BitSet.clear(bitIndex)
Browse files Browse the repository at this point in the history
  • Loading branch information
monmohan committed Nov 23, 2013
1 parent 4402fc7 commit 0d34344
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
14 changes: 10 additions & 4 deletions lib/BitSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
var actualIndex = bitIndex & 0X1F;
if (wordIndex < this._wordsUsed) {
this._words[wordIndex] |= 1 << actualIndex;
} else {
throw new Error('Attempt to set bit, ' + bitIndex +
' ,beyond the BitSet size, ' + this.size());
}

};

BitSet.prototype.get = function (bitIndex) {
Expand All @@ -36,6 +32,16 @@

};

BitSet.prototype.clear = function (bitIndex) {
var wordIndex = _getWordIndex(bitIndex);
var actualIndex = bitIndex & 0X1F;

if (wordIndex < this._wordsUsed) {
this._words[wordIndex] &= ~(1 << actualIndex);
}

};

BitSet.prototype.size = function () {
return this._wordsUsed * BITS_PER_WORD;
};
Expand Down
49 changes: 33 additions & 16 deletions tests/TestBitSet.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
var BitSet = require('../lib/BitSet.js'), assert = require('assert'), fs = require('fs');
(function(){
function testBasic(){
var bs=new BitSet();
bs.set(15);
assert.equal(bs.get(15),true,'setting bit failed');
assert.equal(bs.get(19),false,'wrong value set');
assert.equal(bs.size(),32,'wrong size');
(function () {
function testBasic() {
var bs = new BitSet();
bs.set(15);
assert.equal(bs.get(15), true, 'setting bit failed');
assert.equal(bs.get(19), false, 'wrong value set');
assert.equal(bs.size(), 32, 'wrong size');
assert.throws(function () {
bs.set(-1);
});
assert.equal(bs.get(19), false, 'wrong value set');
bs.clear(19);
assert.equal(bs.get(19), false, 'clearing problem');
bs.set(19);
assert.equal(bs.get(19), true, 'set after clear failed');

}

function testLargeBitSet(){
var bs=new BitSet(200);
function testLargeBitSet() {
var bs = new BitSet(200);
bs.set(156);
assert.equal(bs.get(156),true,'setting bit failed');
assert.equal(bs.get(19),false,'wrong value set');
assert.equal(bs.size(),224,'wrong size');
assert.equal(bs.get(156), true, 'setting bit failed');
bs.clear(156);
assert.equal(bs.get(156), false, 'clearing bit failed');

assert.equal(bs.size(), 224, 'wrong size');
bs.set(32);
assert.equal(bs.get(32),true,'setting bit failed');
assert.throws(function(){bs.set(235);});
assert.doesNotThrow(function(){bs.get(235);});
assert.equal(bs.get(235),false,'wrong value set');
assert.equal(bs.get(32), true, 'setting bit failed');
bs.clear(32);
assert.equal(bs.get(32), false, 'clearing bit failed');
assert.doesNotThrow(function () {
bs.set(235);
});
assert.doesNotThrow(function () {
bs.get(235);
});
assert.equal(bs.get(235), false, 'wrong value set');

}

testBasic();
testLargeBitSet();

Expand Down

0 comments on commit 0d34344

Please sign in to comment.