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