Add leading zeros to years below 1000 in QDate::toString().
[profile/ivi/qtbase.git] / tests / auto / corelib / tools / qdate / tst_qdate.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include <qdatetime.h>
44 #include <qlocale.h>
45
46 class tst_QDate : public QObject
47 {
48     Q_OBJECT
49 private slots:
50     void isNull_data();
51     void isNull();
52     void isValid_data();
53     void isValid();
54     void julianDay_data();
55     void julianDay();
56     void dayOfWeek_data();
57     void dayOfWeek();
58     void dayOfYear_data();
59     void dayOfYear();
60     void daysInMonth_data();
61     void daysInMonth();
62     void daysInYear();
63     void getDate();
64     void weekNumber_invalid_data();
65     void weekNumber_invalid();
66     void weekNumber_data();
67     void weekNumber();
68     void julianDaysLimits();
69     void addDays_data();
70     void addDays();
71     void addMonths_data();
72     void addMonths();
73     void addYears_data();
74     void addYears();
75     void daysTo();
76     void operator_eq_eq();
77     void operator_not_eq();
78     void operator_lt();
79     void operator_gt();
80     void operator_lt_eq();
81     void operator_gt_eq();
82     void fromStringDateFormat_data();
83     void fromStringDateFormat();
84     void fromStringFormat_data();
85     void fromStringFormat();
86     void toStringFormat_data();
87     void toStringFormat();
88     void toStringDateFormat_data();
89     void toStringDateFormat();
90     void isLeapYear();
91     void yearsZeroToNinetyNine();
92     void negativeYear() const;
93     void printNegativeYear() const;
94     void roundtripGermanLocale() const;
95     void shortDayName() const;
96     void standaloneShortDayName() const;
97     void longDayName() const;
98     void standaloneLongDayName() const;
99     void shortMonthName() const;
100     void standaloneShortMonthName() const;
101     void longMonthName() const;
102     void standaloneLongMonthName() const;
103     void roundtrip() const;
104 private:
105     QDate defDate() const { return QDate(1900, 1, 1); }
106     QDate invalidDate() const { return QDate(); }
107 };
108
109 Q_DECLARE_METATYPE(QDate)
110 Q_DECLARE_METATYPE(Qt::DateFormat)
111
112 void tst_QDate::isNull_data()
113 {
114     QTest::addColumn<qint64>("jd");
115     QTest::addColumn<bool>("null");
116
117     qint64 minJd = std::numeric_limits<qint64>::min() / 2;
118     qint64 maxJd = std::numeric_limits<qint64>::max() / 2;
119
120     QTest::newRow("qint64 min") << std::numeric_limits<qint64>::min() << true;
121     QTest::newRow("minJd - 1")  << minJd - 1                          << true;
122     QTest::newRow("minJd")      << minJd                              << false;
123     QTest::newRow("minJd + 1")  << minJd + 1                          << false;
124     QTest::newRow("maxJd - 1")  << maxJd - 1                          << false;
125     QTest::newRow("maxJd")      << maxJd                              << false;
126     QTest::newRow("maxJd + 1")  << maxJd + 1                          << true;
127     QTest::newRow("qint64 max") << std::numeric_limits<qint64>::max() << true;
128 }
129
130 void tst_QDate::isNull()
131 {
132     QFETCH(qint64, jd);
133
134     QDate d = QDate::fromJulianDay(jd);
135     QTEST(d.isNull(), "null");
136 }
137
138 void tst_QDate::isValid_data()
139 {
140     qint64 nullJd = std::numeric_limits<qint64>::min();
141
142     QTest::addColumn<int>("year");
143     QTest::addColumn<int>("month");
144     QTest::addColumn<int>("day");
145     QTest::addColumn<qint64>("jd");
146     QTest::addColumn<bool>("valid");
147
148     QTest::newRow("0-0-0")    <<    0 <<  0 << 0 << nullJd << false;
149     QTest::newRow("month 0")  << 2000 <<  0 << 1 << nullJd << false;
150     QTest::newRow("day 0")    << 2000 <<  1 << 0 << nullJd << false;
151
152     QTest::newRow("month 13") << 2000 << 13 << 1 << nullJd << false;
153
154     // test leap years
155     QTest::newRow("non-leap")            << 2006 <<  2 << 29 << nullJd  << false;
156     QTest::newRow("normal leap")         << 2004 <<  2 << 29 << qint64(2453065) << true;
157     QTest::newRow("century leap 1900")   << 1900 <<  2 << 29 << nullJd  << false;
158     QTest::newRow("century leap 2100")   << 2100 <<  2 << 29 << nullJd  << false;
159     QTest::newRow("400-years leap 2000") << 2000 <<  2 << 29 << qint64(2451604) << true;
160     QTest::newRow("400-years leap 2400") << 2400 <<  2 << 29 << qint64(2597701) << true;
161     QTest::newRow("400-years leap 1600") << 1600 <<  2 << 29 << qint64(2305507) << true;
162     QTest::newRow("year 0")              <<    0 <<  2 << 27 << nullJd  << false;
163
164     // test the number of days in months:
165     QTest::newRow("jan") << 2000 <<  1 << 31 << qint64(2451575) << true;
166     QTest::newRow("feb") << 2000 <<  2 << 29 << qint64(2451604) << true; // same data as 400-years leap
167     QTest::newRow("mar") << 2000 <<  3 << 31 << qint64(2451635) << true;
168     QTest::newRow("apr") << 2000 <<  4 << 30 << qint64(2451665) << true;
169     QTest::newRow("may") << 2000 <<  5 << 31 << qint64(2451696) << true;
170     QTest::newRow("jun") << 2000 <<  6 << 30 << qint64(2451726) << true;
171     QTest::newRow("jul") << 2000 <<  7 << 31 << qint64(2451757) << true;
172     QTest::newRow("aug") << 2000 <<  8 << 31 << qint64(2451788) << true;
173     QTest::newRow("sep") << 2000 <<  9 << 30 << qint64(2451818) << true;
174     QTest::newRow("oct") << 2000 << 10 << 31 << qint64(2451849) << true;
175     QTest::newRow("nov") << 2000 << 11 << 30 << qint64(2451879) << true;
176     QTest::newRow("dec") << 2000 << 12 << 31 << qint64(2451910) << true;
177
178     // and invalid dates:
179     QTest::newRow("ijan") << 2000 <<  1 << 32 << nullJd << false;
180     QTest::newRow("ifeb") << 2000 <<  2 << 30 << nullJd << false;
181     QTest::newRow("imar") << 2000 <<  3 << 32 << nullJd << false;
182     QTest::newRow("iapr") << 2000 <<  4 << 31 << nullJd << false;
183     QTest::newRow("imay") << 2000 <<  5 << 32 << nullJd << false;
184     QTest::newRow("ijun") << 2000 <<  6 << 31 << nullJd << false;
185     QTest::newRow("ijul") << 2000 <<  7 << 32 << nullJd << false;
186     QTest::newRow("iaug") << 2000 <<  8 << 32 << nullJd << false;
187     QTest::newRow("isep") << 2000 <<  9 << 31 << nullJd << false;
188     QTest::newRow("ioct") << 2000 << 10 << 32 << nullJd << false;
189     QTest::newRow("inov") << 2000 << 11 << 31 << nullJd << false;
190     QTest::newRow("idec") << 2000 << 12 << 32 << nullJd << false;
191
192     // the beginning of the Julian Day calendar:
193     QTest::newRow("jd earliest formula") <<   -4800 <<  1 <<  1 << qint64(   -31738) << true;
194     QTest::newRow("jd -1")               <<   -4714 << 11 << 23 << qint64(       -1) << true;
195     QTest::newRow("jd 0")                <<   -4714 << 11 << 24 << qint64(        0) << true;
196     QTest::newRow("jd 1")                <<   -4714 << 11 << 25 << qint64(        1) << true;
197     QTest::newRow("jd latest formula")   << 1400000 << 12 << 31 << qint64(513060925) << true;
198 }
199
200 void tst_QDate::isValid()
201 {
202     QFETCH(int, year);
203     QFETCH(int, month);
204     QFETCH(int, day);
205     QFETCH(qint64, jd);
206     QFETCH(bool, valid);
207
208     QCOMPARE(QDate::isValid(year, month, day), valid);
209
210     QDate d;
211     d.setDate(year, month, day);
212     QCOMPARE(d.isValid(), valid);
213     QCOMPARE(d.toJulianDay(), jd);
214
215     if (valid) {
216         QCOMPARE(d.year(), year);
217         QCOMPARE(d.month(), month);
218         QCOMPARE(d.day(), day);
219     } else {
220         QCOMPARE(d.year(), 0);
221         QCOMPARE(d.month(), 0);
222         QCOMPARE(d.day(), 0);
223     }
224 }
225
226 void tst_QDate::julianDay_data()
227 {
228     isValid_data();
229 }
230
231 void tst_QDate::julianDay()
232 {
233     QFETCH(int, year);
234     QFETCH(int, month);
235     QFETCH(int, day);
236     QFETCH(qint64, jd);
237
238     {
239         QDate d;
240         d.setDate(year, month, day);
241         QCOMPARE(d.toJulianDay(), jd);
242     }
243
244     if (jd != std::numeric_limits<qint64>::min()) {
245         QDate d = QDate::fromJulianDay(jd);
246         QCOMPARE(d.year(), year);
247         QCOMPARE(d.month(), month);
248         QCOMPARE(d.day(), day);
249     }
250 }
251
252 void tst_QDate::dayOfWeek_data()
253 {
254     QTest::addColumn<int>("year");
255     QTest::addColumn<int>("month");
256     QTest::addColumn<int>("day");
257     QTest::addColumn<int>("dayOfWeek");
258
259     QTest::newRow("data0")  <<     0 <<  0 <<  0 << 0;
260     QTest::newRow("data1")  <<  2000 <<  1 <<  3 << 1;
261     QTest::newRow("data2")  <<  2000 <<  1 <<  4 << 2;
262     QTest::newRow("data3")  <<  2000 <<  1 <<  5 << 3;
263     QTest::newRow("data4")  <<  2000 <<  1 <<  6 << 4;
264     QTest::newRow("data5")  <<  2000 <<  1 <<  7 << 5;
265     QTest::newRow("data6")  <<  2000 <<  1 <<  8 << 6;
266     QTest::newRow("data7")  <<  2000 <<  1 <<  9 << 7;
267     QTest::newRow("data8")  << -4800 <<  1 <<  1 << 1;
268     QTest::newRow("data9")  << -4800 <<  1 <<  2 << 2;
269     QTest::newRow("data10") << -4800 <<  1 <<  3 << 3;
270     QTest::newRow("data11") << -4800 <<  1 <<  4 << 4;
271     QTest::newRow("data12") << -4800 <<  1 <<  5 << 5;
272     QTest::newRow("data13") << -4800 <<  1 <<  6 << 6;
273     QTest::newRow("data14") << -4800 <<  1 <<  7 << 7;
274     QTest::newRow("data15") << -4800 <<  1 <<  8 << 1;
275 }
276
277 void tst_QDate::dayOfWeek()
278 {
279     QFETCH(int, year);
280     QFETCH(int, month);
281     QFETCH(int, day);
282     QFETCH(int, dayOfWeek);
283
284     QDate dt(year, month, day);
285     QCOMPARE(dt.dayOfWeek(), dayOfWeek);
286 }
287
288 void tst_QDate::dayOfYear_data()
289 {
290     QTest::addColumn<int>("year");
291     QTest::addColumn<int>("month");
292     QTest::addColumn<int>("day");
293     QTest::addColumn<int>("dayOfYear");
294
295     QTest::newRow("data0")  <<     0 <<  0 <<  0 <<   0;
296     QTest::newRow("data1")  <<  2000 <<  1 <<  1 <<   1;
297     QTest::newRow("data2")  <<  2000 <<  1 <<  2 <<   2;
298     QTest::newRow("data3")  <<  2000 <<  1 <<  3 <<   3;
299     QTest::newRow("data4")  <<  2000 << 12 << 31 << 366;
300     QTest::newRow("data5")  <<  2001 << 12 << 31 << 365;
301     QTest::newRow("data6")  <<  1815 <<  1 <<  1 <<   1;
302     QTest::newRow("data7")  <<  1815 << 12 << 31 << 365;
303     QTest::newRow("data8")  <<  1500 <<  1 <<  1 <<   1;
304     QTest::newRow("data9")  <<  1500 << 12 << 31 << 365;
305     QTest::newRow("data10") << -1500 <<  1 <<  1 <<   1;
306     QTest::newRow("data11") << -1500 << 12 << 31 << 365;
307     QTest::newRow("data12") << -4800 <<  1 <<  1 <<   1;
308     QTest::newRow("data13") << -4800 << 12 << 31 << 365;
309 }
310
311 void tst_QDate::dayOfYear()
312 {
313     QFETCH(int, year);
314     QFETCH(int, month);
315     QFETCH(int, day);
316     QFETCH(int, dayOfYear);
317
318     QDate dt(year, month, day);
319     QCOMPARE(dt.dayOfYear(), dayOfYear);
320 }
321
322 void tst_QDate::daysInMonth_data()
323 {
324     QTest::addColumn<int>("year");
325     QTest::addColumn<int>("month");
326     QTest::addColumn<int>("day");
327     QTest::addColumn<int>("daysInMonth");
328
329     QTest::newRow("data0")  <<     0 <<  0 <<  0 <<   0;
330     QTest::newRow("data1")  <<  2000 <<  1 <<  1 <<  31;
331     QTest::newRow("data2")  <<  2000 <<  2 <<  1 <<  29;
332     QTest::newRow("data3")  <<  2000 <<  3 <<  1 <<  31;
333     QTest::newRow("data4")  <<  2000 <<  4 <<  1 <<  30;
334     QTest::newRow("data5")  <<  2000 <<  5 <<  1 <<  31;
335     QTest::newRow("data6")  <<  2000 <<  6 <<  1 <<  30;
336     QTest::newRow("data7")  <<  2000 <<  7 <<  1 <<  31;
337     QTest::newRow("data8")  <<  2000 <<  8 <<  1 <<  31;
338     QTest::newRow("data9")  <<  2000 <<  9 <<  1 <<  30;
339     QTest::newRow("data10") <<  2000 << 10 <<  1 <<  31;
340     QTest::newRow("data11") <<  2000 << 11 <<  1 <<  30;
341     QTest::newRow("data12") <<  2000 << 12 <<  1 <<  31;
342     QTest::newRow("data13") <<  2001 <<  2 <<  1 <<  28;
343 }
344
345 void tst_QDate::daysInMonth()
346 {
347     QFETCH(int, year);
348     QFETCH(int, month);
349     QFETCH(int, day);
350     QFETCH(int, daysInMonth);
351
352     QDate dt(year, month, day);
353     QCOMPARE(dt.daysInMonth(), daysInMonth);
354 }
355
356 void tst_QDate::daysInYear()
357 {
358     QDate dt(2000, 1, 1);
359     QCOMPARE(dt.daysInYear(), 366);
360     dt.setDate(2001, 1, 1);
361     QCOMPARE(dt.daysInYear(), 365);
362     dt.setDate(4, 1, 1);
363     QCOMPARE(dt.daysInYear(), 366);
364     dt.setDate(5, 1, 1);
365     QCOMPARE(dt.daysInYear(), 365);
366 }
367
368 void tst_QDate::getDate()
369 {
370     int y, m, d;
371     QDate dt(2000, 1, 1);
372     dt.getDate(&y, &m, &d);
373     QCOMPARE(y, 2000);
374     QCOMPARE(m, 1);
375     QCOMPARE(d, 1);
376     dt.setDate(0, 0, 0);
377     dt.getDate(&y, &m, &d);
378     QCOMPARE(y, 0);
379     QCOMPARE(m, 0);
380     QCOMPARE(d, 0);
381 }
382
383 void tst_QDate::weekNumber_data()
384 {
385     QTest::addColumn<int>("expectedWeekNum");
386     QTest::addColumn<int>("expectedYearNum");
387     QTest::addColumn<int>("year");
388     QTest::addColumn<int>("month");
389     QTest::addColumn<int>("day");
390
391     //next we fill it with data
392     QTest::newRow( "data0" )  << 10 << 2002 << 2002 << 3 << 8;
393     QTest::newRow( "data1" )  << 10 << 2002 << 2002 << 3 << 8;
394     QTest::newRow( "data2" )  << 52 << 1999 << 2000 << 1 << 1;
395     QTest::newRow( "data3" )  << 52 << 1999 << 1999 << 12 << 31;
396     QTest::newRow( "data4" )  << 1 << 2001 << 2001 << 1 << 1;
397     QTest::newRow( "data5" )  << 53 << 1998 << 1998 << 12 << 31;
398     QTest::newRow( "data6" )  << 1 << 1985 << 1984 << 12 << 31;
399     QTest::newRow( "data7" )  << 52 << 2006 << 2006 << 12 << 31;
400 }
401
402 void tst_QDate::weekNumber()
403 {
404     int yearNumber;
405     QFETCH( int, year );
406     QFETCH( int, month );
407     QFETCH( int, day );
408     QFETCH( int, expectedWeekNum );
409     QFETCH( int, expectedYearNum );
410     QDate dt1( year, month, day );
411     QCOMPARE( dt1.weekNumber( &yearNumber ), expectedWeekNum );
412     QCOMPARE( yearNumber, expectedYearNum );
413 }
414
415 void tst_QDate::weekNumber_invalid_data()
416 {
417     QTest::addColumn<int>("year");
418     QTest::addColumn<int>("month");
419     QTest::addColumn<int>("day");
420
421     //next we fill it with data
422     QTest::newRow( "data0" )  << 0 << 0 << 0;
423     QTest::newRow( "data1" )  << 2001 << 1 << 32;
424     QTest::newRow( "data2" )  << 1999 << 2 << 29;
425 }
426
427 void tst_QDate::weekNumber_invalid()
428 {
429     QDate dt;
430     int yearNumber;
431     QCOMPARE( dt.weekNumber( &yearNumber ), 0 );
432 }
433
434 void tst_QDate::julianDaysLimits()
435 {
436     qint64 min = std::numeric_limits<qint64>::min();
437     qint64 max = std::numeric_limits<qint64>::max();
438     qint64 minJd = std::numeric_limits<qint64>::min() / 2;
439     qint64 maxJd = std::numeric_limits<qint64>::max() / 2;
440
441     QDate maxDate = QDate::fromJulianDay(maxJd);
442     QDate minDate = QDate::fromJulianDay(minJd);
443     QDate zeroDate = QDate::fromJulianDay(0);
444
445     QDate dt = QDate::fromJulianDay(min);
446     QCOMPARE(dt.isValid(), false);
447     dt = QDate::fromJulianDay(minJd - 1);
448     QCOMPARE(dt.isValid(), false);
449     dt = QDate::fromJulianDay(minJd);
450     QCOMPARE(dt.isValid(), true);
451     dt = QDate::fromJulianDay(minJd + 1);
452     QCOMPARE(dt.isValid(), true);
453     dt = QDate::fromJulianDay(maxJd - 1);
454     QCOMPARE(dt.isValid(), true);
455     dt = QDate::fromJulianDay(maxJd);
456     QCOMPARE(dt.isValid(), true);
457     dt = QDate::fromJulianDay(maxJd + 1);
458     QCOMPARE(dt.isValid(), false);
459     dt = QDate::fromJulianDay(max);
460     QCOMPARE(dt.isValid(), false);
461
462     dt = maxDate.addDays(1);
463     QCOMPARE(dt.isValid(), false);
464     dt = maxDate.addDays(0);
465     QCOMPARE(dt.isValid(), true);
466     dt = maxDate.addDays(-1);
467     QCOMPARE(dt.isValid(), true);
468     dt = maxDate.addDays(max);
469     QCOMPARE(dt.isValid(), false);
470     dt = maxDate.addDays(min);
471     QCOMPARE(dt.isValid(), false);
472
473     dt = minDate.addDays(-1);
474     QCOMPARE(dt.isValid(), false);
475     dt = minDate.addDays(0);
476     QCOMPARE(dt.isValid(), true);
477     dt = minDate.addDays(1);
478     QCOMPARE(dt.isValid(), true);
479     dt = minDate.addDays(min);
480     QCOMPARE(dt.isValid(), false);
481     dt = minDate.addDays(max);
482     QCOMPARE(dt.isValid(), true);
483
484     dt = zeroDate.addDays(-1);
485     QCOMPARE(dt.isValid(), true);
486     dt = zeroDate.addDays(0);
487     QCOMPARE(dt.isValid(), true);
488     dt = zeroDate.addDays(1);
489     QCOMPARE(dt.isValid(), true);
490     dt = zeroDate.addDays(min);
491     QCOMPARE(dt.isValid(), false);
492     dt = zeroDate.addDays(max);
493     QCOMPARE(dt.isValid(), false);
494 }
495
496 void tst_QDate::addDays()
497 {
498     QFETCH( int, year );
499     QFETCH( int, month );
500     QFETCH( int, day );
501     QFETCH( int, amountToAdd );
502     QFETCH( int, expectedYear );
503     QFETCH( int, expectedMonth );
504     QFETCH( int, expectedDay );
505
506     QDate dt( year, month, day );
507     dt = dt.addDays( amountToAdd );
508
509     QCOMPARE( dt.year(), expectedYear );
510     QCOMPARE( dt.month(), expectedMonth );
511     QCOMPARE( dt.day(), expectedDay );
512 }
513
514 void tst_QDate::addDays_data()
515 {
516     QTest::addColumn<int>("year");
517     QTest::addColumn<int>("month");
518     QTest::addColumn<int>("day");
519     QTest::addColumn<int>("amountToAdd");
520     QTest::addColumn<int>("expectedYear");
521     QTest::addColumn<int>("expectedMonth");
522     QTest::addColumn<int>("expectedDay");
523
524     QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 1 << 2;
525     QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 1;
526     QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 2 << 29;
527     QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 1;
528     QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 1;
529     QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 1;
530     QTest::newRow( "data6" ) << 2001 << 2 << 28 << 30 << 2001 << 3 << 30;
531     QTest::newRow( "data7" ) << 2001 << 3 << 30 << 5 << 2001 << 4 << 4;
532
533     QTest::newRow( "data8" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 31;
534     QTest::newRow( "data9" ) << 2000 << 1 << 31 << -1 << 2000 << 1 << 30;
535     QTest::newRow( "data10" ) << 2000 << 2 << 28 << -1 << 2000 << 2 << 27;
536     QTest::newRow( "data11" ) << 2001 << 2 << 28 << -30 << 2001 << 1 << 29;
537
538     QTest::newRow( "data12" ) << -4713 << 1 << 2 << -2 << -4714 << 12 << 31;
539     QTest::newRow( "data13" ) << -4713 << 1 << 2 <<  2 << -4713 <<  1 <<  4;
540 }
541
542 void tst_QDate::addMonths()
543 {
544     QFETCH( int, year );
545     QFETCH( int, month );
546     QFETCH( int, day );
547     QFETCH( int, amountToAdd );
548     QFETCH( int, expectedYear );
549     QFETCH( int, expectedMonth );
550     QFETCH( int, expectedDay );
551
552     QDate dt( year, month, day );
553     dt = dt.addMonths( amountToAdd );
554
555     QCOMPARE( dt.year(), expectedYear );
556     QCOMPARE( dt.month(), expectedMonth );
557     QCOMPARE( dt.day(), expectedDay );
558 }
559
560 void tst_QDate::addMonths_data()
561 {
562     QTest::addColumn<int>("year");
563     QTest::addColumn<int>("month");
564     QTest::addColumn<int>("day");
565     QTest::addColumn<int>("amountToAdd");
566     QTest::addColumn<int>("expectedYear");
567     QTest::addColumn<int>("expectedMonth");
568     QTest::addColumn<int>("expectedDay");
569
570     QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 2 << 1;
571     QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 29;
572     QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 3 << 28;
573     QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 29;
574     QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 31;
575     QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 28;
576     QTest::newRow( "data6" ) << 2001 << 2 << 28 << 12 << 2002 << 2 << 28;
577     QTest::newRow( "data7" ) << 2000 << 2 << 29 << 12 << 2001 << 2 << 28;
578     QTest::newRow( "data8" ) << 2000 << 10 << 15 << 4 << 2001 << 2 << 15;
579
580     QTest::newRow( "data9" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 1;
581     QTest::newRow( "data10" ) << 2000 << 1 << 31 << -1 << 1999 << 12 << 31;
582     QTest::newRow( "data11" ) << 2000 << 12 << 31 << -1 << 2000 << 11 << 30;
583     QTest::newRow( "data12" ) << 2001 << 2 << 28 << -12 << 2000 << 2 << 28;
584     QTest::newRow( "data13" ) << 2000 << 1 << 31 << -7 << 1999 << 6 << 30;
585     QTest::newRow( "data14" ) << 2000 << 2 << 29 << -12 << 1999 << 2 << 28;
586
587     // year sign change:
588     QTest::newRow( "data15" ) << 1 << 1 << 1 << -1 << -1 << 12 << 1;
589     QTest::newRow( "data16" ) << 1 << 1 << 1 << -12 << -1 << 1 << 1;
590     QTest::newRow( "data17" ) << -1 << 12 << 1 << 1 << 1 << 1 << 1;
591     QTest::newRow( "data18" ) << -1 << 1 << 1 << 12 << 1 << 1 << 1;
592 }
593
594 void tst_QDate::addYears()
595 {
596     QFETCH( int, year );
597     QFETCH( int, month );
598     QFETCH( int, day );
599     QFETCH( int, amountToAdd );
600     QFETCH( int, expectedYear );
601     QFETCH( int, expectedMonth );
602     QFETCH( int, expectedDay );
603
604     QDate dt( year, month, day );
605     dt = dt.addYears( amountToAdd );
606
607     QCOMPARE( dt.year(), expectedYear );
608     QCOMPARE( dt.month(), expectedMonth );
609     QCOMPARE( dt.day(), expectedDay );
610 }
611
612 void tst_QDate::addYears_data()
613 {
614     QTest::addColumn<int>("year");
615     QTest::addColumn<int>("month");
616     QTest::addColumn<int>("day");
617     QTest::addColumn<int>("amountToAdd");
618     QTest::addColumn<int>("expectedYear");
619     QTest::addColumn<int>("expectedMonth");
620     QTest::addColumn<int>("expectedDay");
621
622     QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2001 << 1 << 1;
623     QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2001 << 1 << 31;
624     QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2001 << 2 << 28;
625     QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2001 << 2 << 28;
626     QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 12 << 31;
627     QTest::newRow( "data5" ) << 2001 << 2 << 28 << 3 << 2004 << 2 << 28;
628     QTest::newRow( "data6" ) << 2000 << 2 << 29 << 4 << 2004 << 2 << 29;
629
630     QTest::newRow( "data7" ) << 2000 << 1 << 31 << -1 << 1999 << 1 << 31;
631     QTest::newRow( "data9" ) << 2000 << 2 << 29 << -1 << 1999 << 2 << 28;
632     QTest::newRow( "data10" ) << 2000 << 12 << 31 << -1 << 1999 << 12 << 31;
633     QTest::newRow( "data11" ) << 2001 << 2 << 28 << -3 << 1998 << 2 << 28;
634     QTest::newRow( "data12" ) << 2000 << 2 << 29 << -4 << 1996 << 2 << 29;
635     QTest::newRow( "data13" ) << 2000 << 2 << 29 << -5 << 1995 << 2 << 28;
636
637     QTest::newRow( "data14" ) << 2000 << 1 << 1 << -1999 << 1 << 1 << 1;
638     QTest::newRow( "data15" ) << 2000 << 1 << 1 << -2000 << -1 << 1 << 1;
639     QTest::newRow( "data16" ) << 2000 << 1 << 1 << -2001 << -2 << 1 << 1;
640     QTest::newRow( "data17" ) << -2000 << 1 << 1 << 1999 << -1 << 1 << 1;
641     QTest::newRow( "data18" ) << -2000 << 1 << 1 << 2000 << 1 << 1 << 1;
642     QTest::newRow( "data19" ) << -2000 << 1 << 1 << 2001 << 2 << 1 << 1;
643 }
644
645 void tst_QDate::daysTo()
646 {
647     qint64 minJd = std::numeric_limits<qint64>::min() / 2;
648     qint64 maxJd = std::numeric_limits<qint64>::max() / 2;
649
650     QDate dt1(2000, 1, 1);
651     QDate dt2(2000, 1, 5);
652     QCOMPARE(dt1.daysTo(dt2), (qint64) 4);
653     QCOMPARE(dt2.daysTo(dt1), (qint64) -4);
654
655     dt1.setDate(0, 0, 0);
656     QCOMPARE(dt1.daysTo(dt2), (qint64) 0);
657     dt1.setDate(2000, 1, 1);
658     dt2.setDate(0, 0, 0);
659     QCOMPARE(dt1.daysTo(dt2), (qint64) 0);
660
661
662     QDate maxDate = QDate::fromJulianDay(maxJd);
663     QDate minDate = QDate::fromJulianDay(minJd);
664     QDate zeroDate = QDate::fromJulianDay(0);
665
666     QCOMPARE(maxDate.daysTo(minDate), minJd - maxJd);
667     QCOMPARE(minDate.daysTo(maxDate), maxJd - minJd);
668     QCOMPARE(maxDate.daysTo(zeroDate), -maxJd);
669     QCOMPARE(zeroDate.daysTo(maxDate), maxJd);
670     QCOMPARE(minDate.daysTo(zeroDate), -minJd);
671     QCOMPARE(zeroDate.daysTo(minDate), minJd);
672 }
673
674 void tst_QDate::operator_eq_eq()
675 {
676     QDate d1(2000,1,2);
677     QDate d2(2000,1,2);
678     QVERIFY( d1 == d2 );
679
680     d1 = QDate(2001,12,5);
681     d2 = QDate(2001,12,5);
682     QVERIFY( d1 == d2 );
683
684     d2 = QDate(2002,12,5);
685     QVERIFY( !(d1 == d2) );
686 }
687
688 void tst_QDate::operator_not_eq()
689 {
690     QDate d1(2000,1,2);
691     QDate d2(2000,1,2);
692     QVERIFY( !(d1 != d2) );
693
694     d1 = QDate(2001,12,5);
695     d2 = QDate(2001,12,5);
696     QVERIFY( !(d1 != d2) );
697
698     d2 = QDate(2002,12,5);
699     QVERIFY( d1 != d2 );
700 }
701
702 void tst_QDate::operator_lt()
703 {
704     QDate d1(2000,1,2);
705     QDate d2(2000,1,2);
706     QVERIFY( !(d1 < d2) );
707
708     d1 = QDate(2001,12,4);
709     d2 = QDate(2001,12,5);
710     QVERIFY( d1 < d2 );
711
712     d1 = QDate(2001,11,5);
713     d2 = QDate(2001,12,5);
714     QVERIFY( d1 < d2 );
715
716     d1 = QDate(2000,12,5);
717     d2 = QDate(2001,12,5);
718     QVERIFY( d1 < d2 );
719
720     d1 = QDate(2002,12,5);
721     d2 = QDate(2001,12,5);
722     QVERIFY( !(d1 < d2) );
723
724     d1 = QDate(2001,12,5);
725     d2 = QDate(2001,11,5);
726     QVERIFY( !(d1 < d2) );
727
728     d1 = QDate(2001,12,6);
729     d2 = QDate(2001,12,5);
730     QVERIFY( !(d1 < d2) );
731 }
732
733 void tst_QDate::operator_gt()
734 {
735     QDate d1(2000,1,2);
736     QDate d2(2000,1,2);
737     QVERIFY( !(d1 > d2) );
738
739     d1 = QDate(2001,12,4);
740     d2 = QDate(2001,12,5);
741     QVERIFY( !(d1 > d2) );
742
743     d1 = QDate(2001,11,5);
744     d2 = QDate(2001,12,5);
745     QVERIFY( !(d1 > d2) );
746
747     d1 = QDate(2000,12,5);
748     d2 = QDate(2001,12,5);
749     QVERIFY( !(d1 > d2) );
750
751     d1 = QDate(2002,12,5);
752     d2 = QDate(2001,12,5);
753     QVERIFY( d1 > d2 );
754
755     d1 = QDate(2001,12,5);
756     d2 = QDate(2001,11,5);
757     QVERIFY( d1 > d2 );
758
759     d1 = QDate(2001,12,6);
760     d2 = QDate(2001,12,5);
761     QVERIFY( d1 > d2 );
762 }
763
764 void tst_QDate::operator_lt_eq()
765 {
766     QDate d1(2000,1,2);
767     QDate d2(2000,1,2);
768     QVERIFY( d1 <= d2 );
769
770     d1 = QDate(2001,12,4);
771     d2 = QDate(2001,12,5);
772     QVERIFY( d1 <= d2 );
773
774     d1 = QDate(2001,11,5);
775     d2 = QDate(2001,12,5);
776     QVERIFY( d1 <= d2 );
777
778     d1 = QDate(2000,12,5);
779     d2 = QDate(2001,12,5);
780     QVERIFY( d1 <= d2 );
781
782     d1 = QDate(2002,12,5);
783     d2 = QDate(2001,12,5);
784     QVERIFY( !(d1 <= d2) );
785
786     d1 = QDate(2001,12,5);
787     d2 = QDate(2001,11,5);
788     QVERIFY( !(d1 <= d2) );
789
790     d1 = QDate(2001,12,6);
791     d2 = QDate(2001,12,5);
792     QVERIFY( !(d1 <= d2) );
793 }
794
795 void tst_QDate::operator_gt_eq()
796 {
797     QDate d1(2000,1,2);
798     QDate d2(2000,1,2);
799     QVERIFY( d1 >= d2 );
800
801     d1 = QDate(2001,12,4);
802     d2 = QDate(2001,12,5);
803     QVERIFY( !(d1 >= d2) );
804
805     d1 = QDate(2001,11,5);
806     d2 = QDate(2001,12,5);
807     QVERIFY( !(d1 >= d2) );
808
809     d1 = QDate(2000,12,5);
810     d2 = QDate(2001,12,5);
811     QVERIFY( !(d1 >= d2) );
812
813     d1 = QDate(2002,12,5);
814     d2 = QDate(2001,12,5);
815     QVERIFY( d1 >= d2 );
816
817     d1 = QDate(2001,12,5);
818     d2 = QDate(2001,11,5);
819     QVERIFY( d1 >= d2 );
820
821     d1 = QDate(2001,12,6);
822     d2 = QDate(2001,12,5);
823     QVERIFY( d1 >= d2 );
824 }
825
826 void tst_QDate::fromStringDateFormat_data()
827 {
828     // Since we can't define an element of Qt::DateFormat, d1 will be the date
829     // expected when we have a TextDate, and d2 will be the date expected when
830     // we have an ISODate.
831
832     QTest::addColumn<QString>("str1");
833     QTest::addColumn<QString>("str2");
834     QTest::addColumn<QDate>("d1");
835
836     QTest::newRow( "data0" ) << QString("Sat May 20 1995") << QString("1995-05-20") << QDate(1995,5,20);
837     QTest::newRow( "data1" ) << QString("Tue Dec 17 2002") << QString("2002-12-17") << QDate(2002,12,17);
838     QDate d( 1999, 11, 14 );
839     QTest::newRow( "data2" ) << d.toString( Qt::TextDate ) << d.toString( Qt::ISODate ) << d;
840
841     QTest::newRow( "data3" ) << QString("xxx Jan 1 0999") << QString("0999-01-01") << QDate(999, 1, 1);
842     QTest::newRow( "data3b" ) << QString("xxx Jan 1 999") << QString("0999-01-01") << QDate(999, 1, 1);
843     QTest::newRow( "data4" ) << QString("xxx Jan 1 12345") << QString() << QDate(12345, 1, 1);
844     QTest::newRow( "data5" ) << QString("xxx Jan 1 -0001") << QString() << QDate(-1, 1, 1);
845     QTest::newRow( "data6" ) << QString("xxx Jan 1 -4712") << QString() << QDate(-4712, 1, 1);
846     QTest::newRow( "data7" ) << QString("xxx Nov 25 -4713") << QString() << QDate(-4713, 11, 25);
847 }
848
849 void tst_QDate::fromStringDateFormat()
850 {
851     QFETCH( QString, str1 );
852     QFETCH( QString, str2 );
853     QFETCH( QDate, d1 );
854
855     QCOMPARE( QDate::fromString( str1, Qt::TextDate ), d1 );
856     if (!str2.isEmpty())
857         QCOMPARE( QDate::fromString( str2, Qt::ISODate ), d1 );
858 }
859
860 void tst_QDate::fromStringFormat_data()
861 {
862     QTest::addColumn<QString>("string");
863     QTest::addColumn<QString>("format");
864     QTest::addColumn<QDate>("expected");
865
866     //get localized names
867     QString january = QDate::longMonthName(1);
868     QString february = QDate::longMonthName(2);
869     QString march = QDate::longMonthName(3);
870     QString august = QDate::longMonthName(8);
871     QString mon = QDate::shortDayName(1);
872     QString monday = QDate::longDayName(1);
873     QString tuesday = QDate::longDayName(2);
874     QString wednesday = QDate::longDayName(3);
875     QString thursday = QDate::longDayName(4);
876     QString friday = QDate::longDayName(5);
877     QString saturday = QDate::longDayName(6);
878     QString sunday = QDate::longDayName(7);
879
880     QTest::newRow("data0") << QString("") << QString("") << defDate();
881     QTest::newRow("data1") << QString(" ") << QString("") << invalidDate();
882     QTest::newRow("data2") << QString(" ") << QString(" ") << defDate();
883     QTest::newRow("data3") << QString("-%$%#") << QString("$*(#@") << invalidDate();
884     QTest::newRow("data4") << QString("d") << QString("'d'") << defDate();
885     QTest::newRow("data5") << QString("101010") << QString("dMyy") << QDate(1910, 10, 10);
886     QTest::newRow("data6") << QString("101010b") << QString("dMyy") << invalidDate();
887     QTest::newRow("data7") << january << QString("MMMM") << defDate();
888     QTest::newRow("data8") << QString("ball") << QString("balle") << invalidDate();
889     QTest::newRow("data9") << QString("balleh") << QString("balleh") << defDate();
890     QTest::newRow("data10") << QString("10.01.1") << QString("M.dd.d") << QDate(defDate().year(), 10, 1);
891     QTest::newRow("data11") << QString("-1.01.1") << QString("M.dd.d") << invalidDate();
892     QTest::newRow("data12") << QString("11010") << QString("dMMyy") << invalidDate();
893     QTest::newRow("data13") << QString("-2") << QString("d") << invalidDate();
894     QTest::newRow("data14") << QString("132") << QString("Md") << invalidDate();
895     QTest::newRow("data15") << february << QString("MMMM") << QDate(defDate().year(), 2, 1);
896
897     QString date = mon + " " + august + " 8 2005";
898     QTest::newRow("data16") << date << QString("ddd MMMM d yyyy") << QDate(2005, 8, 8);
899     QTest::newRow("data17") << QString("2000:00") << QString("yyyy:yy") << QDate(2000, 1, 1);
900     QTest::newRow("data18") << QString("1999:99") << QString("yyyy:yy") << QDate(1999, 1, 1);
901     QTest::newRow("data19") << QString("2099:99") << QString("yyyy:yy") << QDate(2099, 1, 1);
902     QTest::newRow("data20") << QString("2001:01") << QString("yyyy:yy") << QDate(2001, 1, 1);
903     QTest::newRow("data21") << QString("99") << QString("yy") << QDate(1999, 1, 1);
904     QTest::newRow("data22") << QString("01") << QString("yy") << QDate(1901, 1, 1);
905
906     QTest::newRow("data23") << monday << QString("dddd") << QDate(1900, 1, 1);
907     QTest::newRow("data24") << tuesday << QString("dddd") << QDate(1900, 1, 2);
908     QTest::newRow("data25") << wednesday << QString("dddd") << QDate(1900, 1, 3);
909     QTest::newRow("data26") << thursday << QString("dddd") << QDate(1900, 1, 4);
910     QTest::newRow("data27") << friday << QString("dddd") << QDate(1900, 1, 5);
911     QTest::newRow("data28") << saturday << QString("dddd") << QDate(1900, 1, 6);
912     QTest::newRow("data29") << sunday << QString("dddd") << QDate(1900, 1, 7);
913
914     QTest::newRow("data30") << monday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 2);
915     QTest::newRow("data31") << tuesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 3);
916     QTest::newRow("data32") << wednesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 4);
917     QTest::newRow("data33") << thursday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 5);
918     QTest::newRow("data34") << friday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 6);
919     QTest::newRow("data35") << saturday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 7);
920     QTest::newRow("data36") << sunday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 1);
921
922     QTest::newRow("data37") << tuesday + " 2007 " + march << QString("dddd yyyy MMMM") << QDate(2007, 3, 6);
923
924     QTest::newRow("data38") << QString("21052006") << QString("ddMMyyyy") << QDate(2006,5,21);
925     QTest::newRow("data39") << QString("210506") << QString("ddMMyy") << QDate(1906,5,21);
926     QTest::newRow("data40") << QString("21/5/2006") << QString("d/M/yyyy") << QDate(2006,5,21);
927     QTest::newRow("data41") << QString("21/5/06") << QString("d/M/yy") << QDate(1906,5,21);
928     QTest::newRow("data42") << QString("20060521") << QString("yyyyMMdd") << QDate(2006,5,21);
929     QTest::newRow("data43") << QString("060521") << QString("yyMMdd") << QDate(1906,5,21);
930 }
931
932
933 void tst_QDate::fromStringFormat()
934 {
935     QFETCH(QString, string);
936     QFETCH(QString, format);
937     QFETCH(QDate, expected);
938
939     QDate dt = QDate::fromString(string, format);
940     QCOMPARE(dt, expected);
941 }
942
943 void tst_QDate::toStringFormat_data()
944 {
945     QTest::addColumn<QDate>("t");
946     QTest::addColumn<QString>("format");
947     QTest::addColumn<QString>("str");
948
949     QTest::newRow( "data0" ) << QDate(1995,5,20) << QString("d-M-yy") << QString("20-5-95");
950     QTest::newRow( "data1" ) << QDate(2002,12,17) << QString("dd-MM-yyyy") << QString("17-12-2002");
951     QTest::newRow( "data2" ) << QDate(1995,5,20) << QString("M-yy") << QString("5-95");
952     QTest::newRow( "data3" ) << QDate(2002,12,17) << QString("dd") << QString("17");
953     QTest::newRow( "data4" ) << QDate() << QString("dd-mm-yyyy") << QString();
954 }
955
956 void tst_QDate::toStringFormat()
957 {
958     QFETCH( QDate, t );
959     QFETCH( QString, format );
960     QFETCH( QString, str );
961
962     QCOMPARE( t.toString( format ), str );
963 }
964
965 void tst_QDate::toStringDateFormat_data()
966 {
967     QTest::addColumn<QDate>("date");
968     QTest::addColumn<Qt::DateFormat>("format");
969     QTest::addColumn<QString>("expectedStr");
970
971     QTest::newRow("data0") << QDate(1,1,1) << Qt::ISODate << QString("0001-01-01");
972     QTest::newRow("data1") << QDate(11,1,1) << Qt::ISODate << QString("0011-01-01");
973     QTest::newRow("data2") << QDate(111,1,1) << Qt::ISODate << QString("0111-01-01");
974     QTest::newRow("data3") << QDate(1974,12,1) << Qt::ISODate << QString("1974-12-01");
975 }
976
977 void tst_QDate::toStringDateFormat()
978 {
979     QFETCH(QDate, date);
980     QFETCH(Qt::DateFormat, format);
981     QFETCH(QString, expectedStr);
982
983     QCOMPARE(date.toString(Qt::SystemLocaleShortDate), QLocale::system().toString(date, QLocale::ShortFormat));
984     QCOMPARE(date.toString(Qt::LocaleDate), QLocale().toString(date, QLocale::ShortFormat));
985     QLocale::setDefault(QLocale::German);
986     QCOMPARE(date.toString(Qt::SystemLocaleShortDate), QLocale::system().toString(date, QLocale::ShortFormat));
987     QCOMPARE(date.toString(Qt::LocaleDate), QLocale().toString(date, QLocale::ShortFormat));
988
989     QCOMPARE(date.toString(format), expectedStr);
990 }
991
992 void tst_QDate::isLeapYear()
993 {
994     QVERIFY(QDate::isLeapYear(-4801));
995     QVERIFY(!QDate::isLeapYear(-4800));
996     QVERIFY(QDate::isLeapYear(-4445));
997     QVERIFY(!QDate::isLeapYear(-4444));
998     QVERIFY(!QDate::isLeapYear(-6));
999     QVERIFY(QDate::isLeapYear(-5));
1000     QVERIFY(!QDate::isLeapYear(-4));
1001     QVERIFY(!QDate::isLeapYear(-3));
1002     QVERIFY(!QDate::isLeapYear(-2));
1003     QVERIFY(QDate::isLeapYear(-1));
1004     QVERIFY(!QDate::isLeapYear(0)); // Doesn't exist
1005     QVERIFY(!QDate::isLeapYear(1));
1006     QVERIFY(!QDate::isLeapYear(2));
1007     QVERIFY(!QDate::isLeapYear(3));
1008     QVERIFY(QDate::isLeapYear(4));
1009     QVERIFY(!QDate::isLeapYear(7));
1010     QVERIFY(QDate::isLeapYear(8));
1011     QVERIFY(!QDate::isLeapYear(100));
1012     QVERIFY(QDate::isLeapYear(400));
1013     QVERIFY(!QDate::isLeapYear(700));
1014     QVERIFY(!QDate::isLeapYear(1500));
1015     QVERIFY(QDate::isLeapYear(1600));
1016     QVERIFY(!QDate::isLeapYear(1700));
1017     QVERIFY(!QDate::isLeapYear(1800));
1018     QVERIFY(!QDate::isLeapYear(1900));
1019     QVERIFY(QDate::isLeapYear(2000));
1020     QVERIFY(!QDate::isLeapYear(2100));
1021     QVERIFY(!QDate::isLeapYear(2200));
1022     QVERIFY(!QDate::isLeapYear(2300));
1023     QVERIFY(QDate::isLeapYear(2400));
1024     QVERIFY(!QDate::isLeapYear(2500));
1025     QVERIFY(!QDate::isLeapYear(2600));
1026     QVERIFY(!QDate::isLeapYear(2700));
1027     QVERIFY(QDate::isLeapYear(2800));
1028
1029     for (int i = -4713; i <= 10000; ++i) {
1030         if (i == 0)
1031             continue;
1032         QVERIFY(!QDate(i, 2, 29).isValid() == !QDate::isLeapYear(i));
1033     }
1034 }
1035
1036 void tst_QDate::yearsZeroToNinetyNine()
1037 {
1038     {
1039         QDate dt(-1, 2, 3);
1040         QCOMPARE(dt.year(), -1);
1041         QCOMPARE(dt.month(), 2);
1042         QCOMPARE(dt.day(), 3);
1043     }
1044
1045     {
1046         QDate dt(1, 2, 3);
1047         QCOMPARE(dt.year(), 1);
1048         QCOMPARE(dt.month(), 2);
1049         QCOMPARE(dt.day(), 3);
1050     }
1051
1052     {
1053         QDate dt(99, 2, 3);
1054         QCOMPARE(dt.year(), 99);
1055         QCOMPARE(dt.month(), 2);
1056         QCOMPARE(dt.day(), 3);
1057     }
1058
1059     QVERIFY(!QDate::isValid(0, 2, 3));
1060     QVERIFY(QDate::isValid(1, 2, 3));
1061     QVERIFY(QDate::isValid(-1, 2, 3));
1062
1063 #if QT_DEPRECATED_SINCE(5,0)
1064     {
1065         QDate dt;
1066         dt.setYMD(1, 2, 3);
1067         QCOMPARE(dt.year(), 1901);
1068         QCOMPARE(dt.month(), 2);
1069         QCOMPARE(dt.day(), 3);
1070     }
1071 #endif
1072
1073     {
1074         QDate dt;
1075         dt.setDate(1, 2, 3);
1076         QCOMPARE(dt.year(), 1);
1077         QCOMPARE(dt.month(), 2);
1078         QCOMPARE(dt.day(), 3);
1079
1080         dt.setDate(0, 2, 3);
1081         QVERIFY(!dt.isValid());
1082     }
1083 }
1084
1085
1086 void tst_QDate::negativeYear() const
1087 {
1088     QDate y(-20, 3, 4);
1089     QVERIFY(y.isValid());
1090     QCOMPARE(y.year(), -20);
1091 }
1092
1093 void tst_QDate::printNegativeYear() const
1094 {
1095     {
1096         QDate date(-500, 3, 4);
1097         QVERIFY(date.isValid());
1098         QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0500"));
1099     }
1100
1101     {
1102         QDate date(-10, 3, 4);
1103         QVERIFY(date.isValid());
1104         QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0010"));
1105     }
1106
1107     {
1108         QDate date(-2, 3, 4);
1109         QVERIFY(date.isValid());
1110         QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0002"));
1111     }
1112 }
1113
1114 void tst_QDate::roundtripGermanLocale() const
1115 {
1116     /* This code path should not result in warnings. */
1117     const QDate theDate(QDate::currentDate());
1118     theDate.fromString(theDate.toString(Qt::TextDate), Qt::TextDate);
1119
1120     const QDateTime theDateTime(QDateTime::currentDateTime());
1121     theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate);
1122 }
1123
1124 void tst_QDate::shortDayName() const
1125 {
1126     QCOMPARE(QDate::shortDayName(0), QString());
1127
1128     if (QLocale::system().language() == QLocale::C) {
1129         QCOMPARE(QDate::shortDayName(1), QLatin1String("Mon"));
1130         QCOMPARE(QDate::shortDayName(7), QLatin1String("Sun"));
1131     }
1132
1133     QLocale locale = QLocale::system();
1134     for(int i = 1; i <= 7; ++i) {
1135         QCOMPARE(QDate::shortDayName(i), locale.dayName(i, QLocale::ShortFormat));
1136     }
1137 }
1138
1139 void tst_QDate::standaloneShortDayName() const
1140 {
1141     QCOMPARE(QDate::shortDayName(0, QDate::StandaloneFormat), QString());
1142
1143     if (QLocale::system().language() == QLocale::C) {
1144         QCOMPARE(QDate::shortDayName(1, QDate::StandaloneFormat), QLatin1String("Mon"));
1145         QCOMPARE(QDate::shortDayName(7, QDate::StandaloneFormat), QLatin1String("Sun"));
1146     }
1147
1148     QLocale locale = QLocale::system();
1149     for(int i = 1; i <= 7; ++i) {
1150         QCOMPARE(QDate::shortDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::ShortFormat));
1151     }
1152 }
1153
1154 void tst_QDate::longDayName() const
1155 {
1156     QCOMPARE(QDate::longDayName(0), QString());
1157
1158     if (QLocale::system().language() == QLocale::C) {
1159         QCOMPARE(QDate::longDayName(1), QLatin1String("Monday"));
1160         QCOMPARE(QDate::longDayName(7), QLatin1String("Sunday"));
1161     }
1162
1163     QLocale locale = QLocale::system();
1164     for(int i = 1; i <= 7; ++i) {
1165         QCOMPARE(QDate::longDayName(i), locale.dayName(i, QLocale::LongFormat));
1166     }
1167 }
1168
1169 void tst_QDate::standaloneLongDayName() const
1170 {
1171     QCOMPARE(QDate::longDayName(0, QDate::StandaloneFormat), QString());
1172
1173     if (QLocale::system().language() == QLocale::C) {
1174         QCOMPARE(QDate::longDayName(1, QDate::StandaloneFormat), QLatin1String("Monday"));
1175         QCOMPARE(QDate::longDayName(7, QDate::StandaloneFormat), QLatin1String("Sunday"));
1176     }
1177
1178     QLocale locale = QLocale::system();
1179     for(int i = 1; i <= 7; ++i) {
1180         QCOMPARE(QDate::longDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::LongFormat));
1181     }
1182 }
1183
1184 void tst_QDate::shortMonthName() const
1185 {
1186     QCOMPARE(QDate::shortMonthName(0), QString());
1187
1188     if (QLocale::system().language() == QLocale::C) {
1189         QCOMPARE(QDate::shortMonthName(1), QLatin1String("Jan"));
1190         QCOMPARE(QDate::shortMonthName(8), QLatin1String("Aug"));
1191     }
1192
1193     QLocale locale = QLocale::system();
1194     for(int i = 1; i <= 12; ++i) {
1195         QCOMPARE(QDate::shortMonthName(i), locale.monthName(i, QLocale::ShortFormat));
1196     }
1197 }
1198
1199 void tst_QDate::standaloneShortMonthName() const
1200 {
1201     QCOMPARE(QDate::shortMonthName(0, QDate::StandaloneFormat), QString());
1202
1203     if (QLocale::system().language() == QLocale::C) {
1204         QCOMPARE(QDate::shortMonthName(1, QDate::StandaloneFormat), QLatin1String("Jan"));
1205         QCOMPARE(QDate::shortMonthName(8, QDate::StandaloneFormat), QLatin1String("Aug"));
1206     }
1207
1208     QLocale locale = QLocale::system();
1209     for(int i = 1; i <= 12; ++i) {
1210         QCOMPARE(QDate::shortMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::ShortFormat));
1211     }
1212 }
1213
1214 void tst_QDate::longMonthName() const
1215 {
1216     QCOMPARE(QDate::longMonthName(0), QString());
1217
1218     if (QLocale::system().language() == QLocale::C) {
1219         QCOMPARE(QDate::longMonthName(1), QLatin1String("January"));
1220         QCOMPARE(QDate::longMonthName(8), QLatin1String("August"));
1221     }
1222
1223     QLocale locale = QLocale::system();
1224     for(int i = 1; i <= 12; ++i) {
1225         QCOMPARE(QDate::longMonthName(i), locale.monthName(i, QLocale::LongFormat));
1226     }
1227 }
1228
1229 void tst_QDate::standaloneLongMonthName() const
1230 {
1231     QCOMPARE(QDate::longMonthName(0, QDate::StandaloneFormat), QString());
1232
1233     if (QLocale::system().language() == QLocale::C) {
1234         QCOMPARE(QDate::longMonthName(1, QDate::StandaloneFormat), QLatin1String("January"));
1235         QCOMPARE(QDate::longMonthName(8, QDate::StandaloneFormat), QLatin1String("August"));
1236     }
1237
1238     QLocale locale = QLocale::system();
1239     for(int i = 1; i <= 12; ++i) {
1240         QCOMPARE(QDate::longMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::LongFormat));
1241     }
1242 }
1243
1244 void tst_QDate::roundtrip() const
1245 {
1246     // Test round trip, this exercises setDate(), isValid(), isLeapYear(),
1247     // year(), month(), day(), julianDayFromDate(), and getDateFromJulianDay()
1248     // to ensure they are internally consistent (but doesn't guarantee correct)
1249
1250     // Test Julian round trip around JD 0 and current low end of valid range
1251     QDate testDate;
1252     QDate loopDate = QDate::fromJulianDay(-31738); // 1 Jan 4800 BC
1253     while (loopDate.toJulianDay() <= 5150) {     // 31 Dec 4700 BC
1254         testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1255         QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1256         loopDate = loopDate.addDays(1);
1257     }
1258
1259     // Test Julian round trip in both BC and AD
1260     loopDate = QDate::fromJulianDay(1684901);       //  1 Jan 100 BC
1261     while (loopDate.toJulianDay() <= 1757949) {   // 31 Dec 100 AD
1262         testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1263         QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1264         loopDate = loopDate.addDays(1);
1265     }
1266
1267     // Test Gregorian round trip during current useful period
1268     loopDate = QDate::fromJulianDay(2378497);     //  1 Jan 1900 AD
1269     while (loopDate.toJulianDay() <= 2488433) { // 31 Dec 2100 AD
1270         testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1271         QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1272         loopDate = loopDate.addDays(1);
1273     }
1274
1275     // Test Gregorian round trip at top end of widget/format range
1276     loopDate = QDate::fromJulianDay(5336961);     //  1 Jan 9900 AD
1277     while (loopDate.toJulianDay() <= 5373484) { // 31 Dec 9999 AD
1278         testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1279         QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1280         loopDate = loopDate.addDays(1);
1281     }
1282
1283     // Test Gregorian round trip at top end of conversion range
1284     loopDate = QDate::fromJulianDay(513024036);     //  1 Jan 1399900 AD
1285     while (loopDate.toJulianDay() <= 513060925) { // 31 Dec 1400000 AD
1286         testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1287         QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1288         loopDate = loopDate.addDays(1);
1289     }
1290 }
1291
1292 QTEST_APPLESS_MAIN(tst_QDate)
1293 #include "tst_qdate.moc"