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

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: clean up unnecessary hoop-jumping 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));
floitsch 2012/08/15 08:44:26 This test should still fail. It failed because of
dominich 2012/08/17 16:38:33 Done.
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(dtBase.year, dt.year);
432 Expect.equals(dtBase.month, dt.month);
433 Expect.equals(dtBase.day, dt.day);
434 Expect.equals(dtBase.hour, dt.hour);
435 Expect.equals(dtBase.minute, dt.minute);
436 Expect.equals(dtBase.second + 1, dt.second);
437 Expect.equals(0, dt.millisecond);
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(dtBase.year, dt.year);
443 Expect.equals(dtBase.month, dt.month);
444 Expect.equals(dtBase.day, dt.day);
445 Expect.equals(dtBase.hour, dt.hour);
446 Expect.equals(dtBase.minute, dt.minute);
447 Expect.equals(dtBase.second - 1, dt.second);
448 Expect.equals(0, dt.millisecond);
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(dtBase.year, dt.year);
455 Expect.equals(dtBase.month, dt.month);
456 Expect.equals(dtBase.day, dt.day);
457 Expect.equals(dtBase.hour, dt.hour);
458 Expect.equals(dtBase.minute + 1, dt.minute);
459 Expect.equals(0, dt.second);
460 Expect.equals(dtBase.millisecond, dt.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(dtBase.year, dt.year);
466 Expect.equals(dtBase.month, dt.month);
467 Expect.equals(dtBase.day, dt.day);
468 Expect.equals(dtBase.hour, dt.hour);
469 Expect.equals(dtBase.minute - 1, dt.minute);
470 Expect.equals(0, dt.second);
471 Expect.equals(dtBase.millisecond, dt.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(dtBase.year, dt.year);
478 Expect.equals(dtBase.month, dt.month);
479 Expect.equals(dtBase.day, dt.day);
480 Expect.equals(dtBase.hour + 1, dt.hour);
481 Expect.equals(0, dt.minute);
482 Expect.equals(dtBase.second, dt.second);
483 Expect.equals(dtBase.millisecond, dt.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(dtBase.year, dt.year);
489 Expect.equals(dtBase.month, dt.month);
490 Expect.equals(dtBase.day, dt.day);
491 Expect.equals(dtBase.hour - 1, dt.hour);
492 Expect.equals(0, dt.minute);
493 Expect.equals(dtBase.second, dt.second);
494 Expect.equals(dtBase.millisecond, dt.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(dtBase.year, dt.year);
501 Expect.equals(dtBase.month, dt.month);
502 Expect.equals(dtBase.day + 1, dt.day);
503 Expect.equals(0, dt.hour);
504 Expect.equals(dtBase.minute, dt.minute);
505 Expect.equals(dtBase.second, dt.second);
506 Expect.equals(dtBase.millisecond, dt.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(dtBase.year, dt.year);
512 Expect.equals(dtBase.month, dt.month);
513 Expect.equals(dtBase.day - 1, dt.day);
514 Expect.equals(0, dt.hour);
515 Expect.equals(dtBase.minute, dt.minute);
516 Expect.equals(dtBase.second, dt.second);
517 Expect.equals(dtBase.millisecond, dt.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(dtBase.year, dt.year);
524 Expect.equals(dtBase.month + 1, dt.month);
525 Expect.equals(1, dt.day);
526 Expect.equals(dtBase.hour, dt.hour);
527 Expect.equals(dtBase.minute, dt.minute);
528 Expect.equals(dtBase.second, dt.second);
529 Expect.equals(dtBase.millisecond, dt.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(dtBase.year, dt.year);
535 Expect.equals(dtBase.month - 1, dt.month);
536 Expect.equals(1, dt.day);
537 Expect.equals(dtBase.hour, dt.hour);
538 Expect.equals(dtBase.minute, dt.minute);
539 Expect.equals(dtBase.second, dt.second);
540 Expect.equals(dtBase.millisecond, dt.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(dtBase.year + 1, dt.year);
547 Expect.equals(1, dt.month);
548 Expect.equals(dtBase.day, dt.day);
549 Expect.equals(dtBase.hour, dt.hour);
550 Expect.equals(dtBase.minute, dt.minute);
551 Expect.equals(dtBase.second, dt.second);
552 Expect.equals(dtBase.millisecond, dt.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(dtBase.year - 1, dt.year);
558 Expect.equals(1, dt.month);
559 Expect.equals(dtBase.day, dt.day);
560 Expect.equals(dtBase.hour, dt.hour);
561 Expect.equals(dtBase.minute, dt.minute);
562 Expect.equals(dtBase.second, dt.second);
563 Expect.equals(dtBase.millisecond, dt.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(dtBase1.year + 1, dtTick.year);
572 Expect.equals(1, dtTick.month);
573 Expect.equals(1, dtTick.day);
574 Expect.equals(0, dtTick.hour);
575 Expect.equals(0, dtTick.minute);
576 Expect.equals(0, dtTick.second);
577 Expect.equals(0, dtTick.millisecond);
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(dtBase1.year - 1, dtTick.year);
584 Expect.equals(12, dtTick.month);
585 Expect.equals(31, dtTick.day);
586 Expect.equals(23, dtTick.hour);
587 Expect.equals(59, dtTick.minute);
588 Expect.equals(59, dtTick.second);
589 Expect.equals(999, dtTick.millisecond);
590
591 print(" >>> extra underflow");
592 dtTick = new Date(dtBase1.year, dtBase1.month, dtBase1.day, -17520,
593 dtBase1.minute, dtBase1.second, dtBase1.millisecond);
594 Expect.equals(dtBase1.year - 2, dtTick.year);
595 Expect.equals(dtBase1.month, dtTick.month);
596 Expect.equals(dtBase1.day, dtTick.day);
597 Expect.equals(dtBase1.hour, dtTick.hour);
598 Expect.equals(dtBase1.minute, dtTick.minute);
599 Expect.equals(dtBase1.second, dtTick.second);
600 Expect.equals(dtBase1.millisecond, dtTick.millisecond);
601 }
602
430 static void testDateStrings() { 603 static void testDateStrings() {
431 // TODO(floitsch): Clean up the Date API that deals with strings. 604 // TODO(floitsch): Clean up the Date API that deals with strings.
432 var dt1 = new Date.fromString("2011-05-11 18:58:35Z"); 605 var dt1 = new Date.fromString("2011-05-11 18:58:35Z");
433 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 606 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
434 Expect.isTrue(dt1.isUtc); 607 Expect.isTrue(dt1.isUtc);
435 dt1 = new Date.fromString("20110511 18:58:35z"); 608 dt1 = new Date.fromString("20110511 18:58:35z");
436 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 609 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
437 Expect.isTrue(dt1.isUtc); 610 Expect.isTrue(dt1.isUtc);
438 dt1 = new Date.fromString("+20110511 18:58:35z"); 611 dt1 = new Date.fromString("+20110511 18:58:35z");
439 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch); 612 Expect.equals(1305140315000, dt1.millisecondsSinceEpoch);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 } 802 }
630 803
631 static void testMain() { 804 static void testMain() {
632 testNow(); 805 testNow();
633 testValue(); 806 testValue();
634 testConstructors(); 807 testConstructors();
635 testUTCGetters(); 808 testUTCGetters();
636 testLocalGetters(); 809 testLocalGetters();
637 testChangeTimeZone(); 810 testChangeTimeZone();
638 testSubAdd(); 811 testSubAdd();
812 testUnderflowAndOverflow();
639 testDateStrings(); 813 testDateStrings();
640 testEquivalentYears(); 814 testEquivalentYears();
641 testExtremes(); 815 testExtremes();
642 testFarAwayDates(); 816 testFarAwayDates();
643 testWeekday(); 817 testWeekday();
644 } 818 }
645 } 819 }
646 820
647 main() { 821 main() {
648 DateTest.testMain(); 822 DateTest.testMain();
649 } 823 }
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