]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_3/Date/shell.js
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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
23 * JavaScript shared functions file for running the tests in either
24 * stand-alone JavaScript engine. To run a test, first load this file,
25 * then load the test script.
28 var completed
= false;
39 var GLOBAL
= "[object global]";
40 var PASSED
= " PASSED!"
41 var FAILED
= " FAILED! expected: ";
46 * Wrapper for test case constructor that doesn't require the SECTION argument.
48 function AddTestCase( description
, expect
, actual
)
50 testcases
[tc
++] = new TestCase( SECTION
, description
, expect
, actual
);
55 * TestCase constructor
57 function TestCase( n
, d
, e
, a
)
65 this.bugnumber
= BUGNUMBER
;
66 this.passed
= getTestCaseResult( this.expect
, this.actual
);
67 if ( DEBUG
) {writeLineToLog("added " + this.description
);}
72 * Set up test environment.
78 // JavaScript 1.3 is supposed to be compliant ECMA version 1.0
79 if (VERSION
== "ECMA_1" ) {version ("130");}
80 if (VERSION
== "JS_1.3" ) {version ( "130");}
81 if (VERSION
== "JS_1.2" ) {version ( "120");}
82 if (VERSION
== "JS_1.1" ) {version( "110");}
84 // for ECMA version 2.0, we will leave the JavaScript version
85 // to the default ( for now ).
88 // print out bugnumber
91 writeLineToLog ("BUGNUMBER: " + BUGNUMBER
);
94 testcases
= new Array();
101 for ( tc
=0; tc
< testcases
.length
; tc
++ )
103 testcases
[tc
].passed
= writeTestCaseResult(
104 testcases
[tc
].expect
,
105 testcases
[tc
].actual
,
106 testcases
[tc
].description
+" = "+ testcases
[tc
].actual
);
108 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
112 return ( testcases
);
117 * Compare expected result to the actual result and figure out whether
118 * the test case passed.
120 function getTestCaseResult(expect
, actual
)
122 //because ( NaN == NaN ) always returns false, need to do
123 //a special compare to see if we got the right result.
124 if ( actual
!= actual
)
126 if ( typeof actual
== "object" ) {actual
= "NaN object";}
127 else {actual
= "NaN number";}
130 if ( expect
!= expect
)
132 if ( typeof expect
== "object" ) {expect
= "NaN object";}
133 else {expect
= "NaN number";}
136 var passed
= ( expect
== actual
) ? true : false;
138 // if both objects are numbers, need to replace w/ IEEE standard for rounding
139 if ( !passed
&& typeof(actual
) == "number" && typeof(expect
) == "number" )
141 if ( Math
.abs(actual
-expect
) < 0.0000001 ) {passed
= true;}
144 //verify type is the same
145 if ( typeof(expect
) != typeof(actual
) ) {passed
= false;}
152 * Begin printing functions. These functions use the shell's print function.
153 * When running tests in the browser, override these functions with functions
154 * that use document.write.
156 function writeTestCaseResult( expect
, actual
, string
)
158 var passed
= getTestCaseResult(expect
, actual
);
159 writeFormattedResult( expect
, actual
, string
, passed
);
164 function writeFormattedResult( expect
, actual
, string
, passed
)
167 s
+= ( passed
) ? PASSED : FAILED
+ expect
;
173 function writeLineToLog( string
)
179 function writeHeaderToLog( string
)
183 /* End of printing functions */
187 * When running in the shell, run the garbage collector after the test has completed.
192 if ( gc
!= undefined )
200 * Convenience function for displaying failed test cases.
201 * Useful when running tests manually.
203 function getFailedCases()
205 for (var i
= 0; i
< testcases
.length
; i
++ )
207 if ( !testcases
[i
].passed
)
209 print( testcases
[i
].description
+ " = " + testcases
[i
].actual
+ " expected: " + testcases
[i
].expect
);
216 * Date constants and functions used by tests in Date suite
218 var msPerDay
= 86400000;
219 var HoursPerDay
= 24;
220 var MinutesPerHour
= 60;
221 var SecondsPerMinute
= 60;
222 var msPerSecond
= 1000;
223 var msPerMinute
= 60000; // msPerSecond * SecondsPerMinute
224 var msPerHour
= 3600000; // msPerMinute * MinutesPerHour
225 var TZ_DIFF
= getTimeZoneDiff();
226 var TZ_ADJUST
= TZ_DIFF
* msPerHour
;
228 var TIME_2000
= 946684800000;
229 var TIME_1900
= -2208988800000;
230 var UTC_29_FEB_2000
= TIME_2000
+ 31*msPerDay
+ 28*msPerDay
;
231 var UTC_1_JAN_2005
= TIME_2000
+ TimeInYear(2000) + TimeInYear(2001) +
232 TimeInYear(2002) + TimeInYear(2003) + TimeInYear(2004);
233 var now
= new Date();
234 var TIME_NOW
= now
.valueOf(); //valueOf() is to accurate to the millisecond
235 //Date.parse() is accurate only to the second
240 * Originally, the test suite used a hard-coded value TZ_DIFF = -8.
241 * But that was only valid for testers in the Pacific Standard Time Zone!
242 * We calculate the proper number dynamically for any tester. We just
243 * have to be careful to use a date not subject to Daylight Savings Time...
245 function getTimeZoneDiff()
247 return -((new Date(2000, 1, 1)).getTimezoneOffset())/60;
253 return ( Math
.floor( t
/msPerDay
) );
257 function DaysInYear( y
)
259 if ( y
% 4 != 0 ) {return 365;}
261 if ( (y
%4 == 0) && (y
%100 != 0) ) {return 366;}
263 if ( (y
%100 == 0) && (y
%400 != 0) ) {return 365;}
265 if ( (y
%400 == 0)){return 366;}
266 else {return "ERROR: DaysInYear(" + y
+ ") case not covered";}
270 function TimeInYear( y
)
272 return ( DaysInYear(y
) * msPerDay
);
276 function DayNumber( t
)
278 return ( Math
.floor( t
/ msPerDay
) );
282 function TimeWithinDay( t
)
284 if ( t
< 0 ) {return ( (t
%msPerDay
) + msPerDay
);}
285 else {return ( t
% msPerDay
);}
289 function YearNumber( t
)
294 function TimeFromYear( y
)
296 return ( msPerDay
* DayFromYear(y
) );
300 function DayFromYear( y
)
302 return ( 365*(y
-1970) + Math
.floor((y
-1969)/4) - Math
.floor((y
-1901)/100)
303 + Math
.floor((y
-1601)/400) );
307 function InLeapYear( t
)
309 if ( DaysInYear(YearFromTime(t
)) == 365 ) {return 0;}
311 if ( DaysInYear(YearFromTime(t
)) == 366 ) {return 1;}
312 else {return "ERROR: InLeapYear(" + t
+ ") case not covered";}
316 function YearFromTime( t
)
319 var sign
= ( t
< 0 ) ? -1 : 1;
320 var year
= ( sign
< 0 ) ? 1969 : 1970;
322 for (var timeToTimeZero
= t
; ; )
324 // subtract the current year's time from the time that's left.
325 timeToTimeZero
-= sign
* TimeInYear(year
)
327 // if there's less than the current year's worth of time left, then break.
330 if ( sign
* timeToTimeZero
<= 0 ) {break;}
335 if ( sign
* timeToTimeZero
< 0 ) {break;}
344 function MonthFromTime( t
)
346 var day
= DayWithinYear( t
);
347 var leap
= InLeapYear(t
);
349 // I know I could use switch but I'd rather not until it's part of ECMA
350 if ( (0 <= day
) && (day
< 31) ) {return 0;}
351 if ( (31 <= day
) && (day
< (59+leap
) )) {return 1;}
352 if ( ((59+leap
) <= day
) && (day
< (90+leap
) )) {return 2;}
353 if ( ((90+leap
) <= day
) && (day
< (120+leap
) )) {return 3;}
354 if ( ((120+leap
) <= day
) && (day
< (151+leap
) )) {return 4;}
355 if ( ((151+leap
) <= day
) && (day
< (181+leap
) )) {return 5;}
356 if ( ((181+leap
) <= day
) && (day
< (212+leap
) )) {return 6;}
357 if ( ((212+leap
) <= day
) && (day
< (243+leap
)) ) {return 7;}
358 if ( ((243+leap
) <= day
) && (day
< (273+leap
) )) {return 8;}
359 if ( ((273+leap
) <= day
) && (day
< (304+leap
)) ) {return 9;}
360 if ( ((304+leap
) <= day
) && (day
< (334+leap
)) ) {return 10;}
361 if ( ((334+leap
) <= day
) && (day
< (365+leap
)) ) {return 11;}
362 else {return "ERROR: MonthFromTime(" + t
+ ") not known";}
366 function DayWithinYear( t
)
368 return(Day(t
) - DayFromYear(YearFromTime(t
)) );
372 function DateFromTime( t
)
374 var day
= DayWithinYear(t
);
375 var month
= MonthFromTime(t
);
377 if ( month
== 0) {return ( day
+ 1 );}
378 if ( month
== 1) {return ( day
- 30 );}
379 if ( month
== 2) {return ( day
- 58 - InLeapYear(t
) );}
380 if ( month
== 3) {return ( day
- 89 - InLeapYear(t
));}
381 if ( month
== 4) {return ( day
- 119 - InLeapYear(t
));}
382 if ( month
== 5) {return ( day
- 150 - InLeapYear(t
));}
383 if ( month
== 6) {return ( day
- 180 - InLeapYear(t
));}
384 if ( month
== 7) {return ( day
- 211 - InLeapYear(t
));}
385 if ( month
== 8) {return ( day
- 242 - InLeapYear(t
));}
386 if ( month
== 9) {return ( day
- 272 - InLeapYear(t
));}
387 if ( month
== 10) {return ( day
- 303 - InLeapYear(t
));}
388 if ( month
== 11) {return ( day
- 333 - InLeapYear(t
));}
389 return ("ERROR: DateFromTime("+t
+") not known" );
393 function WeekDay( t
)
395 var weekday
= (Day(t
)+4)%7;
396 return( weekday
< 0 ? 7+weekday : weekday
);
400 // missing daylight savings time adjustment
403 function HourFromTime( t
)
405 var h
= Math
.floor( t
/ msPerHour
)%HoursPerDay
;
406 return ( (h
<0) ? HoursPerDay
+ h : h
);
410 function MinFromTime( t
)
412 var min
= Math
.floor( t
/ msPerMinute
)%MinutesPerHour
;
413 return( (min
< 0 ) ? MinutesPerHour
+ min : min
);
417 function SecFromTime( t
)
419 var sec
= Math
.floor( t
/ msPerSecond
)%SecondsPerMinute
;
420 return ( (sec
< 0 ) ? SecondsPerMinute
+ sec : sec
);
424 function msFromTime( t
)
426 var ms
= t
%msPerSecond
;
427 return ( (ms
< 0 ) ? msPerSecond
+ ms : ms
);
433 return ( TZ_DIFF
* msPerHour
);
439 return ( t
- LocalTZA() - DaylightSavingTA(t
- LocalTZA()) );
443 function DaylightSavingTA( t
)
447 var dst_start
= GetSecondSundayInMarch(t
) + 2*msPerHour
;
448 var dst_end
= GetFirstSundayInNovember(t
) + 2*msPerHour
;
450 if ( t
>= dst_start
&& t
< dst_end
) {return msPerHour
;}
453 // Daylight Savings Time starts on the first Sunday in April at 2:00AM in PST.
454 // Other time zones will need to override this function.
456 print( new Date( UTC(dst_start
+ LocalTZA())) );
457 return UTC(dst_start
+ LocalTZA());
460 function GetFirstSundayInApril( t
) {
461 var year
= YearFromTime(t
);
462 var leap
= InLeapYear(t
);
464 var april
= TimeFromYear(year
) + TimeInMonth(0, leap
) + TimeInMonth(1,leap
) +
467 for ( var first_sunday
= april
; WeekDay(first_sunday
) > 0;
468 first_sunday
+= msPerDay
)
475 function GetLastSundayInOctober( t
) {
476 var year
= YearFromTime(t
);
477 var leap
= InLeapYear(t
);
479 for ( var oct
= TimeFromYear(year
), m
= 0; m
< 9; m
++ ) {
480 oct
+= TimeInMonth(m
, leap
);
482 for ( var last_sunday
= oct
+ 30*msPerDay
; WeekDay(last_sunday
) > 0;
483 last_sunday
-= msPerDay
)
490 // Added these two functions because DST rules changed for the US.
491 function GetSecondSundayInMarch( t
) {
492 var year
= YearFromTime(t
);
493 var leap
= InLeapYear(t
);
495 var march
= TimeFromYear(year
) + TimeInMonth(0, leap
) + TimeInMonth(1,leap
);
499 for ( var second_sunday
= march
; flag
; second_sunday
+= msPerDay
)
501 if (WeekDay(second_sunday
) == 0) {
502 if(++sundayCount
== 2)
507 return second_sunday
;
509 function GetFirstSundayInNovember( t
) {
510 var year
= YearFromTime(t
);
511 var leap
= InLeapYear(t
);
513 for ( var nov
= TimeFromYear(year
), m
= 0; m
< 10; m
++ ) {
514 nov
+= TimeInMonth(m
, leap
);
516 for ( var first_sunday
= nov
; WeekDay(first_sunday
) > 0;
517 first_sunday
+= msPerDay
)
525 function LocalTime( t
)
527 return ( t
+ LocalTZA() + DaylightSavingTA(t
) );
531 function MakeTime( hour
, min
, sec
, ms
)
533 if ( isNaN(hour
) || isNaN(min
) || isNaN(sec
) || isNaN(ms
) ){return Number
.NaN
;}
535 hour
= ToInteger(hour
);
536 min
= ToInteger( min
);
537 sec
= ToInteger( sec
);
538 ms
= ToInteger( ms
);
540 return( (hour
*msPerHour
) + (min
*msPerMinute
) + (sec
*msPerSecond
) + ms
);
544 function MakeDay( year
, month
, date
)
546 if ( isNaN(year
) || isNaN(month
) || isNaN(date
)) {return Number
.NaN
;}
548 year
= ToInteger(year
);
549 month
= ToInteger(month
);
550 date
= ToInteger(date
);
552 var sign
= ( year
< 1970 ) ? -1 : 1;
553 var t
= ( year
< 1970 ) ? 1 : 0;
554 var y
= ( year
< 1970 ) ? 1969 : 1970;
556 var result5
= year
+ Math
.floor( month
/12 );
557 var result6
= month
%12;
561 for ( y
= 1969; y
>= year
; y
+= sign
)
563 t
+= sign
* TimeInYear(y
);
568 for ( y
= 1970 ; y
< year
; y
+= sign
)
570 t
+= sign
* TimeInYear(y
);
574 var leap
= InLeapYear( t
);
576 for ( var m
= 0; m
< month
; m
++)
578 t
+= TimeInMonth( m
, leap
);
581 if ( YearFromTime(t
) != result5
) {return Number
.NaN
;}
582 if ( MonthFromTime(t
) != result6
) {return Number
.NaN
;}
583 if ( DateFromTime(t
) != 1 ){return Number
.NaN
;}
585 return ( (Day(t
)) + date
- 1 );
589 function TimeInMonth( month
, leap
)
591 // Jan 0 Feb 1 Mar 2 Apr 3 May 4 June 5 Jul 6 Aug 7 Sep 8 Oct 9 Nov 10 Dec11
593 // April June September November
594 if ( month
== 3 || month
== 5 || month
== 8 || month
== 10 ) {return ( 30*msPerDay
);}
597 if ( month
== 0 || month
== 2 || month
== 4 || month
== 6 ||
598 month
== 7 || month
== 9 || month
== 11 ) {return ( 31*msPerDay
);}
601 return ( (leap
== 0) ? 28*msPerDay : 29*msPerDay
);
605 function MakeDate( day
, time
)
607 if (day
== Number
.POSITIVE_INFINITY
||
608 day
== Number
.NEGATIVE_INFINITY
||
614 if ( time
== Number
.POSITIVE_INFINITY
||
615 time
== Number
.POSITIVE_INFINITY
||
621 return ( day
* msPerDay
) + time
;
625 function TimeClip( t
)
627 if ( isNaN( t
)) {return ( Number
.NaN
);}
628 if ( Math
.abs( t
) > 8.64e15
) {return ( Number
.NaN
);}
630 return ( ToInteger( t
) );
634 function ToInteger( t
)
638 if ( isNaN( t
)) {return ( Number
.NaN
);}
640 if ( t
== 0 || t
== -0 ||
641 t
== Number
.POSITIVE_INFINITY
||
642 t
== Number
.NEGATIVE_INFINITY
)
647 var sign
= ( t
< 0 ) ? -1 : 1;
649 return ( sign
* Math
.floor( Math
.abs( t
) ) );
653 function Enumerate( o
)
656 for ( p
in o
) {print( p
+ ": " + o
[p
] );}
660 /* these functions are useful for running tests manually in Rhino */
662 function GetContext()
664 return Packages
.com
.netscape
.javascript
.Context
.getCurrentContext();
668 function OptLevel( i
)
671 var cx
= GetContext();
672 cx
.setOptimizationLevel(i
);
675 /* end of Rhino functions */