Skip to content

Commit

Permalink
Fixing Math.clz32 specs and implementation. Fixes #269.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 5, 2014
1 parent 696916b commit 687d1a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
1 change: 0 additions & 1 deletion es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,6 @@
clz32: function(value) {
// See https://bugs.ecmascript.org/show_bug.cgi?id=2465
value = Number(value);
if (Number.isNaN(value)) return NaN;
var number = ES.ToUint32(value);
if (number === 0) {
return 32;
Expand Down
35 changes: 22 additions & 13 deletions test/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,48 @@ describe('Math', function() {
describe('.clz32()', function() {
it('should have proper uint32 conversion', function() {
var integers = [5295, -5295, -9007199254740991, 9007199254740991, 0, -0];
var infinities = [Infinity, -Infinity];
var nonNumbers = [undefined, true, null, {}, [], 'str'];
var nonIntegers = [-9007199254741992, 9007199254741992, 5.9];

infinities.forEach(function(item) {
expect(Math.clz32(item)).to.equal(32);
});
integers.forEach(function(item) {
expect(Math.clz32(item)).to.be.within(0, 32);
});
nonIntegers.forEach(function(item) {
expect(Math.clz32(item)).to.be.within(0, 32);
});
expect(Number.isNaN(Math.clz32(undefined))).to.be.ok;
expect(Math.clz32(null)).to.equal(Math.clz32(0));
expect(Math.clz32(false)).to.equal(Math.clz32(0));
expect(Math.clz32(true)).to.equal(Math.clz32(1));
expect(Math.clz32('')).to.equal(Math.clz32(0));
expect(Math.clz32('10')).to.equal(Math.clz32(10));
expect(Math.clz32([])).to.equal(Math.clz32(''));
expect(Number.isNaN(Math.clz32('str'))).to.be.ok;
expect(Number.isNaN(Math.clz32({}))).to.be.ok;
expect(Number.isNaN(Math.clz32(0/0))).to.be.ok;
expect(Math.clz32(0x100000000)).to.equal(32);
expect(Math.clz32(0.1)).to.equal(32);
expect(Math.clz32(-1)).to.equal(0);
expect(Math.clz32(0)).to.equal(32);
expect(Math.clz32(1)).to.equal(31);
expect(Math.clz32(0xFFFFFFFF)).to.equal(0);
expect(Math.clz32(0x1FFFFFFFF)).to.equal(0);
expect(Math.clz32(0x111111111)).to.equal(3);
expect(Math.clz32(0x11111111)).to.equal(3);
});

it('returns 32 for numbers that coerce to 0', function () {
var zeroishes = [
0,
-0,
NaN,
Infinity,
-Infinity,
0x100000000,
undefined,
null,
false,
'',
'str',
{},
[],
[1, 2]
];
zeroishes.forEach(function (zeroish) {
expect(Math.clz32(zeroish)).to.equal(32);
});
});
});

describe('#cosh()', function() {
Expand Down

0 comments on commit 687d1a4

Please sign in to comment.