Debian Codegraph @

rhino/testsrc/tests/ecma/shell.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestsuite = 'ecma';
7
8 /*
9 * Date functions used by tests in Date suite
10 *
11 */
12 var msPerDay = 86400000;
13 var HoursPerDay = 24;
14 var MinutesPerHour = 60;
15 var SecondsPerMinute = 60;
16 var msPerSecond = 1000;
17 var msPerMinute = 60000; // msPerSecond * SecondsPerMinute
18 var msPerHour = 3600000; // msPerMinute * MinutesPerHour
19 var TZ_DIFF = getTimeZoneDiff(); // offset of tester's timezone from UTC
20 var TZ_ADJUST = TZ_DIFF * msPerHour;
21 var TZ_PST = -8; // offset of Pacific Standard Time from UTC
22 var PST_DIFF = TZ_DIFF - TZ_PST; // offset of tester's timezone from PST
23 var PST_ADJUST = TZ_PST * msPerHour;
24 var TIME_0000 = (function ()
25 { // calculate time for year 0
26 for ( var time = 0, year = 1969; year >= 0; year-- ) {
27 time -= TimeInYear(year);
28 }
29 return time;
30 })();
31 var TIME_1970 = 0;
32 var TIME_2000 = 946684800000;
33 var TIME_1900 = -2208988800000;
34 var UTC_FEB_29_2000 = TIME_2000 + 31*msPerDay + 28*msPerDay;
35 var UTC_JAN_1_2005 = TIME_2000 + TimeInYear(2000) + TimeInYear(2001) +
36 TimeInYear(2002) + TimeInYear(2003) + TimeInYear(2004);
37 var now = new Date();
38 var TIME_NOW = now.valueOf(); //valueOf() is to accurate to the millisecond
39 //Date.parse() is accurate only to the second
40
41 /*
42 * Originally, the test suite used a hard-coded value TZ_DIFF = -8.
43 * But that was only valid for testers in the Pacific Standard Time Zone!
44 * We calculate the proper number dynamically for any tester. We just
45 * have to be careful not to use a date subject to Daylight Savings Time...
46 */
47 function getTimeZoneDiff()
48 {
49 return -((new Date(2000, 1, 1)).getTimezoneOffset())/60;
50 }
51
52 /*
53 * Date test "ResultArrays" are hard-coded for Pacific Standard Time.
54 * We must adjust them for the tester's own timezone -
55 */
56 function adjustResultArray(ResultArray, msMode)
57 {
58 // If the tester's system clock is in PST, no need to continue -
59 // if (!PST_DIFF) {return;}
60
61 /* The date gTestcases instantiate Date objects in two different ways:
62 *
63 * millisecond mode: e.g. dt = new Date(10000000);
64 * year-month-day mode: dt = new Date(2000, 5, 1, ...);
65 *
66 * In the first case, the date is measured from Time 0 in Greenwich (i.e. UTC).
67 * In the second case, it is measured with reference to the tester's local timezone.
68 *
69 * In the first case we must correct those values expected for local measurements,
70 * like dt.getHours() etc. No correction is necessary for dt.getUTCHours() etc.
71 *
72 * In the second case, it is exactly the other way around -
73 */
74 if (msMode)
75 {
76 // The hard-coded UTC milliseconds from Time 0 derives from a UTC date.
77 // Shift to the right by the offset between UTC and the tester.
78 var t = ResultArray[TIME] + TZ_DIFF*msPerHour;
79
80 // Use our date arithmetic functions to determine the local hour, day, etc.
81 ResultArray[HOURS] = HourFromTime(t);
82 ResultArray[DAY] = WeekDay(t);
83 ResultArray[DATE] = DateFromTime(t);
84 ResultArray[MONTH] = MonthFromTime(t);
85 ResultArray[YEAR] = YearFromTime(t);
86 }
87 else
88 {
89 // The hard-coded UTC milliseconds from Time 0 derives from a PST date.
90 // Shift to the left by the offset between PST and the tester.
91 var t = ResultArray[TIME] - PST_DIFF*msPerHour;
92
93 // Use our date arithmetic functions to determine the UTC hour, day, etc.
94 ResultArray[TIME] = t;
95 ResultArray[UTC_HOURS] = HourFromTime(t);
96 ResultArray[UTC_DAY] = WeekDay(t);
97 ResultArray[UTC_DATE] = DateFromTime(t);
98 ResultArray[UTC_MONTH] = MonthFromTime(t);
99 ResultArray[UTC_YEAR] = YearFromTime(t);
100 }
101 }
102
103 function Day( t ) {
104 return ( Math.floor(t/msPerDay ) );
105 }
106 function DaysInYear( y ) {
107 if ( y % 4 != 0 ) {
108 return 365;
109 }
110 if ( (y % 4 == 0) && (y % 100 != 0) ) {
111 return 366;
112 }
113 if ( (y % 100 == 0) && (y % 400 != 0) ) {
114 return 365;
115 }
116 if ( (y % 400 == 0) ){
117 return 366;
118 } else {
119 return "ERROR: DaysInYear(" + y + ") case not covered";
120 }
121 }
122 function TimeInYear( y ) {
123 return ( DaysInYear(y) * msPerDay );
124 }
125 function DayNumber( t ) {
126 return ( Math.floor( t / msPerDay ) );
127 }
128 function TimeWithinDay( t ) {
129
130 var r = t % msPerDay;
131
132 if (r < 0)
133 {
134 r += msPerDay;
135 }
136 return r;
137
138 }
139 function YearNumber( t ) {
140 }
141 function TimeFromYear( y ) {
142 return ( msPerDay * DayFromYear(y) );
143 }
144 function DayFromYear( y ) {
145 return ( 365*(y-1970) +
146 Math.floor((y-1969)/4) -
147 Math.floor((y-1901)/100) +
148 Math.floor((y-1601)/400) );
149 }
150 function InLeapYear( t ) {
151 if ( DaysInYear(YearFromTime(t)) == 365 ) {
152 return 0;
153 }
154 if ( DaysInYear(YearFromTime(t)) == 366 ) {
155 return 1;
156 } else {
157 return "ERROR: InLeapYear("+ t + ") case not covered";
158 }
159 }
160 function YearFromTime( t ) {
161 t = Number( t );
162 var sign = ( t < 0 ) ? -1 : 1;
163 var year = ( sign < 0 ) ? 1969 : 1970;
164 for ( var timeToTimeZero = t; ; ) {
165 // subtract the current year's time from the time that's left.
166 timeToTimeZero -= sign * TimeInYear(year)
167
168 // if there's less than the current year's worth of time left, then break.
169 if ( sign < 0 ) {
170 if ( sign * timeToTimeZero <= 0 ) {
171 break;
172 } else {
173 year += sign;
174 }
175 } else {
176 if ( sign * timeToTimeZero < 0 ) {
177 break;
178 } else {
179 year += sign;
180 }
181 }
182 }
183 return ( year );
184 }
185 function MonthFromTime( t ) {
186 // i know i could use switch but i'd rather not until it's part of ECMA
187 var day = DayWithinYear( t );
188 var leap = InLeapYear(t);
189
190 if ( (0 <= day) && (day < 31) ) {
191 return 0;
192 }
193 if ( (31 <= day) && (day < (59+leap)) ) {
194 return 1;
195 }
196 if ( ((59+leap) <= day) && (day < (90+leap)) ) {
197 return 2;
198 }
199 if ( ((90+leap) <= day) && (day < (120+leap)) ) {
200 return 3;
201 }
202 if ( ((120+leap) <= day) && (day < (151+leap)) ) {
203 return 4;
204 }
205 if ( ((151+leap) <= day) && (day < (181+leap)) ) {
206 return 5;
207 }
208 if ( ((181+leap) <= day) && (day < (212+leap)) ) {
209 return 6;
210 }
211 if ( ((212+leap) <= day) && (day < (243+leap)) ) {
212 return 7;
213 }
214 if ( ((243+leap) <= day) && (day < (273+leap)) ) {
215 return 8;
216 }
217 if ( ((273+leap) <= day) && (day < (304+leap)) ) {
218 return 9;
219 }
220 if ( ((304+leap) <= day) && (day < (334+leap)) ) {
221 return 10;
222 }
223 if ( ((334+leap) <= day) && (day < (365+leap)) ) {
224 return 11;
225 } else {
226 return "ERROR: MonthFromTime("+t+") not known";
227 }
228 }
229 function DayWithinYear( t ) {
230 return( Day(t) - DayFromYear(YearFromTime(t)));
231 }
232 function DateFromTime( t ) {
233 var day = DayWithinYear(t);
234 var month = MonthFromTime(t);
235
236 if ( month == 0 ) {
237 return ( day + 1 );
238 }
239 if ( month == 1 ) {
240 return ( day - 30 );
241 }
242 if ( month == 2 ) {
243 return ( day - 58 - InLeapYear(t) );
244 }
245 if ( month == 3 ) {
246 return ( day - 89 - InLeapYear(t));
247 }
248 if ( month == 4 ) {
249 return ( day - 119 - InLeapYear(t));
250 }
251 if ( month == 5 ) {
252 return ( day - 150- InLeapYear(t));
253 }
254 if ( month == 6 ) {
255 return ( day - 180- InLeapYear(t));
256 }
257 if ( month == 7 ) {
258 return ( day - 211- InLeapYear(t));
259 }
260 if ( month == 8 ) {
261 return ( day - 242- InLeapYear(t));
262 }
263 if ( month == 9 ) {
264 return ( day - 272- InLeapYear(t));
265 }
266 if ( month == 10 ) {
267 return ( day - 303- InLeapYear(t));
268 }
269 if ( month == 11 ) {
270 return ( day - 333- InLeapYear(t));
271 }
272
273 return ("ERROR: DateFromTime("+t+") not known" );
274 }
275 function WeekDay( t ) {
276 var weekday = (Day(t)+4) % 7;
277 return( weekday < 0 ? 7 + weekday : weekday );
278 }
279
280 // missing daylight savings time adjustment
281
282 function HourFromTime( t ) {
283 var h = Math.floor( t / msPerHour ) % HoursPerDay;
284 return ( (h<0) ? HoursPerDay + h : h );
285 }
286 function MinFromTime( t ) {
287 var min = Math.floor( t / msPerMinute ) % MinutesPerHour;
288 return( ( min < 0 ) ? MinutesPerHour + min : min );
289 }
290 function SecFromTime( t ) {
291 var sec = Math.floor( t / msPerSecond ) % SecondsPerMinute;
292 return ( (sec < 0 ) ? SecondsPerMinute + sec : sec );
293 }
294 function msFromTime( t ) {
295 var ms = t % msPerSecond;
296 return ( (ms < 0 ) ? msPerSecond + ms : ms );
297 }
298 function LocalTZA() {
299 return ( TZ_DIFF * msPerHour );
300 }
301 function UTC( t ) {
302 return ( t - LocalTZA() - DaylightSavingTA(t - LocalTZA()) );
303 }
304 function LocalTime( t ) {
305 return ( t + LocalTZA() + DaylightSavingTA(t) );
306 }
307 function DaylightSavingTA( t ) {
308 t = t - LocalTZA();
309
310 var dst_start = GetDSTStart(t);
311 var dst_end = GetDSTEnd(t);
312
313 if ( t >= dst_start && t < dst_end )
314 return msPerHour;
315
316 return 0;
317 }
318
319 function GetFirstSundayInMonth( t, m ) {
320 var year = YearFromTime(t);
321 var leap = InLeapYear(t);
322
323 // month m 0..11
324 // april == 3
325 // march == 2
326
327 // set time to first day of month m
328 var time = TimeFromYear(year);
329 for (var i = 0; i < m; ++i)
330 {
331 time += TimeInMonth(i, leap);
332 }
333
334 for ( var first_sunday = time; WeekDay(first_sunday) > 0;
335 first_sunday += msPerDay )
336 {
337 ;
338 }
339
340 return first_sunday;
341 }
342
343 function GetLastSundayInMonth( t, m ) {
344 var year = YearFromTime(t);
345 var leap = InLeapYear(t);
346
347 // month m 0..11
348 // april == 3
349 // march == 2
350
351 // first day of following month
352 var time = TimeFromYear(year);
353 for (var i = 0; i <= m; ++i)
354 {
355 time += TimeInMonth(i, leap);
356 }
357 // prev day == last day of month
358 time -= msPerDay;
359
360 for ( var last_sunday = time; WeekDay(last_sunday) > 0;
361 last_sunday -= msPerDay )
362 {
363 ;
364 }
365 return last_sunday;
366 }
367
368 /*
369 15.9.1.9 Daylight Saving Time Adjustment
370
371 The implementation of ECMAScript should not try to determine whether
372 the exact time was subject to daylight saving time, but just whether
373 daylight saving time would have been in effect if the current
374 daylight saving time algorithm had been used at the time. This avoids
375 complications such as taking into account the years that the locale
376 observed daylight saving time year round.
377 */
378
379 /*
380 US DST algorithm
381
382 Before 2007, DST starts first Sunday in April at 2 AM and ends last
383 Sunday in October at 2 AM
384
385 Starting in 2007, DST starts second Sunday in March at 2 AM and ends
386 first Sunday in November at 2 AM
387
388 Note that different operating systems behave differently.
389
390 Fully patched Windows XP uses the 2007 algorithm for all dates while
391 fully patched Fedora Core 6 and RHEL 4 Linux use the algorithm in
392 effect at the time.
393
394 Since pre-2007 DST is a subset of 2007 DST rules, this only affects
395 tests that occur in the period Mar-Apr and Oct-Nov where the two
396 algorithms do not agree.
397
398 */
399
400 function GetDSTStart( t )
401 {
402 return (GetFirstSundayInMonth(t, 2) + 7*msPerDay + 2*msPerHour - LocalTZA());
403 }
404
405 function GetDSTEnd( t )
406 {
407 return (GetFirstSundayInMonth(t, 10) + 2*msPerHour - LocalTZA());
408 }
409
410 function GetOldDSTStart( t )
411 {
412 return (GetFirstSundayInMonth(t, 3) + 2*msPerHour - LocalTZA());
413 }
414
415 function GetOldDSTEnd( t )
416 {
417 return (GetLastSundayInMonth(t, 9) + 2*msPerHour - LocalTZA());
418 }
419
420 function MakeTime( hour, min, sec, ms ) {
421 if ( isNaN( hour ) || isNaN( min ) || isNaN( sec ) || isNaN( ms ) ) {
422 return Number.NaN;
423 }
424
425 hour = ToInteger(hour);
426 min = ToInteger( min);
427 sec = ToInteger( sec);
428 ms = ToInteger( ms );
429
430 return( (hour*msPerHour) + (min*msPerMinute) +
431 (sec*msPerSecond) + ms );
432 }
433 function MakeDay( year, month, date ) {
434 if ( isNaN(year) || isNaN(month) || isNaN(date) ) {
435 return Number.NaN;
436 }
437 year = ToInteger(year);
438 month = ToInteger(month);
439 date = ToInteger(date );
440
441 var sign = ( year < 1970 ) ? -1 : 1;
442 var t = ( year < 1970 ) ? 1 : 0;
443 var y = ( year < 1970 ) ? 1969 : 1970;
444
445 var result5 = year + Math.floor( month/12 );
446 var result6 = month % 12;
447
448 if ( year < 1970 ) {
449 for ( y = 1969; y >= year; y += sign ) {
450 t += sign * TimeInYear(y);
451 }
452 } else {
453 for ( y = 1970 ; y < year; y += sign ) {
454 t += sign * TimeInYear(y);
455 }
456 }
457
458 var leap = InLeapYear( t );
459
460 for ( var m = 0; m < month; m++ ) {
461 t += TimeInMonth( m, leap );
462 }
463
464 if ( YearFromTime(t) != result5 ) {
465 return Number.NaN;
466 }
467 if ( MonthFromTime(t) != result6 ) {
468 return Number.NaN;
469 }
470 if ( DateFromTime(t) != 1 ) {
471 return Number.NaN;
472 }
473
474 return ( (Day(t)) + date - 1 );
475 }
476 function TimeInMonth( month, leap ) {
477 // september april june november
478 // jan 0 feb 1 mar 2 apr 3 may 4 june 5 jul 6
479 // aug 7 sep 8 oct 9 nov 10 dec 11
480
481 if ( month == 3 || month == 5 || month == 8 || month == 10 ) {
482 return ( 30*msPerDay );
483 }
484
485 // all the rest
486 if ( month == 0 || month == 2 || month == 4 || month == 6 ||
487 month == 7 || month == 9 || month == 11 ) {
488 return ( 31*msPerDay );
489 }
490
491 // save february
492 return ( (leap == 0) ? 28*msPerDay : 29*msPerDay );
493 }
494 function MakeDate( day, time ) {
495 if ( day == Number.POSITIVE_INFINITY ||
496 day == Number.NEGATIVE_INFINITY ) {
497 return Number.NaN;
498 }
499 if ( time == Number.POSITIVE_INFINITY ||
500 time == Number.NEGATIVE_INFINITY ) {
501 return Number.NaN;
502 }
503 return ( day * msPerDay ) + time;
504 }
505 function TimeClip( t ) {
506 if ( isNaN( t ) ) {
507 return ( Number.NaN );
508 }
509 if ( Math.abs( t ) > 8.64e15 ) {
510 return ( Number.NaN );
511 }
512
513 return ( ToInteger( t ) );
514 }
515 function ToInteger( t ) {
516 t = Number( t );
517
518 if ( isNaN( t ) ){
519 return ( Number.NaN );
520 }
521 if ( t == 0 || t == -0 ||
522 t == Number.POSITIVE_INFINITY || t == Number.NEGATIVE_INFINITY ) {
523 return 0;
524 }
525
526 var sign = ( t < 0 ) ? -1 : 1;
527
528 return ( sign * Math.floor( Math.abs( t ) ) );
529 }
530 function Enumerate ( o ) {
531 var p;
532 for ( p in o ) {
533 print( p +": " + o[p] );
534 }
535 }
536
537 /* these functions are useful for running tests manually in Rhino */
538
539 function GetContext() {
540 return Packages.com.netscape.javascript.Context.getCurrentContext();
541 }
542 function OptLevel( i ) {
543 i = Number(i);
544 var cx = GetContext();
545 cx.setOptimizationLevel(i);
546 }
547 /* end of Rhino functions */
548