Add ol.math.roundUpToPowerOfTwo
This commit is contained in:
@@ -29,6 +29,16 @@ ol.math.csch = function(x) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} x X.
|
||||
* @return {number} The smallest power of two greater than or equal to x.
|
||||
*/
|
||||
ol.math.roundUpToPowerOfTwo = function(x) {
|
||||
goog.asserts.assert(0 < x);
|
||||
return Math.pow(2, Math.ceil(Math.log(x) / Math.log(2)));
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} x X.
|
||||
* @return {number} Hyperbolic secant of x.
|
||||
|
||||
@@ -64,6 +64,46 @@ describe('ol.math.csch', function() {
|
||||
});
|
||||
|
||||
|
||||
describe('ol.math.roundUpToPowerOfTwo', function() {
|
||||
|
||||
it('raises an exception when x is negative', function() {
|
||||
expect(function() {
|
||||
ol.math.roundUpToPowerOfTwo(-1);
|
||||
}).to.throwException();
|
||||
});
|
||||
|
||||
it('raises an exception when x is zero', function() {
|
||||
expect(function() {
|
||||
ol.math.roundUpToPowerOfTwo(0);
|
||||
}).to.throwException();
|
||||
});
|
||||
|
||||
it('returns the expected value for simple powers of two', function() {
|
||||
expect(ol.math.roundUpToPowerOfTwo(1)).to.be(1);
|
||||
expect(ol.math.roundUpToPowerOfTwo(2)).to.be(2);
|
||||
expect(ol.math.roundUpToPowerOfTwo(4)).to.be(4);
|
||||
expect(ol.math.roundUpToPowerOfTwo(8)).to.be(8);
|
||||
expect(ol.math.roundUpToPowerOfTwo(16)).to.be(16);
|
||||
expect(ol.math.roundUpToPowerOfTwo(32)).to.be(32);
|
||||
expect(ol.math.roundUpToPowerOfTwo(64)).to.be(64);
|
||||
expect(ol.math.roundUpToPowerOfTwo(128)).to.be(128);
|
||||
expect(ol.math.roundUpToPowerOfTwo(256)).to.be(256);
|
||||
});
|
||||
|
||||
it('returns the expected value for simple powers of ten', function() {
|
||||
expect(ol.math.roundUpToPowerOfTwo(1)).to.be(1);
|
||||
expect(ol.math.roundUpToPowerOfTwo(10)).to.be(16);
|
||||
expect(ol.math.roundUpToPowerOfTwo(100)).to.be(128);
|
||||
expect(ol.math.roundUpToPowerOfTwo(1000)).to.be(1024);
|
||||
expect(ol.math.roundUpToPowerOfTwo(10000)).to.be(16384);
|
||||
expect(ol.math.roundUpToPowerOfTwo(100000)).to.be(131072);
|
||||
expect(ol.math.roundUpToPowerOfTwo(1000000)).to.be(1048576);
|
||||
expect(ol.math.roundUpToPowerOfTwo(10000000)).to.be(16777216);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('ol.math.sech', function() {
|
||||
|
||||
it('returns the correct value at -Infinity', function() {
|
||||
|
||||
Reference in New Issue
Block a user