Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
possibleAddresses → possibleSubnets, add tests
  • Loading branch information
beaugunderson committed Dec 13, 2014
1 parent 493af0e commit fb47caa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/ipv6.js
Expand Up @@ -215,13 +215,20 @@ v6.Address.prototype.mask = function (optionalMask) {
/*
* Returns the number of possible subnets of a given size in the address
*/
v6.Address.prototype.possibleAddresses = function (optionalSubnetSize) {
v6.Address.prototype.possibleSubnets = function (optionalSubnetSize) {
if (optionalSubnetSize === undefined) {
optionalSubnetSize = 0;
optionalSubnetSize = 128;
}

return addCommas(new BigInteger('2', 10).pow((v6.BITS - this.subnetMask) -
(v6.BITS - optionalSubnetSize)).toString(10));
var availableBits = v6.BITS - this.subnetMask;
var subnetBits = Math.abs(optionalSubnetSize - v6.BITS);
var subnetPowers = availableBits - subnetBits;

if (subnetPowers < 0) {
return '0';
}

return addCommas(new BigInteger('2', 10).pow(subnetPowers).toString(10));
};

/*
Expand Down
10 changes: 10 additions & 0 deletions test/functionality-v6-test.js
Expand Up @@ -98,6 +98,16 @@ describe('v6', function () {

topic.isInSubnet(same).should.equal(true);
});

it('calculates and formats the subnet size', function () {
topic.possibleSubnets().should.equal('18,446,744,073,709,551,616');
topic.possibleSubnets(128).should.equal('18,446,744,073,709,551,616');
topic.possibleSubnets(96).should.equal('4,294,967,296');
topic.possibleSubnets(65).should.equal('2');
topic.possibleSubnets(64).should.equal('1');
topic.possibleSubnets(63).should.equal('0');
topic.possibleSubnets(0).should.equal('0');
});
});

describe('Small subnets', function () {
Expand Down

0 comments on commit fb47caa

Please sign in to comment.