]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/jsref.js
13 var GLOBAL
= "[object global]";
14 var PASSED
= " PASSED!"
15 var FAILED
= " FAILED! expected: ";
36 var PASSED
= " PASSED!"
37 var FAILED
= " FAILED! expected: ";
40 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
41 testcases
[tc
].passed
= writeTestCaseResult(
44 testcases
[tc
].description
+" = "+
45 testcases
[tc
].actual
);
47 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
52 /* wrapper for test cas constructor that doesn't require the SECTION
56 function AddTestCase( description
, expect
, actual
) {
57 testcases
[tc
++] = new TestCase( SECTION
, description
, expect
, actual
);
60 function TestCase( n
, d
, e
, a
) {
67 this.bugnumber
= BUGNUMBER
;
69 this.passed
= getTestCaseResult( this.expect
, this.actual
);
71 writeLineToLog( "added " + this.description
);
76 * Set up test environment.
79 function startTest() {
81 // JavaScript 1.3 is supposed to be compliant ecma version 1.0
82 if ( VERSION
== "ECMA_1" ) {
85 if ( VERSION
== "JS_1.3" ) {
88 if ( VERSION
== "JS_1.2" ) {
91 if ( VERSION
== "JS_1.1" ) {
94 // for ecma version 2.0, we will leave the javascript version to
95 // the default ( for now ).
98 // print out bugnumber
101 writeLineToLog ("BUGNUMBER: " + BUGNUMBER
);
104 testcases
= new Array();
110 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
111 testcases
[tc
].passed
= writeTestCaseResult(
112 testcases
[tc
].expect
,
113 testcases
[tc
].actual
,
114 testcases
[tc
].description
+" = "+ testcases
[tc
].actual
);
115 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
118 return ( testcases
);
122 function getTestCaseResult( expect
, actual
) {
123 // because ( NaN == NaN ) always returns false, need to do
124 // a special compare to see if we got the right result.
125 if ( actual
!= actual
) {
126 if ( typeof actual
== "object" ) {
127 actual
= "NaN object";
129 actual
= "NaN number";
132 if ( expect
!= expect
) {
133 if ( typeof expect
== "object" ) {
134 expect
= "NaN object";
136 expect
= "NaN number";
140 var passed
= ( expect
== actual
) ? true : false;
142 // if both objects are numbers
143 // need to replace w/ IEEE standard for rounding
145 && typeof(actual
) == "number"
146 && typeof(expect
) == "number"
148 if ( Math
.abs(actual
-expect
) < 0.0000001 ) {
153 // verify type is the same
154 if ( typeof(expect
) != typeof(actual
) ) {
160 function writeTestCaseResult( expect
, actual
, string
) {
161 var passed
= getTestCaseResult( expect
, actual
);
162 writeFormattedResult( expect
, actual
, string
, passed
);
165 function writeFormattedResult( expect
, actual
, string
, passed
) {
166 var s
= TT
+ string
;
169 k
< (60 - string
.length
>= 0 ? 60 - string
.length : 5) ;
174 s
+= ( passed
) ? FONT_GREEN
+ NBSP
+ PASSED : FONT_RED
+ NBSP
+ FAILED
+ expect
+ TT_
;
176 writeLineToLog( s
+ FONT_
+ B_
+ TT_
);
181 function writeLineToLog( string
) {
182 print( string
+ BR
+ CR
);
184 function writeHeaderToLog( string
) {
185 print( H2
+ string
+ H2_
);
189 var sizeTag
= "<#TEST CASES SIZE>";
190 var doneTag
= "<#TEST CASES DONE>";
191 var beginTag
= "<#TEST CASE ";
195 print(testcases
.length
);
196 for (tc
= 0; tc
< testcases
.length
; tc
++)
198 print(beginTag
+ 'PASSED' + endTag
);
199 print(testcases
[tc
].passed
);
200 print(beginTag
+ 'NAME' + endTag
);
201 print(testcases
[tc
].name
);
202 print(beginTag
+ 'EXPECTED' + endTag
);
203 print(testcases
[tc
].expect
);
204 print(beginTag
+ 'ACTUAL' + endTag
);
205 print(testcases
[tc
].actual
);
206 print(beginTag
+ 'DESCRIPTION' + endTag
);
207 print(testcases
[tc
].description
);
208 print(beginTag
+ 'REASON' + endTag
);
209 print(( testcases
[tc
].passed
) ? "" : "wrong value ");
210 print(beginTag
+ 'BUGNUMBER' + endTag
);
217 function getFailedCases() {
218 for ( var i
= 0; i
< testcases
.length
; i
++ ) {
219 if ( ! testcases
[i
].passed
) {
220 print( testcases
[i
].description
+" = " +testcases
[i
].actual
+" expected: "+ testcases
[i
].expect
);
224 function err( msg
, page
, line
) {
225 testcases
[tc
].actual
= "error";
226 testcases
[tc
].reason
= msg
;
227 writeTestCaseResult( testcases
[tc
].expect
,
228 testcases
[tc
].actual
,
229 testcases
[tc
].description
+" = "+ testcases
[tc
].actual
+
230 ": " + testcases
[tc
].reason
);
236 * Type Conversion functions used by Type Conversion
243 * Date functions used by tests in Date suite
246 var msPerDay
= 86400000;
247 var HoursPerDay
= 24;
248 var MinutesPerHour
= 60;
249 var SecondsPerMinute
= 60;
250 var msPerSecond
= 1000;
251 var msPerMinute
= 60000; // msPerSecond * SecondsPerMinute
252 var msPerHour
= 3600000; // msPerMinute * MinutesPerHour
255 var TIME_2000
= 946684800000;
256 var TIME_1900
= -2208988800000;
259 return ( Math
.floor(t
/msPerDay
) );
261 function DaysInYear( y
) {
265 if ( (y
% 4 == 0) && (y
% 100 != 0) ) {
268 if ( (y
% 100 == 0) && (y
% 400 != 0) ) {
271 if ( (y
% 400 == 0) ){
274 return "ERROR: DaysInYear(" + y
+ ") case not covered";
277 function TimeInYear( y
) {
278 return ( DaysInYear(y
) * msPerDay
);
280 function DayNumber( t
) {
281 return ( Math
.floor( t
/ msPerDay
) );
283 function TimeWithinDay( t
) {
285 return ( (t
% msPerDay
) + msPerDay
);
287 return ( t
% msPerDay
);
290 function YearNumber( t
) {
292 function TimeFromYear( y
) {
293 return ( msPerDay
* DayFromYear(y
) );
295 function DayFromYear( y
) {
296 return ( 365*(y
-1970) +
297 Math
.floor((y
-1969)/4) -
298 Math
.floor((y
-1901)/100) +
299 Math
.floor((y
-1601)/400) );
301 function InLeapYear( t
) {
302 if ( DaysInYear(YearFromTime(t
)) == 365 ) {
305 if ( DaysInYear(YearFromTime(t
)) == 366 ) {
308 return "ERROR: InLeapYear("+t
+") case not covered";
311 function YearFromTime( t
) {
313 var sign
= ( t
< 0 ) ? -1 : 1;
314 var year
= ( sign
< 0 ) ? 1969 : 1970;
315 for ( var timeToTimeZero
= t
; ; ) {
316 // subtract the current year's time from the time that's left.
317 timeToTimeZero
-= sign
* TimeInYear(year
)
319 // if there's less than the current year's worth of time left, then break.
321 if ( sign
* timeToTimeZero
<= 0 ) {
327 if ( sign
* timeToTimeZero
< 0 ) {
336 function MonthFromTime( t
) {
337 // i know i could use switch but i'd rather not until it's part of ECMA
338 var day
= DayWithinYear( t
);
339 var leap
= InLeapYear(t
);
341 if ( (0 <= day
) && (day
< 31) ) {
344 if ( (31 <= day
) && (day
< (59+leap
)) ) {
347 if ( ((59+leap
) <= day
) && (day
< (90+leap
)) ) {
350 if ( ((90+leap
) <= day
) && (day
< (120+leap
)) ) {
353 if ( ((120+leap
) <= day
) && (day
< (151+leap
)) ) {
356 if ( ((151+leap
) <= day
) && (day
< (181+leap
)) ) {
359 if ( ((181+leap
) <= day
) && (day
< (212+leap
)) ) {
362 if ( ((212+leap
) <= day
) && (day
< (243+leap
)) ) {
365 if ( ((243+leap
) <= day
) && (day
< (273+leap
)) ) {
368 if ( ((273+leap
) <= day
) && (day
< (304+leap
)) ) {
371 if ( ((304+leap
) <= day
) && (day
< (334+leap
)) ) {
374 if ( ((334+leap
) <= day
) && (day
< (365+leap
)) ) {
377 return "ERROR: MonthFromTime("+t
+") not known";
380 function DayWithinYear( t
) {
381 return( Day(t
) - DayFromYear(YearFromTime(t
)));
383 function DateFromTime( t
) {
384 var day
= DayWithinYear(t
);
385 var month
= MonthFromTime(t
);
394 return ( day
- 58 - InLeapYear(t
) );
397 return ( day
- 89 - InLeapYear(t
));
400 return ( day
- 119 - InLeapYear(t
));
403 return ( day
- 150- InLeapYear(t
));
406 return ( day
- 180- InLeapYear(t
));
409 return ( day
- 211- InLeapYear(t
));
412 return ( day
- 242- InLeapYear(t
));
415 return ( day
- 272- InLeapYear(t
));
418 return ( day
- 303- InLeapYear(t
));
421 return ( day
- 333- InLeapYear(t
));
424 return ("ERROR: DateFromTime("+t
+") not known" );
426 function WeekDay( t
) {
427 var weekday
= (Day(t
)+4) % 7;
428 return( weekday
< 0 ? 7 + weekday : weekday
);
431 // missing daylight savins time adjustment
433 function HourFromTime( t
) {
434 var h
= Math
.floor( t
/ msPerHour
) % HoursPerDay
;
435 return ( (h
<0) ? HoursPerDay
+ h : h
);
437 function MinFromTime( t
) {
438 var min
= Math
.floor( t
/ msPerMinute
) % MinutesPerHour
;
439 return( ( min
< 0 ) ? MinutesPerHour
+ min : min
);
441 function SecFromTime( t
) {
442 var sec
= Math
.floor( t
/ msPerSecond
) % SecondsPerMinute
;
443 return ( (sec
< 0 ) ? SecondsPerMinute
+ sec : sec
);
445 function msFromTime( t
) {
446 var ms
= t
% msPerSecond
;
447 return ( (ms
< 0 ) ? msPerSecond
+ ms : ms
);
449 function LocalTZA() {
450 return ( TZ_DIFF
* msPerHour
);
453 return ( t
- LocalTZA() - DaylightSavingTA(t
- LocalTZA()) );
455 function DaylightSavingTA( t
) {
458 var dst_start
= GetSecondSundayInMarch(t
) + 2*msPerHour
;
459 var dst_end
= GetFirstSundayInNovember(t
)+ 2*msPerHour
;
461 if ( t
>= dst_start
&& t
< dst_end
) {
467 // Daylight Savings Time starts on the first Sunday in April at 2:00AM in
468 // PST. Other time zones will need to override this function.
470 print( new Date( UTC(dst_start
+ LocalTZA())) );
472 return UTC(dst_start
+ LocalTZA());
474 function GetFirstSundayInApril( t
) {
475 var year
= YearFromTime(t
);
476 var leap
= InLeapYear(t
);
478 var april
= TimeFromYear(year
) + TimeInMonth(0, leap
) + TimeInMonth(1,leap
) +
481 for ( var first_sunday
= april
; WeekDay(first_sunday
) > 0;
482 first_sunday
+= msPerDay
)
489 function GetLastSundayInOctober( t
) {
490 var year
= YearFromTime(t
);
491 var leap
= InLeapYear(t
);
493 for ( var oct
= TimeFromYear(year
), m
= 0; m
< 9; m
++ ) {
494 oct
+= TimeInMonth(m
, leap
);
496 for ( var last_sunday
= oct
+ 30*msPerDay
; WeekDay(last_sunday
) > 0;
497 last_sunday
-= msPerDay
)
504 // Added these two functions because DST rules changed for the US.
505 function GetSecondSundayInMarch( t
) {
506 var year
= YearFromTime(t
);
507 var leap
= InLeapYear(t
);
509 var march
= TimeFromYear(year
) + TimeInMonth(0, leap
) + TimeInMonth(1,leap
);
513 for ( var second_sunday
= march
; flag
; second_sunday
+= msPerDay
)
515 if (WeekDay(second_sunday
) == 0) {
516 if(++sundayCount
== 2)
521 return second_sunday
;
523 function GetFirstSundayInNovember( t
) {
524 var year
= YearFromTime(t
);
525 var leap
= InLeapYear(t
);
527 for ( var nov
= TimeFromYear(year
), m
= 0; m
< 10; m
++ ) {
528 nov
+= TimeInMonth(m
, leap
);
530 for ( var first_sunday
= nov
; WeekDay(first_sunday
) > 0;
531 first_sunday
+= msPerDay
)
537 function LocalTime( t
) {
538 return ( t
+ LocalTZA() + DaylightSavingTA(t
) );
540 function MakeTime( hour
, min
, sec
, ms
) {
541 if ( isNaN( hour
) || isNaN( min
) || isNaN( sec
) || isNaN( ms
) ) {
545 hour
= ToInteger(hour
);
546 min
= ToInteger( min
);
547 sec
= ToInteger( sec
);
548 ms
= ToInteger( ms
);
550 return( (hour
*msPerHour
) + (min
*msPerMinute
) +
551 (sec
*msPerSecond
) + ms
);
553 function MakeDay( year
, month
, date
) {
554 if ( isNaN(year
) || isNaN(month
) || isNaN(date
) ) {
557 year
= ToInteger(year
);
558 month
= ToInteger(month
);
559 date
= ToInteger(date
);
561 var sign
= ( year
< 1970 ) ? -1 : 1;
562 var t
= ( year
< 1970 ) ? 1 : 0;
563 var y
= ( year
< 1970 ) ? 1969 : 1970;
565 var result5
= year
+ Math
.floor( month
/12 );
566 var result6
= month
% 12;
569 for ( y
= 1969; y
>= year
; y
+= sign
) {
570 t
+= sign
* TimeInYear(y
);
573 for ( y
= 1970 ; y
< year
; y
+= sign
) {
574 t
+= sign
* TimeInYear(y
);
578 var leap
= InLeapYear( t
);
580 for ( var m
= 0; m
< month
; m
++ ) {
581 t
+= TimeInMonth( m
, leap
);
584 if ( YearFromTime(t
) != result5
) {
587 if ( MonthFromTime(t
) != result6
) {
590 if ( DateFromTime(t
) != 1 ) {
594 return ( (Day(t
)) + date
- 1 );
596 function TimeInMonth( month
, leap
) {
597 // september april june november
598 // jan 0 feb 1 mar 2 apr 3 may 4 june 5 jul 6
599 // aug 7 sep 8 oct 9 nov 10 dec 11
601 if ( month
== 3 || month
== 5 || month
== 8 || month
== 10 ) {
602 return ( 30*msPerDay
);
606 if ( month
== 0 || month
== 2 || month
== 4 || month
== 6 ||
607 month
== 7 || month
== 9 || month
== 11 ) {
608 return ( 31*msPerDay
);
612 return ( (leap
== 0) ? 28*msPerDay : 29*msPerDay
);
614 function MakeDate( day
, time
) {
615 if ( day
== Number
.POSITIVE_INFINITY
||
616 day
== Number
.NEGATIVE_INFINITY
||
617 day
== Number
.NaN
) {
620 if ( time
== Number
.POSITIVE_INFINITY
||
621 time
== Number
.POSITIVE_INFINITY
||
625 return ( day
* msPerDay
) + time
;
627 function TimeClip( t
) {
629 return ( Number
.NaN
);
631 if ( Math
.abs( t
) > 8.64e15
) {
632 return ( Number
.NaN
);
635 return ( ToInteger( t
) );
637 function ToInteger( t
) {
641 return ( Number
.NaN
);
643 if ( t
== 0 || t
== -0 ||
644 t
== Number
.POSITIVE_INFINITY
|| t
== Number
.NEGATIVE_INFINITY
) {
648 var sign
= ( t
< 0 ) ? -1 : 1;
650 return ( sign
* Math
.floor( Math
.abs( t
) ) );
652 function Enumerate ( o
) {
653 var properties
= new Array();
655 properties
[ properties
.length
] = new Array( p
, o
[p
] );
659 function AddTestCase( description
, expect
, actual
) {
660 testcases
[tc
++] = new TestCase( SECTION
, description
, expect
, actual
);
663 function getFailedCases() {
664 for ( var i
= 0; i
< testcases
.length
; i
++ ) {
665 if ( ! testcases
[i
].passed
) {
666 print( testcases
[i
].description
+" = " +testcases
[i
].actual
+" expected: "+ testcases
[i
].expect
);