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

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: remove loops Created 8 years, 4 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
« runtime/lib/date.dart ('K') | « runtime/lib/date.dart ('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,
273 dt.hour, dt.minute, 0, -1));
274 } 268 }
275 269
276 static void testUTCGetters() { 270 static void testUTCGetters() {
277 var dt = new Date.fromMillisecondsSinceEpoch(1305140315000, isUtc: true); 271 var dt = new Date.fromMillisecondsSinceEpoch(1305140315000, isUtc: true);
278 Expect.equals(2011, dt.year); 272 Expect.equals(2011, dt.year);
279 Expect.equals(5, dt.month); 273 Expect.equals(5, dt.month);
280 Expect.equals(11, dt.day); 274 Expect.equals(11, dt.day);
281 Expect.equals(18, dt.hour); 275 Expect.equals(18, dt.hour);
282 Expect.equals(58, dt.minute); 276 Expect.equals(58, dt.minute);
283 Expect.equals(35, dt.second); 277 Expect.equals(35, dt.second);
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 Expect.equals(dt1.hour, dt2.hour); 414 Expect.equals(dt1.hour, dt2.hour);
421 Expect.equals(dt1.minute, dt2.minute); 415 Expect.equals(dt1.minute, dt2.minute);
422 Expect.equals(dt1.second + 3, dt2.second); 416 Expect.equals(dt1.second + 3, dt2.second);
423 Expect.equals(dt1.millisecond + 5, dt2.millisecond); 417 Expect.equals(dt1.millisecond + 5, dt2.millisecond);
424 var dt3 = dt2.subtract(new Duration(milliseconds: 418 var dt3 = dt2.subtract(new Duration(milliseconds:
425 3 * Duration.MILLISECONDS_PER_SECOND + 5)); 419 3 * Duration.MILLISECONDS_PER_SECOND + 5));
426 Expect.equals(true, dt1 == dt3); 420 Expect.equals(true, dt1 == dt3);
427 Expect.equals(false, dt1 == dt2); 421 Expect.equals(false, dt1 == dt2);
428 } 422 }
429 423
424 static void testUnderflowAndOverflow() {
425 final dtBase = new Date(2012, 6, 20, 12, 30, 30, 500);
426
427 // Millisecond
428 print(" >>> Millisecond+");
429 var dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
430 dtBase.minute, dtBase.second, 1000);
431 Expect.equals(dt.year, dtBase.year);
432 Expect.equals(dt.month, dtBase.month);
433 Expect.equals(dt.day, dtBase.day);
434 Expect.equals(dt.hour, dtBase.hour);
435 Expect.equals(dt.minute, dtBase.minute);
436 Expect.equals(dt.second, dtBase.second + 1);
437 Expect.equals(dt.millisecond, 0);
438
439 print(" >>> Millisecond-");
440 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
441 dtBase.minute, dtBase.second, -1000);
442 Expect.equals(dt.year, dtBase.year);
443 Expect.equals(dt.month, dtBase.month);
444 Expect.equals(dt.day, dtBase.day);
445 Expect.equals(dt.hour, dtBase.hour);
446 Expect.equals(dt.minute, dtBase.minute);
447 Expect.equals(dt.second, dtBase.second - 1);
448 Expect.equals(dt.millisecond, 0);
449
450 // Second
451 print(" >>> Second+");
452 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
453 dtBase.minute, 60, dtBase.millisecond);
454 Expect.equals(dt.year, dtBase.year);
455 Expect.equals(dt.month, dtBase.month);
456 Expect.equals(dt.day, dtBase.day);
457 Expect.equals(dt.hour, dtBase.hour);
458 Expect.equals(dt.minute, dtBase.minute + 1);
459 Expect.equals(dt.second, 0);
460 Expect.equals(dt.millisecond, dtBase.millisecond);
461
462 print(" >>> Second-");
463 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour,
464 dtBase.minute, -60, dtBase.millisecond);
465 Expect.equals(dt.year, dtBase.year);
466 Expect.equals(dt.month, dtBase.month);
467 Expect.equals(dt.day, dtBase.day);
468 Expect.equals(dt.hour, dtBase.hour);
469 Expect.equals(dt.minute, dtBase.minute - 1);
470 Expect.equals(dt.second, 0);
471 Expect.equals(dt.millisecond, dtBase.millisecond);
472
473 // Minute
474 print(" >>> Minute+");
475 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, 60,
476 dtBase.second, dtBase.millisecond);
477 Expect.equals(dt.year, dtBase.year);
478 Expect.equals(dt.month, dtBase.month);
479 Expect.equals(dt.day, dtBase.day);
480 Expect.equals(dt.hour, dtBase.hour + 1);
481 Expect.equals(dt.minute, 0);
482 Expect.equals(dt.second, dtBase.second);
483 Expect.equals(dt.millisecond, dtBase.millisecond);
484
485 print(" >>> Minute-");
486 dt = new Date(dtBase.year, dtBase.month, dtBase.day, dtBase.hour, -60,
487 dtBase.second, dtBase.millisecond);
488 Expect.equals(dt.year, dtBase.year);
489 Expect.equals(dt.month, dtBase.month);
490 Expect.equals(dt.day, dtBase.day);
491 Expect.equals(dt.hour, dtBase.hour - 1);
492 Expect.equals(dt.minute, 0);
493 Expect.equals(dt.second, dtBase.second);
494 Expect.equals(dt.millisecond, dtBase.millisecond);
495
496 // Hour
497 print(" >>> Hour+");
498 dt = new Date(dtBase.year, dtBase.month, dtBase.day, 24, dtBase.minute,
499 dtBase.second, dtBase.millisecond);
500 Expect.equals(dt.year, dtBase.year);
501 Expect.equals(dt.month, dtBase.month);
502 Expect.equals(dt.day, dtBase.day + 1);
503 Expect.equals(dt.hour, 0);
504 Expect.equals(dt.minute, dtBase.minute);
505 Expect.equals(dt.second, dtBase.second);
506 Expect.equals(dt.millisecond, dtBase.millisecond);
507
508 print(" >>> Hour-");
509 dt = new Date(dtBase.year, dtBase.month, dtBase.day, -24, dtBase.minute,
510 dtBase.second, dtBase.millisecond);
511 Expect.equals(dt.year, dtBase.year);
512 Expect.equals(dt.month, dtBase.month);
513 Expect.equals(dt.day, dtBase.day - 1);
514 Expect.equals(dt.hour, 0);
515 Expect.equals(dt.minute, dtBase.minute);
516 Expect.equals(dt.second, dtBase.second);
517 Expect.equals(dt.millisecond, dtBase.millisecond);
518
519 // Day
520 print(" >>> Day+");
521 dt = new Date(dtBase.year, dtBase.month, 31, dtBase.hour, dtBase.minute,
522 dtBase.second, dtBase.millisecond);
523 Expect.equals(dt.year, dtBase.year);
524 Expect.equals(dt.month, dtBase.month + 1);
525 Expect.equals(dt.day, 1);
526 Expect.equals(dt.hour, dtBase.hour);
527 Expect.equals(dt.minute, dtBase.minute);
528 Expect.equals(dt.second, dtBase.second);
529 Expect.equals(dt.millisecond, dtBase.millisecond);
530
531 print(" >>> Day-");
532 dt = new Date(dtBase.year, dtBase.month, -30, dtBase.hour, dtBase.minute,
533 dtBase.second, dtBase.millisecond);
534 Expect.equals(dt.year, dtBase.year);
535 Expect.equals(dt.month, dtBase.month - 1);
536 Expect.equals(dt.day, 1);
537 Expect.equals(dt.hour, dtBase.hour);
538 Expect.equals(dt.minute, dtBase.minute);
539 Expect.equals(dt.second, dtBase.second);
540 Expect.equals(dt.millisecond, dtBase.millisecond);
541
542 // Month
543 print(" >>> Month+");
544 dt = new Date(dtBase.year, 13, dtBase.day, dtBase.hour, dtBase.minute,
545 dtBase.second, dtBase.millisecond);
546 Expect.equals(dt.year, dtBase.year + 1);
547 Expect.equals(dt.month, 1);
548 Expect.equals(dt.day, dtBase.day);
549 Expect.equals(dt.hour, dtBase.hour);
550 Expect.equals(dt.minute, dtBase.minute);
551 Expect.equals(dt.second, dtBase.second);
552 Expect.equals(dt.millisecond, dtBase.millisecond);
553
554 print(" >>> Month-");
555 dt = new Date(dtBase.year, -11, dtBase.day, dtBase.hour, dtBase.minute,
556 dtBase.second, dtBase.millisecond);
557 Expect.equals(dt.year, dtBase.year - 1);
558 Expect.equals(dt.month, 1);
559 Expect.equals(dt.day, dtBase.day);
560 Expect.equals(dt.hour, dtBase.hour);
561 Expect.equals(dt.minute, dtBase.minute);
562 Expect.equals(dt.second, dtBase.second);
563 Expect.equals(dt.millisecond, dtBase.millisecond);
564
565 // Flowing all the way up the chain.
566 print(" >>> Flow+");
567 var dtBase1 = new Date(2012, 12, 31, 23, 59, 59, 999);
568 var dtTick = new Date(dtBase1.year, dtBase1.month, dtBase1.day,
569 dtBase1.hour, dtBase1.minute, dtBase1.second,
570 dtBase1.millisecond + 1);
571 Expect.equals(dtTick.year, dtBase1.year + 1);
572 Expect.equals(dtTick.month, 1);
573 Expect.equals(dtTick.day, 1);
574 Expect.equals(dtTick.hour, 0);
575 Expect.equals(dtTick.minute, 0);
576 Expect.equals(dtTick.second, 0);
577 Expect.equals(dtTick.millisecond, 0);
578
579 print(" >>> Flow-");
580 dtBase1 = new Date(2012, 1, 1, 0, 0, 0, 0);
581 dtTick = new Date(dtBase1.year, dtBase1.month, dtBase1.day, dtBase1.hour,
582 dtBase1.minute, dtBase1.second, dtBase1.millisecond - 1);
583 Expect.equals(dtTick.year, dtBase1.year - 1);
584 Expect.equals(dtTick.month, 12);
585 Expect.equals(dtTick.day, 31);
586 Expect.equals(dtTick.hour, 23);
587 Expect.equals(dtTick.minute, 59);
588 Expect.equals(dtTick.second, 59);
589 Expect.equals(dtTick.millisecond, 999);
590 }
591
430 static void testDateStrings() { 592 static void testDateStrings() {
431 // TODO(floitsch): Clean up the Date API that deals with strings. 593 // TODO(floitsch): Clean up the Date API that deals with strings.
432 var dt1 = new Date.fromString("2011-05-11 18:58:35Z"); 594 var dt1 = new Date.fromString("2011-05-11 18:58:35Z");
433 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 595 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
434 Expect.isTrue(dt1.isUtc); 596 Expect.isTrue(dt1.isUtc);
435 dt1 = new Date.fromString("20110511 18:58:35z"); 597 dt1 = new Date.fromString("20110511 18:58:35z");
436 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 598 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
437 Expect.isTrue(dt1.isUtc); 599 Expect.isTrue(dt1.isUtc);
438 dt1 = new Date.fromString("+20110511 18:58:35z"); 600 dt1 = new Date.fromString("+20110511 18:58:35z");
439 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 601 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 } 791 }
630 792
631 static void testMain() { 793 static void testMain() {
632 testNow(); 794 testNow();
633 testValue(); 795 testValue();
634 testConstructors(); 796 testConstructors();
635 testUTCGetters(); 797 testUTCGetters();
636 testLocalGetters(); 798 testLocalGetters();
637 testChangeTimeZone(); 799 testChangeTimeZone();
638 testSubAdd(); 800 testSubAdd();
801 testUnderflowAndOverflow();
639 testDateStrings(); 802 testDateStrings();
640 testEquivalentYears(); 803 testEquivalentYears();
641 testExtremes(); 804 testExtremes();
642 testFarAwayDates(); 805 testFarAwayDates();
643 testWeekday(); 806 testWeekday();
644 } 807 }
645 } 808 }
646 809
647 main() { 810 main() {
648 DateTest.testMain(); 811 DateTest.testMain();
649 } 812 }
OLDNEW
« runtime/lib/date.dart ('K') | « runtime/lib/date.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698