1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qdatetime.h>
#include <qlocale.h>
//TESTED_CLASS=
//TESTED_FILES=corelib/tools/qdatetime.h corelib/tools/qdatetime.cpp
class tst_QDate : public QObject
{
Q_OBJECT
public:
tst_QDate();
virtual ~tst_QDate();
public slots:
void init();
void cleanup();
private slots:
void toString();
void isValid_data();
void isValid();
void julianDay_data();
void julianDay();
void weekNumber_invalid_data();
void weekNumber_invalid();
void weekNumber_data();
void weekNumber();
void addDays_data();
void addDays();
void addMonths_data();
void addMonths();
void addYears_data();
void addYears();
void operator_eq_eq();
void operator_not_eq();
void operator_lt();
void operator_gt();
void operator_lt_eq();
void operator_gt_eq();
void fromString_data();
void fromString();
void fromString_format_data();
void fromString_format();
void toString_format_data();
void toString_format();
void isLeapYear();
void yearsZeroToNinetyNine();
void negativeYear() const;
void printNegativeYear() const;
void roundtripGermanLocale() const;
void shortDayName() const;
void standaloneShortDayName() const;
void longDayName() const;
void standaloneLongDayName() const;
void shortMonthName() const;
void standaloneShortMonthName() const;
void longMonthName() const;
void standaloneLongMonthName() const;
void roundtrip() const;
};
Q_DECLARE_METATYPE(QDate)
tst_QDate::tst_QDate()
{
}
tst_QDate::~tst_QDate()
{
}
void tst_QDate::init()
{
// This will be executed immediately before each test is run.
// TODO: Add initialization code here.
}
void tst_QDate::cleanup()
{
// This will be executed immediately after each test is run.
// TODO: Add cleanup code here.
}
void tst_QDate::isValid_data()
{
QTest::addColumn<int>("year");
QTest::addColumn<int>("month");
QTest::addColumn<int>("day");
QTest::addColumn<uint>("jd");
QTest::addColumn<bool>("valid");
QTest::newRow("0-0-0") << 0 << 0 << 0 << 0U << false;
QTest::newRow("month 0") << 2000 << 0 << 1 << 0U << false;
QTest::newRow("day 0") << 2000 << 1 << 0 << 0U << false;
QTest::newRow("month 13") << 2000 << 13 << 1 << 0U << false;
// test leap years
QTest::newRow("non-leap") << 2006 << 2 << 29 << 0U << false;
QTest::newRow("normal leap") << 2004 << 2 << 29 << 2453065U << true;
QTest::newRow("century leap") << 1900 << 2 << 29 << 0U << false;
QTest::newRow("century leap") << 2100 << 2 << 29 << 0U << false;
QTest::newRow("400-years leap") << 2000 << 2 << 29 << 2451604U << true;
QTest::newRow("400-years leap 2") << 2400 << 2 << 29 << 2597701U << true;
QTest::newRow("400-years leap 3") << 1600 << 2 << 29 << 2305507U << true;
QTest::newRow("year 0") << 0 << 2 << 27 << 0U << false;
// test the number of days in months:
QTest::newRow("jan") << 2000 << 1 << 31 << 2451575U << true;
QTest::newRow("feb") << 2000 << 2 << 29 << 2451604U << true; // same data as 400-years leap
QTest::newRow("mar") << 2000 << 3 << 31 << 2451635U << true;
QTest::newRow("apr") << 2000 << 4 << 30 << 2451665U << true;
QTest::newRow("may") << 2000 << 5 << 31 << 2451696U << true;
QTest::newRow("jun") << 2000 << 6 << 30 << 2451726U << true;
QTest::newRow("jul") << 2000 << 7 << 31 << 2451757U << true;
QTest::newRow("aug") << 2000 << 8 << 31 << 2451788U << true;
QTest::newRow("sep") << 2000 << 9 << 30 << 2451818U << true;
QTest::newRow("oct") << 2000 << 10 << 31 << 2451849U << true;
QTest::newRow("nov") << 2000 << 11 << 30 << 2451879U << true;
QTest::newRow("dec") << 2000 << 12 << 31 << 2451910U << true;
// and invalid dates:
QTest::newRow("ijan") << 2000 << 1 << 32 << 0U << false;
QTest::newRow("ifeb") << 2000 << 2 << 30 << 0U << false;
QTest::newRow("imar") << 2000 << 3 << 32 << 0U << false;
QTest::newRow("iapr") << 2000 << 4 << 31 << 0U << false;
QTest::newRow("imay") << 2000 << 5 << 32 << 0U << false;
QTest::newRow("ijun") << 2000 << 6 << 31 << 0U << false;
QTest::newRow("ijul") << 2000 << 7 << 32 << 0U << false;
QTest::newRow("iaug") << 2000 << 8 << 32 << 0U << false;
QTest::newRow("isep") << 2000 << 9 << 31 << 0U << false;
QTest::newRow("ioct") << 2000 << 10 << 32 << 0U << false;
QTest::newRow("inov") << 2000 << 11 << 31 << 0U << false;
QTest::newRow("idec") << 2000 << 12 << 32 << 0U << false;
// the beginning of the Julian Day calendar:
QTest::newRow("jd negative1") << -4714 << 1 << 1 << 0U << false;
QTest::newRow("jd negative2") << -4713 << 1 << 1 << 0U << false;
QTest::newRow("jd negative3") << -4713 << 1 << 2 << 1U << true;
QTest::newRow("jd negative4") << -4713 << 1 << 3 << 2U << true;
QTest::newRow("jd 0") << -4713 << 1 << 1 << 0U << false;
QTest::newRow("jd 1") << -4713 << 1 << 2 << 1U << true;
QTest::newRow("imminent overflow") << 11754508 << 12 << 13 << 4294967295U << true;
}
void tst_QDate::isValid()
{
QFETCH(int, year);
QFETCH(int, month);
QFETCH(int, day);
QTEST(QDate::isValid(year, month, day), "valid");
QDate d;
d.setDate(year, month, day);
QTEST(d.isValid(), "valid");
}
void tst_QDate::julianDay_data()
{
isValid_data();
}
void tst_QDate::julianDay()
{
QFETCH(int, year);
QFETCH(int, month);
QFETCH(int, day);
QFETCH(uint, jd);
{
QDate d;
d.setDate(year, month, day);
QCOMPARE(uint(d.toJulianDay()), jd);
}
if (jd) {
QDate d = QDate::fromJulianDay(jd);
QCOMPARE(d.year(), year);
QCOMPARE(d.month(), month);
QCOMPARE(d.day(), day);
}
}
void tst_QDate::weekNumber_data()
{
QTest::addColumn<int>("expectedWeekNum");
QTest::addColumn<int>("expectedYearNum");
QTest::addColumn<int>("year");
QTest::addColumn<int>("month");
QTest::addColumn<int>("day");
//next we fill it with data
QTest::newRow( "data0" ) << 10 << 2002 << 2002 << 3 << 8;
QTest::newRow( "data1" ) << 10 << 2002 << 2002 << 3 << 8;
QTest::newRow( "data2" ) << 52 << 1999 << 2000 << 1 << 1;
QTest::newRow( "data3" ) << 52 << 1999 << 1999 << 12 << 31;
QTest::newRow( "data4" ) << 1 << 2001 << 2001 << 1 << 1;
QTest::newRow( "data5" ) << 53 << 1998 << 1998 << 12 << 31;
QTest::newRow( "data6" ) << 1 << 1985 << 1984 << 12 << 31;
QTest::newRow( "data7" ) << 52 << 2006 << 2006 << 12 << 31;
}
void tst_QDate::weekNumber()
{
int yearNumber;
QFETCH( int, year );
QFETCH( int, month );
QFETCH( int, day );
QFETCH( int, expectedWeekNum );
QFETCH( int, expectedYearNum );
QDate dt1( year, month, day );
QCOMPARE( dt1.weekNumber( &yearNumber ), expectedWeekNum );
QCOMPARE( yearNumber, expectedYearNum );
}
void tst_QDate::weekNumber_invalid_data()
{
QTest::addColumn<int>("year");
QTest::addColumn<int>("month");
QTest::addColumn<int>("day");
//next we fill it with data
QTest::newRow( "data0" ) << 0 << 0 << 0;
QTest::newRow( "data1" ) << 2001 << 1 << 32;
QTest::newRow( "data2" ) << 1999 << 2 << 29;
}
void tst_QDate::weekNumber_invalid()
{
QDate dt;
int yearNumber;
QCOMPARE( dt.weekNumber( &yearNumber ), 0 );
}
void tst_QDate::addDays()
{
QFETCH( int, year );
QFETCH( int, month );
QFETCH( int, day );
QFETCH( int, amountToAdd );
QFETCH( int, expectedYear );
QFETCH( int, expectedMonth );
QFETCH( int, expectedDay );
QDate dt( year, month, day );
dt = dt.addDays( amountToAdd );
QCOMPARE( dt.year(), expectedYear );
QCOMPARE( dt.month(), expectedMonth );
QCOMPARE( dt.day(), expectedDay );
}
void tst_QDate::addDays_data()
{
QTest::addColumn<int>("year");
QTest::addColumn<int>("month");
QTest::addColumn<int>("day");
QTest::addColumn<int>("amountToAdd");
QTest::addColumn<int>("expectedYear");
QTest::addColumn<int>("expectedMonth");
QTest::addColumn<int>("expectedDay");
QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 1 << 2;
QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 1;
QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 2 << 29;
QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 1;
QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 1;
QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 1;
QTest::newRow( "data6" ) << 2001 << 2 << 28 << 30 << 2001 << 3 << 30;
QTest::newRow( "data7" ) << 2001 << 3 << 30 << 5 << 2001 << 4 << 4;
QTest::newRow( "data8" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 31;
QTest::newRow( "data9" ) << 2000 << 1 << 31 << -1 << 2000 << 1 << 30;
QTest::newRow( "data10" ) << 2000 << 2 << 28 << -1 << 2000 << 2 << 27;
QTest::newRow( "data11" ) << 2001 << 2 << 28 << -30 << 2001 << 1 << 29;
QDate invalid;
QTest::newRow( "data12" ) << -4713 << 1 << 2 << -2
<< invalid.year() << invalid.month() << invalid.day();
}
void tst_QDate::addMonths()
{
QFETCH( int, year );
QFETCH( int, month );
QFETCH( int, day );
QFETCH( int, amountToAdd );
QFETCH( int, expectedYear );
QFETCH( int, expectedMonth );
QFETCH( int, expectedDay );
QDate dt( year, month, day );
dt = dt.addMonths( amountToAdd );
QCOMPARE( dt.year(), expectedYear );
QCOMPARE( dt.month(), expectedMonth );
QCOMPARE( dt.day(), expectedDay );
}
void tst_QDate::addMonths_data()
{
QTest::addColumn<int>("year");
QTest::addColumn<int>("month");
QTest::addColumn<int>("day");
QTest::addColumn<int>("amountToAdd");
QTest::addColumn<int>("expectedYear");
QTest::addColumn<int>("expectedMonth");
QTest::addColumn<int>("expectedDay");
QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 2 << 1;
QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 29;
QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 3 << 28;
QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 29;
QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 31;
QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 28;
QTest::newRow( "data6" ) << 2001 << 2 << 28 << 12 << 2002 << 2 << 28;
QTest::newRow( "data7" ) << 2000 << 2 << 29 << 12 << 2001 << 2 << 28;
QTest::newRow( "data8" ) << 2000 << 10 << 15 << 4 << 2001 << 2 << 15;
QTest::newRow( "data9" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 1;
QTest::newRow( "data10" ) << 2000 << 1 << 31 << -1 << 1999 << 12 << 31;
QTest::newRow( "data11" ) << 2000 << 12 << 31 << -1 << 2000 << 11 << 30;
QTest::newRow( "data12" ) << 2001 << 2 << 28 << -12 << 2000 << 2 << 28;
QTest::newRow( "data13" ) << 2000 << 1 << 31 << -7 << 1999 << 6 << 30;
QTest::newRow( "data14" ) << 2000 << 2 << 29 << -12 << 1999 << 2 << 28;
// year sign change:
QTest::newRow( "data14" ) << 1 << 1 << 1 << -1 << -1 << 12 << 1;
QTest::newRow( "data15" ) << 1 << 1 << 1 << -12 << -1 << 1 << 1;
QTest::newRow( "data16" ) << -1 << 12 << 1 << 1 << 1 << 1 << 1;
QTest::newRow( "data17" ) << -1 << 1 << 1 << 12 << 1 << 1 << 1;
// Gregorian/Julian switchover
QTest::newRow( "data18" ) << 1582 << 9 << 4 << 1 << 1582 << 10 << 4;
QTest::newRow( "data19" ) << 1582 << 9 << 10 << 1 << 1582 << 10 << 15;
QTest::newRow( "data20" ) << 1582 << 9 << 20 << 1 << 1582 << 10 << 20;
QTest::newRow( "data21" ) << 1582 << 11 << 4 << -1 << 1582 << 10 << 4;
QTest::newRow( "data22" ) << 1582 << 11 << 10 << -1 << 1582 << 10 << 4;
QTest::newRow( "data23" ) << 1582 << 11 << 20 << -1 << 1582 << 10 << 20;
}
void tst_QDate::addYears()
{
QFETCH( int, year );
QFETCH( int, month );
QFETCH( int, day );
QFETCH( int, amountToAdd );
QFETCH( int, expectedYear );
QFETCH( int, expectedMonth );
QFETCH( int, expectedDay );
QDate dt( year, month, day );
dt = dt.addYears( amountToAdd );
QCOMPARE( dt.year(), expectedYear );
QCOMPARE( dt.month(), expectedMonth );
QCOMPARE( dt.day(), expectedDay );
}
void tst_QDate::addYears_data()
{
QTest::addColumn<int>("year");
QTest::addColumn<int>("month");
QTest::addColumn<int>("day");
QTest::addColumn<int>("amountToAdd");
QTest::addColumn<int>("expectedYear");
QTest::addColumn<int>("expectedMonth");
QTest::addColumn<int>("expectedDay");
QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2001 << 1 << 1;
QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2001 << 1 << 31;
QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2001 << 2 << 28;
QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2001 << 2 << 28;
QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 12 << 31;
QTest::newRow( "data5" ) << 2001 << 2 << 28 << 3 << 2004 << 2 << 28;
QTest::newRow( "data6" ) << 2000 << 2 << 29 << 4 << 2004 << 2 << 29;
QTest::newRow( "data7" ) << 2000 << 1 << 31 << -1 << 1999 << 1 << 31;
QTest::newRow( "data9" ) << 2000 << 2 << 29 << -1 << 1999 << 2 << 28;
QTest::newRow( "data10" ) << 2000 << 12 << 31 << -1 << 1999 << 12 << 31;
QTest::newRow( "data11" ) << 2001 << 2 << 28 << -3 << 1998 << 2 << 28;
QTest::newRow( "data12" ) << 2000 << 2 << 29 << -4 << 1996 << 2 << 29;
QTest::newRow( "data13" ) << 2000 << 2 << 29 << -5 << 1995 << 2 << 28;
QTest::newRow( "data14" ) << 2000 << 1 << 1 << -1999 << 1 << 1 << 1;
QTest::newRow( "data15" ) << 2000 << 1 << 1 << -2000 << -1 << 1 << 1;
QTest::newRow( "data16" ) << 2000 << 1 << 1 << -2001 << -2 << 1 << 1;
QTest::newRow( "data17" ) << -2000 << 1 << 1 << 1999 << -1 << 1 << 1;
QTest::newRow( "data18" ) << -2000 << 1 << 1 << 2000 << 1 << 1 << 1;
QTest::newRow( "data19" ) << -2000 << 1 << 1 << 2001 << 2 << 1 << 1;
}
void tst_QDate::operator_eq_eq()
{
QDate d1(2000,1,2);
QDate d2(2000,1,2);
QVERIFY( d1 == d2 );
d1 = QDate(2001,12,5);
d2 = QDate(2001,12,5);
QVERIFY( d1 == d2 );
d2 = QDate(2002,12,5);
QVERIFY( !(d1 == d2) );
}
void tst_QDate::operator_not_eq()
{
QDate d1(2000,1,2);
QDate d2(2000,1,2);
QVERIFY( !(d1 != d2) );
d1 = QDate(2001,12,5);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 != d2) );
d2 = QDate(2002,12,5);
QVERIFY( d1 != d2 );
}
void tst_QDate::operator_lt()
{
QDate d1(2000,1,2);
QDate d2(2000,1,2);
QVERIFY( !(d1 < d2) );
d1 = QDate(2001,12,4);
d2 = QDate(2001,12,5);
QVERIFY( d1 < d2 );
d1 = QDate(2001,11,5);
d2 = QDate(2001,12,5);
QVERIFY( d1 < d2 );
d1 = QDate(2000,12,5);
d2 = QDate(2001,12,5);
QVERIFY( d1 < d2 );
d1 = QDate(2002,12,5);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 < d2) );
d1 = QDate(2001,12,5);
d2 = QDate(2001,11,5);
QVERIFY( !(d1 < d2) );
d1 = QDate(2001,12,6);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 < d2) );
}
void tst_QDate::operator_gt()
{
QDate d1(2000,1,2);
QDate d2(2000,1,2);
QVERIFY( !(d1 > d2) );
d1 = QDate(2001,12,4);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 > d2) );
d1 = QDate(2001,11,5);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 > d2) );
d1 = QDate(2000,12,5);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 > d2) );
d1 = QDate(2002,12,5);
d2 = QDate(2001,12,5);
QVERIFY( d1 > d2 );
d1 = QDate(2001,12,5);
d2 = QDate(2001,11,5);
QVERIFY( d1 > d2 );
d1 = QDate(2001,12,6);
d2 = QDate(2001,12,5);
QVERIFY( d1 > d2 );
}
void tst_QDate::operator_lt_eq()
{
QDate d1(2000,1,2);
QDate d2(2000,1,2);
QVERIFY( d1 <= d2 );
d1 = QDate(2001,12,4);
d2 = QDate(2001,12,5);
QVERIFY( d1 <= d2 );
d1 = QDate(2001,11,5);
d2 = QDate(2001,12,5);
QVERIFY( d1 <= d2 );
d1 = QDate(2000,12,5);
d2 = QDate(2001,12,5);
QVERIFY( d1 <= d2 );
d1 = QDate(2002,12,5);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 <= d2) );
d1 = QDate(2001,12,5);
d2 = QDate(2001,11,5);
QVERIFY( !(d1 <= d2) );
d1 = QDate(2001,12,6);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 <= d2) );
}
void tst_QDate::operator_gt_eq()
{
QDate d1(2000,1,2);
QDate d2(2000,1,2);
QVERIFY( d1 >= d2 );
d1 = QDate(2001,12,4);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 >= d2) );
d1 = QDate(2001,11,5);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 >= d2) );
d1 = QDate(2000,12,5);
d2 = QDate(2001,12,5);
QVERIFY( !(d1 >= d2) );
d1 = QDate(2002,12,5);
d2 = QDate(2001,12,5);
QVERIFY( d1 >= d2 );
d1 = QDate(2001,12,5);
d2 = QDate(2001,11,5);
QVERIFY( d1 >= d2 );
d1 = QDate(2001,12,6);
d2 = QDate(2001,12,5);
QVERIFY( d1 >= d2 );
}
void tst_QDate::fromString_data()
{
// Since we can't define an element of Qt::DateFormat, d1 will be the date
// expected when we have a TextDate, and d2 will be the date expected when
// we have an ISODate.
QTest::addColumn<QString>("str1");
QTest::addColumn<QString>("str2");
QTest::addColumn<QDate>("d1");
QTest::newRow( "data0" ) << QString("Sat May 20 1995") << QString("1995-05-20") << QDate(1995,5,20);
QTest::newRow( "data1" ) << QString("Tue Dec 17 2002") << QString("2002-12-17") << QDate(2002,12,17);
QDate d( 1999, 11, 14 );
QTest::newRow( "data2" ) << d.toString( Qt::TextDate ) << d.toString( Qt::ISODate ) << d;
QTest::newRow( "data3" ) << QString("xxx Jan 1 0999") << QString("0999-01-01") << QDate(999, 1, 1);
QTest::newRow( "data3b" ) << QString("xxx Jan 1 999") << QString("0999-01-01") << QDate(999, 1, 1);
QTest::newRow( "data4" ) << QString("xxx Jan 1 12345") << QString() << QDate(12345, 1, 1);
QTest::newRow( "data5" ) << QString("xxx Jan 1 -0001") << QString() << QDate(-1, 1, 1);
QTest::newRow( "data6" ) << QString("xxx Jan 1 -4712") << QString() << QDate(-4712, 1, 1);
QTest::newRow( "data7" ) << QString("xxx Nov 25 -4713") << QString() << QDate(-4713, 11, 25);
}
void tst_QDate::fromString()
{
QFETCH( QString, str1 );
QFETCH( QString, str2 );
QFETCH( QDate, d1 );
QCOMPARE( QDate::fromString( str1, Qt::TextDate ), d1 );
if (!str2.isEmpty())
QCOMPARE( QDate::fromString( str2, Qt::ISODate ), d1 );
}
void tst_QDate::fromString_format_data()
{
QTest::addColumn<QString>("string");
QTest::addColumn<QString>("format");
QTest::addColumn<QDate>("date");
//year with yy is always 19xx for compatibility
QTest::newRow( "data0" ) << QString("21052006") << QString("ddMMyyyy") << QDate(2006,5,21);
QTest::newRow( "data1" ) << QString("210506") << QString("ddMMyy") << QDate(1906,5,21);
QTest::newRow( "data2" ) << QString("21/5/2006") << QString("d/M/yyyy") << QDate(2006,5,21);
QTest::newRow( "data3" ) << QString("21/5/06") << QString("d/M/yy") << QDate(1906,5,21);
QTest::newRow( "data4" ) << QString("20060521") << QString("yyyyMMdd") << QDate(2006,5,21);
QTest::newRow( "data5" ) << QString("060521") << QString("yyMMdd") << QDate(1906,5,21);
}
void tst_QDate::fromString_format()
{
QFETCH( QString, string );
QFETCH( QString, format );
QFETCH( QDate, date );
QCOMPARE( QDate::fromString( string, format ), date );
}
void tst_QDate::toString_format_data()
{
QTest::addColumn<QDate>("t");
QTest::addColumn<QString>("format");
QTest::addColumn<QString>("str");
QTest::newRow( "data0" ) << QDate(1995,5,20) << QString("d-M-yy") << QString("20-5-95");
QTest::newRow( "data1" ) << QDate(2002,12,17) << QString("dd-MM-yyyy") << QString("17-12-2002");
QTest::newRow( "data2" ) << QDate(1995,5,20) << QString("M-yy") << QString("5-95");
QTest::newRow( "data3" ) << QDate(2002,12,17) << QString("dd") << QString("17");
QTest::newRow( "data4" ) << QDate() << QString("dd-mm-yyyy") << QString();
}
void tst_QDate::toString_format()
{
QFETCH( QDate, t );
QFETCH( QString, format );
QFETCH( QString, str );
QCOMPARE( t.toString( format ), str );
}
void tst_QDate::isLeapYear()
{
QVERIFY(QDate::isLeapYear(-4445));
QVERIFY(!QDate::isLeapYear(-4444));
QVERIFY(!QDate::isLeapYear(-6));
QVERIFY(QDate::isLeapYear(-5));
QVERIFY(!QDate::isLeapYear(-4));
QVERIFY(!QDate::isLeapYear(-3));
QVERIFY(!QDate::isLeapYear(-2));
QVERIFY(QDate::isLeapYear(-1));
QVERIFY(!QDate::isLeapYear(0)); // Doesn't exist
QVERIFY(!QDate::isLeapYear(1));
QVERIFY(!QDate::isLeapYear(2));
QVERIFY(!QDate::isLeapYear(3));
QVERIFY(QDate::isLeapYear(4));
QVERIFY(!QDate::isLeapYear(7));
QVERIFY(QDate::isLeapYear(8));
QVERIFY(QDate::isLeapYear(100));
QVERIFY(QDate::isLeapYear(400));
QVERIFY(QDate::isLeapYear(700));
QVERIFY(QDate::isLeapYear(1500));
QVERIFY(QDate::isLeapYear(1600));
QVERIFY(!QDate::isLeapYear(1700));
QVERIFY(!QDate::isLeapYear(1800));
QVERIFY(!QDate::isLeapYear(1900));
QVERIFY(QDate::isLeapYear(2000));
QVERIFY(!QDate::isLeapYear(2100));
QVERIFY(!QDate::isLeapYear(2200));
QVERIFY(!QDate::isLeapYear(2300));
QVERIFY(QDate::isLeapYear(2400));
QVERIFY(!QDate::isLeapYear(2500));
QVERIFY(!QDate::isLeapYear(2600));
QVERIFY(!QDate::isLeapYear(2700));
QVERIFY(QDate::isLeapYear(2800));
for (int i = -4713; i <= 10000; ++i) {
if (i == 0)
continue;
QVERIFY(!QDate(i, 2, 29).isValid() == !QDate::isLeapYear(i));
}
}
void tst_QDate::yearsZeroToNinetyNine()
{
{
QDate dt(-1, 2, 3);
QCOMPARE(dt.year(), -1);
QCOMPARE(dt.month(), 2);
QCOMPARE(dt.day(), 3);
}
{
QDate dt(1, 2, 3);
QCOMPARE(dt.year(), 1);
QCOMPARE(dt.month(), 2);
QCOMPARE(dt.day(), 3);
}
{
QDate dt(99, 2, 3);
QCOMPARE(dt.year(), 99);
QCOMPARE(dt.month(), 2);
QCOMPARE(dt.day(), 3);
}
QVERIFY(!QDate::isValid(0, 2, 3));
QVERIFY(QDate::isValid(1, 2, 3));
QVERIFY(QDate::isValid(-1, 2, 3));
{
QDate dt;
dt.setYMD(1, 2, 3);
QCOMPARE(dt.year(), 1901);
QCOMPARE(dt.month(), 2);
QCOMPARE(dt.day(), 3);
}
{
QDate dt;
dt.setDate(1, 2, 3);
QCOMPARE(dt.year(), 1);
QCOMPARE(dt.month(), 2);
QCOMPARE(dt.day(), 3);
dt.setDate(0, 2, 3);
QVERIFY(!dt.isValid());
}
}
void tst_QDate::toString()
{
QDate date(1974,12,1);
QCOMPARE(date.toString(Qt::SystemLocaleDate),
QLocale::system().toString(date, QLocale::ShortFormat));
QCOMPARE(date.toString(Qt::LocaleDate),
QLocale().toString(date, QLocale::ShortFormat));
QLocale::setDefault(QLocale::German);
QCOMPARE(date.toString(Qt::SystemLocaleDate),
QLocale::system().toString(date, QLocale::ShortFormat));
QCOMPARE(date.toString(Qt::LocaleDate),
QLocale().toString(date, QLocale::ShortFormat));
}
void tst_QDate::negativeYear() const
{
QDate y(-20, 3, 4);
QVERIFY(y.isValid());
QCOMPARE(y.year(), -20);
}
void tst_QDate::printNegativeYear() const
{
{
QDate date(-500, 3, 4);
QVERIFY(date.isValid());
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0500"));
}
{
QDate date(-10, 3, 4);
QVERIFY(date.isValid());
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0010"));
}
{
QDate date(-2, 3, 4);
QVERIFY(date.isValid());
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0002"));
}
}
void tst_QDate::roundtripGermanLocale() const
{
/* This code path should not result in warnings. */
const QDate theDate(QDate::currentDate());
theDate.fromString(theDate.toString(Qt::TextDate), Qt::TextDate);
const QDateTime theDateTime(QDateTime::currentDateTime());
theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate);
}
void tst_QDate::shortDayName() const
{
if (QLocale::system().language() == QLocale::C) {
QCOMPARE(QDate::shortDayName(1), QLatin1String("Mon"));
QCOMPARE(QDate::shortDayName(7), QLatin1String("Sun"));
}
QLocale locale = QLocale::system();
for(int i = 1; i <= 7; ++i) {
QCOMPARE(QDate::shortDayName(i), locale.dayName(i, QLocale::ShortFormat));
}
}
void tst_QDate::standaloneShortDayName() const
{
if (QLocale::system().language() == QLocale::C) {
QCOMPARE(QDate::shortDayName(1, QDate::StandaloneFormat), QLatin1String("Mon"));
QCOMPARE(QDate::shortDayName(7, QDate::StandaloneFormat), QLatin1String("Sun"));
}
QLocale locale = QLocale::system();
for(int i = 1; i <= 7; ++i) {
QCOMPARE(QDate::shortDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::ShortFormat));
}
}
void tst_QDate::longDayName() const
{
if (QLocale::system().language() == QLocale::C) {
QCOMPARE(QDate::longDayName(1), QLatin1String("Monday"));
QCOMPARE(QDate::longDayName(7), QLatin1String("Sunday"));
}
QLocale locale = QLocale::system();
for(int i = 1; i <= 7; ++i) {
QCOMPARE(QDate::longDayName(i), locale.dayName(i, QLocale::LongFormat));
}
}
void tst_QDate::standaloneLongDayName() const
{
if (QLocale::system().language() == QLocale::C) {
QCOMPARE(QDate::longDayName(1, QDate::StandaloneFormat), QLatin1String("Monday"));
QCOMPARE(QDate::longDayName(7, QDate::StandaloneFormat), QLatin1String("Sunday"));
}
QLocale locale = QLocale::system();
for(int i = 1; i <= 7; ++i) {
QCOMPARE(QDate::longDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::LongFormat));
}
}
void tst_QDate::shortMonthName() const
{
if (QLocale::system().language() == QLocale::C) {
QCOMPARE(QDate::shortMonthName(1), QLatin1String("Jan"));
QCOMPARE(QDate::shortMonthName(8), QLatin1String("Aug"));
}
QLocale locale = QLocale::system();
for(int i = 1; i <= 12; ++i) {
QCOMPARE(QDate::shortMonthName(i), locale.monthName(i, QLocale::ShortFormat));
}
}
void tst_QDate::standaloneShortMonthName() const
{
if (QLocale::system().language() == QLocale::C) {
QCOMPARE(QDate::shortMonthName(1, QDate::StandaloneFormat), QLatin1String("Jan"));
QCOMPARE(QDate::shortMonthName(8, QDate::StandaloneFormat), QLatin1String("Aug"));
}
QLocale locale = QLocale::system();
for(int i = 1; i <= 12; ++i) {
QCOMPARE(QDate::shortMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::ShortFormat));
}
}
void tst_QDate::longMonthName() const
{
if (QLocale::system().language() == QLocale::C) {
QCOMPARE(QDate::longMonthName(1), QLatin1String("January"));
QCOMPARE(QDate::longMonthName(8), QLatin1String("August"));
}
QLocale locale = QLocale::system();
for(int i = 1; i <= 12; ++i) {
QCOMPARE(QDate::longMonthName(i), locale.monthName(i, QLocale::LongFormat));
}
}
void tst_QDate::standaloneLongMonthName() const
{
if (QLocale::system().language() == QLocale::C) {
QCOMPARE(QDate::longMonthName(1, QDate::StandaloneFormat), QLatin1String("January"));
QCOMPARE(QDate::longMonthName(8, QDate::StandaloneFormat), QLatin1String("August"));
}
QLocale locale = QLocale::system();
for(int i = 1; i <= 12; ++i) {
QCOMPARE(QDate::longMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::LongFormat));
}
}
void tst_QDate::roundtrip() const
{
// Test round trip, this exercises setDate(), isValid(), isLeapYear(),
// year(), month(), day(), julianDayFromDate(), and getDateFromJulianDay()
// to ensure they are internally consistent (but doesn't guarantee correct)
// Test Julian round trip in both BC and AD
QDate testDate;
QDate loopDate = QDate::fromJulianDay(1684899); // 1 Jan 100 BC
while ( loopDate.toJulianDay() <= 1757948 ) { // 31 Dec 100 AD
testDate.setDate( loopDate.year(), loopDate.month(), loopDate.day() );
QCOMPARE( loopDate.toJulianDay(), testDate.toJulianDay() );
loopDate = loopDate.addDays(1);
}
// Test Julian and Gregorian round trip during changeover period
loopDate = QDate::fromJulianDay(2298153); // 1 Jan 1580 AD
while ( loopDate.toJulianDay() <= 2300334 ) { // 31 Dec 1585 AD
testDate.setDate( loopDate.year(), loopDate.month(), loopDate.day() );
QCOMPARE( loopDate.toJulianDay(), testDate.toJulianDay() );
loopDate = loopDate.addDays(1);
}
// Test Gregorian round trip during current useful period
loopDate = QDate::fromJulianDay(2378497); // 1 Jan 1900 AD
while ( loopDate.toJulianDay() <= 2488433 ) { // 31 Dec 2100 AD
testDate.setDate( loopDate.year(), loopDate.month(), loopDate.day() );
QCOMPARE( loopDate.toJulianDay(), testDate.toJulianDay() );
loopDate = loopDate.addDays(1);
}
}
QTEST_APPLESS_MAIN(tst_QDate)
#include "tst_qdate.moc"
|