Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: tests/corelib/date_time_test.dart

Issue 10832166: Updated VM and JS versions of Date to allow underflow and overflow. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/co19/co19-runtime.status ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Dart test program for Date. 5 // Dart test program for Date.
6 6
7 class DateTest { 7 class DateTest {
8 // Tests if the time moves eventually forward. 8 // Tests if the time moves eventually forward.
9 static void testNow() { 9 static void testNow() {
10 var t1 = new Date.now(); 10 var t1 = new Date.now();
11 bool timeMovedForward = false; 11 bool timeMovedForward = false;
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 isUtc: true)); 258 isUtc: true));
259 Expect.throws(() => new Date.fromMillisecondsSinceEpoch(-8640000000000001, 259 Expect.throws(() => new Date.fromMillisecondsSinceEpoch(-8640000000000001,
260 isUtc: true)); 260 isUtc: true));
261 Expect.throws(() => new Date.fromMillisecondsSinceEpoch(8640000000000001)); 261 Expect.throws(() => new Date.fromMillisecondsSinceEpoch(8640000000000001));
262 Expect.throws( 262 Expect.throws(
263 () => new Date.fromMillisecondsSinceEpoch(-8640000000000001)); 263 () => new Date.fromMillisecondsSinceEpoch(-8640000000000001));
264 dt = new Date.fromMillisecondsSinceEpoch(8640000000000000); 264 dt = new Date.fromMillisecondsSinceEpoch(8640000000000000);
265 Expect.throws(() => new Date(dt.year, dt.month, dt.day, 265 Expect.throws(() => new Date(dt.year, dt.month, dt.day,
266 dt.hour, dt.minute, 0, 1)); 266 dt.hour, dt.minute, 0, 1));
267 dt = new Date.fromMillisecondsSinceEpoch(-8640000000000000); 267 dt = new Date.fromMillisecondsSinceEpoch(-8640000000000000);
268 // TODO(floitsch): Update comment after refactoring.
269 // This test currently fails because the arguments must not be negative.
270 // However we are going to allow negative (and overflowing) arguments and
271 // this line will then throw for the correct reason.
272 Expect.throws(() => new Date(dt.year, dt.month, dt.day, 268 Expect.throws(() => new Date(dt.year, dt.month, dt.day,
273 dt.hour, dt.minute, 0, -1)); 269 dt.hour, dt.minute, 0, -1, isUtc: true));
274 } 270 }
275 271
276 static void testUTCGetters() { 272 static void testUTCGetters() {
277 var dt = new Date.fromMillisecondsSinceEpoch(1305140315000, isUtc: true); 273 var dt = new Date.fromMillisecondsSinceEpoch(1305140315000, isUtc: true);
278 Expect.equals(2011, dt.year); 274 Expect.equals(2011, dt.year);
279 Expect.equals(5, dt.month); 275 Expect.equals(5, dt.month);
280 Expect.equals(11, dt.day); 276 Expect.equals(11, dt.day);
281 Expect.equals(18, dt.hour); 277 Expect.equals(18, dt.hour);
282 Expect.equals(58, dt.minute); 278 Expect.equals(58, dt.minute);
283 Expect.equals(35, dt.second); 279 Expect.equals(35, dt.second);
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 Expect.equals(dt1.hour, dt2.hour); 416 Expect.equals(dt1.hour, dt2.hour);
421 Expect.equals(dt1.minute, dt2.minute); 417 Expect.equals(dt1.minute, dt2.minute);
422 Expect.equals(dt1.second + 3, dt2.second); 418 Expect.equals(dt1.second + 3, dt2.second);
423 Expect.equals(dt1.millisecond + 5, dt2.millisecond); 419 Expect.equals(dt1.millisecond + 5, dt2.millisecond);
424 var dt3 = dt2.subtract(new Duration(milliseconds: 420 var dt3 = dt2.subtract(new Duration(milliseconds:
425 3 * Duration.MILLISECONDS_PER_SECOND + 5)); 421 3 * Duration.MILLISECONDS_PER_SECOND + 5));
426 Expect.equals(true, dt1 == dt3); 422 Expect.equals(true, dt1 == dt3);
427 Expect.equals(false, dt1 == dt2); 423 Expect.equals(false, dt1 == dt2);
428 } 424 }
429 425
426 static void testUnderflowAndOverflow() {
427 final dtBase = new Date(2012, 6, 20, 12, 30, 30, 500);
428
429 // Millisecond
430 print(" >>> Millisecond+");
431 var dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
432 dtBase.minute, dtBase.second, 1000);
433 Expect.equals(dtBase.year, dt.year);
434 Expect.equals(dtBase.month, dt.month);
435 Expect.equals(dtBase.day, dt.day);
436 Expect.equals(dtBase.hour, dt.hour);
437 Expect.equals(dtBase.minute, dt.minute);
438 Expect.equals(dtBase.second + 1, dt.second);
439 Expect.equals(0, dt.millisecond);
440
441 print(" >>> Millisecond-");
442 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
443 dtBase.minute, dtBase.second, -1000);
444 Expect.equals(dtBase.year, dt.year);
445 Expect.equals(dtBase.month, dt.month);
446 Expect.equals(dtBase.day, dt.day);
447 Expect.equals(dtBase.hour, dt.hour);
448 Expect.equals(dtBase.minute, dt.minute);
449 Expect.equals(dtBase.second - 1, dt.second);
450 Expect.equals(0, dt.millisecond);
451
452 // Second
453 print(" >>> Second+");
454 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
455 dtBase.minute, 60, dtBase.millisecond);
456 Expect.equals(dtBase.year, dt.year);
457 Expect.equals(dtBase.month, dt.month);
458 Expect.equals(dtBase.day, dt.day);
459 Expect.equals(dtBase.hour, dt.hour);
460 Expect.equals(dtBase.minute + 1, dt.minute);
461 Expect.equals(0, dt.second);
462 Expect.equals(dtBase.millisecond, dt.millisecond);
463
464 print(" >>> Second-");
465 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
466 dtBase.minute, -60, dtBase.millisecond);
467 Expect.equals(dtBase.year, dt.year);
468 Expect.equals(dtBase.month, dt.month);
469 Expect.equals(dtBase.day, dt.day);
470 Expect.equals(dtBase.hour, dt.hour);
471 Expect.equals(dtBase.minute - 1, dt.minute);
472 Expect.equals(0, dt.second);
473 Expect.equals(dtBase.millisecond, dt.millisecond);
474
475 // Minute
476 print(" >>> Minute+");
477 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, 60,
478 dtBase.second, dtBase.millisecond);
479 Expect.equals(dtBase.year, dt.year);
480 Expect.equals(dtBase.month, dt.month);
481 Expect.equals(dtBase.day, dt.day);
482 Expect.equals(dtBase.hour + 1, dt.hour);
483 Expect.equals(0, dt.minute);
484 Expect.equals(dtBase.second, dt.second);
485 Expect.equals(dtBase.millisecond, dt.millisecond);
486
487 print(" >>> Minute-");
488 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, -60,
489 dtBase.second, dtBase.millisecond);
490 Expect.equals(dtBase.year, dt.year);
491 Expect.equals(dtBase.month, dt.month);
492 Expect.equals(dtBase.day, dt.day);
493 Expect.equals(dtBase.hour - 1, dt.hour);
494 Expect.equals(0, dt.minute);
495 Expect.equals(dtBase.second, dt.second);
496 Expect.equals(dtBase.millisecond, dt.millisecond);
497
498 // Hour
499 print(" >>> Hour+");
500 dt = new Date(dtBase.year, dtBase.month, dtBase.day, 24, dtBase.minute,
501 dtBase.second, dtBase.millisecond);
502 Expect.equals(dtBase.year, dt.year);
503 Expect.equals(dtBase.month, dt.month);
504 Expect.equals(dtBase.day + 1, dt.day);
505 Expect.equals(0, dt.hour);
506 Expect.equals(dtBase.minute, dt.minute);
507 Expect.equals(dtBase.second, dt.second);
508 Expect.equals(dtBase.millisecond, dt.millisecond);
509
510 print(" >>> Hour-");
511 dt = new Date(dtBase.year, dtBase.month, dtBase.day, -24, dtBase.minute,
512 dtBase.second, dtBase.millisecond);
513 Expect.equals(dtBase.year, dt.year);
514 Expect.equals(dtBase.month, dt.month);
515 Expect.equals(dtBase.day - 1, dt.day);
516 Expect.equals(0, dt.hour);
517 Expect.equals(dtBase.minute, dt.minute);
518 Expect.equals(dtBase.second, dt.second);
519 Expect.equals(dtBase.millisecond, dt.millisecond);
520
521 // Day
522 print(" >>> Day+");
523 dt = new Date(dtBase.year, dtBase.month, 31, dtBase.hour, dtBase.minute,
524 dtBase.second, dtBase.millisecond);
525 Expect.equals(dtBase.year, dt.year);
526 Expect.equals(dtBase.month + 1, dt.month);
527 Expect.equals(1, dt.day);
528 Expect.equals(dtBase.hour, dt.hour);
529 Expect.equals(dtBase.minute, dt.minute);
530 Expect.equals(dtBase.second, dt.second);
531 Expect.equals(dtBase.millisecond, dt.millisecond);
532
533 print(" >>> Day-");
534 dt = new Date(dtBase.year, dtBase.month, -30, dtBase.hour, dtBase.minute,
535 dtBase.second, dtBase.millisecond);
536 Expect.equals(dtBase.year, dt.year);
537 Expect.equals(dtBase.month - 1, dt.month);
538 Expect.equals(1, dt.day);
539 Expect.equals(dtBase.hour, dt.hour);
540 Expect.equals(dtBase.minute, dt.minute);
541 Expect.equals(dtBase.second, dt.second);
542 Expect.equals(dtBase.millisecond, dt.millisecond);
543
544 // Month
545 print(" >>> Month+");
546 dt = new Date(dtBase.year, 13, dtBase.day, dtBase.hour, dtBase.minute,
547 dtBase.second, dtBase.millisecond);
548 Expect.equals(dtBase.year + 1, dt.year);
549 Expect.equals(1, dt.month);
550 Expect.equals(dtBase.day, dt.day);
551 Expect.equals(dtBase.hour, dt.hour);
552 Expect.equals(dtBase.minute, dt.minute);
553 Expect.equals(dtBase.second, dt.second);
554 Expect.equals(dtBase.millisecond, dt.millisecond);
555
556 print(" >>> Month-");
557 dt = new Date(dtBase.year, -11, dtBase.day, dtBase.hour, dtBase.minute,
558 dtBase.second, dtBase.millisecond);
559 Expect.equals(dtBase.year - 1, dt.year);
560 Expect.equals(1, dt.month);
561 Expect.equals(dtBase.day, dt.day);
562 Expect.equals(dtBase.hour, dt.hour);
563 Expect.equals(dtBase.minute, dt.minute);
564 Expect.equals(dtBase.second, dt.second);
565 Expect.equals(dtBase.millisecond, dt.millisecond);
566
567 // Flowing all the way up the chain.
568 print(" >>> Flow+");
569 var dtBase1 = new Date(2012, 12, 31, 23, 59, 59, 999);
570 var dtTick = new Date(dtBase1.year, dtBase1.month, dtBase1.day,
571 dtBase1.hour, dtBase1.minute, dtBase1.second,
572 dtBase1.millisecond + 1);
573 Expect.equals(dtBase1.year + 1, dtTick.year);
574 Expect.equals(1, dtTick.month);
575 Expect.equals(1, dtTick.day);
576 Expect.equals(0, dtTick.hour);
577 Expect.equals(0, dtTick.minute);
578 Expect.equals(0, dtTick.second);
579 Expect.equals(0, dtTick.millisecond);
580
581 print(" >>> Flow-");
582 dtBase1 = new Date(2012, 1, 1, 0, 0, 0, 0);
583 dtTick = new Date(dtBase1.year, dtBase1.month, dtBase1.day, dtBase1.hour,
584 dtBase1.minute, dtBase1.second, dtBase1.millisecond - 1);
585 Expect.equals(dtBase1.year - 1, dtTick.year);
586 Expect.equals(12, dtTick.month);
587 Expect.equals(31, dtTick.day);
588 Expect.equals(23, dtTick.hour);
589 Expect.equals(59, dtTick.minute);
590 Expect.equals(59, dtTick.second);
591 Expect.equals(999, dtTick.millisecond);
592
593 print(" >>> extra underflow");
594 dtTick = new Date(dtBase1.year, dtBase1.month, dtBase1.day, -17520,
595 dtBase1.minute, dtBase1.second, dtBase1.millisecond);
596 Expect.equals(dtBase1.year - 2, dtTick.year);
597 Expect.equals(dtBase1.month, dtTick.month);
598 Expect.equals(dtBase1.day, dtTick.day);
599 Expect.equals(dtBase1.hour, dtTick.hour);
600 Expect.equals(dtBase1.minute, dtTick.minute);
601 Expect.equals(dtBase1.second, dtTick.second);
602 Expect.equals(dtBase1.millisecond, dtTick.millisecond);
603 }
604
430 static void testDateStrings() { 605 static void testDateStrings() {
431 // TODO(floitsch): Clean up the Date API that deals with strings. 606 // TODO(floitsch): Clean up the Date API that deals with strings.
432 var dt1 = new Date.fromString("2011-05-11 18:58:35Z"); 607 var dt1 = new Date.fromString("2011-05-11 18:58:35Z");
433 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 608 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
434 Expect.isTrue(dt1.isUtc); 609 Expect.isTrue(dt1.isUtc);
435 dt1 = new Date.fromString("20110511 18:58:35z"); 610 dt1 = new Date.fromString("20110511 18:58:35z");
436 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 611 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
437 Expect.isTrue(dt1.isUtc); 612 Expect.isTrue(dt1.isUtc);
438 dt1 = new Date.fromString("+20110511 18:58:35z"); 613 dt1 = new Date.fromString("+20110511 18:58:35z");
439 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 614 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 } 804 }
630 805
631 static void testMain() { 806 static void testMain() {
632 testNow(); 807 testNow();
633 testValue(); 808 testValue();
634 testConstructors(); 809 testConstructors();
635 testUTCGetters(); 810 testUTCGetters();
636 testLocalGetters(); 811 testLocalGetters();
637 testChangeTimeZone(); 812 testChangeTimeZone();
638 testSubAdd(); 813 testSubAdd();
814 testUnderflowAndOverflow();
639 testDateStrings(); 815 testDateStrings();
640 testEquivalentYears(); 816 testEquivalentYears();
641 testExtremes(); 817 testExtremes();
642 testFarAwayDates(); 818 testFarAwayDates();
643 testWeekday(); 819 testWeekday();
644 } 820 }
645 } 821 }
646 822
647 main() { 823 main() {
648 DateTest.testMain(); 824 DateTest.testMain();
649 } 825 }
OLDNEW
« no previous file with comments | « tests/co19/co19-runtime.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698