JE

 view release on metacpan or  search on metacpan

t/15.01.02-global-functions.t  view on Meta::CPAN

#!perl -T
do './t/jstest.pl' or die __DATA__

function is_nan(n){ // checks to see whether the number is *really* NaN
                    // & not something which converts to NaN when numified
	return n!=n
}

// ===================================================
// 15.1.2.1: eval
// 16 tests
// ===================================================

is(typeof eval, 'function', 'typeof eval');
is(Object.prototype.toString.apply(eval), '[object Function]',
	'class of eval')
ok(Function.prototype.isPrototypeOf(eval), 'eval\'s prototype')
$catched = false;
try{ new eval } catch(e) { $catched = e }
ok($catched, 'new eval fails')
ok(!('prototype' in eval), 'eval has no prototype property')
ok(eval.length === 1, 'eval.length')
ok(!eval.propertyIsEnumerable('length'), 'eval.length is not enumerable')
ok(!delete eval.length, 'eval.length cannot be deleted')
is((eval.length++, eval.length), 1, 'eval.length is read-only')

ok(eval('3+3; 4+4;') === 8,      'successful eval with return value')
ok(eval('var x')     === void 0, 'successful eval with no return value')

$catched = false;
try { eval('throw void 0') }
catch(phrase) {	phrase === undefined && ($catched = true) }
ok($catched, 'eval(\'throw\') (see whether errors propagate)')

$catched = false;
try { eval('Y@#%*^@#%*(^$') }
catch(phrase) {	phrase instanceof SyntaxError && ($catched = true) }
ok($catched, 'eval(invalid syntax)')

ok(eval(0) === 0, 'eval(number)')
ok(eval(new String('this isn\'t really a string')) instanceof String,
	'eval(new String)')

ok(eval() === undefined, 'argless eval()')

// ===================================================
// 15.1.2.2: parseInt
// ===================================================

// 10 tests (boilerplate stuff for built-ins)
is(typeof parseInt, 'function', 'typeof parseInt');
is(Object.prototype.toString.apply(parseInt), '[object Function]',
	'class of parseInt')
ok(Function.prototype.isPrototypeOf(parseInt), 'parseInt\'s prototype')
$catched = false;
try{ new parseInt } catch(e) { $catched = e }
ok($catched, 'new parseInt fails')
ok(!('prototype' in parseInt), 'parseInt has no prototype property')
ok(parseInt.length === 2, 'parseInt.length')
ok(!parseInt.propertyIsEnumerable('length'),
	'parseInt.length is not enumerable')
ok(!delete parseInt.length, 'parseInt.length cannot be deleted')
is((parseInt.length++, parseInt.length), 2, 'parseInt.length is read-only')
ok(is_nan(parseInt()), 'parseInt() w/o args')


bad_radices = [ // 14 elems
	2147483648,
	3000000000,
	4000000000.23,
	6442450944,
	6442450946.74,
	-1,
	-32.5,
	-5000000000,
	-4294967298.479,
	-6442450942,
	-6442450943.674,
	-6442450944,
	37,
	true,
]
nought_radices = [ // 11 elems
	undefined,
	null,
	false,
	'a',
	{},
	NaN,
	+0,
	-0,
	Infinity,
	-Infinity,
	4294967296,
]
// We’ll use '2' for 2 to test type conversion at the same time.
other_radices = [ // 34 useful elems
	0,0,0, // placeholders
	3,
	4.6,
	4294967301,     // 5
	4294967302.479, // 6
	-4294967289,    // 7
	-4294967288.23, // 8
	-8589934583,    // 9
	-8589934582.74, // 10
	11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
	30,31,32,33,34,35,36,
]
// There are 60 test radices altogether

/* parseInt(undefined) */
// 60 tests

for(i = 0; i< bad_radices.length; ++i)
	ok(is_nan(parseInt(undefined, bad_radices[i])),
		'parseInt(undefined, ' + bad_radices[i] + ')')
for(i = 0; i< nought_radices.length; ++i)
	ok(is_nan(parseInt(undefined, nought_radices[i])),
		'parseInt(undefined, ' + nought_radices[i] + ')')
ok(is_nan(parseInt(undefined, '2')), 'parseInt(undefined,"2")')
for(i = 3; i< 31; ++i) {
	ok(is_nan(parseInt(undefined, other_radices[i])),
		'parseInt(undefined, ' + other_radices[i] + ')')
}
ok(parseInt(void 0, 31) === 26231474015353, 'parseInt(undefined, 31)')
ok(parseInt(void 0, 32) === 33790067563981, 'parseInt(undefined, 32)')
ok(parseInt(void 0, 33) === 43189838176003, 'parseInt(undefined, 33)')
ok(parseInt(void 0, 34) === 54802593533125, 'parseInt(undefined, 34)')
ok(parseInt(void 0, 35) === 69060721616053, 'parseInt(undefined, 35)')
ok(parseInt(void 0, 36) === 86464843759093, 'parseInt(undefined, 36)')

/* parseInt(null) */
// 60 tests

for(i = 0; i< bad_radices.length; ++i)
	ok(is_nan(parseInt(null, bad_radices[i])),
		'parseInt(null, ' + bad_radices[i] + ')')
for(i = 0; i< nought_radices.length; ++i)
	ok(is_nan(parseInt(null, nought_radices[i])),
		'parseInt(null, ' + nought_radices[i] + ')')
ok(is_nan(parseInt(null, '2')), 'parseInt(null,"2")')
for(i = 3; i<=23; ++i) {
	ok(is_nan(parseInt(null, other_radices[i])),
		'parseInt(null, ' + other_radices[i] + ')')
}
for(i = 24; i< 31; ++i) {
	ok(parseInt(null, other_radices[i]) === 23,
		'parseInt(null, ' + other_radices[i] + ')')

t/15.01.02-global-functions.t  view on Meta::CPAN

for(i = 0; i< bad_radices.length; ++i)
	ok(is_nan(parseInt(str, bad_radices[i])),
		'parseInt("-10!", ' + bad_radices[i] + ')')
for(i = 0; i< nought_radices.length; ++i)
	ok(parseInt(str, nought_radices[i]) == -10,
		'parseInt("-10!", ' + nought_radices[i] + ')')
ok(parseInt(str, '2') == -2, 'parseInt("-10!","2")')
for(i = 3; i<=36; ++i) {
	ok(parseInt(str, other_radices[i]) == -i,
		'parseInt("-10!", ' + other_radices[i] + ')')
}

/* parseInt('-str') when str has 0X */
// 60 tests

str = '-0X10'

for(i = 0; i< bad_radices.length; ++i)
	ok(is_nan(parseInt(str, bad_radices[i])),
		'parseInt("-0X10", ' + bad_radices[i] + ')')
for(i = 0; i< nought_radices.length; ++i)
	ok(parseInt(str, nought_radices[i]) === -16,
		'parseInt("-0X10", ' + nought_radices[i] + ')')
ok(parseInt(str, '2') === 0, 'parseInt("-0X10","2")')
for(i = 3; i<=33; ++i) {
	ok(parseInt(str, other_radices[i]) === (i == 16 ? -16 : 0),
		'parseInt("-0X10", ' + other_radices[i] + ')')
}
ok(parseInt(str, 34) === -38182, 'parseInt("-0X10",34)')
ok(parseInt(str, 35) === -40460, 'parseInt("-0X10",35)')
ok(parseInt(str, 36) === -42804, 'parseInt("-0X10",36)')

/* parseInt('-str') — plain old integer */
// 60 tests

str = '-10';

for(i = 0; i< bad_radices.length; ++i)
	ok(is_nan(parseInt(str, bad_radices[i])),
		'parseInt("-10", ' + bad_radices[i] + ')')
for(i = 0; i< nought_radices.length; ++i)
	ok(parseInt(str, nought_radices[i]) == -10,
		'parseInt("-10", ' + nought_radices[i] + ')')
ok(parseInt(str, '2') == -2, 'parseInt("-10","2")')
for(i = 3; i<=36; ++i) {
	ok(parseInt(str, other_radices[i]) == -i,
		'parseInt("-10", ' + other_radices[i] + ')')
}

/* parseInt(object) */
// 1 test

ok(parseInt(new String("100"), 23) === 23*23, 'parseInt(object)') 

/* parseInt(surrogate) */
// 1 test

result = 0;
try{result = parseInt('\ud800')}
catch(e){result = e}
is(result, 'NaN', 'parseInt(surrogate)')

// ===================================================
// 15.1.2.3: parseFloat
// ===================================================

// 10 tests (boilerplate stuff for built-ins)
is(typeof parseFloat, 'function', 'typeof parseFloat');
is(Object.prototype.toString.apply(parseFloat), '[object Function]',
	'class of parseFloat')
ok(Function.prototype.isPrototypeOf(parseFloat), 'parseFloat\'s prototype')
$catched = false;
try{ new parseFloat } catch(e) { $catched = e }
ok($catched, 'new parseFloat fails')
ok(!('prototype' in parseFloat), 'parseFloat has no prototype property')
ok(parseFloat.length === 1, 'parseFloat.length')
ok(!parseFloat.propertyIsEnumerable('length'),
	'parseFloat.length is not enumerable')
ok(!delete parseFloat.length, 'parseFloat.length cannot be deleted')
is((parseFloat.length++, parseFloat.length), 1,
	'parseFloat.length is read-only')
ok(is_nan(parseFloat()), 'parseFloat() w/o args')

// 7 tests
ok(is_nan(parseFloat(undefined)), 'parseFloat(undefined)')
ok(is_nan(parseFloat(null     )), 'parseFloat(null)')
ok(is_nan(parseFloat(true     )), 'parseFloat(true)')
ok(is_nan(parseFloat(false    )), 'parseFloat(false)')
ok(parseFloat(0        ) === 0, 'parseFloat(0)')
ok(parseFloat(Infinity)===Infinity,  'parseFloat(Infinity)')
ok(is_nan(parseFloat({})),         'parseFloat({})')

// 685 tests (57 * 12 + 1)
pf_tests = [
	// num lit.  value     test name  
	['0',          0,      'digit 0'],
	['1',          1,      'digit 1'],
	['2',          2,      'digit 2'],
	['3',          3,      'digit 3'],
	['4',          4,      'digit 4'],
	['5',          5,      'digit 5'],
	['6',          6,      'digit 6'],
	['7',          7,      'digit 7'],
	['8',          8,      'digit 8'],
	['9',          9,      'digit 9'],
	['10',        10,      'multiple digits'],
	['100',      100,      'multiple digits'],
	['1000',    1000,      'multiple digits'],
	['10.5',      10.5,    'decimal point'],
	['10.',       10,      'trailing decimal point'],
	['0.7',         .7,    '"0." followed by digit'],
	['0.',         0,      '"0."'],
	['.6',          .6,    'leading decimal point'],
	['.6E6',       6e5,    'leading decimal point + E digit'],
	['.6E-6',      6e-7,   'leading decimal point + E-digit'],
	['.6E+6',      6e5,    'leading decimal point + E+digit'],
	['.6e6',       6e5,    'leading decimal point + e digit'],
	['.6e-6',      6e-7,   'leading decimal point + e-digit'],
	['.6e+6',      6e5,    'leading decimal point + e+digit'],
	['0E0',        0,      'integer with E'],
	['10E0',      10,      'integer with E'],

t/15.01.02-global-functions.t  view on Meta::CPAN

	['13E+2',   1300,      'integer with E+digit'],
	['13.E+2',  1300,      'trailing decimal point with E+digit'],
	['13.0E+2', 1300,      'decimal point with E+digit'],
	['13E-2',       .13,   'integer with E-digit'],
	['13.E-2',      .13,   'trailing decimal point with E-digit'],
	['13.0E-2',     .13,   'decimal point with E-digit'],
	['0.4e3',    400,      '0.digit with e digit'],
	['0.4e+3',   400,      '0.digit with e+digit'],
	['0.4e-3',      .0004, '0.digit with e-digit'],
	['0.4E3',    400,      '0.digit with E digit'],
	['0.4E+3',   400,      '0.digit with E+digit'],
	['0.4E-3',      .0004, '0.digit with E-digit'],
	['0.e3',       0,      '0. with e digit'],
	['0.e+3',      0,      '0. with e+digit'],
	['0.e-3',      0,      '0. with e-digit'],
	['0.E3',       0,      '0. with E digit'],
	['0.E+3',      0,      '0. with E+digit'],
	['0.E-3',      0,      '0. with E-digit'],
	['Infinity', Infinity, 'Infinity'],
]

for(var i = 0; i<pf_tests.length;++i)
	ok(parseFloat(pf_tests[i][0])===pf_tests[i][1],
		'parseFloat: ' + pf_tests[i][2]),
	ok(parseFloat('\t\f \u00a0\u2002' + pf_tests[i][0])
			===pf_tests[i][1],
		'parseFloat: ws ' + pf_tests[i][2]),
	ok(parseFloat(pf_tests[i][0] + '@!#@')===pf_tests[i][1],
		'parseFloat: ' + pf_tests[i][2] + ' + gibberish'),
	ok(parseFloat('\t\f \u00a0\u2002' + pf_tests[i][0] + '@!#@')
			===pf_tests[i][1],
		'parseFloat: ws ' + pf_tests[i][2] + ' + gibberish'),
	ok(parseFloat('+'+pf_tests[i][0])===pf_tests[i][1],
		'parseFloat: + ' + pf_tests[i][2]),
	ok(parseFloat('\t\f \u00a0\u2002+' + pf_tests[i][0])
			===pf_tests[i][1],
		'parseFloat: ws + ' + pf_tests[i][2]),
	ok(parseFloat('+'+pf_tests[i][0] + '@!#@')===pf_tests[i][1],
		'parseFloat: + ' + pf_tests[i][2] + ' + gibberish'),
	ok(parseFloat('\t\f \u00a0\u2002+' + pf_tests[i][0] + '@!#@')
			===pf_tests[i][1],
		'parseFloat: ws + ' + pf_tests[i][2] + ' + gibberish'),
	ok(parseFloat('-'+pf_tests[i][0])===-pf_tests[i][1],
		'parseFloat: + ' + pf_tests[i][2]),
	ok(parseFloat('\t\f \u00a0\u2002-' + pf_tests[i][0])
			===-pf_tests[i][1],
		'parseFloat: ws - ' + pf_tests[i][2]),
	ok(parseFloat('-'+pf_tests[i][0] + '@!#@')===-pf_tests[i][1],
		'parseFloat: - ' + pf_tests[i][2] + ' + gibberish'),
	ok(parseFloat('\t\f \u00a0\u2002-' + pf_tests[i][0] + '@!#@')
			===-pf_tests[i][1],
		'parseFloat: ws - ' + pf_tests[i][2] + ' + gibberish')
ok(is_nan(parseFloat('nonsens')), 'parseFloat(gibberish)')

/* parseInt(surrogate) */
// 1 test

result = 0;
try{result = parseFloat('\ud800')}
catch(e){result = e}
is(result, 'NaN', 'parseFloat(surrogate)')

// ===================================================
// 15.1.2.4: isNaN
// ===================================================

// 10 tests (boilerplate stuff for built-ins)
is(typeof isNaN, 'function', 'typeof isNaN');
is(Object.prototype.toString.apply(isNaN), '[object Function]',
	'class of isNaN')
ok(Function.prototype.isPrototypeOf(isNaN), 'isNaN\'s prototype')
$catched = false;
try{ new isNaN } catch(e) { $catched = e }
ok($catched, 'new isNaN fails')
ok(!('prototype' in isNaN), 'isNaN has no prototype property')
ok(isNaN.length === 1, 'isNaN.length')
ok(!isNaN.propertyIsEnumerable('length'),
	'isNaN.length is not enumerable')
ok(!delete isNaN.length, 'isNaN.length cannot be deleted')
is((isNaN.length++, isNaN.length), 1,
	'isNaN.length is read-only')
ok(isNaN() === true, 'isNaN() w/o args')

// 10 tests
ok(isNaN(undefined) === true, 'isNaN(undefined)')
ok(isNaN(null     ) === false, 'isNaN(null)')
ok(isNaN(true     ) === false, 'isNaN(true)')
ok(isNaN(false    ) === false, 'isNaN(false)')
ok(isNaN(0        ) === false, 'isNaN(0)')
ok(isNaN(Infinity)===false,  'isNaN(Infinity)')
ok(isNaN(NaN)===true,  'isNaN(NaN)')
ok(isNaN(' astring')===true,  'isNaN(string)')
ok(isNaN('33')===false,  'isNaN(numeric string)')
ok(isNaN({})===true,         'isNaN({})')

// ===================================================
// 15.1.2.5: isFinite
// ===================================================

// 10 tests (boilerplate stuff for built-ins)
is(typeof isFinite, 'function', 'typeof isFinite');
is(Object.prototype.toString.apply(isFinite), '[object Function]',
	'class of isFinite')
ok(Function.prototype.isPrototypeOf(isFinite), 'isFinite\'s prototype')
$catched = false;
try{ new isFinite } catch(e) { $catched = e }
ok($catched, 'new isFinite fails')
ok(!('prototype' in isFinite), 'isFinite has no prototype property')
ok(isFinite.length === 1, 'isFinite.length')
ok(!isFinite.propertyIsEnumerable('length'),
	'isFinite.length is not enumerable')
ok(!delete isFinite.length, 'isFinite.length cannot be deleted')
is((isFinite.length++, isFinite.length), 1,
	'isFinite.length is read-only')
ok(isFinite()===false, 'isFinite() w/o args')

// 11 tests
ok(isFinite(undefined) === false, 'isFinite(undefined)')
ok(isFinite(null     ) === true, 'isFinite(null)')
ok(isFinite(true     ) === true, 'isFinite(true)')
ok(isFinite(false    ) === true, 'isFinite(false)')
ok(isFinite(0        ) === true, 'isFinite(0)')
ok(isFinite(Infinity)===false,  'isFinite(Infinity)')
ok(isFinite(-Infinity)===false,  'isFinite(-Infinity)')
ok(isFinite(NaN)===false,  'isFinite(NaN)')
ok(isFinite(' astring')===false,  'isFinite(string)')
ok(isFinite('33')===true,  'isFinite(numeric string)')
ok(isFinite({})===false,         'isFinite({})')



( run in 0.476 second using v1.01-cache-2.11-cpan-804bf51f3ce )