1 /* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22 /** 23 File Name: 9.4-1.js 24 ECMA Section: 9.4 ToInteger 25 Description: 1. Call ToNumber on the input argument 26 2. If Result(1) is NaN, return +0 27 3. If Result(1) is +0, -0, Infinity, or -Infinity, 28 return Result(1). 29 4. Compute sign(Result(1)) * floor(abs(Result(1))). 30 5. Return Result(4). 31 32 To test ToInteger, this test uses new Date(value), 33 15.9.3.7. The Date constructor sets the [[Value]] 34 property of the new object to TimeClip(value), which 35 uses the rules: 36 37 TimeClip(time) 38 1. If time is not finite, return NaN 39 2. If abs(Result(1)) > 8.64e15, return NaN 40 3. Return an implementation dependent choice of either 41 ToInteger(Result(2)) or ToInteger(Result(2)) + (+0) 42 (Adding a positive 0 converts -0 to +0). 43 44 This tests ToInteger for values -8.64e15 > value > 8.64e15, 45 not including -0 and +0. 46 47 For additional special cases (0, +0, Infinity, -Infinity, 48 and NaN, see 9.4-2.js). For value is String, see 9.4-3.js. 49 50 Author: christine (at) netscape.com 51 Date: 10 july 1997 52 53 */ 54 var SECTION = "9.4-1"; 55 var VERSION = "ECMA_1"; 56 startTest(); 57 var TITLE = "ToInteger"; 58 59 writeHeaderToLog( SECTION + " "+ TITLE); 60 61 var testcases = getTestCases(); 62 test(); 63 64 function test() { 65 for ( tc=0; tc < testcases.length; tc++ ) { 66 testcases[tc].passed = writeTestCaseResult( 67 testcases[tc].expect, 68 testcases[tc].actual, 69 testcases[tc].description +" = "+ 70 testcases[tc].actual ); 71 72 73 testcases[tc].passed = writeTestCaseResult( 74 testcases[tc].expect, 75 testcases[tc].actual, 76 testcases[tc].description +" = "+ testcases[tc].actual ); 77 78 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 79 } 80 stopTest(); 81 return ( testcases ); 82 } 83 function getTestCases() { 84 var array = new Array(); 85 var item = 0; 86 87 // some special cases 88 89 array[item++] = new TestCase( SECTION, "td = new Date(Number.NaN); td.valueOf()", Number.NaN, eval("td = new Date(Number.NaN); td.valueOf()") ); 90 array[item++] = new TestCase( SECTION, "td = new Date(Infinity); td.valueOf()", Number.NaN, eval("td = new Date(Number.POSITIVE_INFINITY); td.valueOf()") ); 91 array[item++] = new TestCase( SECTION, "td = new Date(-Infinity); td.valueOf()", Number.NaN, eval("td = new Date(Number.NEGATIVE_INFINITY); td.valueOf()") ); 92 array[item++] = new TestCase( SECTION, "td = new Date(-0); td.valueOf()", -0, eval("td = new Date(-0); td.valueOf()" ) ); 93 array[item++] = new TestCase( SECTION, "td = new Date(0); td.valueOf()", 0, eval("td = new Date(0); td.valueOf()") ); 94 95 // value is not an integer 96 97 array[item++] = new TestCase( SECTION, "td = new Date(3.14159); td.valueOf()", 3, eval("td = new Date(3.14159); td.valueOf()") ); 98 array[item++] = new TestCase( SECTION, "td = new Date(Math.PI); td.valueOf()", 3, eval("td = new Date(Math.PI); td.valueOf()") ); 99 array[item++] = new TestCase( SECTION, "td = new Date(-Math.PI);td.valueOf()", -3, eval("td = new Date(-Math.PI);td.valueOf()") ); 100 array[item++] = new TestCase( SECTION, "td = new Date(3.14159e2); td.valueOf()", 314, eval("td = new Date(3.14159e2); td.valueOf()") ); 101 102 array[item++] = new TestCase( SECTION, "td = new Date(.692147e1); td.valueOf()", 6, eval("td = new Date(.692147e1);td.valueOf()") ); 103 array[item++] = new TestCase( SECTION, "td = new Date(-.692147e1);td.valueOf()", -6, eval("td = new Date(-.692147e1);td.valueOf()") ); 104 105 // value is not a number 106 107 array[item++] = new TestCase( SECTION, "td = new Date(true); td.valueOf()", 1, eval("td = new Date(true); td.valueOf()" ) ); 108 array[item++] = new TestCase( SECTION, "td = new Date(false); td.valueOf()", 0, eval("td = new Date(false); td.valueOf()") ); 109 110 array[item++] = new TestCase( SECTION, "td = new Date(new Number(Math.PI)); td.valueOf()", 3, eval("td = new Date(new Number(Math.PI)); td.valueOf()") ); 111 array[item++] = new TestCase( SECTION, "td = new Date(new Number(Math.PI)); td.valueOf()", 3, eval("td = new Date(new Number(Math.PI)); td.valueOf()") ); 112 113 // edge cases 114 array[item++] = new TestCase( SECTION, "td = new Date(8.64e15); td.valueOf()", 8.64e15, eval("td = new Date(8.64e15); td.valueOf()") ); 115 array[item++] = new TestCase( SECTION, "td = new Date(-8.64e15); td.valueOf()", -8.64e15, eval("td = new Date(-8.64e15); td.valueOf()") ); 116 array[item++] = new TestCase( SECTION, "td = new Date(8.64e-15); td.valueOf()", 0, eval("td = new Date(8.64e-15); td.valueOf()") ); 117 array[item++] = new TestCase( SECTION, "td = new Date(-8.64e-15); td.valueOf()", 0, eval("td = new Date(-8.64e-15); td.valueOf()") ); 118 119 return ( array ); 120 } 121