]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_2/jsref.js
31 var PASSED
= " PASSED!"
32 var FAILED
= " FAILED! expected: ";
34 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
35 testcases
[tc
].passed
= writeTestCaseResult(
38 testcases
[tc
].description
+" = "+
39 testcases
[tc
].actual
);
41 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
47 function TestCase( n
, d
, e
, a
) {
54 this.bugnumber
= BUGNUMBER
;
56 this.passed
= getTestCaseResult( this.expect
, this.actual
);
58 writeLineToLog( "added " + this.description
);
61 function startTest() {
62 // JavaScript 1.3 is supposed to be compliant ecma version 1.0
63 if ( VERSION
== "ECMA_1" ) {
66 if ( VERSION
== "JS_13" ) {
69 if ( VERSION
== "JS_12" ) {
72 if ( VERSION
== "JS_11" ) {
75 // for ecma version 2.0, we will leave the javascript version to
76 // the default ( for now ).
77 writeHeaderToLog( SECTION
+ " "+ TITLE
);
78 testcases
= new Array();
82 function getTestCaseResult( expect
, actual
) {
83 // because ( NaN == NaN ) always returns false, need to do
84 // a special compare to see if we got the right result.
85 if ( actual
!= actual
) {
86 if ( typeof actual
== "object" ) {
87 actual
= "NaN object";
89 actual
= "NaN number";
92 if ( expect
!= expect
) {
93 if ( typeof expect
== "object" ) {
94 expect
= "NaN object";
96 expect
= "NaN number";
100 var passed
= ( expect
== actual
) ? true : false;
102 // if both objects are numbers
103 // need to replace w/ IEEE standard for rounding
105 && typeof(actual
) == "number"
106 && typeof(expect
) == "number"
108 if ( Math
.abs(actual
-expect
) < 0.0000001 ) {
113 // verify type is the same
114 if ( typeof(expect
) != typeof(actual
) ) {
120 function writeTestCaseResult( expect
, actual
, string
) {
121 var passed
= getTestCaseResult( expect
, actual
);
122 writeFormattedResult( expect
, actual
, string
, passed
);
125 function writeFormattedResult( expect
, actual
, string
, passed
) {
126 var s
= TT
+ string
;
129 k
< (60 - string
.length
>= 0 ? 60 - string
.length : 5) ;
134 s
+= ( passed
) ? FONT_GREEN
+ NBSP
+ PASSED : FONT_RED
+ NBSP
+ FAILED
+ expect
+ TT_
;
136 writeLineToLog( s
+ FONT_
+ B_
+ TT_
);
141 function writeLineToLog( string
) {
142 print( string
+ BR
+ CR
);
144 function writeHeaderToLog( string
) {
145 print( H2
+ string
+ H2_
);
149 var sizeTag
= "<#TEST CASES SIZE>";
150 var doneTag
= "<#TEST CASES DONE>";
151 var beginTag
= "<#TEST CASE ";
155 print(testcases
.length
);
156 for (tc
= 0; tc
< testcases
.length
; tc
++)
158 print(beginTag
+ 'PASSED' + endTag
);
159 print(testcases
[tc
].passed
);
160 print(beginTag
+ 'NAME' + endTag
);
161 print(testcases
[tc
].name
);
162 print(beginTag
+ 'EXPECTED' + endTag
);
163 print(testcases
[tc
].expect
);
164 print(beginTag
+ 'ACTUAL' + endTag
);
165 print(testcases
[tc
].actual
);
166 print(beginTag
+ 'DESCRIPTION' + endTag
);
167 print(testcases
[tc
].description
);
168 print(beginTag
+ 'REASON' + endTag
);
169 print(( testcases
[tc
].passed
) ? "" : "wrong value ");
170 print(beginTag
+ 'BUGNUMBER' + endTag
);
177 function getFailedCases() {
178 for ( var i
= 0; i
< testcases
.length
; i
++ ) {
179 if ( ! testcases
[i
].passed
) {
180 print( testcases
[i
].description
+" = " +testcases
[i
].actual
+" expected: "+ testcases
[i
].expect
);
184 function err( msg
, page
, line
) {
185 testcases
[tc
].actual
= "error";
186 testcases
[tc
].reason
= msg
;
187 writeTestCaseResult( testcases
[tc
].expect
,
188 testcases
[tc
].actual
,
189 testcases
[tc
].description
+" = "+ testcases
[tc
].actual
+
190 ": " + testcases
[tc
].reason
);
196 * Type Conversion functions used by Type Conversion
203 * Date functions used by tests in Date suite
206 var msPerDay
= 86400000;
207 var HoursPerDay
= 24;
208 var MinutesPerHour
= 60;
209 var SecondsPerMinute
= 60;
210 var msPerSecond
= 1000;
211 var msPerMinute
= 60000; // msPerSecond * SecondsPerMinute
212 var msPerHour
= 3600000; // msPerMinute * MinutesPerHour
215 var TIME_2000
= 946684800000;
216 var TIME_1900
= -2208988800000;
219 return ( Math
.floor(t
/msPerDay
) );
221 function DaysInYear( y
) {
225 if ( (y
% 4 == 0) && (y
% 100 != 0) ) {
228 if ( (y
% 100 == 0) && (y
% 400 != 0) ) {
231 if ( (y
% 400 == 0) ){
234 return "ERROR: DaysInYear(" + y
+ ") case not covered";
237 function TimeInYear( y
) {
238 return ( DaysInYear(y
) * msPerDay
);
240 function DayNumber( t
) {
241 return ( Math
.floor( t
/ msPerDay
) );
243 function TimeWithinDay( t
) {
245 return ( (t
% msPerDay
) + msPerDay
);
247 return ( t
% msPerDay
);
250 function YearNumber( t
) {
252 function TimeFromYear( y
) {
253 return ( msPerDay
* DayFromYear(y
) );
255 function DayFromYear( y
) {
256 return ( 365*(y
-1970) +
257 Math
.floor((y
-1969)/4) -
258 Math
.floor((y
-1901)/100) +
259 Math
.floor((y
-1601)/400) );
261 function InLeapYear( t
) {
262 if ( DaysInYear(YearFromTime(t
)) == 365 ) {
265 if ( DaysInYear(YearFromTime(t
)) == 366 ) {
268 return "ERROR: InLeapYear("+t
+") case not covered";
271 function YearFromTime( t
) {
273 var sign
= ( t
< 0 ) ? -1 : 1;
274 var year
= ( sign
< 0 ) ? 1969 : 1970;
275 for ( var timeToTimeZero
= t
; ; ) {
276 // subtract the current year's time from the time that's left.
277 timeToTimeZero
-= sign
* TimeInYear(year
)
279 // if there's less than the current year's worth of time left, then break.
281 if ( sign
* timeToTimeZero
<= 0 ) {
287 if ( sign
* timeToTimeZero
< 0 ) {
296 function MonthFromTime( t
) {
297 // i know i could use switch but i'd rather not until it's part of ECMA
298 var day
= DayWithinYear( t
);
299 var leap
= InLeapYear(t
);
301 if ( (0 <= day
) && (day
< 31) ) {
304 if ( (31 <= day
) && (day
< (59+leap
)) ) {
307 if ( ((59+leap
) <= day
) && (day
< (90+leap
)) ) {
310 if ( ((90+leap
) <= day
) && (day
< (120+leap
)) ) {
313 if ( ((120+leap
) <= day
) && (day
< (151+leap
)) ) {
316 if ( ((151+leap
) <= day
) && (day
< (181+leap
)) ) {
319 if ( ((181+leap
) <= day
) && (day
< (212+leap
)) ) {
322 if ( ((212+leap
) <= day
) && (day
< (243+leap
)) ) {
325 if ( ((243+leap
) <= day
) && (day
< (273+leap
)) ) {
328 if ( ((273+leap
) <= day
) && (day
< (304+leap
)) ) {
331 if ( ((304+leap
) <= day
) && (day
< (334+leap
)) ) {
334 if ( ((334+leap
) <= day
) && (day
< (365+leap
)) ) {
337 return "ERROR: MonthFromTime("+t
+") not known";
340 function DayWithinYear( t
) {
341 return( Day(t
) - DayFromYear(YearFromTime(t
)));
343 function DateFromTime( t
) {
344 var day
= DayWithinYear(t
);
345 var month
= MonthFromTime(t
);
354 return ( day
- 58 - InLeapYear(t
) );
357 return ( day
- 89 - InLeapYear(t
));
360 return ( day
- 119 - InLeapYear(t
));
363 return ( day
- 150- InLeapYear(t
));
366 return ( day
- 180- InLeapYear(t
));
369 return ( day
- 211- InLeapYear(t
));
372 return ( day
- 242- InLeapYear(t
));
375 return ( day
- 272- InLeapYear(t
));
378 return ( day
- 303- InLeapYear(t
));
381 return ( day
- 333- InLeapYear(t
));
384 return ("ERROR: DateFromTime("+t
+") not known" );
386 function WeekDay( t
) {
387 var weekday
= (Day(t
)+4) % 7;
388 return( weekday
< 0 ? 7 + weekday : weekday
);
391 // missing daylight savins time adjustment
393 function HourFromTime( t
) {
394 var h
= Math
.floor( t
/ msPerHour
) % HoursPerDay
;
395 return ( (h
<0) ? HoursPerDay
+ h : h
);
397 function MinFromTime( t
) {
398 var min
= Math
.floor( t
/ msPerMinute
) % MinutesPerHour
;
399 return( ( min
< 0 ) ? MinutesPerHour
+ min : min
);
401 function SecFromTime( t
) {
402 var sec
= Math
.floor( t
/ msPerSecond
) % SecondsPerMinute
;
403 return ( (sec
< 0 ) ? SecondsPerMinute
+ sec : sec
);
405 function msFromTime( t
) {
406 var ms
= t
% msPerSecond
;
407 return ( (ms
< 0 ) ? msPerSecond
+ ms : ms
);
409 function LocalTZA() {
410 return ( TZ_DIFF
* msPerHour
);
413 return ( t
- LocalTZA() - DaylightSavingTA(t
- LocalTZA()) );
415 function DaylightSavingTA( t
) {
418 var dst_start
= GetSecondSundayInMarch(t
) + 2*msPerHour
;
419 var dst_end
= GetFirstSundayInNovember(t
)+ 2*msPerHour
;
421 if ( t
>= dst_start
&& t
< dst_end
) {
427 // Daylight Savings Time starts on the first Sunday in April at 2:00AM in
428 // PST. Other time zones will need to override this function.
430 print( new Date( UTC(dst_start
+ LocalTZA())) );
432 return UTC(dst_start
+ LocalTZA());
435 function GetFirstSundayInApril( t
) {
436 var year
= YearFromTime(t
);
437 var leap
= InLeapYear(t
);
439 var april
= TimeFromYear(year
) + TimeInMonth(0, leap
) + TimeInMonth(1,leap
) +
442 for ( var first_sunday
= april
; WeekDay(first_sunday
) > 0;
443 first_sunday
+= msPerDay
)
450 function GetLastSundayInOctober( t
) {
451 var year
= YearFromTime(t
);
452 var leap
= InLeapYear(t
);
454 for ( var oct
= TimeFromYear(year
), m
= 0; m
< 9; m
++ ) {
455 oct
+= TimeInMonth(m
, leap
);
457 for ( var last_sunday
= oct
+ 30*msPerDay
; WeekDay(last_sunday
) > 0;
458 last_sunday
-= msPerDay
)
465 // Added these two functions because DST rules changed for the US.
466 function GetSecondSundayInMarch( t
) {
467 var year
= YearFromTime(t
);
468 var leap
= InLeapYear(t
);
470 var march
= TimeFromYear(year
) + TimeInMonth(0, leap
) + TimeInMonth(1,leap
);
474 for ( var second_sunday
= march
; flag
; second_sunday
+= msPerDay
)
476 if (WeekDay(second_sunday
) == 0) {
477 if(++sundayCount
== 2)
482 return second_sunday
;
484 function GetFirstSundayInNovember( t
) {
485 var year
= YearFromTime(t
);
486 var leap
= InLeapYear(t
);
488 for ( var nov
= TimeFromYear(year
), m
= 0; m
< 10; m
++ ) {
489 nov
+= TimeInMonth(m
, leap
);
491 for ( var first_sunday
= nov
; WeekDay(first_sunday
) > 0;
492 first_sunday
+= msPerDay
)
498 function LocalTime( t
) {
499 return ( t
+ LocalTZA() + DaylightSavingTA(t
) );
501 function MakeTime( hour
, min
, sec
, ms
) {
502 if ( isNaN( hour
) || isNaN( min
) || isNaN( sec
) || isNaN( ms
) ) {
506 hour
= ToInteger(hour
);
507 min
= ToInteger( min
);
508 sec
= ToInteger( sec
);
509 ms
= ToInteger( ms
);
511 return( (hour
*msPerHour
) + (min
*msPerMinute
) +
512 (sec
*msPerSecond
) + ms
);
514 function MakeDay( year
, month
, date
) {
515 if ( isNaN(year
) || isNaN(month
) || isNaN(date
) ) {
518 year
= ToInteger(year
);
519 month
= ToInteger(month
);
520 date
= ToInteger(date
);
522 var sign
= ( year
< 1970 ) ? -1 : 1;
523 var t
= ( year
< 1970 ) ? 1 : 0;
524 var y
= ( year
< 1970 ) ? 1969 : 1970;
526 var result5
= year
+ Math
.floor( month
/12 );
527 var result6
= month
% 12;
530 for ( y
= 1969; y
>= year
; y
+= sign
) {
531 t
+= sign
* TimeInYear(y
);
534 for ( y
= 1970 ; y
< year
; y
+= sign
) {
535 t
+= sign
* TimeInYear(y
);
539 var leap
= InLeapYear( t
);
541 for ( var m
= 0; m
< month
; m
++ ) {
542 t
+= TimeInMonth( m
, leap
);
545 if ( YearFromTime(t
) != result5
) {
548 if ( MonthFromTime(t
) != result6
) {
551 if ( DateFromTime(t
) != 1 ) {
555 return ( (Day(t
)) + date
- 1 );
557 function TimeInMonth( month
, leap
) {
558 // september april june november
559 // jan 0 feb 1 mar 2 apr 3 may 4 june 5 jul 6
560 // aug 7 sep 8 oct 9 nov 10 dec 11
562 if ( month
== 3 || month
== 5 || month
== 8 || month
== 10 ) {
563 return ( 30*msPerDay
);
567 if ( month
== 0 || month
== 2 || month
== 4 || month
== 6 ||
568 month
== 7 || month
== 9 || month
== 11 ) {
569 return ( 31*msPerDay
);
573 return ( (leap
== 0) ? 28*msPerDay : 29*msPerDay
);
575 function MakeDate( day
, time
) {
576 if ( day
== Number
.POSITIVE_INFINITY
||
577 day
== Number
.NEGATIVE_INFINITY
||
578 day
== Number
.NaN
) {
581 if ( time
== Number
.POSITIVE_INFINITY
||
582 time
== Number
.POSITIVE_INFINITY
||
586 return ( day
* msPerDay
) + time
;
588 function TimeClip( t
) {
590 return ( Number
.NaN
);
592 if ( Math
.abs( t
) > 8.64e15
) {
593 return ( Number
.NaN
);
596 return ( ToInteger( t
) );
598 function ToInteger( t
) {
602 return ( Number
.NaN
);
604 if ( t
== 0 || t
== -0 ||
605 t
== Number
.POSITIVE_INFINITY
|| t
== Number
.NEGATIVE_INFINITY
) {
609 var sign
= ( t
< 0 ) ? -1 : 1;
611 return ( sign
* Math
.floor( Math
.abs( t
) ) );
613 function Enumerate ( o
) {
614 var properties
= new Array();
616 properties
[ properties
.length
] = new Array( p
, o
[p
] );
620 function AddTestCase( description
, expect
, actual
) {
621 testcases
[tc
++] = new TestCase( SECTION
, description
, expect
, actual
);
623 function getFailedCases() {
624 for ( var i
= 0; i
< testcases
.length
; i
++ ) {
625 if ( ! testcases
[i
].passed
) {
626 print( testcases
[i
].description
+" = " +testcases
[i
].actual
+" expected: "+ testcases
[i
].expect
);