Created
October 11, 2012 16:10
-
-
Save EvanHahn/3873495 to your computer and use it in GitHub Desktop.
Testing the behavior of Object.is
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test my hypothesis that Object.is(a, b) is the same as a === b. | |
// Log an error if they're different. | |
var numTests = 0; | |
function test(a, b) { | |
if ((Object.is(a, b)) !== (a === b)) { | |
console.error('Object.is is different from === for ' + a + ' and ' + b); | |
} | |
numTests ++; | |
} | |
// Basic number tests | |
for (var i = -100; i <= 100; i += .1) { | |
for (var j = -100; j <= 100; j += .1) { | |
test(i, j); | |
} | |
} | |
// A function to generate random strings | |
function randomString() { | |
var result = ''; | |
var i = Math.floor(Math.random() * 100); | |
while (i --) { | |
var charCode = Math.floor(Math.random() * 255); | |
result += String.fromCharCode(charCode); | |
} | |
return result; | |
} | |
// Test the same string | |
for (var i = 0; i < 1000; i ++) { | |
var s = randomString(); | |
test(s, s); | |
} | |
// Test different strings | |
for (var i = 0; i < 1000; i ++) { | |
var a = randomString(); | |
var b = randomString(); | |
test(a, b); | |
} | |
// Test different String objects | |
for (var i = 0; i < 1000; i ++) { | |
var a = new String(randomString()); | |
var b = new String(randomString()); | |
test(a, b); | |
} | |
// Test booleans | |
test(true, true); | |
test(true, false); | |
test(false, true); | |
test(false, false); | |
test(new Boolean(true), new Boolean(true)); | |
test(new Boolean(true), new Boolean(false)); | |
test(new Boolean(false), new Boolean(true)); | |
test(new Boolean(false), new Boolean(false)); | |
// Test objects | |
test({}, {}); | |
test({ color: 'red' }, { color: 'red' }); | |
test({ color: 'red' }, { color: 'orange' }); | |
test({ tall: true }, { tall: false }); | |
test({ tall: true }, { tall: true }); | |
// Test arrays | |
test([], []); | |
test([5], [5]); | |
test([5], [12]); | |
// Test them weird types | |
var und = void 0; | |
var nan = 0 / 0; | |
var inf = 1 / 0; | |
test(und, und); | |
test(und, null); | |
test(und, nan); | |
test(und, inf); | |
test(null, und); | |
test(null, null); | |
test(null, nan); | |
test(null, inf); | |
test(nan, und); | |
test(nan, null); | |
test(nan, nan); | |
test(nan, inf); | |
test(inf, und); | |
test(inf, null); | |
test(inf, nan); | |
test(inf, inf); | |
// Test only one input | |
test(); | |
for (var i = -100; i <= 100; i += .1) | |
test(i); | |
test(true); | |
test(false); | |
test({}); | |
test([]); | |
test(und); | |
test(null); | |
test(nan); | |
test(inf); | |
// All done! | |
console.log('Ran ' + numTests + ' tests.'); | |
/* | |
See more about Object.is at this blog post: | |
<http://evanhahn.com/?p=1353> | |
by Evan Hahn (evanhahn.com) | |
************ | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment